- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_system.h
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.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 #if defined(OS_CHROMEOS)
22 namespace chromeos {
23 class DeviceLocalAccountManagementPolicyProvider;
24 }
25 #endif  // defined(OS_CHROMEOS)
26
27 namespace content {
28 class BrowserContext;
29 }
30
31 namespace extensions {
32 class Blacklist;
33 class ErrorConsole;
34 class EventRouter;
35 class Extension;
36 class ExtensionSystemSharedFactory;
37 class ExtensionWarningBadgeService;
38 class ExtensionWarningService;
39 class LazyBackgroundTaskQueue;
40 class ManagementPolicy;
41 class NavigationObserver;
42 class StandardManagementPolicyProvider;
43 class StateStore;
44 class UserScriptMaster;
45
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 {
52  public:
53   ExtensionSystem();
54   virtual ~ExtensionSystem();
55
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);
59
60   // Returns the same instance as Get() above.
61   static ExtensionSystem* GetForBrowserContext(
62       content::BrowserContext* profile);
63
64   // BrowserContextKeyedService implementation.
65   virtual void Shutdown() OVERRIDE {}
66
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;
75
76   // The ExtensionService is created at startup.
77   virtual ExtensionService* extension_service() = 0;
78
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;
83
84   // The UserScriptMaster is created at startup.
85   virtual UserScriptMaster* user_script_master() = 0;
86
87   // The ExtensionProcessManager is created at startup.
88   virtual ExtensionProcessManager* process_manager() = 0;
89
90   // The StateStore is created at startup.
91   virtual StateStore* state_store() = 0;
92
93   // The rules store is created at startup.
94   virtual StateStore* rules_store() = 0;
95
96   // Returns the IO-thread-accessible extension data.
97   virtual ExtensionInfoMap* info_map() = 0;
98
99   // The LazyBackgroundTaskQueue is created at startup.
100   virtual LazyBackgroundTaskQueue* lazy_background_task_queue() = 0;
101
102   // The EventRouter is created at startup.
103   virtual EventRouter* event_router() = 0;
104
105   // The ExtensionWarningService is created at startup.
106   virtual ExtensionWarningService* warning_service() = 0;
107
108   // The blacklist is created at startup.
109   virtual Blacklist* blacklist() = 0;
110
111   // The ErrorConsole is created at startup.
112   virtual ErrorConsole* error_console() = 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,
147                                      bool defer_background_creation) OVERRIDE;
148
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()
156       OVERRIDE;  // shared
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;
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     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_; }
203
204    private:
205     Profile* profile_;
206
207     // The services that are shared between normal and incognito profiles.
208
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_;
229
230 #if defined(OS_CHROMEOS)
231     scoped_ptr<chromeos::DeviceLocalAccountManagementPolicyProvider>
232         device_local_account_management_policy_provider_;
233 #endif
234
235     OneShotEvent ready_;
236   };
237
238   Profile* profile_;
239
240   Shared* shared_;
241
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_;
247
248   DISALLOW_COPY_AND_ASSIGN(ExtensionSystemImpl);
249 };
250
251 }  // namespace extensions
252
253 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SYSTEM_H_