blob: eabc0c5e178486516a4315ae572646ced246db2d [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);
dcheng9168b2f2014-10-21 12:38:2449 ~ProcessManager() override;
[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
yoza9bf5602014-09-19 02:03:31151 // Sets the time in milliseconds that an extension event page can
152 // be idle before it is shut down; must be > 0.
153 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
154
155 // Sets the time in milliseconds that an extension event page has
156 // between being notified of its impending unload and that unload
157 // happening.
158 static void SetEventPageSuspendingTimeForTesting(
159 unsigned suspending_time_msec);
160
[email protected]6b54fda2014-07-22 02:13:47161 // Creates a non-incognito instance for tests. |registry| allows unit tests
162 // to inject an ExtensionRegistry that is not managed by the usual
163 // BrowserContextKeyedServiceFactory system.
164 static ProcessManager* CreateForTesting(content::BrowserContext* context,
165 ExtensionRegistry* registry);
166
167 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08168 static ProcessManager* CreateIncognitoForTesting(
169 content::BrowserContext* incognito_context,
170 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47171 ProcessManager* original_manager,
172 ExtensionRegistry* registry);
173
174 bool startup_background_hosts_created_for_test() const {
175 return startup_background_hosts_created_;
176 }
[email protected]b9f6ba32014-03-10 18:34:08177
[email protected]bc535ee52010-08-31 18:40:32178 protected:
[email protected]7f474212013-11-05 04:26:16179 // If |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47180 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
181 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07182 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47183 content::BrowserContext* original_context,
184 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32185
[email protected]6c2381d2011-10-19 02:52:53186 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24187 void Observe(int type,
188 const content::NotificationSource& source,
189 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32190
[email protected]6c2381d2011-10-19 02:52:53191 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06192
[email protected]d1fe1352012-04-26 00:47:32193 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49194 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05195
[email protected]72daaa92012-01-18 13:39:02196 // A SiteInstance related to the SiteInstance for all extensions in
197 // this profile. We create it in such a way that a new
198 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33199 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05200
[email protected]6b54fda2014-07-22 02:13:47201 // Not owned. Also used by IncognitoProcessManager.
202 ExtensionRegistry* extension_registry_;
203
[email protected]3e194992011-10-20 05:39:10204 private:
[email protected]98b6d942013-11-10 00:34:07205 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16206
[email protected]103f19f2012-04-02 19:30:12207 // Extra information we keep for each extension's background page.
208 struct BackgroundPageData;
209 typedef std::string ExtensionId;
210 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11211 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23212 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10213
[email protected]6b54fda2014-07-22 02:13:47214 // Load all background pages once the profile data is ready and the pages
215 // should be loaded.
216 void CreateStartupBackgroundHosts();
217
[email protected]94de8cb2013-11-07 06:29:21218 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07219 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21220
[email protected]06024c62011-10-20 20:57:12221 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07222 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12223
[email protected]388770152013-12-03 01:25:32224 // Internal implementation of DecrementLazyKeepaliveCount with an
225 // |extension_id| known to have a lazy background page.
226 void DecrementLazyKeepaliveCount(const std::string& extension_id);
227
228 // Checks if keepalive impulses have occured, and adjusts keep alive count.
229 void OnKeepaliveImpulseCheck();
230
[email protected]6baff0b52012-03-06 01:30:18231 // These are called when the extension transitions between idle and active.
232 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20233 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37234 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18235 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42236 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37237 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18238
[email protected]35e0e0792012-11-30 02:35:48239 // Potentially registers a RenderViewHost, if it is associated with an
240 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15241 // Returns true, if render_view_host was registered (it is associated
242 // with an extension).
243 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12244
[email protected]5b3ee852013-09-26 06:33:10245 // Unregister RenderViewHosts and clear background page data for an extension
246 // which has been unloaded.
247 void UnregisterExtension(const std::string& extension_id);
248
[email protected]caffe702012-05-03 05:30:17249 // Clears background page data for this extension.
250 void ClearBackgroundPageData(const std::string& extension_id);
251
[email protected]a23f62e2013-04-26 13:13:02252 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
253
[email protected]35e0e0792012-11-30 02:35:48254 // Contains all active extension-related RenderViewHost instances for all
255 // extensions. We also keep a cache of the host's view type, because that
256 // information is not accessible at registration/deregistration time.
257 ExtensionRenderViews all_extension_views_;
258
[email protected]103f19f2012-04-02 19:30:12259 BackgroundPageDataMap background_page_data_;
260
[email protected]aa3dd492013-11-05 17:09:09261 // True if we have created the startup set of background hosts.
262 bool startup_background_hosts_created_;
263
[email protected]75e7bac2013-11-01 23:33:42264 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
265
[email protected]b6cd7202014-01-07 17:46:55266 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
267 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
268
[email protected]08d469b22014-03-31 00:51:24269 ObserverList<ProcessManagerObserver> observer_list_;
270
[email protected]77a538f2014-07-14 22:25:37271 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
272 // members. These IDs are tracked per extension in background_page_data_ and
273 // are used to verify that nothing has interrupted the process of closing a
274 // lazy background process.
275 //
276 // Any interruption obtains a new ID by incrementing
277 // last_background_close_sequence_id_ and storing it in background_page_data_
278 // for a particular extension. Callbacks and round-trip IPC messages store the
279 // value of the extension's close_sequence_id at the beginning of the process.
280 // Thus comparisons can be done to halt when IDs no longer match.
281 //
282 // This counter provides unique IDs even when BackgroundPageData objects are
283 // reset.
284 uint64 last_background_close_sequence_id_;
285
286 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07287 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16288
[email protected]98b6d942013-11-10 00:34:07289 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05290};
291
[email protected]98b6d942013-11-10 00:34:07292} // namespace extensions
293
294#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_