OLD | NEW |
| (Empty) |
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_PROCESS_MANAGER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/time/time.h" | |
17 #include "content/public/browser/notification_observer.h" | |
18 #include "content/public/browser/notification_registrar.h" | |
19 #include "extensions/common/view_type.h" | |
20 | |
21 class GURL; | |
22 | |
23 namespace content { | |
24 class BrowserContext; | |
25 class DevToolsAgentHost; | |
26 class RenderViewHost; | |
27 class SiteInstance; | |
28 }; | |
29 | |
30 namespace extensions { | |
31 class Extension; | |
32 class ExtensionHost; | |
33 } | |
34 | |
35 // Manages dynamic state of running Chromium extensions. There is one instance | |
36 // of this class per Profile. OTR Profiles have a separate instance that keeps | |
37 // track of split-mode extensions only. | |
38 class ExtensionProcessManager : public content::NotificationObserver { | |
39 public: | |
40 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet; | |
41 typedef ExtensionHostSet::const_iterator const_iterator; | |
42 | |
43 static ExtensionProcessManager* Create(content::BrowserContext* context); | |
44 virtual ~ExtensionProcessManager(); | |
45 | |
46 const ExtensionHostSet& background_hosts() const { | |
47 return background_hosts_; | |
48 } | |
49 | |
50 typedef std::set<content::RenderViewHost*> ViewSet; | |
51 const ViewSet GetAllViews() const; | |
52 | |
53 // Creates a new UI-less extension instance. Like CreateViewHost, but not | |
54 // displayed anywhere. | |
55 virtual extensions::ExtensionHost* CreateBackgroundHost( | |
56 const extensions::Extension* extension, | |
57 const GURL& url); | |
58 | |
59 // Gets the ExtensionHost for the background page for an extension, or NULL if | |
60 // the extension isn't running or doesn't have a background page. | |
61 extensions::ExtensionHost* GetBackgroundHostForExtension( | |
62 const std::string& extension_id); | |
63 | |
64 // Returns the SiteInstance that the given URL belongs to. | |
65 // TODO(aa): This only returns correct results for extensions and packaged | |
66 // apps, not hosted apps. | |
67 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); | |
68 | |
69 // Unregisters a RenderViewHost as hosting any extension. | |
70 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host); | |
71 | |
72 // Returns all RenderViewHosts that are registered for the specified | |
73 // extension. | |
74 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension( | |
75 const std::string& extension_id); | |
76 | |
77 // Returns the extension associated with the specified RenderViewHost, or | |
78 // NULL. | |
79 const extensions::Extension* GetExtensionForRenderViewHost( | |
80 content::RenderViewHost* render_view_host); | |
81 | |
82 // Returns true if the (lazy) background host for the given extension has | |
83 // already been sent the unload event and is shutting down. | |
84 bool IsBackgroundHostClosing(const std::string& extension_id); | |
85 | |
86 // Getter and setter for the lazy background page's keepalive count. This is | |
87 // the count of how many outstanding "things" are keeping the page alive. | |
88 // When this reaches 0, we will begin the process of shutting down the page. | |
89 // "Things" include pending events, resource loads, and API calls. | |
90 int GetLazyKeepaliveCount(const extensions::Extension* extension); | |
91 int IncrementLazyKeepaliveCount(const extensions::Extension* extension); | |
92 int DecrementLazyKeepaliveCount(const extensions::Extension* extension); | |
93 | |
94 void IncrementLazyKeepaliveCountForView( | |
95 content::RenderViewHost* render_view_host); | |
96 | |
97 // Handles a response to the ShouldSuspend message, used for lazy background | |
98 // pages. | |
99 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id); | |
100 | |
101 // Same as above, for the Suspend message. | |
102 void OnSuspendAck(const std::string& extension_id); | |
103 | |
104 // Tracks network requests for a given RenderViewHost, used to know | |
105 // when network activity is idle for lazy background pages. | |
106 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host); | |
107 void OnNetworkRequestDone(content::RenderViewHost* render_view_host); | |
108 | |
109 // Prevents |extension|'s background page from being closed and sends the | |
110 // onSuspendCanceled() event to it. | |
111 void CancelSuspend(const extensions::Extension* extension); | |
112 | |
113 // If |defer| is true background host creation is to be deferred until this is | |
114 // called again with |defer| set to false, at which point all deferred | |
115 // background hosts will be created. Defaults to false. | |
116 void DeferBackgroundHostCreation(bool defer); | |
117 | |
118 // Ensures background hosts are loaded for a new browser window. | |
119 void OnBrowserWindowReady(); | |
120 | |
121 // Gets the BrowserContext associated with site_instance_ and all other | |
122 // related SiteInstances. | |
123 content::BrowserContext* GetBrowserContext() const; | |
124 | |
125 protected: | |
126 // If |context| is incognito pass the master context as |original_context|. | |
127 // Otherwise pass the same context for both. | |
128 ExtensionProcessManager(content::BrowserContext* context, | |
129 content::BrowserContext* original_context); | |
130 | |
131 // Called on browser shutdown to close our extension hosts. | |
132 void CloseBackgroundHosts(); | |
133 | |
134 // content::NotificationObserver: | |
135 virtual void Observe(int type, | |
136 const content::NotificationSource& source, | |
137 const content::NotificationDetails& details) OVERRIDE; | |
138 | |
139 // Load all background pages once the profile data is ready and the pages | |
140 // should be loaded. | |
141 void CreateBackgroundHostsForProfileStartup(); | |
142 | |
143 content::NotificationRegistrar registrar_; | |
144 | |
145 // The set of ExtensionHosts running viewless background extensions. | |
146 ExtensionHostSet background_hosts_; | |
147 | |
148 // A SiteInstance related to the SiteInstance for all extensions in | |
149 // this profile. We create it in such a way that a new | |
150 // browsing instance is created. This controls process grouping. | |
151 scoped_refptr<content::SiteInstance> site_instance_; | |
152 | |
153 private: | |
154 friend class ExtensionProcessManagerTest; | |
155 | |
156 // Extra information we keep for each extension's background page. | |
157 struct BackgroundPageData; | |
158 typedef std::string ExtensionId; | |
159 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; | |
160 typedef std::map<content::RenderViewHost*, | |
161 extensions::ViewType> ExtensionRenderViews; | |
162 | |
163 // Called just after |host| is created so it can be registered in our lists. | |
164 void OnBackgroundHostCreated(extensions::ExtensionHost* host); | |
165 | |
166 // Close the given |host| iff it's a background page. | |
167 void CloseBackgroundHost(extensions::ExtensionHost* host); | |
168 | |
169 // These are called when the extension transitions between idle and active. | |
170 // They control the process of closing the background page when idle. | |
171 void OnLazyBackgroundPageIdle(const std::string& extension_id, | |
172 int sequence_id); | |
173 void OnLazyBackgroundPageActive(const std::string& extension_id); | |
174 void CloseLazyBackgroundPageNow(const std::string& extension_id, | |
175 int sequence_id); | |
176 | |
177 // Potentially registers a RenderViewHost, if it is associated with an | |
178 // extension. Does nothing if this is not an extension renderer. | |
179 void RegisterRenderViewHost(content::RenderViewHost* render_view_host); | |
180 | |
181 // Unregister RenderViewHosts and clear background page data for an extension | |
182 // which has been unloaded. | |
183 void UnregisterExtension(const std::string& extension_id); | |
184 | |
185 // Clears background page data for this extension. | |
186 void ClearBackgroundPageData(const std::string& extension_id); | |
187 | |
188 // Returns true if loading background pages should be deferred. | |
189 bool DeferLoadingBackgroundHosts() const; | |
190 | |
191 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); | |
192 | |
193 // Contains all active extension-related RenderViewHost instances for all | |
194 // extensions. We also keep a cache of the host's view type, because that | |
195 // information is not accessible at registration/deregistration time. | |
196 ExtensionRenderViews all_extension_views_; | |
197 | |
198 BackgroundPageDataMap background_page_data_; | |
199 | |
200 // The time to delay between an extension becoming idle and | |
201 // sending a ShouldSuspend message; read from command-line switch. | |
202 base::TimeDelta event_page_idle_time_; | |
203 | |
204 // The time to delay between sending a ShouldSuspend message and | |
205 // sending a Suspend message; read from command-line switch. | |
206 base::TimeDelta event_page_suspending_time_; | |
207 | |
208 // If true, then creation of background hosts is suspended. | |
209 bool defer_background_host_creation_; | |
210 | |
211 // True if we have created the startup set of background hosts. | |
212 bool startup_background_hosts_created_; | |
213 | |
214 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; | |
215 | |
216 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_; | |
217 | |
218 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); | |
219 }; | |
220 | |
221 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | |
OLD | NEW |