blob: 60056a1fc5d2cbc28a6184338f3b32649d324a50 [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"
[email protected]cb2edf22013-04-01 20:25:2321#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0522
[email protected]481e1a42009-05-06 20:56:0523class GURL;
[email protected]b6583592012-01-25 19:52:3324
25namespace content {
[email protected]7f474212013-11-05 04:26:1626class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0227class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1128class RenderViewHost;
[email protected]9dfed872013-12-30 23:08:5629class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0530class SiteInstance;
[email protected]b6583592012-01-25 19:52:3331};
[email protected]481e1a42009-05-06 20:56:0532
[email protected]1c321ee2012-05-21 03:02:3433namespace extensions {
[email protected]98b6d942013-11-10 00:34:0734
[email protected]1c321ee2012-05-21 03:02:3435class Extension;
[email protected]3a1dc572012-07-31 22:25:1336class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4737class ExtensionRegistry;
38class ProcessManagerDelegate;
[email protected]08d469b22014-03-31 00:51:2439class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3440
[email protected]bc535ee52010-08-31 18:40:3241// Manages dynamic state of running Chromium extensions. There is one instance
42// of this class per Profile. OTR Profiles have a separate instance that keeps
43// track of split-mode extensions only.
reillyg0ea3fa902014-10-28 15:30:2344class ProcessManager : public KeyedService,
45 public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0546 public:
[email protected]3a1dc572012-07-31 22:25:1347 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3248 typedef ExtensionHostSet::const_iterator const_iterator;
49
reillyg0ea3fa902014-10-28 15:30:2350 static ProcessManager* Get(content::BrowserContext* context);
dcheng9168b2f2014-10-21 12:38:2451 ~ProcessManager() override;
[email protected]481e1a42009-05-06 20:56:0552
[email protected]d1fe1352012-04-26 00:47:3253 const ExtensionHostSet& background_hosts() const {
54 return background_hosts_;
55 }
56
[email protected]d1fe1352012-04-26 00:47:3257 typedef std::set<content::RenderViewHost*> ViewSet;
58 const ViewSet GetAllViews() const;
59
[email protected]08d469b22014-03-31 00:51:2460 // The typical observer interface.
61 void AddObserver(ProcessManagerObserver* observer);
62 void RemoveObserver(ProcessManagerObserver* observer);
63
[email protected]029ad372011-05-20 17:12:5664 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4165 // displayed anywhere. Returns false if no background host can be created,
66 // for example for hosted apps and extensions that aren't enabled in
67 // Incognito.
68 virtual bool CreateBackgroundHost(const Extension* extension,
69 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3270
[email protected]4814b512009-11-07 00:12:2971 // Gets the ExtensionHost for the background page for an extension, or NULL if
72 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0773 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2974
[email protected]481e1a42009-05-06 20:56:0575 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4676 // TODO(aa): This only returns correct results for extensions and packaged
77 // apps, not hosted apps.
rdevlin.cronin3d4261522015-02-10 00:48:1578 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
79 const GURL& url);
[email protected]481e1a42009-05-06 20:56:0580
hashimoto5bca2272014-11-04 07:35:1881 // If the view isn't keeping the lazy background page alive, increments the
82 // keepalive count to do so.
83 void AcquireLazyKeepaliveCountForView(
84 content::RenderViewHost* render_view_host);
85
86 // If the view is keeping the lazy background page alive, decrements the
87 // keepalive count to stop doing it.
88 void ReleaseLazyKeepaliveCountForView(
89 content::RenderViewHost* render_view_host);
90
[email protected]3e194992011-10-20 05:39:1091 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1192 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1093
94 // Returns all RenderViewHosts that are registered for the specified
95 // extension.
[email protected]eaabba22012-03-07 15:02:1196 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1097 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5898
[email protected]d1fe1352012-04-26 00:47:3299 // Returns the extension associated with the specified RenderViewHost, or
100 // NULL.
[email protected]98b6d942013-11-10 00:34:07101 const Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32102 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09103
[email protected]7042b682012-04-19 22:57:51104 // Returns true if the (lazy) background host for the given extension has
105 // already been sent the unload event and is shutting down.
106 bool IsBackgroundHostClosing(const std::string& extension_id);
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
[email protected]e0ff1aa2014-08-16 01:38:11121 // Triggers a keepalive impulse for a plug-in (e.g NaCl).
122 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]6b54fda2014-07-22 02:13:47144 // Creates background hosts if the embedder is ready and they are not already
145 // loaded.
146 void MaybeCreateStartupBackgroundHosts();
[email protected]aa3dd492013-11-05 17:09:09147
[email protected]9602db42014-07-25 05:24:37148 // Called on shutdown to close our extension hosts.
149 void CloseBackgroundHosts();
150
[email protected]aa3dd492013-11-05 17:09:09151 // Gets the BrowserContext associated with site_instance_ and all other
152 // related SiteInstances.
153 content::BrowserContext* GetBrowserContext() const;
154
[email protected]b6cd7202014-01-07 17:46:55155 // Sets callbacks for testing keepalive impulse behavior.
156 typedef base::Callback<void(const std::string& extension_id)>
157 ImpulseCallbackForTesting;
158 void SetKeepaliveImpulseCallbackForTesting(
159 const ImpulseCallbackForTesting& callback);
160 void SetKeepaliveImpulseDecrementCallbackForTesting(
161 const ImpulseCallbackForTesting& callback);
162
yoza9bf5602014-09-19 02:03:31163 // Sets the time in milliseconds that an extension event page can
164 // be idle before it is shut down; must be > 0.
165 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
166
167 // Sets the time in milliseconds that an extension event page has
168 // between being notified of its impending unload and that unload
169 // happening.
170 static void SetEventPageSuspendingTimeForTesting(
171 unsigned suspending_time_msec);
172
[email protected]6b54fda2014-07-22 02:13:47173 // Creates a non-incognito instance for tests. |registry| allows unit tests
174 // to inject an ExtensionRegistry that is not managed by the usual
175 // BrowserContextKeyedServiceFactory system.
176 static ProcessManager* CreateForTesting(content::BrowserContext* context,
177 ExtensionRegistry* registry);
178
179 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08180 static ProcessManager* CreateIncognitoForTesting(
181 content::BrowserContext* incognito_context,
182 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47183 ExtensionRegistry* registry);
184
185 bool startup_background_hosts_created_for_test() const {
186 return startup_background_hosts_created_;
187 }
[email protected]b9f6ba32014-03-10 18:34:08188
[email protected]bc535ee52010-08-31 18:40:32189 protected:
reillyg0ea3fa902014-10-28 15:30:23190 static ProcessManager* Create(content::BrowserContext* context);
191
192 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47193 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
194 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07195 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47196 content::BrowserContext* original_context,
197 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32198
[email protected]6c2381d2011-10-19 02:52:53199 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24200 void Observe(int type,
201 const content::NotificationSource& source,
202 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32203
[email protected]6c2381d2011-10-19 02:52:53204 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06205
[email protected]d1fe1352012-04-26 00:47:32206 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49207 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05208
[email protected]72daaa92012-01-18 13:39:02209 // A SiteInstance related to the SiteInstance for all extensions in
210 // this profile. We create it in such a way that a new
211 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33212 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05213
[email protected]6b54fda2014-07-22 02:13:47214 // Not owned. Also used by IncognitoProcessManager.
215 ExtensionRegistry* extension_registry_;
216
[email protected]3e194992011-10-20 05:39:10217 private:
reillyg0ea3fa902014-10-28 15:30:23218 friend class ProcessManagerFactory;
[email protected]98b6d942013-11-10 00:34:07219 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16220
[email protected]103f19f2012-04-02 19:30:12221 // Extra information we keep for each extension's background page.
222 struct BackgroundPageData;
hashimoto5bca2272014-11-04 07:35:18223 struct ExtensionRenderViewData;
[email protected]103f19f2012-04-02 19:30:12224 typedef std::string ExtensionId;
225 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
hashimoto5bca2272014-11-04 07:35:18226 typedef std::map<content::RenderViewHost*, ExtensionRenderViewData>
227 ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10228
[email protected]6b54fda2014-07-22 02:13:47229 // Load all background pages once the profile data is ready and the pages
230 // should be loaded.
231 void CreateStartupBackgroundHosts();
232
[email protected]94de8cb2013-11-07 06:29:21233 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07234 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21235
[email protected]06024c62011-10-20 20:57:12236 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07237 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12238
[email protected]388770152013-12-03 01:25:32239 // Internal implementation of DecrementLazyKeepaliveCount with an
240 // |extension_id| known to have a lazy background page.
241 void DecrementLazyKeepaliveCount(const std::string& extension_id);
242
243 // Checks if keepalive impulses have occured, and adjusts keep alive count.
244 void OnKeepaliveImpulseCheck();
245
[email protected]6baff0b52012-03-06 01:30:18246 // These are called when the extension transitions between idle and active.
247 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20248 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37249 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18250 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42251 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37252 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18253
[email protected]35e0e0792012-11-30 02:35:48254 // Potentially registers a RenderViewHost, if it is associated with an
255 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15256 // Returns true, if render_view_host was registered (it is associated
257 // with an extension).
258 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12259
[email protected]5b3ee852013-09-26 06:33:10260 // Unregister RenderViewHosts and clear background page data for an extension
261 // 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
[email protected]a23f62e2013-04-26 13:13:02267 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
268
[email protected]35e0e0792012-11-30 02:35:48269 // Contains all active extension-related RenderViewHost instances for all
270 // extensions. We also keep a cache of the host's view type, because that
271 // information is not accessible at registration/deregistration time.
272 ExtensionRenderViews all_extension_views_;
273
[email protected]103f19f2012-04-02 19:30:12274 BackgroundPageDataMap background_page_data_;
275
[email protected]aa3dd492013-11-05 17:09:09276 // True if we have created the startup set of background hosts.
277 bool startup_background_hosts_created_;
278
[email protected]75e7bac2013-11-01 23:33:42279 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
280
[email protected]b6cd7202014-01-07 17:46:55281 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
282 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
283
[email protected]08d469b22014-03-31 00:51:24284 ObserverList<ProcessManagerObserver> observer_list_;
285
[email protected]77a538f2014-07-14 22:25:37286 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
287 // members. These IDs are tracked per extension in background_page_data_ and
288 // are used to verify that nothing has interrupted the process of closing a
289 // lazy background process.
290 //
291 // Any interruption obtains a new ID by incrementing
292 // last_background_close_sequence_id_ and storing it in background_page_data_
293 // for a particular extension. Callbacks and round-trip IPC messages store the
294 // value of the extension's close_sequence_id at the beginning of the process.
295 // Thus comparisons can be done to halt when IDs no longer match.
296 //
297 // This counter provides unique IDs even when BackgroundPageData objects are
298 // reset.
299 uint64 last_background_close_sequence_id_;
300
301 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07302 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16303
[email protected]98b6d942013-11-10 00:34:07304 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05305};
306
[email protected]98b6d942013-11-10 00:34:07307} // namespace extensions
308
309#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_