Home | History | Annotate | Download | only in management
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_
      6 #define CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
     10 #include "chrome/browser/extensions/event_router.h"
     11 #include "chrome/browser/extensions/extension_function.h"
     12 #include "chrome/browser/extensions/extension_install_prompt.h"
     13 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
     14 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     15 #include "content/public/browser/notification_observer.h"
     16 #include "content/public/browser/notification_registrar.h"
     17 
     18 class ExtensionService;
     19 class ExtensionUninstallDialog;
     20 
     21 namespace extensions {
     22 
     23 class ManagementFunction : public SyncExtensionFunction {
     24  protected:
     25   virtual ~ManagementFunction() {}
     26 
     27   ExtensionService* service();
     28 };
     29 
     30 class AsyncManagementFunction : public AsyncExtensionFunction {
     31  protected:
     32   virtual ~AsyncManagementFunction() {}
     33 
     34   ExtensionService* service();
     35 };
     36 
     37 class ManagementGetAllFunction : public ManagementFunction {
     38  public:
     39   DECLARE_EXTENSION_FUNCTION("management.getAll", MANAGEMENT_GETALL)
     40 
     41  protected:
     42   virtual ~ManagementGetAllFunction() {}
     43 
     44   // ExtensionFunction:
     45   virtual bool RunImpl() OVERRIDE;
     46 };
     47 
     48 class ManagementGetFunction : public ManagementFunction {
     49  public:
     50   DECLARE_EXTENSION_FUNCTION("management.get", MANAGEMENT_GET)
     51 
     52  protected:
     53   virtual ~ManagementGetFunction() {}
     54 
     55   // ExtensionFunction:
     56   virtual bool RunImpl() OVERRIDE;
     57 };
     58 
     59 class ManagementGetPermissionWarningsByIdFunction : public ManagementFunction {
     60  public:
     61   DECLARE_EXTENSION_FUNCTION("management.getPermissionWarningsById",
     62                              MANAGEMENT_GETPERMISSIONWARNINGSBYID)
     63 
     64  protected:
     65   virtual ~ManagementGetPermissionWarningsByIdFunction() {}
     66 
     67   // ExtensionFunction:
     68   virtual bool RunImpl() OVERRIDE;
     69 };
     70 
     71 class ManagementGetPermissionWarningsByManifestFunction
     72     : public AsyncExtensionFunction {
     73  public:
     74   DECLARE_EXTENSION_FUNCTION(
     75       "management.getPermissionWarningsByManifest",
     76       MANAGEMENT_GETPERMISSIONWARNINGSBYMANIFEST);
     77 
     78   // Called when utility process finishes.
     79   void OnParseSuccess(base::DictionaryValue* parsed_manifest);
     80   void OnParseFailure(const std::string& error);
     81 
     82  protected:
     83   virtual ~ManagementGetPermissionWarningsByManifestFunction() {}
     84 
     85   // ExtensionFunction:
     86   virtual bool RunImpl() OVERRIDE;
     87 };
     88 
     89 class ManagementLaunchAppFunction : public ManagementFunction {
     90  public:
     91   DECLARE_EXTENSION_FUNCTION("management.launchApp", MANAGEMENT_LAUNCHAPP)
     92 
     93  protected:
     94   virtual ~ManagementLaunchAppFunction() {}
     95 
     96   // ExtensionFunction:
     97   virtual bool RunImpl() OVERRIDE;
     98 };
     99 
    100 class ManagementSetEnabledFunction : public AsyncManagementFunction,
    101                            public ExtensionInstallPrompt::Delegate {
    102  public:
    103   DECLARE_EXTENSION_FUNCTION("management.setEnabled", MANAGEMENT_SETENABLED)
    104 
    105   ManagementSetEnabledFunction();
    106 
    107  protected:
    108   virtual ~ManagementSetEnabledFunction();
    109 
    110   // ExtensionFunction:
    111   virtual bool RunImpl() OVERRIDE;
    112 
    113   // ExtensionInstallPrompt::Delegate.
    114   virtual void InstallUIProceed() OVERRIDE;
    115   virtual void InstallUIAbort(bool user_initiated) OVERRIDE;
    116 
    117  private:
    118   std::string extension_id_;
    119 
    120   // Used for prompting to re-enable items with permissions escalation updates.
    121   scoped_ptr<ExtensionInstallPrompt> install_prompt_;
    122 };
    123 
    124 class ManagementUninstallFunctionBase : public AsyncManagementFunction,
    125                           public ExtensionUninstallDialog::Delegate {
    126  public:
    127   ManagementUninstallFunctionBase();
    128 
    129   static void SetAutoConfirmForTest(bool should_proceed);
    130 
    131   // ExtensionUninstallDialog::Delegate implementation.
    132   virtual void ExtensionUninstallAccepted() OVERRIDE;
    133   virtual void ExtensionUninstallCanceled() OVERRIDE;
    134 
    135  protected:
    136   virtual ~ManagementUninstallFunctionBase();
    137 
    138   bool Uninstall(const std::string& extension_id, bool show_confirm_dialog);
    139  private:
    140 
    141   // If should_uninstall is true, this method does the actual uninstall.
    142   // If |show_uninstall_dialog|, then this function will be called by one of the
    143   // Accepted/Canceled callbacks. Otherwise, it's called directly from RunImpl.
    144   void Finish(bool should_uninstall);
    145 
    146   std::string extension_id_;
    147   scoped_ptr<ExtensionUninstallDialog> extension_uninstall_dialog_;
    148 };
    149 
    150 class ManagementUninstallFunction : public ManagementUninstallFunctionBase {
    151  public:
    152   DECLARE_EXTENSION_FUNCTION("management.uninstall", MANAGEMENT_UNINSTALL)
    153 
    154   ManagementUninstallFunction();
    155 
    156  private:
    157   virtual ~ManagementUninstallFunction();
    158 
    159   virtual bool RunImpl() OVERRIDE;
    160 };
    161 
    162 class ManagementUninstallSelfFunction : public ManagementUninstallFunctionBase {
    163  public:
    164   DECLARE_EXTENSION_FUNCTION("management.uninstallSelf",
    165       MANAGEMENT_UNINSTALLSELF);
    166 
    167   ManagementUninstallSelfFunction();
    168 
    169  private:
    170   virtual ~ManagementUninstallSelfFunction();
    171 
    172   virtual bool RunImpl() OVERRIDE;
    173 };
    174 
    175 class ManagementEventRouter : public content::NotificationObserver {
    176  public:
    177   explicit ManagementEventRouter(Profile* profile);
    178   virtual ~ManagementEventRouter();
    179 
    180  private:
    181   // content::NotificationObserver implementation.
    182   virtual void Observe(int type,
    183                        const content::NotificationSource& source,
    184                        const content::NotificationDetails& details) OVERRIDE;
    185 
    186   content::NotificationRegistrar registrar_;
    187 
    188   Profile* profile_;
    189 
    190   DISALLOW_COPY_AND_ASSIGN(ManagementEventRouter);
    191 };
    192 
    193 class ManagementAPI : public ProfileKeyedAPI,
    194                       public extensions::EventRouter::Observer {
    195  public:
    196   explicit ManagementAPI(Profile* profile);
    197   virtual ~ManagementAPI();
    198 
    199   // BrowserContextKeyedService implementation.
    200   virtual void Shutdown() OVERRIDE;
    201 
    202   // ProfileKeyedAPI implementation.
    203   static ProfileKeyedAPIFactory<ManagementAPI>* GetFactoryInstance();
    204 
    205   // EventRouter::Observer implementation.
    206   virtual void OnListenerAdded(const extensions::EventListenerInfo& details)
    207       OVERRIDE;
    208 
    209  private:
    210   friend class ProfileKeyedAPIFactory<ManagementAPI>;
    211 
    212   Profile* profile_;
    213 
    214   // ProfileKeyedAPI implementation.
    215   static const char* service_name() {
    216     return "ManagementAPI";
    217   }
    218   static const bool kServiceIsNULLWhileTesting = true;
    219 
    220   // Created lazily upon OnListenerAdded.
    221   scoped_ptr<ManagementEventRouter> management_event_router_;
    222 
    223   DISALLOW_COPY_AND_ASSIGN(ManagementAPI);
    224 };
    225 
    226 }  // namespace extensions
    227 
    228 #endif  // CHROME_BROWSER_EXTENSIONS_API_MANAGEMENT_MANAGEMENT_API_H_
    229