blob: e3c6bcf06aa550e2ca8d350312b1e0732ec01d0b [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"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]cb2edf22013-04-01 20:25:2320#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0521
[email protected]481e1a42009-05-06 20:56:0522class GURL;
[email protected]b6583592012-01-25 19:52:3323
24namespace content {
[email protected]7f474212013-11-05 04:26:1625class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0226class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1127class RenderViewHost;
[email protected]9dfed872013-12-30 23:08:5628class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0529class SiteInstance;
[email protected]b6583592012-01-25 19:52:3330};
[email protected]481e1a42009-05-06 20:56:0531
[email protected]1c321ee2012-05-21 03:02:3432namespace extensions {
[email protected]98b6d942013-11-10 00:34:0733
[email protected]1c321ee2012-05-21 03:02:3434class Extension;
[email protected]3a1dc572012-07-31 22:25:1335class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4736class ExtensionRegistry;
37class ProcessManagerDelegate;
[email protected]08d469b22014-03-31 00:51:2438class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3439
[email protected]bc535ee52010-08-31 18:40:3240// Manages dynamic state of running Chromium extensions. There is one instance
41// of this class per Profile. OTR Profiles have a separate instance that keeps
42// track of split-mode extensions only.
[email protected]49e7741e2014-07-21 01:57:4243class ProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0544 public:
[email protected]3a1dc572012-07-31 22:25:1345 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3246 typedef ExtensionHostSet::const_iterator const_iterator;
47
[email protected]98b6d942013-11-10 00:34:0748 static ProcessManager* Create(content::BrowserContext* context);
49 virtual ~ProcessManager();
[email protected]481e1a42009-05-06 20:56:0550
[email protected]d1fe1352012-04-26 00:47:3251 const ExtensionHostSet& background_hosts() const {
52 return background_hosts_;
53 }
54
[email protected]d1fe1352012-04-26 00:47:3255 typedef std::set<content::RenderViewHost*> ViewSet;
56 const ViewSet GetAllViews() const;
57
[email protected]08d469b22014-03-31 00:51:2458 // The typical observer interface.
59 void AddObserver(ProcessManagerObserver* observer);
60 void RemoveObserver(ProcessManagerObserver* observer);
61
[email protected]029ad372011-05-20 17:12:5662 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4163 // displayed anywhere. Returns false if no background host can be created,
64 // for example for hosted apps and extensions that aren't enabled in
65 // Incognito.
66 virtual bool CreateBackgroundHost(const Extension* extension,
67 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3268
[email protected]4814b512009-11-07 00:12:2969 // Gets the ExtensionHost for the background page for an extension, or NULL if
70 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0771 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2972
[email protected]481e1a42009-05-06 20:56:0573 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4674 // TODO(aa): This only returns correct results for extensions and packaged
75 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3376 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0577
[email protected]3e194992011-10-20 05:39:1078 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1179 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1080
81 // Returns all RenderViewHosts that are registered for the specified
82 // extension.
[email protected]eaabba22012-03-07 15:02:1183 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1084 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5885
[email protected]d1fe1352012-04-26 00:47:3286 // Returns the extension associated with the specified RenderViewHost, or
87 // NULL.
[email protected]98b6d942013-11-10 00:34:0788 const Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:3289 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:0990
[email protected]7042b682012-04-19 22:57:5191 // Returns true if the (lazy) background host for the given extension has
92 // already been sent the unload event and is shutting down.
93 bool IsBackgroundHostClosing(const std::string& extension_id);
94
[email protected]720ad1312012-02-27 23:07:3695 // Getter and setter for the lazy background page's keepalive count. This is
96 // the count of how many outstanding "things" are keeping the page alive.
97 // When this reaches 0, we will begin the process of shutting down the page.
98 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:0799 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32100 void IncrementLazyKeepaliveCount(const Extension* extension);
101 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36102
[email protected]d72d3a62012-05-10 03:45:08103 void IncrementLazyKeepaliveCountForView(
104 content::RenderViewHost* render_view_host);
105
[email protected]388770152013-12-03 01:25:32106 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
107 // impulses will only keep the page alive for a limited amount of time unless
108 // called regularly.
109 void KeepaliveImpulse(const Extension* extension);
110
[email protected]584e6572013-02-16 07:02:33111 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36112 // pages.
[email protected]77a538f2014-07-14 22:25:37113 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12114
[email protected]584e6572013-02-16 07:02:33115 // Same as above, for the Suspend message.
116 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12117
[email protected]9dfed872013-12-30 23:08:56118 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18119 // when network activity is idle for lazy background pages.
[email protected]9dfed872013-12-30 23:08:56120 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
121 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18122
[email protected]0d475e072012-07-26 02:30:42123 // Prevents |extension|'s background page from being closed and sends the
124 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07125 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42126
[email protected]6b54fda2014-07-22 02:13:47127 // Creates background hosts if the embedder is ready and they are not already
128 // loaded.
129 void MaybeCreateStartupBackgroundHosts();
[email protected]aa3dd492013-11-05 17:09:09130
131 // Gets the BrowserContext associated with site_instance_ and all other
132 // related SiteInstances.
133 content::BrowserContext* GetBrowserContext() const;
134
[email protected]b6cd7202014-01-07 17:46:55135 // Sets callbacks for testing keepalive impulse behavior.
136 typedef base::Callback<void(const std::string& extension_id)>
137 ImpulseCallbackForTesting;
138 void SetKeepaliveImpulseCallbackForTesting(
139 const ImpulseCallbackForTesting& callback);
140 void SetKeepaliveImpulseDecrementCallbackForTesting(
141 const ImpulseCallbackForTesting& callback);
142
[email protected]6b54fda2014-07-22 02:13:47143 // Creates a non-incognito instance for tests. |registry| allows unit tests
144 // to inject an ExtensionRegistry that is not managed by the usual
145 // BrowserContextKeyedServiceFactory system.
146 static ProcessManager* CreateForTesting(content::BrowserContext* context,
147 ExtensionRegistry* registry);
148
149 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08150 static ProcessManager* CreateIncognitoForTesting(
151 content::BrowserContext* incognito_context,
152 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47153 ProcessManager* original_manager,
154 ExtensionRegistry* registry);
155
156 bool startup_background_hosts_created_for_test() const {
157 return startup_background_hosts_created_;
158 }
[email protected]b9f6ba32014-03-10 18:34:08159
[email protected]bc535ee52010-08-31 18:40:32160 protected:
[email protected]7f474212013-11-05 04:26:16161 // If |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47162 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
163 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07164 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47165 content::BrowserContext* original_context,
166 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32167
[email protected]ae5497f2009-11-05 00:39:46168 // Called on browser shutdown to close our extension hosts.
169 void CloseBackgroundHosts();
170
[email protected]6c2381d2011-10-19 02:52:53171 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27172 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53173 const content::NotificationSource& source,
174 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32175
[email protected]6c2381d2011-10-19 02:52:53176 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06177
[email protected]d1fe1352012-04-26 00:47:32178 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49179 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05180
[email protected]72daaa92012-01-18 13:39:02181 // A SiteInstance related to the SiteInstance for all extensions in
182 // this profile. We create it in such a way that a new
183 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33184 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05185
[email protected]6b54fda2014-07-22 02:13:47186 // Not owned. Also used by IncognitoProcessManager.
187 ExtensionRegistry* extension_registry_;
188
[email protected]3e194992011-10-20 05:39:10189 private:
[email protected]98b6d942013-11-10 00:34:07190 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16191
[email protected]103f19f2012-04-02 19:30:12192 // Extra information we keep for each extension's background page.
193 struct BackgroundPageData;
194 typedef std::string ExtensionId;
195 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11196 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23197 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10198
[email protected]6b54fda2014-07-22 02:13:47199 // Load all background pages once the profile data is ready and the pages
200 // should be loaded.
201 void CreateStartupBackgroundHosts();
202
[email protected]94de8cb2013-11-07 06:29:21203 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07204 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21205
[email protected]06024c62011-10-20 20:57:12206 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07207 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12208
[email protected]388770152013-12-03 01:25:32209 // Internal implementation of DecrementLazyKeepaliveCount with an
210 // |extension_id| known to have a lazy background page.
211 void DecrementLazyKeepaliveCount(const std::string& extension_id);
212
213 // Checks if keepalive impulses have occured, and adjusts keep alive count.
214 void OnKeepaliveImpulseCheck();
215
[email protected]6baff0b52012-03-06 01:30:18216 // These are called when the extension transitions between idle and active.
217 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20218 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37219 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18220 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42221 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37222 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18223
[email protected]35e0e0792012-11-30 02:35:48224 // Potentially registers a RenderViewHost, if it is associated with an
225 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15226 // Returns true, if render_view_host was registered (it is associated
227 // with an extension).
228 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12229
[email protected]5b3ee852013-09-26 06:33:10230 // Unregister RenderViewHosts and clear background page data for an extension
231 // which has been unloaded.
232 void UnregisterExtension(const std::string& extension_id);
233
[email protected]caffe702012-05-03 05:30:17234 // Clears background page data for this extension.
235 void ClearBackgroundPageData(const std::string& extension_id);
236
[email protected]a23f62e2013-04-26 13:13:02237 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
238
[email protected]35e0e0792012-11-30 02:35:48239 // Contains all active extension-related RenderViewHost instances for all
240 // extensions. We also keep a cache of the host's view type, because that
241 // information is not accessible at registration/deregistration time.
242 ExtensionRenderViews all_extension_views_;
243
[email protected]103f19f2012-04-02 19:30:12244 BackgroundPageDataMap background_page_data_;
245
[email protected]99c49b52012-05-02 01:50:20246 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33247 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20248 base::TimeDelta event_page_idle_time_;
249
[email protected]584e6572013-02-16 07:02:33250 // The time to delay between sending a ShouldSuspend message and
251 // sending a Suspend message; read from command-line switch.
252 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20253
[email protected]aa3dd492013-11-05 17:09:09254 // True if we have created the startup set of background hosts.
255 bool startup_background_hosts_created_;
256
[email protected]75e7bac2013-11-01 23:33:42257 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
258
[email protected]b6cd7202014-01-07 17:46:55259 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
260 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
261
[email protected]08d469b22014-03-31 00:51:24262 ObserverList<ProcessManagerObserver> observer_list_;
263
[email protected]77a538f2014-07-14 22:25:37264 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
265 // members. These IDs are tracked per extension in background_page_data_ and
266 // are used to verify that nothing has interrupted the process of closing a
267 // lazy background process.
268 //
269 // Any interruption obtains a new ID by incrementing
270 // last_background_close_sequence_id_ and storing it in background_page_data_
271 // for a particular extension. Callbacks and round-trip IPC messages store the
272 // value of the extension's close_sequence_id at the beginning of the process.
273 // Thus comparisons can be done to halt when IDs no longer match.
274 //
275 // This counter provides unique IDs even when BackgroundPageData objects are
276 // reset.
277 uint64 last_background_close_sequence_id_;
278
279 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07280 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16281
[email protected]98b6d942013-11-10 00:34:07282 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05283};
284
[email protected]98b6d942013-11-10 00:34:07285} // namespace extensions
286
287#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_