blob: 2b7b64fb7c1f248ff29fe4fc10bcdcc1fa386b30 [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
[email protected]45776222009-07-15 20:21:588#include <map>
[email protected]8a17bd52009-06-06 08:19:499#include <set>
[email protected]45776222009-07-15 20:21:5810#include <string>
[email protected]481e1a42009-05-06 20:56:0511
[email protected]a23f62e2013-04-26 13:13:0212#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5413#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2015#include "base/memory/weak_ptr.h"
[email protected]08d469b22014-03-31 00:51:2416#include "base/observer_list.h"
reillyg0ea3fa902014-10-28 15:30:2317#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
kmarshall24b29b22015-04-29 01:41:4720#include "extensions/browser/event_page_tracker.h"
limasdfe0f061b2015-02-27 00:35:0621#include "extensions/browser/extension_registry_observer.h"
rdevlin.cronin6ae04a012015-04-03 20:19:4022#include "extensions/common/extension.h"
[email protected]cb2edf22013-04-01 20:25:2323#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0524
[email protected]481e1a42009-05-06 20:56:0525class GURL;
[email protected]b6583592012-01-25 19:52:3326
27namespace content {
[email protected]7f474212013-11-05 04:26:1628class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0229class DevToolsAgentHost;
[email protected]9dfed872013-12-30 23:08:5630class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0531class SiteInstance;
rdevlin.cronin6ae04a012015-04-03 20:19:4032class WebContents;
[email protected]b6583592012-01-25 19:52:3333};
[email protected]481e1a42009-05-06 20:56:0534
[email protected]1c321ee2012-05-21 03:02:3435namespace extensions {
[email protected]98b6d942013-11-10 00:34:0736
[email protected]1c321ee2012-05-21 03:02:3437class Extension;
[email protected]3a1dc572012-07-31 22:25:1338class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4739class ExtensionRegistry;
[email protected]08d469b22014-03-31 00:51:2440class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3441
[email protected]bc535ee52010-08-31 18:40:3242// Manages dynamic state of running Chromium extensions. There is one instance
43// of this class per Profile. OTR Profiles have a separate instance that keeps
44// track of split-mode extensions only.
reillyg0ea3fa902014-10-28 15:30:2345class ProcessManager : public KeyedService,
limasdfe0f061b2015-02-27 00:35:0646 public content::NotificationObserver,
kmarshall24b29b22015-04-29 01:41:4747 public ExtensionRegistryObserver,
48 public EventPageTracker {
[email protected]481e1a42009-05-06 20:56:0549 public:
rdevlin.cronin6ae04a012015-04-03 20:19:4050 using ExtensionHostSet = std::set<extensions::ExtensionHost*>;
[email protected]d1fe1352012-04-26 00:47:3251
reillyg0ea3fa902014-10-28 15:30:2352 static ProcessManager* Get(content::BrowserContext* context);
dcheng9168b2f2014-10-21 12:38:2453 ~ProcessManager() override;
[email protected]481e1a42009-05-06 20:56:0554
rdevlin.cronin6ae04a012015-04-03 20:19:4055 void RegisterRenderFrameHost(content::WebContents* web_contents,
56 content::RenderFrameHost* render_frame_host,
57 const Extension* extension);
58 void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host);
[email protected]d1fe1352012-04-26 00:47:3259
rdevlin.cronin6ae04a012015-04-03 20:19:4060 // Returns the SiteInstance that the given URL belongs to.
61 // TODO(aa): This only returns correct results for extensions and packaged
62 // apps, not hosted apps.
63 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
64 const GURL& url);
[email protected]d1fe1352012-04-26 00:47:3265
rdevlin.cronin6ae04a012015-04-03 20:19:4066 using FrameSet = std::set<content::RenderFrameHost*>;
67 const FrameSet GetAllFrames() const;
68
69 // Returns all RenderFrameHosts that are registered for the specified
70 // extension.
71 ProcessManager::FrameSet GetRenderFrameHostsForExtension(
72 const std::string& extension_id);
73
[email protected]08d469b22014-03-31 00:51:2474 void AddObserver(ProcessManagerObserver* observer);
75 void RemoveObserver(ProcessManagerObserver* observer);
76
[email protected]029ad372011-05-20 17:12:5677 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4178 // displayed anywhere. Returns false if no background host can be created,
79 // for example for hosted apps and extensions that aren't enabled in
80 // Incognito.
81 virtual bool CreateBackgroundHost(const Extension* extension,
82 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3283
rdevlin.cronin6ae04a012015-04-03 20:19:4084 // Creates background hosts if the embedder is ready and they are not already
85 // loaded.
86 void MaybeCreateStartupBackgroundHosts();
87
88 // Gets the ExtensionHost for the background page for an extension, or null if
[email protected]4814b512009-11-07 00:12:2989 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0790 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2991
[email protected]7042b682012-04-19 22:57:5192 // Returns true if the (lazy) background host for the given extension has
93 // already been sent the unload event and is shutting down.
94 bool IsBackgroundHostClosing(const std::string& extension_id);
95
rdevlin.cronin6ae04a012015-04-03 20:19:4096 // Returns the extension associated with the specified RenderFrameHost/
97 // WebContents, or null.
98 const Extension* GetExtensionForRenderFrameHost(
99 content::RenderFrameHost* render_frame_host);
100 const Extension* GetExtensionForWebContents(
101 content::WebContents* web_contents);
102
[email protected]720ad1312012-02-27 23:07:36103 // Getter and setter for the lazy background page's keepalive count. This is
104 // the count of how many outstanding "things" are keeping the page alive.
105 // When this reaches 0, we will begin the process of shutting down the page.
106 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07107 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32108 void IncrementLazyKeepaliveCount(const Extension* extension);
109 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36110
[email protected]388770152013-12-03 01:25:32111 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
112 // impulses will only keep the page alive for a limited amount of time unless
113 // called regularly.
114 void KeepaliveImpulse(const Extension* extension);
115
tommyclie86b2982015-03-16 20:16:45116 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11117 static void OnKeepaliveFromPlugin(int render_process_id,
118 int render_frame_id,
119 const std::string& extension_id);
120
[email protected]584e6572013-02-16 07:02:33121 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36122 // pages.
[email protected]77a538f2014-07-14 22:25:37123 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12124
[email protected]584e6572013-02-16 07:02:33125 // Same as above, for the Suspend message.
126 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12127
[email protected]9dfed872013-12-30 23:08:56128 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18129 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25130 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
131 uint64 request_id);
132 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
133 uint64 request_id);
[email protected]6baff0b52012-03-06 01:30:18134
[email protected]0d475e072012-07-26 02:30:42135 // Prevents |extension|'s background page from being closed and sends the
136 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07137 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42138
[email protected]9602db42014-07-25 05:24:37139 // Called on shutdown to close our extension hosts.
140 void CloseBackgroundHosts();
141
[email protected]b6cd7202014-01-07 17:46:55142 // Sets callbacks for testing keepalive impulse behavior.
rdevlin.cronin6ae04a012015-04-03 20:19:40143 using ImpulseCallbackForTesting =
144 base::Callback<void(const std::string& extension_id)>;
[email protected]b6cd7202014-01-07 17:46:55145 void SetKeepaliveImpulseCallbackForTesting(
146 const ImpulseCallbackForTesting& callback);
147 void SetKeepaliveImpulseDecrementCallbackForTesting(
148 const ImpulseCallbackForTesting& callback);
149
kmarshall24b29b22015-04-29 01:41:47150 // EventPageTracker implementation.
151 bool IsEventPageSuspended(const std::string& extension_id) override;
152 bool WakeEventPage(const std::string& extension_id,
153 const base::Callback<void(bool)>& callback) override;
154
yoza9bf5602014-09-19 02:03:31155 // Sets the time in milliseconds that an extension event page can
156 // be idle before it is shut down; must be > 0.
157 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
158
159 // Sets the time in milliseconds that an extension event page has
160 // between being notified of its impending unload and that unload
161 // happening.
162 static void SetEventPageSuspendingTimeForTesting(
163 unsigned suspending_time_msec);
164
[email protected]6b54fda2014-07-22 02:13:47165 // Creates a non-incognito instance for tests. |registry| allows unit tests
166 // to inject an ExtensionRegistry that is not managed by the usual
167 // BrowserContextKeyedServiceFactory system.
168 static ProcessManager* CreateForTesting(content::BrowserContext* context,
169 ExtensionRegistry* registry);
170
171 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08172 static ProcessManager* CreateIncognitoForTesting(
173 content::BrowserContext* incognito_context,
174 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47175 ExtensionRegistry* registry);
176
rdevlin.cronin6ae04a012015-04-03 20:19:40177 content::BrowserContext* browser_context() const { return browser_context_; }
178
179 const ExtensionHostSet& background_hosts() const {
180 return background_hosts_;
181 }
182
[email protected]6b54fda2014-07-22 02:13:47183 bool startup_background_hosts_created_for_test() const {
184 return startup_background_hosts_created_;
185 }
[email protected]b9f6ba32014-03-10 18:34:08186
[email protected]bc535ee52010-08-31 18:40:32187 protected:
reillyg0ea3fa902014-10-28 15:30:23188 static ProcessManager* Create(content::BrowserContext* context);
189
rdevlin.cronin6ae04a012015-04-03 20:19:40190 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47191 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
192 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07193 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47194 content::BrowserContext* original_context,
195 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32196
rdevlin.cronin6ae04a012015-04-03 20:19:40197 // Not owned. Also used by IncognitoProcessManager.
198 ExtensionRegistry* extension_registry_;
199
200 private:
201 friend class ProcessManagerFactory;
202 friend class ProcessManagerTest;
203
[email protected]6c2381d2011-10-19 02:52:53204 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24205 void Observe(int type,
206 const content::NotificationSource& source,
207 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32208
limasdfe0f061b2015-02-27 00:35:06209 // ExtensionRegistryObserver:
210 void OnExtensionLoaded(content::BrowserContext* browser_context,
211 const Extension* extension) override;
212 void OnExtensionUnloaded(content::BrowserContext* browser_context,
213 const Extension* extension,
214 UnloadedExtensionInfo::Reason reason) override;
215
[email protected]103f19f2012-04-02 19:30:12216 // Extra information we keep for each extension's background page.
217 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40218 struct ExtensionRenderFrameData;
219 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
220 using ExtensionRenderFrames =
221 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10222
[email protected]6b54fda2014-07-22 02:13:47223 // Load all background pages once the profile data is ready and the pages
224 // should be loaded.
225 void CreateStartupBackgroundHosts();
226
[email protected]94de8cb2013-11-07 06:29:21227 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07228 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21229
[email protected]06024c62011-10-20 20:57:12230 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07231 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12232
rdevlin.cronin6ae04a012015-04-03 20:19:40233 // If the frame isn't keeping the lazy background page alive, increments the
234 // keepalive count to do so.
235 void AcquireLazyKeepaliveCountForFrame(
236 content::RenderFrameHost* render_frame_host);
237
238 // If the frame is keeping the lazy background page alive, decrements the
239 // keepalive count to stop doing it.
240 void ReleaseLazyKeepaliveCountForFrame(
241 content::RenderFrameHost* render_frame_host);
242
[email protected]388770152013-12-03 01:25:32243 // Internal implementation of DecrementLazyKeepaliveCount with an
244 // |extension_id| known to have a lazy background page.
245 void DecrementLazyKeepaliveCount(const std::string& extension_id);
246
247 // Checks if keepalive impulses have occured, and adjusts keep alive count.
248 void OnKeepaliveImpulseCheck();
249
[email protected]6baff0b52012-03-06 01:30:18250 // These are called when the extension transitions between idle and active.
251 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20252 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37253 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18254 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42255 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37256 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18257
rdevlin.cronin6ae04a012015-04-03 20:19:40258 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
[email protected]06024c62011-10-20 20:57:12259
rdevlin.cronin6ae04a012015-04-03 20:19:40260 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10261 // which has been unloaded.
262 void UnregisterExtension(const std::string& extension_id);
263
[email protected]caffe702012-05-03 05:30:17264 // Clears background page data for this extension.
265 void ClearBackgroundPageData(const std::string& extension_id);
266
rdevlin.cronin6ae04a012015-04-03 20:19:40267 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02268
rdevlin.cronin6ae04a012015-04-03 20:19:40269 // The set of ExtensionHosts running viewless background extensions.
270 ExtensionHostSet background_hosts_;
271
272 // A SiteInstance related to the SiteInstance for all extensions in
273 // this profile. We create it in such a way that a new
274 // browsing instance is created. This controls process grouping.
275 scoped_refptr<content::SiteInstance> site_instance_;
276
277 // The browser context associated with the |site_instance_|.
278 content::BrowserContext* browser_context_;
279
280 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48281 // extensions. We also keep a cache of the host's view type, because that
282 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40283 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48284
[email protected]103f19f2012-04-02 19:30:12285 BackgroundPageDataMap background_page_data_;
286
[email protected]aa3dd492013-11-05 17:09:09287 // True if we have created the startup set of background hosts.
288 bool startup_background_hosts_created_;
289
[email protected]75e7bac2013-11-01 23:33:42290 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
291
[email protected]b6cd7202014-01-07 17:46:55292 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
293 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
294
[email protected]08d469b22014-03-31 00:51:24295 ObserverList<ProcessManagerObserver> observer_list_;
296
[email protected]77a538f2014-07-14 22:25:37297 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
298 // members. These IDs are tracked per extension in background_page_data_ and
299 // are used to verify that nothing has interrupted the process of closing a
300 // lazy background process.
301 //
302 // Any interruption obtains a new ID by incrementing
303 // last_background_close_sequence_id_ and storing it in background_page_data_
304 // for a particular extension. Callbacks and round-trip IPC messages store the
305 // value of the extension's close_sequence_id at the beginning of the process.
306 // Thus comparisons can be done to halt when IDs no longer match.
307 //
308 // This counter provides unique IDs even when BackgroundPageData objects are
309 // reset.
310 uint64 last_background_close_sequence_id_;
311
312 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07313 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16314
[email protected]98b6d942013-11-10 00:34:07315 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05316};
317
[email protected]98b6d942013-11-10 00:34:07318} // namespace extensions
319
320#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_