blob: d897d292f90e0c5f7664c97c6fd16e6ac52f0672 [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"
[email protected]41a17c52013-06-28 00:27:5317#include "base/time/time.h"
reillyg0ea3fa902014-10-28 15:30:2318#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5319#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
limasdfe0f061b2015-02-27 00:35:0621#include "extensions/browser/extension_registry_observer.h"
[email protected]cb2edf22013-04-01 20:25:2322#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0523
[email protected]481e1a42009-05-06 20:56:0524class GURL;
[email protected]b6583592012-01-25 19:52:3325
26namespace content {
[email protected]7f474212013-11-05 04:26:1627class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0228class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1129class RenderViewHost;
[email protected]9dfed872013-12-30 23:08:5630class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0531class SiteInstance;
[email protected]b6583592012-01-25 19:52:3332};
[email protected]481e1a42009-05-06 20:56:0533
[email protected]1c321ee2012-05-21 03:02:3434namespace extensions {
[email protected]98b6d942013-11-10 00:34:0735
[email protected]1c321ee2012-05-21 03:02:3436class Extension;
[email protected]3a1dc572012-07-31 22:25:1337class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4738class ExtensionRegistry;
39class ProcessManagerDelegate;
[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,
47 public ExtensionRegistryObserver {
[email protected]481e1a42009-05-06 20:56:0548 public:
[email protected]3a1dc572012-07-31 22:25:1349 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3250 typedef ExtensionHostSet::const_iterator const_iterator;
51
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
[email protected]d1fe1352012-04-26 00:47:3255 const ExtensionHostSet& background_hosts() const {
56 return background_hosts_;
57 }
58
[email protected]d1fe1352012-04-26 00:47:3259 typedef std::set<content::RenderViewHost*> ViewSet;
60 const ViewSet GetAllViews() const;
61
[email protected]08d469b22014-03-31 00:51:2462 // The typical observer interface.
63 void AddObserver(ProcessManagerObserver* observer);
64 void RemoveObserver(ProcessManagerObserver* observer);
65
[email protected]029ad372011-05-20 17:12:5666 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4167 // displayed anywhere. Returns false if no background host can be created,
68 // for example for hosted apps and extensions that aren't enabled in
69 // Incognito.
70 virtual bool CreateBackgroundHost(const Extension* extension,
71 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3272
[email protected]4814b512009-11-07 00:12:2973 // Gets the ExtensionHost for the background page for an extension, or NULL if
74 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0775 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2976
[email protected]481e1a42009-05-06 20:56:0577 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4678 // TODO(aa): This only returns correct results for extensions and packaged
79 // apps, not hosted apps.
rdevlin.cronin3d4261522015-02-10 00:48:1580 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
81 const GURL& url);
[email protected]481e1a42009-05-06 20:56:0582
hashimoto5bca2272014-11-04 07:35:1883 // If the view isn't keeping the lazy background page alive, increments the
84 // keepalive count to do so.
85 void AcquireLazyKeepaliveCountForView(
86 content::RenderViewHost* render_view_host);
87
88 // If the view is keeping the lazy background page alive, decrements the
89 // keepalive count to stop doing it.
90 void ReleaseLazyKeepaliveCountForView(
91 content::RenderViewHost* render_view_host);
92
[email protected]3e194992011-10-20 05:39:1093 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1194 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1095
96 // Returns all RenderViewHosts that are registered for the specified
97 // extension.
[email protected]eaabba22012-03-07 15:02:1198 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1099 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:58100
[email protected]d1fe1352012-04-26 00:47:32101 // Returns the extension associated with the specified RenderViewHost, or
102 // NULL.
[email protected]98b6d942013-11-10 00:34:07103 const Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32104 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09105
[email protected]7042b682012-04-19 22:57:51106 // Returns true if the (lazy) background host for the given extension has
107 // already been sent the unload event and is shutting down.
108 bool IsBackgroundHostClosing(const std::string& extension_id);
109
[email protected]720ad1312012-02-27 23:07:36110 // Getter and setter for the lazy background page's keepalive count. This is
111 // the count of how many outstanding "things" are keeping the page alive.
112 // When this reaches 0, we will begin the process of shutting down the page.
113 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07114 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32115 void IncrementLazyKeepaliveCount(const Extension* extension);
116 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36117
[email protected]388770152013-12-03 01:25:32118 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
119 // impulses will only keep the page alive for a limited amount of time unless
120 // called regularly.
121 void KeepaliveImpulse(const Extension* extension);
122
tommyclie86b2982015-03-16 20:16:45123 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11124 static void OnKeepaliveFromPlugin(int render_process_id,
125 int render_frame_id,
126 const std::string& extension_id);
127
[email protected]584e6572013-02-16 07:02:33128 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36129 // pages.
[email protected]77a538f2014-07-14 22:25:37130 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12131
[email protected]584e6572013-02-16 07:02:33132 // Same as above, for the Suspend message.
133 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12134
[email protected]9dfed872013-12-30 23:08:56135 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18136 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25137 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
138 uint64 request_id);
139 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
140 uint64 request_id);
[email protected]6baff0b52012-03-06 01:30:18141
[email protected]0d475e072012-07-26 02:30:42142 // Prevents |extension|'s background page from being closed and sends the
143 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07144 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42145
[email protected]6b54fda2014-07-22 02:13:47146 // Creates background hosts if the embedder is ready and they are not already
147 // loaded.
148 void MaybeCreateStartupBackgroundHosts();
[email protected]aa3dd492013-11-05 17:09:09149
[email protected]9602db42014-07-25 05:24:37150 // Called on shutdown to close our extension hosts.
151 void CloseBackgroundHosts();
152
[email protected]aa3dd492013-11-05 17:09:09153 // Gets the BrowserContext associated with site_instance_ and all other
154 // related SiteInstances.
155 content::BrowserContext* GetBrowserContext() const;
156
[email protected]b6cd7202014-01-07 17:46:55157 // Sets callbacks for testing keepalive impulse behavior.
158 typedef base::Callback<void(const std::string& extension_id)>
159 ImpulseCallbackForTesting;
160 void SetKeepaliveImpulseCallbackForTesting(
161 const ImpulseCallbackForTesting& callback);
162 void SetKeepaliveImpulseDecrementCallbackForTesting(
163 const ImpulseCallbackForTesting& callback);
164
yoza9bf5602014-09-19 02:03:31165 // Sets the time in milliseconds that an extension event page can
166 // be idle before it is shut down; must be > 0.
167 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
168
169 // Sets the time in milliseconds that an extension event page has
170 // between being notified of its impending unload and that unload
171 // happening.
172 static void SetEventPageSuspendingTimeForTesting(
173 unsigned suspending_time_msec);
174
[email protected]6b54fda2014-07-22 02:13:47175 // Creates a non-incognito instance for tests. |registry| allows unit tests
176 // to inject an ExtensionRegistry that is not managed by the usual
177 // BrowserContextKeyedServiceFactory system.
178 static ProcessManager* CreateForTesting(content::BrowserContext* context,
179 ExtensionRegistry* registry);
180
181 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08182 static ProcessManager* CreateIncognitoForTesting(
183 content::BrowserContext* incognito_context,
184 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47185 ExtensionRegistry* registry);
186
187 bool startup_background_hosts_created_for_test() const {
188 return startup_background_hosts_created_;
189 }
[email protected]b9f6ba32014-03-10 18:34:08190
[email protected]bc535ee52010-08-31 18:40:32191 protected:
reillyg0ea3fa902014-10-28 15:30:23192 static ProcessManager* Create(content::BrowserContext* context);
193
194 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47195 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
196 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07197 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47198 content::BrowserContext* original_context,
199 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32200
[email protected]6c2381d2011-10-19 02:52:53201 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24202 void Observe(int type,
203 const content::NotificationSource& source,
204 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32205
limasdfe0f061b2015-02-27 00:35:06206 // ExtensionRegistryObserver:
207 void OnExtensionLoaded(content::BrowserContext* browser_context,
208 const Extension* extension) override;
209 void OnExtensionUnloaded(content::BrowserContext* browser_context,
210 const Extension* extension,
211 UnloadedExtensionInfo::Reason reason) override;
212
[email protected]6c2381d2011-10-19 02:52:53213 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06214
[email protected]d1fe1352012-04-26 00:47:32215 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49216 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05217
[email protected]72daaa92012-01-18 13:39:02218 // A SiteInstance related to the SiteInstance for all extensions in
219 // this profile. We create it in such a way that a new
220 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33221 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05222
[email protected]6b54fda2014-07-22 02:13:47223 // Not owned. Also used by IncognitoProcessManager.
224 ExtensionRegistry* extension_registry_;
225
[email protected]3e194992011-10-20 05:39:10226 private:
reillyg0ea3fa902014-10-28 15:30:23227 friend class ProcessManagerFactory;
[email protected]98b6d942013-11-10 00:34:07228 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16229
[email protected]103f19f2012-04-02 19:30:12230 // Extra information we keep for each extension's background page.
231 struct BackgroundPageData;
hashimoto5bca2272014-11-04 07:35:18232 struct ExtensionRenderViewData;
[email protected]103f19f2012-04-02 19:30:12233 typedef std::string ExtensionId;
234 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
hashimoto5bca2272014-11-04 07:35:18235 typedef std::map<content::RenderViewHost*, ExtensionRenderViewData>
236 ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10237
[email protected]6b54fda2014-07-22 02:13:47238 // Load all background pages once the profile data is ready and the pages
239 // should be loaded.
240 void CreateStartupBackgroundHosts();
241
[email protected]94de8cb2013-11-07 06:29:21242 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07243 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21244
[email protected]06024c62011-10-20 20:57:12245 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07246 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12247
[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
[email protected]35e0e0792012-11-30 02:35:48263 // Potentially registers a RenderViewHost, if it is associated with an
264 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15265 // Returns true, if render_view_host was registered (it is associated
266 // with an extension).
267 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12268
[email protected]5b3ee852013-09-26 06:33:10269 // Unregister RenderViewHosts and clear background page data for an extension
270 // which has been unloaded.
271 void UnregisterExtension(const std::string& extension_id);
272
[email protected]caffe702012-05-03 05:30:17273 // Clears background page data for this extension.
274 void ClearBackgroundPageData(const std::string& extension_id);
275
[email protected]a23f62e2013-04-26 13:13:02276 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
277
[email protected]35e0e0792012-11-30 02:35:48278 // Contains all active extension-related RenderViewHost instances for all
279 // extensions. We also keep a cache of the host's view type, because that
280 // information is not accessible at registration/deregistration time.
281 ExtensionRenderViews all_extension_views_;
282
[email protected]103f19f2012-04-02 19:30:12283 BackgroundPageDataMap background_page_data_;
284
[email protected]aa3dd492013-11-05 17:09:09285 // True if we have created the startup set of background hosts.
286 bool startup_background_hosts_created_;
287
[email protected]75e7bac2013-11-01 23:33:42288 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
289
[email protected]b6cd7202014-01-07 17:46:55290 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
291 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
292
[email protected]08d469b22014-03-31 00:51:24293 ObserverList<ProcessManagerObserver> observer_list_;
294
[email protected]77a538f2014-07-14 22:25:37295 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
296 // members. These IDs are tracked per extension in background_page_data_ and
297 // are used to verify that nothing has interrupted the process of closing a
298 // lazy background process.
299 //
300 // Any interruption obtains a new ID by incrementing
301 // last_background_close_sequence_id_ and storing it in background_page_data_
302 // for a particular extension. Callbacks and round-trip IPC messages store the
303 // value of the extension's close_sequence_id at the beginning of the process.
304 // Thus comparisons can be done to halt when IDs no longer match.
305 //
306 // This counter provides unique IDs even when BackgroundPageData objects are
307 // reset.
308 uint64 last_background_close_sequence_id_;
309
310 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07311 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16312
[email protected]98b6d942013-11-10 00:34:07313 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05314};
315
[email protected]98b6d942013-11-10 00:34:07316} // namespace extensions
317
318#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_