Home | History | Annotate | Download | only in extensions
      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_EXTENSION_SYSTEM_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_
      7 
      8 #include <string>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
     13 #include "extensions/common/extension.h"
     14 #include "extensions/common/one_shot_event.h"
     15 
     16 class ExtensionService;
     17 class Profile;
     18 
     19 #if defined(OS_CHROMEOS)
     20 namespace chromeos {
     21 class DeviceLocalAccountManagementPolicyProvider;
     22 }
     23 #endif  // defined(OS_CHROMEOS)
     24 
     25 namespace content {
     26 class BrowserContext;
     27 }
     28 
     29 namespace extensions {
     30 class Blacklist;
     31 class ErrorConsole;
     32 class EventRouter;
     33 class Extension;
     34 class ExtensionSystemSharedFactory;
     35 class ExtensionWarningBadgeService;
     36 class ExtensionWarningService;
     37 class InfoMap;
     38 class InstallVerifier;
     39 class LazyBackgroundTaskQueue;
     40 class ManagementPolicy;
     41 class NavigationObserver;
     42 class ProcessManager;
     43 class StandardManagementPolicyProvider;
     44 class StateStore;
     45 class UserScriptMaster;
     46 
     47 // The ExtensionSystem manages the creation and destruction of services
     48 // related to extensions. Most objects are shared between normal
     49 // and incognito Profiles, except as called out in comments.
     50 // This interface supports using TestExtensionSystem for TestingProfiles
     51 // that don't want all of the extensions baggage in their tests.
     52 class ExtensionSystem : public BrowserContextKeyedService {
     53  public:
     54   ExtensionSystem();
     55   virtual ~ExtensionSystem();
     56 
     57   // Returns the instance for the given profile, or NULL if none. This is
     58   // a convenience wrapper around ExtensionSystemFactory::GetForProfile.
     59   static ExtensionSystem* Get(Profile* profile);
     60 
     61   // Returns the same instance as Get() above.
     62   static ExtensionSystem* GetForBrowserContext(
     63       content::BrowserContext* profile);
     64 
     65   // BrowserContextKeyedService implementation.
     66   virtual void Shutdown() OVERRIDE {}
     67 
     68   // Initializes extensions machinery.
     69   // Component extensions are always enabled, external and user extensions are
     70   // controlled by |extensions_enabled|.
     71   virtual void InitForRegularProfile(bool extensions_enabled) = 0;
     72 
     73   // The ExtensionService is created at startup.
     74   virtual ExtensionService* extension_service() = 0;
     75 
     76   // The class controlling whether users are permitted to perform certain
     77   // actions on extensions (install, uninstall, disable, etc.).
     78   // The ManagementPolicy is created at startup.
     79   virtual ManagementPolicy* management_policy() = 0;
     80 
     81   // The UserScriptMaster is created at startup.
     82   virtual UserScriptMaster* user_script_master() = 0;
     83 
     84   // The ProcessManager is created at startup.
     85   virtual ProcessManager* process_manager() = 0;
     86 
     87   // The StateStore is created at startup.
     88   virtual StateStore* state_store() = 0;
     89 
     90   // The rules store is created at startup.
     91   virtual StateStore* rules_store() = 0;
     92 
     93   // Returns the IO-thread-accessible extension data.
     94   virtual InfoMap* info_map() = 0;
     95 
     96   // The LazyBackgroundTaskQueue is created at startup.
     97   virtual LazyBackgroundTaskQueue* lazy_background_task_queue() = 0;
     98 
     99   // The EventRouter is created at startup.
    100   virtual EventRouter* event_router() = 0;
    101 
    102   // The ExtensionWarningService is created at startup.
    103   virtual ExtensionWarningService* warning_service() = 0;
    104 
    105   // The blacklist is created at startup.
    106   virtual Blacklist* blacklist() = 0;
    107 
    108   // The ErrorConsole is created at startup.
    109   virtual ErrorConsole* error_console() = 0;
    110 
    111   // The InstallVerifier is created at startup.
    112   virtual InstallVerifier* install_verifier() = 0;
    113 
    114   // Called by the ExtensionService that lives in this system. Gives the
    115   // info map a chance to react to the load event before the EXTENSION_LOADED
    116   // notification has fired. The purpose for handling this event first is to
    117   // avoid race conditions by making sure URLRequestContexts learn about new
    118   // extensions before anything else needs them to know.
    119   virtual void RegisterExtensionWithRequestContexts(
    120       const Extension* extension) {}
    121 
    122   // Called by the ExtensionService that lives in this system. Lets the
    123   // info map clean up its RequestContexts once all the listeners to the
    124   // EXTENSION_UNLOADED notification have finished running.
    125   virtual void UnregisterExtensionWithRequestContexts(
    126       const std::string& extension_id,
    127       const UnloadedExtensionInfo::Reason reason) {}
    128 
    129   // Signaled when the extension system has completed its startup tasks.
    130   virtual const OneShotEvent& ready() const = 0;
    131 };
    132 
    133 // The ExtensionSystem for ProfileImpl and OffTheRecordProfileImpl.
    134 // Implementation details: non-shared services are owned by
    135 // ExtensionSystemImpl, a BrowserContextKeyedService with separate incognito
    136 // instances. A private Shared class (also a BrowserContextKeyedService,
    137 // but with a shared instance for incognito) keeps the common services.
    138 class ExtensionSystemImpl : public ExtensionSystem {
    139  public:
    140   explicit ExtensionSystemImpl(Profile* profile);
    141   virtual ~ExtensionSystemImpl();
    142 
    143   // BrowserContextKeyedService implementation.
    144   virtual void Shutdown() OVERRIDE;
    145 
    146   virtual void InitForRegularProfile(bool extensions_enabled) OVERRIDE;
    147 
    148   virtual ExtensionService* extension_service() OVERRIDE;  // shared
    149   virtual ManagementPolicy* management_policy() OVERRIDE;  // shared
    150   virtual UserScriptMaster* user_script_master() OVERRIDE;  // shared
    151   virtual ProcessManager* process_manager() OVERRIDE;
    152   virtual StateStore* state_store() OVERRIDE;  // shared
    153   virtual StateStore* rules_store() OVERRIDE;  // shared
    154   virtual LazyBackgroundTaskQueue* lazy_background_task_queue()
    155       OVERRIDE;  // shared
    156   virtual InfoMap* info_map() OVERRIDE; // shared
    157   virtual EventRouter* event_router() OVERRIDE;  // shared
    158   virtual ExtensionWarningService* warning_service() OVERRIDE;
    159   virtual Blacklist* blacklist() OVERRIDE;  // shared
    160   virtual ErrorConsole* error_console() OVERRIDE;
    161   virtual InstallVerifier* install_verifier() OVERRIDE;
    162 
    163   virtual void RegisterExtensionWithRequestContexts(
    164       const Extension* extension) OVERRIDE;
    165 
    166   virtual void UnregisterExtensionWithRequestContexts(
    167       const std::string& extension_id,
    168       const UnloadedExtensionInfo::Reason reason) OVERRIDE;
    169 
    170   virtual const OneShotEvent& ready() const OVERRIDE;
    171 
    172  private:
    173   friend class ExtensionSystemSharedFactory;
    174 
    175   // Owns the Extension-related systems that have a single instance
    176   // shared between normal and incognito profiles.
    177   class Shared : public BrowserContextKeyedService {
    178    public:
    179     explicit Shared(Profile* profile);
    180     virtual ~Shared();
    181 
    182     // Initialization takes place in phases.
    183     virtual void InitPrefs();
    184     // This must not be called until all the providers have been created.
    185     void RegisterManagementPolicyProviders();
    186     void Init(bool extensions_enabled);
    187 
    188     // BrowserContextKeyedService implementation.
    189     virtual void Shutdown() OVERRIDE;
    190 
    191     StateStore* state_store();
    192     StateStore* rules_store();
    193     ExtensionService* extension_service();
    194     ManagementPolicy* management_policy();
    195     UserScriptMaster* user_script_master();
    196     Blacklist* blacklist();
    197     InfoMap* info_map();
    198     LazyBackgroundTaskQueue* lazy_background_task_queue();
    199     EventRouter* event_router();
    200     ExtensionWarningService* warning_service();
    201     ErrorConsole* error_console();
    202     InstallVerifier* install_verifier();
    203     const OneShotEvent& ready() const { return ready_; }
    204 
    205    private:
    206     Profile* profile_;
    207 
    208     // The services that are shared between normal and incognito profiles.
    209 
    210     scoped_ptr<StateStore> state_store_;
    211     scoped_ptr<StateStore> rules_store_;
    212     // LazyBackgroundTaskQueue is a dependency of
    213     // MessageService and EventRouter.
    214     scoped_ptr<LazyBackgroundTaskQueue> lazy_background_task_queue_;
    215     scoped_ptr<EventRouter> event_router_;
    216     scoped_ptr<NavigationObserver> navigation_observer_;
    217     scoped_refptr<UserScriptMaster> user_script_master_;
    218     scoped_ptr<Blacklist> blacklist_;
    219     // StandardManagementPolicyProvider depends on Blacklist.
    220     scoped_ptr<StandardManagementPolicyProvider>
    221         standard_management_policy_provider_;
    222     // ExtensionService depends on StateStore and Blacklist.
    223     scoped_ptr<ExtensionService> extension_service_;
    224     scoped_ptr<ManagementPolicy> management_policy_;
    225     // extension_info_map_ needs to outlive process_manager_.
    226     scoped_refptr<InfoMap> extension_info_map_;
    227     scoped_ptr<ExtensionWarningService> extension_warning_service_;
    228     scoped_ptr<ExtensionWarningBadgeService> extension_warning_badge_service_;
    229     scoped_ptr<ErrorConsole> error_console_;
    230     scoped_ptr<InstallVerifier> install_verifier_;
    231 
    232 #if defined(OS_CHROMEOS)
    233     scoped_ptr<chromeos::DeviceLocalAccountManagementPolicyProvider>
    234         device_local_account_management_policy_provider_;
    235 #endif
    236 
    237     OneShotEvent ready_;
    238   };
    239 
    240   Profile* profile_;
    241 
    242   Shared* shared_;
    243 
    244   // |process_manager_| must be destroyed before the Profile's |io_data_|. While
    245   // |process_manager_| still lives, we handle incoming resource requests from
    246   // extension processes and those require access to the ResourceContext owned
    247   // by |io_data_|.
    248   scoped_ptr<ProcessManager> process_manager_;
    249 
    250   DISALLOW_COPY_AND_ASSIGN(ExtensionSystemImpl);
    251 };
    252 
    253 }  // namespace extensions
    254 
    255 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_
    256