blob: f2a1138f76274db367e606122fbf3f62a6d10383 [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
rdevlin.croninb48a98e2015-05-01 00:00:2892 // Returns the ExtensionHost for the given |render_frame_host|, if there is
93 // one.
94 ExtensionHost* GetExtensionHostForRenderFrameHost(
95 content::RenderFrameHost* render_frame_host);
96
[email protected]7042b682012-04-19 22:57:5197 // Returns true if the (lazy) background host for the given extension has
98 // already been sent the unload event and is shutting down.
99 bool IsBackgroundHostClosing(const std::string& extension_id);
100
rdevlin.cronin6ae04a012015-04-03 20:19:40101 // Returns the extension associated with the specified RenderFrameHost/
102 // WebContents, or null.
103 const Extension* GetExtensionForRenderFrameHost(
104 content::RenderFrameHost* render_frame_host);
105 const Extension* GetExtensionForWebContents(
106 content::WebContents* web_contents);
107
[email protected]720ad1312012-02-27 23:07:36108 // Getter and setter for the lazy background page's keepalive count. This is
109 // the count of how many outstanding "things" are keeping the page alive.
110 // When this reaches 0, we will begin the process of shutting down the page.
111 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07112 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32113 void IncrementLazyKeepaliveCount(const Extension* extension);
114 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36115
[email protected]388770152013-12-03 01:25:32116 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
117 // impulses will only keep the page alive for a limited amount of time unless
118 // called regularly.
119 void KeepaliveImpulse(const Extension* extension);
120
tommyclie86b2982015-03-16 20:16:45121 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11122 static void OnKeepaliveFromPlugin(int render_process_id,
123 int render_frame_id,
124 const std::string& extension_id);
125
[email protected]584e6572013-02-16 07:02:33126 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36127 // pages.
[email protected]77a538f2014-07-14 22:25:37128 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12129
[email protected]584e6572013-02-16 07:02:33130 // Same as above, for the Suspend message.
131 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12132
[email protected]9dfed872013-12-30 23:08:56133 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18134 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25135 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
136 uint64 request_id);
137 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
138 uint64 request_id);
[email protected]6baff0b52012-03-06 01:30:18139
[email protected]0d475e072012-07-26 02:30:42140 // Prevents |extension|'s background page from being closed and sends the
141 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07142 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42143
[email protected]9602db42014-07-25 05:24:37144 // Called on shutdown to close our extension hosts.
145 void CloseBackgroundHosts();
146
[email protected]b6cd7202014-01-07 17:46:55147 // Sets callbacks for testing keepalive impulse behavior.
rdevlin.cronin6ae04a012015-04-03 20:19:40148 using ImpulseCallbackForTesting =
149 base::Callback<void(const std::string& extension_id)>;
[email protected]b6cd7202014-01-07 17:46:55150 void SetKeepaliveImpulseCallbackForTesting(
151 const ImpulseCallbackForTesting& callback);
152 void SetKeepaliveImpulseDecrementCallbackForTesting(
153 const ImpulseCallbackForTesting& callback);
154
kmarshall24b29b22015-04-29 01:41:47155 // EventPageTracker implementation.
156 bool IsEventPageSuspended(const std::string& extension_id) override;
157 bool WakeEventPage(const std::string& extension_id,
158 const base::Callback<void(bool)>& callback) override;
159
yoza9bf5602014-09-19 02:03:31160 // Sets the time in milliseconds that an extension event page can
161 // be idle before it is shut down; must be > 0.
162 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
163
164 // Sets the time in milliseconds that an extension event page has
165 // between being notified of its impending unload and that unload
166 // happening.
167 static void SetEventPageSuspendingTimeForTesting(
168 unsigned suspending_time_msec);
169
[email protected]6b54fda2014-07-22 02:13:47170 // Creates a non-incognito instance for tests. |registry| allows unit tests
171 // to inject an ExtensionRegistry that is not managed by the usual
172 // BrowserContextKeyedServiceFactory system.
173 static ProcessManager* CreateForTesting(content::BrowserContext* context,
174 ExtensionRegistry* registry);
175
176 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08177 static ProcessManager* CreateIncognitoForTesting(
178 content::BrowserContext* incognito_context,
179 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47180 ExtensionRegistry* registry);
181
rdevlin.cronin6ae04a012015-04-03 20:19:40182 content::BrowserContext* browser_context() const { return browser_context_; }
183
184 const ExtensionHostSet& background_hosts() const {
185 return background_hosts_;
186 }
187
[email protected]6b54fda2014-07-22 02:13:47188 bool startup_background_hosts_created_for_test() const {
189 return startup_background_hosts_created_;
190 }
[email protected]b9f6ba32014-03-10 18:34:08191
[email protected]bc535ee52010-08-31 18:40:32192 protected:
reillyg0ea3fa902014-10-28 15:30:23193 static ProcessManager* Create(content::BrowserContext* context);
194
rdevlin.cronin6ae04a012015-04-03 20:19:40195 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47196 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
197 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07198 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47199 content::BrowserContext* original_context,
200 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32201
rdevlin.cronin6ae04a012015-04-03 20:19:40202 // Not owned. Also used by IncognitoProcessManager.
203 ExtensionRegistry* extension_registry_;
204
205 private:
206 friend class ProcessManagerFactory;
207 friend class ProcessManagerTest;
208
[email protected]6c2381d2011-10-19 02:52:53209 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24210 void Observe(int type,
211 const content::NotificationSource& source,
212 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32213
limasdfe0f061b2015-02-27 00:35:06214 // ExtensionRegistryObserver:
215 void OnExtensionLoaded(content::BrowserContext* browser_context,
216 const Extension* extension) override;
217 void OnExtensionUnloaded(content::BrowserContext* browser_context,
218 const Extension* extension,
219 UnloadedExtensionInfo::Reason reason) override;
220
[email protected]103f19f2012-04-02 19:30:12221 // Extra information we keep for each extension's background page.
222 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40223 struct ExtensionRenderFrameData;
224 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
225 using ExtensionRenderFrames =
226 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10227
[email protected]6b54fda2014-07-22 02:13:47228 // Load all background pages once the profile data is ready and the pages
229 // should be loaded.
230 void CreateStartupBackgroundHosts();
231
[email protected]94de8cb2013-11-07 06:29:21232 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07233 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21234
[email protected]06024c62011-10-20 20:57:12235 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07236 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12237
rdevlin.cronin6ae04a012015-04-03 20:19:40238 // If the frame isn't keeping the lazy background page alive, increments the
239 // keepalive count to do so.
240 void AcquireLazyKeepaliveCountForFrame(
241 content::RenderFrameHost* render_frame_host);
242
243 // If the frame is keeping the lazy background page alive, decrements the
244 // keepalive count to stop doing it.
245 void ReleaseLazyKeepaliveCountForFrame(
246 content::RenderFrameHost* render_frame_host);
247
[email protected]388770152013-12-03 01:25:32248 // Internal implementation of DecrementLazyKeepaliveCount with an
249 // |extension_id| known to have a lazy background page.
250 void DecrementLazyKeepaliveCount(const std::string& extension_id);
251
252 // Checks if keepalive impulses have occured, and adjusts keep alive count.
253 void OnKeepaliveImpulseCheck();
254
[email protected]6baff0b52012-03-06 01:30:18255 // These are called when the extension transitions between idle and active.
256 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20257 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37258 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18259 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42260 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37261 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18262
rdevlin.cronin6ae04a012015-04-03 20:19:40263 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
[email protected]06024c62011-10-20 20:57:12264
rdevlin.cronin6ae04a012015-04-03 20:19:40265 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10266 // which has been unloaded.
267 void UnregisterExtension(const std::string& extension_id);
268
[email protected]caffe702012-05-03 05:30:17269 // Clears background page data for this extension.
270 void ClearBackgroundPageData(const std::string& extension_id);
271
rdevlin.cronin6ae04a012015-04-03 20:19:40272 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02273
rdevlin.cronin6ae04a012015-04-03 20:19:40274 // The set of ExtensionHosts running viewless background extensions.
275 ExtensionHostSet background_hosts_;
276
277 // A SiteInstance related to the SiteInstance for all extensions in
278 // this profile. We create it in such a way that a new
279 // browsing instance is created. This controls process grouping.
280 scoped_refptr<content::SiteInstance> site_instance_;
281
282 // The browser context associated with the |site_instance_|.
283 content::BrowserContext* browser_context_;
284
285 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48286 // extensions. We also keep a cache of the host's view type, because that
287 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40288 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48289
[email protected]103f19f2012-04-02 19:30:12290 BackgroundPageDataMap background_page_data_;
291
[email protected]aa3dd492013-11-05 17:09:09292 // True if we have created the startup set of background hosts.
293 bool startup_background_hosts_created_;
294
[email protected]75e7bac2013-11-01 23:33:42295 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
296
[email protected]b6cd7202014-01-07 17:46:55297 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
298 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
299
[email protected]08d469b22014-03-31 00:51:24300 ObserverList<ProcessManagerObserver> observer_list_;
301
[email protected]77a538f2014-07-14 22:25:37302 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
303 // members. These IDs are tracked per extension in background_page_data_ and
304 // are used to verify that nothing has interrupted the process of closing a
305 // lazy background process.
306 //
307 // Any interruption obtains a new ID by incrementing
308 // last_background_close_sequence_id_ and storing it in background_page_data_
309 // for a particular extension. Callbacks and round-trip IPC messages store the
310 // value of the extension's close_sequence_id at the beginning of the process.
311 // Thus comparisons can be done to halt when IDs no longer match.
312 //
313 // This counter provides unique IDs even when BackgroundPageData objects are
314 // reset.
315 uint64 last_background_close_sequence_id_;
316
317 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07318 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16319
[email protected]98b6d942013-11-10 00:34:07320 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05321};
322
[email protected]98b6d942013-11-10 00:34:07323} // namespace extensions
324
325#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_