blob: c192e402be669762542f6218adc92b85958d7d0e [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"
dgozman47679eb12016-10-17 17:30:1821#include "content/public/browser/devtools_agent_host_observer.h"
[email protected]6c2381d2011-10-19 02:52:5322#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
kmarshall24b29b22015-04-29 01:41:4724#include "extensions/browser/event_page_tracker.h"
limasdfe0f061b2015-02-27 00:35:0625#include "extensions/browser/extension_registry_observer.h"
rdevlin.cronin0670b562016-07-02 02:05:4326#include "extensions/common/extension_id.h"
[email protected]cb2edf22013-04-01 20:25:2327#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0528
[email protected]481e1a42009-05-06 20:56:0529class GURL;
[email protected]b6583592012-01-25 19:52:3330
31namespace content {
[email protected]7f474212013-11-05 04:26:1632class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0233class DevToolsAgentHost;
[email protected]9dfed872013-12-30 23:08:5634class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0535class SiteInstance;
rdevlin.cronin6ae04a012015-04-03 20:19:4036class WebContents;
[email protected]b6583592012-01-25 19:52:3337};
[email protected]481e1a42009-05-06 20:56:0538
[email protected]1c321ee2012-05-21 03:02:3439namespace extensions {
[email protected]98b6d942013-11-10 00:34:0740
[email protected]1c321ee2012-05-21 03:02:3441class Extension;
[email protected]3a1dc572012-07-31 22:25:1342class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4743class ExtensionRegistry;
[email protected]08d469b22014-03-31 00:51:2444class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3445
[email protected]bc535ee52010-08-31 18:40:3246// Manages dynamic state of running Chromium extensions. There is one instance
47// of this class per Profile. OTR Profiles have a separate instance that keeps
48// track of split-mode extensions only.
reillyg0ea3fa902014-10-28 15:30:2349class ProcessManager : public KeyedService,
limasdfe0f061b2015-02-27 00:35:0650 public content::NotificationObserver,
kmarshall24b29b22015-04-29 01:41:4751 public ExtensionRegistryObserver,
dgozman47679eb12016-10-17 17:30:1852 public EventPageTracker,
53 public content::DevToolsAgentHostObserver {
[email protected]481e1a42009-05-06 20:56:0554 public:
rdevlin.cronin6ae04a012015-04-03 20:19:4055 using ExtensionHostSet = std::set<extensions::ExtensionHost*>;
[email protected]d1fe1352012-04-26 00:47:3256
reillyg0ea3fa902014-10-28 15:30:2357 static ProcessManager* Get(content::BrowserContext* context);
dcheng9168b2f2014-10-21 12:38:2458 ~ProcessManager() override;
[email protected]481e1a42009-05-06 20:56:0559
rdevlin.cronin6ae04a012015-04-03 20:19:4060 void RegisterRenderFrameHost(content::WebContents* web_contents,
61 content::RenderFrameHost* render_frame_host,
62 const Extension* extension);
63 void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host);
robcdcc4b82015-12-06 12:39:4564 void DidNavigateRenderFrameHost(content::RenderFrameHost* render_frame_host);
[email protected]d1fe1352012-04-26 00:47:3265
rdevlin.cronin6ae04a012015-04-03 20:19:4066 // Returns the SiteInstance that the given URL belongs to.
67 // TODO(aa): This only returns correct results for extensions and packaged
68 // apps, not hosted apps.
69 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
70 const GURL& url);
[email protected]d1fe1352012-04-26 00:47:3271
rdevlin.cronin6ae04a012015-04-03 20:19:4072 using FrameSet = std::set<content::RenderFrameHost*>;
73 const FrameSet GetAllFrames() const;
74
75 // Returns all RenderFrameHosts that are registered for the specified
76 // extension.
77 ProcessManager::FrameSet GetRenderFrameHostsForExtension(
78 const std::string& extension_id);
79
robcdcc4b82015-12-06 12:39:4580 bool IsRenderFrameHostRegistered(content::RenderFrameHost* render_frame_host);
81
[email protected]08d469b22014-03-31 00:51:2482 void AddObserver(ProcessManagerObserver* observer);
83 void RemoveObserver(ProcessManagerObserver* observer);
84
[email protected]029ad372011-05-20 17:12:5685 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4186 // displayed anywhere. Returns false if no background host can be created,
87 // for example for hosted apps and extensions that aren't enabled in
88 // Incognito.
89 virtual bool CreateBackgroundHost(const Extension* extension,
90 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3291
rdevlin.cronin6ae04a012015-04-03 20:19:4092 // Creates background hosts if the embedder is ready and they are not already
93 // loaded.
94 void MaybeCreateStartupBackgroundHosts();
95
96 // Gets the ExtensionHost for the background page for an extension, or null if
[email protected]4814b512009-11-07 00:12:2997 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0798 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2999
rdevlin.croninb48a98e2015-05-01 00:00:28100 // Returns the ExtensionHost for the given |render_frame_host|, if there is
101 // one.
102 ExtensionHost* GetExtensionHostForRenderFrameHost(
103 content::RenderFrameHost* render_frame_host);
104
[email protected]7042b682012-04-19 22:57:51105 // Returns true if the (lazy) background host for the given extension has
106 // already been sent the unload event and is shutting down.
107 bool IsBackgroundHostClosing(const std::string& extension_id);
108
rdevlin.cronin6ae04a012015-04-03 20:19:40109 // Returns the extension associated with the specified RenderFrameHost/
110 // WebContents, or null.
111 const Extension* GetExtensionForRenderFrameHost(
112 content::RenderFrameHost* render_frame_host);
113 const Extension* GetExtensionForWebContents(
lazyboy9db2523f2015-05-06 14:56:55114 const content::WebContents* web_contents);
rdevlin.cronin6ae04a012015-04-03 20:19:40115
[email protected]720ad1312012-02-27 23:07:36116 // Getter and setter for the lazy background page's keepalive count. This is
117 // the count of how many outstanding "things" are keeping the page alive.
118 // When this reaches 0, we will begin the process of shutting down the page.
119 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07120 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32121 void IncrementLazyKeepaliveCount(const Extension* extension);
122 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36123
[email protected]388770152013-12-03 01:25:32124 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
125 // impulses will only keep the page alive for a limited amount of time unless
126 // called regularly.
127 void KeepaliveImpulse(const Extension* extension);
128
tommyclie86b2982015-03-16 20:16:45129 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11130 static void OnKeepaliveFromPlugin(int render_process_id,
131 int render_frame_id,
132 const std::string& extension_id);
133
[email protected]584e6572013-02-16 07:02:33134 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36135 // pages.
avic9cec102015-12-23 00:39:26136 void OnShouldSuspendAck(const std::string& extension_id,
137 uint64_t sequence_id);
[email protected]103f19f2012-04-02 19:30:12138
[email protected]584e6572013-02-16 07:02:33139 // Same as above, for the Suspend message.
140 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12141
[email protected]9dfed872013-12-30 23:08:56142 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18143 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25144 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26145 uint64_t request_id);
chirantan79788f62015-02-02 23:57:25146 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26147 uint64_t request_id);
[email protected]6baff0b52012-03-06 01:30:18148
[email protected]0d475e072012-07-26 02:30:42149 // Prevents |extension|'s background page from being closed and sends the
150 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07151 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42152
[email protected]9602db42014-07-25 05:24:37153 // Called on shutdown to close our extension hosts.
154 void CloseBackgroundHosts();
155
[email protected]b6cd7202014-01-07 17:46:55156 // Sets callbacks for testing keepalive impulse behavior.
rdevlin.cronin6ae04a012015-04-03 20:19:40157 using ImpulseCallbackForTesting =
158 base::Callback<void(const std::string& extension_id)>;
[email protected]b6cd7202014-01-07 17:46:55159 void SetKeepaliveImpulseCallbackForTesting(
160 const ImpulseCallbackForTesting& callback);
161 void SetKeepaliveImpulseDecrementCallbackForTesting(
162 const ImpulseCallbackForTesting& callback);
163
kmarshall24b29b22015-04-29 01:41:47164 // EventPageTracker implementation.
165 bool IsEventPageSuspended(const std::string& extension_id) override;
166 bool WakeEventPage(const std::string& extension_id,
167 const base::Callback<void(bool)>& callback) override;
168
yoza9bf5602014-09-19 02:03:31169 // Sets the time in milliseconds that an extension event page can
170 // be idle before it is shut down; must be > 0.
171 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
172
173 // Sets the time in milliseconds that an extension event page has
174 // between being notified of its impending unload and that unload
175 // happening.
176 static void SetEventPageSuspendingTimeForTesting(
177 unsigned suspending_time_msec);
178
[email protected]6b54fda2014-07-22 02:13:47179 // Creates a non-incognito instance for tests. |registry| allows unit tests
180 // to inject an ExtensionRegistry that is not managed by the usual
181 // BrowserContextKeyedServiceFactory system.
182 static ProcessManager* CreateForTesting(content::BrowserContext* context,
183 ExtensionRegistry* registry);
184
185 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08186 static ProcessManager* CreateIncognitoForTesting(
187 content::BrowserContext* incognito_context,
188 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47189 ExtensionRegistry* registry);
190
rdevlin.cronin6ae04a012015-04-03 20:19:40191 content::BrowserContext* browser_context() const { return browser_context_; }
192
193 const ExtensionHostSet& background_hosts() const {
194 return background_hosts_;
195 }
196
[email protected]6b54fda2014-07-22 02:13:47197 bool startup_background_hosts_created_for_test() const {
198 return startup_background_hosts_created_;
199 }
[email protected]b9f6ba32014-03-10 18:34:08200
[email protected]bc535ee52010-08-31 18:40:32201 protected:
reillyg0ea3fa902014-10-28 15:30:23202 static ProcessManager* Create(content::BrowserContext* context);
203
rdevlin.cronin6ae04a012015-04-03 20:19:40204 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47205 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
206 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07207 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47208 content::BrowserContext* original_context,
209 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32210
rdevlin.cronin6ae04a012015-04-03 20:19:40211 // Not owned. Also used by IncognitoProcessManager.
212 ExtensionRegistry* extension_registry_;
213
214 private:
215 friend class ProcessManagerFactory;
216 friend class ProcessManagerTest;
217
[email protected]6c2381d2011-10-19 02:52:53218 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24219 void Observe(int type,
220 const content::NotificationSource& source,
221 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32222
limasdfe0f061b2015-02-27 00:35:06223 // ExtensionRegistryObserver:
224 void OnExtensionLoaded(content::BrowserContext* browser_context,
225 const Extension* extension) override;
226 void OnExtensionUnloaded(content::BrowserContext* browser_context,
227 const Extension* extension,
228 UnloadedExtensionInfo::Reason reason) override;
229
[email protected]103f19f2012-04-02 19:30:12230 // Extra information we keep for each extension's background page.
231 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40232 struct ExtensionRenderFrameData;
233 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
234 using ExtensionRenderFrames =
235 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10236
[email protected]6b54fda2014-07-22 02:13:47237 // Load all background pages once the profile data is ready and the pages
238 // should be loaded.
239 void CreateStartupBackgroundHosts();
240
[email protected]94de8cb2013-11-07 06:29:21241 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07242 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21243
[email protected]06024c62011-10-20 20:57:12244 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07245 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12246
rdevlin.cronin6ae04a012015-04-03 20:19:40247 // If the frame isn't keeping the lazy background page alive, increments the
248 // keepalive count to do so.
249 void AcquireLazyKeepaliveCountForFrame(
250 content::RenderFrameHost* render_frame_host);
251
252 // If the frame is keeping the lazy background page alive, decrements the
253 // keepalive count to stop doing it.
254 void ReleaseLazyKeepaliveCountForFrame(
255 content::RenderFrameHost* render_frame_host);
256
[email protected]388770152013-12-03 01:25:32257 // Internal implementation of DecrementLazyKeepaliveCount with an
258 // |extension_id| known to have a lazy background page.
259 void DecrementLazyKeepaliveCount(const std::string& extension_id);
260
261 // Checks if keepalive impulses have occured, and adjusts keep alive count.
262 void OnKeepaliveImpulseCheck();
263
[email protected]6baff0b52012-03-06 01:30:18264 // These are called when the extension transitions between idle and active.
265 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20266 void OnLazyBackgroundPageIdle(const std::string& extension_id,
avic9cec102015-12-23 00:39:26267 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18268 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42269 void CloseLazyBackgroundPageNow(const std::string& extension_id,
avic9cec102015-12-23 00:39:26270 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18271
dgozman47679eb12016-10-17 17:30:18272 const Extension* GetExtensionForAgentHost(
273 content::DevToolsAgentHost* agent_host);
274
275 // content::DevToolsAgentHostObserver overrides.
276 void DevToolsAgentHostAttached(
277 content::DevToolsAgentHost* agent_host) override;
278 void DevToolsAgentHostDetached(
279 content::DevToolsAgentHost* agent_host) override;
[email protected]06024c62011-10-20 20:57:12280
rdevlin.cronin6ae04a012015-04-03 20:19:40281 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10282 // which has been unloaded.
283 void UnregisterExtension(const std::string& extension_id);
284
[email protected]caffe702012-05-03 05:30:17285 // Clears background page data for this extension.
286 void ClearBackgroundPageData(const std::string& extension_id);
287
rdevlin.cronin6ae04a012015-04-03 20:19:40288 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02289
rdevlin.cronin6ae04a012015-04-03 20:19:40290 // The set of ExtensionHosts running viewless background extensions.
291 ExtensionHostSet background_hosts_;
292
293 // A SiteInstance related to the SiteInstance for all extensions in
294 // this profile. We create it in such a way that a new
295 // browsing instance is created. This controls process grouping.
296 scoped_refptr<content::SiteInstance> site_instance_;
297
298 // The browser context associated with the |site_instance_|.
299 content::BrowserContext* browser_context_;
300
301 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48302 // extensions. We also keep a cache of the host's view type, because that
303 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40304 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48305
[email protected]103f19f2012-04-02 19:30:12306 BackgroundPageDataMap background_page_data_;
307
[email protected]aa3dd492013-11-05 17:09:09308 // True if we have created the startup set of background hosts.
309 bool startup_background_hosts_created_;
310
[email protected]b6cd7202014-01-07 17:46:55311 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
312 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
313
brettw236d3172015-06-03 16:31:43314 base::ObserverList<ProcessManagerObserver> observer_list_;
[email protected]08d469b22014-03-31 00:51:24315
[email protected]77a538f2014-07-14 22:25:37316 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
317 // members. These IDs are tracked per extension in background_page_data_ and
318 // are used to verify that nothing has interrupted the process of closing a
319 // lazy background process.
320 //
321 // Any interruption obtains a new ID by incrementing
322 // last_background_close_sequence_id_ and storing it in background_page_data_
323 // for a particular extension. Callbacks and round-trip IPC messages store the
324 // value of the extension's close_sequence_id at the beginning of the process.
325 // Thus comparisons can be done to halt when IDs no longer match.
326 //
327 // This counter provides unique IDs even when BackgroundPageData objects are
328 // reset.
avic9cec102015-12-23 00:39:26329 uint64_t last_background_close_sequence_id_;
[email protected]77a538f2014-07-14 22:25:37330
rockote5703292015-09-28 23:23:09331 // Tracks pending network requests by opaque ID. This is used to ensure proper
332 // keepalive counting in response to request status updates; e.g., if an
333 // extension URLRequest is constructed and then destroyed without ever
334 // starting, we can receive a completion notification without a corresponding
335 // start notification. In that case we want to avoid decrementing keepalive.
naskod13fd292016-06-07 20:33:33336 std::map<int, ExtensionHost*> pending_network_requests_;
rockote5703292015-09-28 23:23:09337
[email protected]77a538f2014-07-14 22:25:37338 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07339 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16340
[email protected]98b6d942013-11-10 00:34:07341 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05342};
343
[email protected]98b6d942013-11-10 00:34:07344} // namespace extensions
345
346#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_