blob: e7be9951e03233c48dfa27100a3bad0ade98e704 [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.
glider34cdb3e2014-09-15 12:04:3043class 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]e0ff1aa2014-08-16 01:38:11111 // Triggers a keepalive impulse for a plug-in (e.g NaCl).
112 static void OnKeepaliveFromPlugin(int render_process_id,
113 int render_frame_id,
114 const std::string& extension_id);
115
[email protected]584e6572013-02-16 07:02:33116 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36117 // pages.
[email protected]77a538f2014-07-14 22:25:37118 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12119
[email protected]584e6572013-02-16 07:02:33120 // Same as above, for the Suspend message.
121 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12122
[email protected]9dfed872013-12-30 23:08:56123 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18124 // when network activity is idle for lazy background pages.
[email protected]9dfed872013-12-30 23:08:56125 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
126 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18127
[email protected]0d475e072012-07-26 02:30:42128 // Prevents |extension|'s background page from being closed and sends the
129 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07130 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42131
[email protected]6b54fda2014-07-22 02:13:47132 // Creates background hosts if the embedder is ready and they are not already
133 // loaded.
134 void MaybeCreateStartupBackgroundHosts();
[email protected]aa3dd492013-11-05 17:09:09135
[email protected]9602db42014-07-25 05:24:37136 // Called on shutdown to close our extension hosts.
137 void CloseBackgroundHosts();
138
[email protected]aa3dd492013-11-05 17:09:09139 // Gets the BrowserContext associated with site_instance_ and all other
140 // related SiteInstances.
141 content::BrowserContext* GetBrowserContext() const;
142
[email protected]b6cd7202014-01-07 17:46:55143 // Sets callbacks for testing keepalive impulse behavior.
144 typedef base::Callback<void(const std::string& extension_id)>
145 ImpulseCallbackForTesting;
146 void SetKeepaliveImpulseCallbackForTesting(
147 const ImpulseCallbackForTesting& callback);
148 void SetKeepaliveImpulseDecrementCallbackForTesting(
149 const ImpulseCallbackForTesting& callback);
150
[email protected]6b54fda2014-07-22 02:13:47151 // Creates a non-incognito instance for tests. |registry| allows unit tests
152 // to inject an ExtensionRegistry that is not managed by the usual
153 // BrowserContextKeyedServiceFactory system.
154 static ProcessManager* CreateForTesting(content::BrowserContext* context,
155 ExtensionRegistry* registry);
156
157 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08158 static ProcessManager* CreateIncognitoForTesting(
159 content::BrowserContext* incognito_context,
160 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47161 ProcessManager* original_manager,
162 ExtensionRegistry* registry);
163
164 bool startup_background_hosts_created_for_test() const {
165 return startup_background_hosts_created_;
166 }
[email protected]b9f6ba32014-03-10 18:34:08167
[email protected]bc535ee52010-08-31 18:40:32168 protected:
[email protected]7f474212013-11-05 04:26:16169 // If |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47170 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
171 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07172 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47173 content::BrowserContext* original_context,
174 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32175
[email protected]6c2381d2011-10-19 02:52:53176 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27177 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53178 const content::NotificationSource& source,
179 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32180
[email protected]6c2381d2011-10-19 02:52:53181 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06182
[email protected]d1fe1352012-04-26 00:47:32183 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49184 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05185
[email protected]72daaa92012-01-18 13:39:02186 // A SiteInstance related to the SiteInstance for all extensions in
187 // this profile. We create it in such a way that a new
188 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33189 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05190
[email protected]6b54fda2014-07-22 02:13:47191 // Not owned. Also used by IncognitoProcessManager.
192 ExtensionRegistry* extension_registry_;
193
[email protected]3e194992011-10-20 05:39:10194 private:
[email protected]98b6d942013-11-10 00:34:07195 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16196
[email protected]103f19f2012-04-02 19:30:12197 // Extra information we keep for each extension's background page.
198 struct BackgroundPageData;
199 typedef std::string ExtensionId;
200 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11201 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23202 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10203
[email protected]6b54fda2014-07-22 02:13:47204 // Load all background pages once the profile data is ready and the pages
205 // should be loaded.
206 void CreateStartupBackgroundHosts();
207
[email protected]94de8cb2013-11-07 06:29:21208 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07209 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21210
[email protected]06024c62011-10-20 20:57:12211 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07212 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12213
[email protected]388770152013-12-03 01:25:32214 // Internal implementation of DecrementLazyKeepaliveCount with an
215 // |extension_id| known to have a lazy background page.
216 void DecrementLazyKeepaliveCount(const std::string& extension_id);
217
218 // Checks if keepalive impulses have occured, and adjusts keep alive count.
219 void OnKeepaliveImpulseCheck();
220
[email protected]6baff0b52012-03-06 01:30:18221 // These are called when the extension transitions between idle and active.
222 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20223 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37224 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18225 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42226 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37227 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18228
[email protected]35e0e0792012-11-30 02:35:48229 // Potentially registers a RenderViewHost, if it is associated with an
230 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15231 // Returns true, if render_view_host was registered (it is associated
232 // with an extension).
233 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12234
[email protected]5b3ee852013-09-26 06:33:10235 // Unregister RenderViewHosts and clear background page data for an extension
236 // which has been unloaded.
237 void UnregisterExtension(const std::string& extension_id);
238
[email protected]caffe702012-05-03 05:30:17239 // Clears background page data for this extension.
240 void ClearBackgroundPageData(const std::string& extension_id);
241
[email protected]a23f62e2013-04-26 13:13:02242 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
243
[email protected]35e0e0792012-11-30 02:35:48244 // Contains all active extension-related RenderViewHost instances for all
245 // extensions. We also keep a cache of the host's view type, because that
246 // information is not accessible at registration/deregistration time.
247 ExtensionRenderViews all_extension_views_;
248
[email protected]103f19f2012-04-02 19:30:12249 BackgroundPageDataMap background_page_data_;
250
[email protected]99c49b52012-05-02 01:50:20251 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33252 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20253 base::TimeDelta event_page_idle_time_;
254
[email protected]584e6572013-02-16 07:02:33255 // The time to delay between sending a ShouldSuspend message and
256 // sending a Suspend message; read from command-line switch.
257 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20258
[email protected]aa3dd492013-11-05 17:09:09259 // True if we have created the startup set of background hosts.
260 bool startup_background_hosts_created_;
261
[email protected]75e7bac2013-11-01 23:33:42262 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
263
[email protected]b6cd7202014-01-07 17:46:55264 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
265 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
266
[email protected]08d469b22014-03-31 00:51:24267 ObserverList<ProcessManagerObserver> observer_list_;
268
[email protected]77a538f2014-07-14 22:25:37269 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
270 // members. These IDs are tracked per extension in background_page_data_ and
271 // are used to verify that nothing has interrupted the process of closing a
272 // lazy background process.
273 //
274 // Any interruption obtains a new ID by incrementing
275 // last_background_close_sequence_id_ and storing it in background_page_data_
276 // for a particular extension. Callbacks and round-trip IPC messages store the
277 // value of the extension's close_sequence_id at the beginning of the process.
278 // Thus comparisons can be done to halt when IDs no longer match.
279 //
280 // This counter provides unique IDs even when BackgroundPageData objects are
281 // reset.
282 uint64 last_background_close_sequence_id_;
283
284 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07285 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16286
[email protected]98b6d942013-11-10 00:34:07287 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05288};
289
[email protected]98b6d942013-11-10 00:34:07290} // namespace extensions
291
292#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_