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