blob: 65ee76f8a1d2ccfda3c83d1f336cd8551aed3a15 [file] [log] [blame]
[email protected]98b6d942013-11-10 00:34:071// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]481e1a42009-05-06 20:56:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]98b6d942013-11-10 00:34:075#ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6#define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
[email protected]481e1a42009-05-06 20:56:057
avic9cec102015-12-23 00:39:268#include <stdint.h>
9
[email protected]45776222009-07-15 20:21:5810#include <map>
[email protected]8a17bd52009-06-06 08:19:4911#include <set>
[email protected]45776222009-07-15 20:21:5812#include <string>
[email protected]481e1a42009-05-06 20:56:0513
[email protected]a23f62e2013-04-26 13:13:0214#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5415#include "base/compiler_specific.h"
avic9cec102015-12-23 00:39:2616#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2018#include "base/memory/weak_ptr.h"
[email protected]08d469b22014-03-31 00:51:2419#include "base/observer_list.h"
reillyg0ea3fa902014-10-28 15:30:2320#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5321#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
kmarshall24b29b22015-04-29 01:41:4723#include "extensions/browser/event_page_tracker.h"
limasdfe0f061b2015-02-27 00:35:0624#include "extensions/browser/extension_registry_observer.h"
rdevlin.cronin6ae04a012015-04-03 20:19:4025#include "extensions/common/extension.h"
[email protected]cb2edf22013-04-01 20:25:2326#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0527
[email protected]481e1a42009-05-06 20:56:0528class GURL;
[email protected]b6583592012-01-25 19:52:3329
30namespace content {
[email protected]7f474212013-11-05 04:26:1631class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0232class DevToolsAgentHost;
[email protected]9dfed872013-12-30 23:08:5633class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0534class SiteInstance;
rdevlin.cronin6ae04a012015-04-03 20:19:4035class WebContents;
[email protected]b6583592012-01-25 19:52:3336};
[email protected]481e1a42009-05-06 20:56:0537
[email protected]1c321ee2012-05-21 03:02:3438namespace extensions {
[email protected]98b6d942013-11-10 00:34:0739
[email protected]1c321ee2012-05-21 03:02:3440class Extension;
[email protected]3a1dc572012-07-31 22:25:1341class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4742class ExtensionRegistry;
[email protected]08d469b22014-03-31 00:51:2443class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3444
[email protected]bc535ee52010-08-31 18:40:3245// Manages dynamic state of running Chromium extensions. There is one instance
46// of this class per Profile. OTR Profiles have a separate instance that keeps
47// track of split-mode extensions only.
reillyg0ea3fa902014-10-28 15:30:2348class ProcessManager : public KeyedService,
limasdfe0f061b2015-02-27 00:35:0649 public content::NotificationObserver,
kmarshall24b29b22015-04-29 01:41:4750 public ExtensionRegistryObserver,
51 public EventPageTracker {
[email protected]481e1a42009-05-06 20:56:0552 public:
rdevlin.cronin6ae04a012015-04-03 20:19:4053 using ExtensionHostSet = std::set<extensions::ExtensionHost*>;
[email protected]d1fe1352012-04-26 00:47:3254
reillyg0ea3fa902014-10-28 15:30:2355 static ProcessManager* Get(content::BrowserContext* context);
dcheng9168b2f2014-10-21 12:38:2456 ~ProcessManager() override;
[email protected]481e1a42009-05-06 20:56:0557
rdevlin.cronin6ae04a012015-04-03 20:19:4058 void RegisterRenderFrameHost(content::WebContents* web_contents,
59 content::RenderFrameHost* render_frame_host,
60 const Extension* extension);
61 void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host);
robcdcc4b82015-12-06 12:39:4562 void DidNavigateRenderFrameHost(content::RenderFrameHost* render_frame_host);
[email protected]d1fe1352012-04-26 00:47:3263
rdevlin.cronin6ae04a012015-04-03 20:19:4064 // 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 scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
68 const GURL& url);
[email protected]d1fe1352012-04-26 00:47:3269
rdevlin.cronin6ae04a012015-04-03 20:19:4070 using FrameSet = std::set<content::RenderFrameHost*>;
71 const FrameSet GetAllFrames() const;
72
73 // Returns all RenderFrameHosts that are registered for the specified
74 // extension.
75 ProcessManager::FrameSet GetRenderFrameHostsForExtension(
76 const std::string& extension_id);
77
robcdcc4b82015-12-06 12:39:4578 bool IsRenderFrameHostRegistered(content::RenderFrameHost* render_frame_host);
79
[email protected]08d469b22014-03-31 00:51:2480 void AddObserver(ProcessManagerObserver* observer);
81 void RemoveObserver(ProcessManagerObserver* observer);
82
[email protected]029ad372011-05-20 17:12:5683 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4184 // displayed anywhere. Returns false if no background host can be created,
85 // for example for hosted apps and extensions that aren't enabled in
86 // Incognito.
87 virtual bool CreateBackgroundHost(const Extension* extension,
88 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3289
rdevlin.cronin6ae04a012015-04-03 20:19:4090 // Creates background hosts if the embedder is ready and they are not already
91 // loaded.
92 void MaybeCreateStartupBackgroundHosts();
93
94 // Gets the ExtensionHost for the background page for an extension, or null if
[email protected]4814b512009-11-07 00:12:2995 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0796 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2997
rdevlin.croninb48a98e2015-05-01 00:00:2898 // Returns the ExtensionHost for the given |render_frame_host|, if there is
99 // one.
100 ExtensionHost* GetExtensionHostForRenderFrameHost(
101 content::RenderFrameHost* render_frame_host);
102
[email protected]7042b682012-04-19 22:57:51103 // Returns true if the (lazy) background host for the given extension has
104 // already been sent the unload event and is shutting down.
105 bool IsBackgroundHostClosing(const std::string& extension_id);
106
rdevlin.cronin6ae04a012015-04-03 20:19:40107 // Returns the extension associated with the specified RenderFrameHost/
108 // WebContents, or null.
109 const Extension* GetExtensionForRenderFrameHost(
110 content::RenderFrameHost* render_frame_host);
111 const Extension* GetExtensionForWebContents(
lazyboy9db2523f2015-05-06 14:56:55112 const content::WebContents* web_contents);
rdevlin.cronin6ae04a012015-04-03 20:19:40113
[email protected]720ad1312012-02-27 23:07:36114 // Getter and setter for the lazy background page's keepalive count. This is
115 // the count of how many outstanding "things" are keeping the page alive.
116 // When this reaches 0, we will begin the process of shutting down the page.
117 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07118 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32119 void IncrementLazyKeepaliveCount(const Extension* extension);
120 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36121
[email protected]388770152013-12-03 01:25:32122 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
123 // impulses will only keep the page alive for a limited amount of time unless
124 // called regularly.
125 void KeepaliveImpulse(const Extension* extension);
126
tommyclie86b2982015-03-16 20:16:45127 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11128 static void OnKeepaliveFromPlugin(int render_process_id,
129 int render_frame_id,
130 const std::string& extension_id);
131
[email protected]584e6572013-02-16 07:02:33132 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36133 // pages.
avic9cec102015-12-23 00:39:26134 void OnShouldSuspendAck(const std::string& extension_id,
135 uint64_t sequence_id);
[email protected]103f19f2012-04-02 19:30:12136
[email protected]584e6572013-02-16 07:02:33137 // Same as above, for the Suspend message.
138 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12139
[email protected]9dfed872013-12-30 23:08:56140 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18141 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25142 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26143 uint64_t request_id);
chirantan79788f62015-02-02 23:57:25144 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26145 uint64_t request_id);
[email protected]6baff0b52012-03-06 01:30:18146
[email protected]0d475e072012-07-26 02:30:42147 // Prevents |extension|'s background page from being closed and sends the
148 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07149 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42150
[email protected]9602db42014-07-25 05:24:37151 // Called on shutdown to close our extension hosts.
152 void CloseBackgroundHosts();
153
[email protected]b6cd7202014-01-07 17:46:55154 // Sets callbacks for testing keepalive impulse behavior.
rdevlin.cronin6ae04a012015-04-03 20:19:40155 using ImpulseCallbackForTesting =
156 base::Callback<void(const std::string& extension_id)>;
[email protected]b6cd7202014-01-07 17:46:55157 void SetKeepaliveImpulseCallbackForTesting(
158 const ImpulseCallbackForTesting& callback);
159 void SetKeepaliveImpulseDecrementCallbackForTesting(
160 const ImpulseCallbackForTesting& callback);
161
kmarshall24b29b22015-04-29 01:41:47162 // EventPageTracker implementation.
163 bool IsEventPageSuspended(const std::string& extension_id) override;
164 bool WakeEventPage(const std::string& extension_id,
165 const base::Callback<void(bool)>& callback) override;
166
yoza9bf5602014-09-19 02:03:31167 // Sets the time in milliseconds that an extension event page can
168 // be idle before it is shut down; must be > 0.
169 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
170
171 // Sets the time in milliseconds that an extension event page has
172 // between being notified of its impending unload and that unload
173 // happening.
174 static void SetEventPageSuspendingTimeForTesting(
175 unsigned suspending_time_msec);
176
[email protected]6b54fda2014-07-22 02:13:47177 // Creates a non-incognito instance for tests. |registry| allows unit tests
178 // to inject an ExtensionRegistry that is not managed by the usual
179 // BrowserContextKeyedServiceFactory system.
180 static ProcessManager* CreateForTesting(content::BrowserContext* context,
181 ExtensionRegistry* registry);
182
183 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08184 static ProcessManager* CreateIncognitoForTesting(
185 content::BrowserContext* incognito_context,
186 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47187 ExtensionRegistry* registry);
188
rdevlin.cronin6ae04a012015-04-03 20:19:40189 content::BrowserContext* browser_context() const { return browser_context_; }
190
191 const ExtensionHostSet& background_hosts() const {
192 return background_hosts_;
193 }
194
[email protected]6b54fda2014-07-22 02:13:47195 bool startup_background_hosts_created_for_test() const {
196 return startup_background_hosts_created_;
197 }
[email protected]b9f6ba32014-03-10 18:34:08198
[email protected]bc535ee52010-08-31 18:40:32199 protected:
reillyg0ea3fa902014-10-28 15:30:23200 static ProcessManager* Create(content::BrowserContext* context);
201
rdevlin.cronin6ae04a012015-04-03 20:19:40202 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47203 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
204 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07205 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47206 content::BrowserContext* original_context,
207 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32208
rdevlin.cronin6ae04a012015-04-03 20:19:40209 // Not owned. Also used by IncognitoProcessManager.
210 ExtensionRegistry* extension_registry_;
211
212 private:
213 friend class ProcessManagerFactory;
214 friend class ProcessManagerTest;
215
[email protected]6c2381d2011-10-19 02:52:53216 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24217 void Observe(int type,
218 const content::NotificationSource& source,
219 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32220
limasdfe0f061b2015-02-27 00:35:06221 // ExtensionRegistryObserver:
222 void OnExtensionLoaded(content::BrowserContext* browser_context,
223 const Extension* extension) override;
224 void OnExtensionUnloaded(content::BrowserContext* browser_context,
225 const Extension* extension,
226 UnloadedExtensionInfo::Reason reason) override;
227
[email protected]103f19f2012-04-02 19:30:12228 // Extra information we keep for each extension's background page.
229 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40230 struct ExtensionRenderFrameData;
231 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
232 using ExtensionRenderFrames =
233 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10234
[email protected]6b54fda2014-07-22 02:13:47235 // Load all background pages once the profile data is ready and the pages
236 // should be loaded.
237 void CreateStartupBackgroundHosts();
238
[email protected]94de8cb2013-11-07 06:29:21239 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07240 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21241
[email protected]06024c62011-10-20 20:57:12242 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07243 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12244
rdevlin.cronin6ae04a012015-04-03 20:19:40245 // If the frame isn't keeping the lazy background page alive, increments the
246 // keepalive count to do so.
247 void AcquireLazyKeepaliveCountForFrame(
248 content::RenderFrameHost* render_frame_host);
249
250 // If the frame is keeping the lazy background page alive, decrements the
251 // keepalive count to stop doing it.
252 void ReleaseLazyKeepaliveCountForFrame(
253 content::RenderFrameHost* render_frame_host);
254
[email protected]388770152013-12-03 01:25:32255 // Internal implementation of DecrementLazyKeepaliveCount with an
256 // |extension_id| known to have a lazy background page.
257 void DecrementLazyKeepaliveCount(const std::string& extension_id);
258
259 // Checks if keepalive impulses have occured, and adjusts keep alive count.
260 void OnKeepaliveImpulseCheck();
261
[email protected]6baff0b52012-03-06 01:30:18262 // These are called when the extension transitions between idle and active.
263 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20264 void OnLazyBackgroundPageIdle(const std::string& extension_id,
avic9cec102015-12-23 00:39:26265 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18266 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42267 void CloseLazyBackgroundPageNow(const std::string& extension_id,
avic9cec102015-12-23 00:39:26268 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18269
rdevlin.cronin6ae04a012015-04-03 20:19:40270 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
[email protected]06024c62011-10-20 20:57:12271
rdevlin.cronin6ae04a012015-04-03 20:19:40272 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10273 // which has been unloaded.
274 void UnregisterExtension(const std::string& extension_id);
275
[email protected]caffe702012-05-03 05:30:17276 // Clears background page data for this extension.
277 void ClearBackgroundPageData(const std::string& extension_id);
278
rdevlin.cronin6ae04a012015-04-03 20:19:40279 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02280
rdevlin.cronin6ae04a012015-04-03 20:19:40281 // The set of ExtensionHosts running viewless background extensions.
282 ExtensionHostSet background_hosts_;
283
284 // A SiteInstance related to the SiteInstance for all extensions in
285 // this profile. We create it in such a way that a new
286 // browsing instance is created. This controls process grouping.
287 scoped_refptr<content::SiteInstance> site_instance_;
288
289 // The browser context associated with the |site_instance_|.
290 content::BrowserContext* browser_context_;
291
292 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48293 // extensions. We also keep a cache of the host's view type, because that
294 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40295 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48296
[email protected]103f19f2012-04-02 19:30:12297 BackgroundPageDataMap background_page_data_;
298
[email protected]aa3dd492013-11-05 17:09:09299 // True if we have created the startup set of background hosts.
300 bool startup_background_hosts_created_;
301
[email protected]75e7bac2013-11-01 23:33:42302 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
303
[email protected]b6cd7202014-01-07 17:46:55304 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
305 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
306
brettw236d3172015-06-03 16:31:43307 base::ObserverList<ProcessManagerObserver> observer_list_;
[email protected]08d469b22014-03-31 00:51:24308
[email protected]77a538f2014-07-14 22:25:37309 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
310 // members. These IDs are tracked per extension in background_page_data_ and
311 // are used to verify that nothing has interrupted the process of closing a
312 // lazy background process.
313 //
314 // Any interruption obtains a new ID by incrementing
315 // last_background_close_sequence_id_ and storing it in background_page_data_
316 // for a particular extension. Callbacks and round-trip IPC messages store the
317 // value of the extension's close_sequence_id at the beginning of the process.
318 // Thus comparisons can be done to halt when IDs no longer match.
319 //
320 // This counter provides unique IDs even when BackgroundPageData objects are
321 // reset.
avic9cec102015-12-23 00:39:26322 uint64_t last_background_close_sequence_id_;
[email protected]77a538f2014-07-14 22:25:37323
rockote5703292015-09-28 23:23:09324 // Tracks pending network requests by opaque ID. This is used to ensure proper
325 // keepalive counting in response to request status updates; e.g., if an
326 // extension URLRequest is constructed and then destroyed without ever
327 // starting, we can receive a completion notification without a corresponding
328 // start notification. In that case we want to avoid decrementing keepalive.
329 std::set<int> pending_network_requests_;
330
[email protected]77a538f2014-07-14 22:25:37331 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07332 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16333
[email protected]98b6d942013-11-10 00:34:07334 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05335};
336
[email protected]98b6d942013-11-10 00:34:07337} // namespace extensions
338
339#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_