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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/common/extensions/extension.h"
13 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
14 #include "extensions/common/one_shot_event.h"
16 class ExtensionInfoMap;
17 class ExtensionProcessManager;
18 class ExtensionService;
21 #if defined(OS_CHROMEOS)
23 class DeviceLocalAccountManagementPolicyProvider;
25 #endif // defined(OS_CHROMEOS)
31 namespace extensions {
36 class ExtensionSystemSharedFactory;
37 class ExtensionWarningBadgeService;
38 class ExtensionWarningService;
39 class LazyBackgroundTaskQueue;
40 class ManagementPolicy;
41 class NavigationObserver;
42 class StandardManagementPolicyProvider;
44 class UserScriptMaster;
46 // The ExtensionSystem manages the creation and destruction of services
47 // related to extensions. Most objects are shared between normal
48 // and incognito Profiles, except as called out in comments.
49 // This interface supports using TestExtensionSystem for TestingProfiles
50 // that don't want all of the extensions baggage in their tests.
51 class ExtensionSystem : public BrowserContextKeyedService {
54 virtual ~ExtensionSystem();
56 // Returns the instance for the given profile, or NULL if none. This is
57 // a convenience wrapper around ExtensionSystemFactory::GetForProfile.
58 static ExtensionSystem* Get(Profile* profile);
60 // Returns the same instance as Get() above.
61 static ExtensionSystem* GetForBrowserContext(
62 content::BrowserContext* profile);
64 // BrowserContextKeyedService implementation.
65 virtual void Shutdown() OVERRIDE {}
67 // Initializes extensions machinery.
68 // Component extensions are always enabled, external and user extensions
69 // are controlled by |extensions_enabled|. If |defer_background_creation| is
70 // true, then creation of background extension RenderViews will be deferred
71 // until ExtensionProcessManager::DeferBackgroundHostCreation is called with
72 // |defer| set to false.
73 virtual void InitForRegularProfile(bool extensions_enabled,
74 bool defer_background_creation) = 0;
76 // The ExtensionService is created at startup.
77 virtual ExtensionService* extension_service() = 0;
79 // The class controlling whether users are permitted to perform certain
80 // actions on extensions (install, uninstall, disable, etc.).
81 // The ManagementPolicy is created at startup.
82 virtual ManagementPolicy* management_policy() = 0;
84 // The UserScriptMaster is created at startup.
85 virtual UserScriptMaster* user_script_master() = 0;
87 // The ExtensionProcessManager is created at startup.
88 virtual ExtensionProcessManager* process_manager() = 0;
90 // The StateStore is created at startup.
91 virtual StateStore* state_store() = 0;
93 // The rules store is created at startup.
94 virtual StateStore* rules_store() = 0;
96 // Returns the IO-thread-accessible extension data.
97 virtual ExtensionInfoMap* info_map() = 0;
99 // The LazyBackgroundTaskQueue is created at startup.
100 virtual LazyBackgroundTaskQueue* lazy_background_task_queue() = 0;
102 // The EventRouter is created at startup.
103 virtual EventRouter* event_router() = 0;
105 // The ExtensionWarningService is created at startup.
106 virtual ExtensionWarningService* warning_service() = 0;
108 // The blacklist is created at startup.
109 virtual Blacklist* blacklist() = 0;
111 // The ErrorConsole is created at startup.
112 virtual ErrorConsole* error_console() = 0;
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) {}
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) {}
129 // Signaled when the extension system has completed its startup tasks.
130 virtual const OneShotEvent& ready() const = 0;
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 {
140 explicit ExtensionSystemImpl(Profile* profile);
141 virtual ~ExtensionSystemImpl();
143 // BrowserContextKeyedService implementation.
144 virtual void Shutdown() OVERRIDE;
146 virtual void InitForRegularProfile(bool extensions_enabled,
147 bool defer_background_creation) OVERRIDE;
149 virtual ExtensionService* extension_service() OVERRIDE; // shared
150 virtual ManagementPolicy* management_policy() OVERRIDE; // shared
151 virtual UserScriptMaster* user_script_master() OVERRIDE; // shared
152 virtual ExtensionProcessManager* process_manager() OVERRIDE;
153 virtual StateStore* state_store() OVERRIDE; // shared
154 virtual StateStore* rules_store() OVERRIDE; // shared
155 virtual LazyBackgroundTaskQueue* lazy_background_task_queue()
157 virtual ExtensionInfoMap* info_map() OVERRIDE; // shared
158 virtual EventRouter* event_router() OVERRIDE; // shared
159 virtual ExtensionWarningService* warning_service() OVERRIDE;
160 virtual Blacklist* blacklist() OVERRIDE; // shared
161 virtual ErrorConsole* error_console() OVERRIDE;
163 virtual void RegisterExtensionWithRequestContexts(
164 const Extension* extension) OVERRIDE;
166 virtual void UnregisterExtensionWithRequestContexts(
167 const std::string& extension_id,
168 const UnloadedExtensionInfo::Reason reason) OVERRIDE;
170 virtual const OneShotEvent& ready() const OVERRIDE;
173 friend class ExtensionSystemSharedFactory;
175 // Owns the Extension-related systems that have a single instance
176 // shared between normal and incognito profiles.
177 class Shared : public BrowserContextKeyedService {
179 explicit Shared(Profile* profile);
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);
188 // BrowserContextKeyedService implementation.
189 virtual void Shutdown() OVERRIDE;
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 ExtensionInfoMap* info_map();
198 LazyBackgroundTaskQueue* lazy_background_task_queue();
199 EventRouter* event_router();
200 ExtensionWarningService* warning_service();
201 ErrorConsole* error_console();
202 const OneShotEvent& ready() const { return ready_; }
207 // The services that are shared between normal and incognito profiles.
209 scoped_ptr<StateStore> state_store_;
210 scoped_ptr<StateStore> rules_store_;
211 // LazyBackgroundTaskQueue is a dependency of
212 // MessageService and EventRouter.
213 scoped_ptr<LazyBackgroundTaskQueue> lazy_background_task_queue_;
214 scoped_ptr<EventRouter> event_router_;
215 scoped_ptr<NavigationObserver> navigation_observer_;
216 scoped_refptr<UserScriptMaster> user_script_master_;
217 scoped_ptr<Blacklist> blacklist_;
218 // StandardManagementPolicyProvider depends on Blacklist.
219 scoped_ptr<StandardManagementPolicyProvider>
220 standard_management_policy_provider_;
221 // ExtensionService depends on StateStore and Blacklist.
222 scoped_ptr<ExtensionService> extension_service_;
223 scoped_ptr<ManagementPolicy> management_policy_;
224 // extension_info_map_ needs to outlive extension_process_manager_.
225 scoped_refptr<ExtensionInfoMap> extension_info_map_;
226 scoped_ptr<ExtensionWarningService> extension_warning_service_;
227 scoped_ptr<ExtensionWarningBadgeService> extension_warning_badge_service_;
228 scoped_ptr<ErrorConsole> error_console_;
230 #if defined(OS_CHROMEOS)
231 scoped_ptr<chromeos::DeviceLocalAccountManagementPolicyProvider>
232 device_local_account_management_policy_provider_;
242 // |extension_process_manager_| must be destroyed before the Profile's
243 // |io_data_|. While |extension_process_manager_| still lives, we handle
244 // incoming resource requests from extension processes and those require
245 // access to the ResourceContext owned by |io_data_|.
246 scoped_ptr<ExtensionProcessManager> extension_process_manager_;
248 DISALLOW_COPY_AND_ASSIGN(ExtensionSystemImpl);
251 } // namespace extensions
253 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_