blob: c93bd80705d310273019a731b4e7be5e2eed6faa [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"
reillyg0ea3fa902014-10-28 15:30:2317#include "components/keyed_service/core/keyed_service.h"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
kmarshall24b29b22015-04-29 01:41:4720#include "extensions/browser/event_page_tracker.h"
limasdfe0f061b2015-02-27 00:35:0621#include "extensions/browser/extension_registry_observer.h"
rdevlin.cronin6ae04a012015-04-03 20:19:4022#include "extensions/common/extension.h"
[email protected]cb2edf22013-04-01 20:25:2323#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0524
[email protected]481e1a42009-05-06 20:56:0525class GURL;
[email protected]b6583592012-01-25 19:52:3326
27namespace content {
[email protected]7f474212013-11-05 04:26:1628class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0229class DevToolsAgentHost;
[email protected]9dfed872013-12-30 23:08:5630class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0531class SiteInstance;
rdevlin.cronin6ae04a012015-04-03 20:19:4032class WebContents;
[email protected]b6583592012-01-25 19:52:3333};
[email protected]481e1a42009-05-06 20:56:0534
[email protected]1c321ee2012-05-21 03:02:3435namespace extensions {
[email protected]98b6d942013-11-10 00:34:0736
[email protected]1c321ee2012-05-21 03:02:3437class Extension;
[email protected]3a1dc572012-07-31 22:25:1338class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4739class ExtensionRegistry;
[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,
kmarshall24b29b22015-04-29 01:41:4747 public ExtensionRegistryObserver,
48 public EventPageTracker {
[email protected]481e1a42009-05-06 20:56:0549 public:
rdevlin.cronin6ae04a012015-04-03 20:19:4050 using ExtensionHostSet = std::set<extensions::ExtensionHost*>;
[email protected]d1fe1352012-04-26 00:47:3251
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
rdevlin.cronin6ae04a012015-04-03 20:19:4055 void RegisterRenderFrameHost(content::WebContents* web_contents,
56 content::RenderFrameHost* render_frame_host,
57 const Extension* extension);
58 void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host);
robcdcc4b82015-12-06 12:39:4559 void DidNavigateRenderFrameHost(content::RenderFrameHost* render_frame_host);
[email protected]d1fe1352012-04-26 00:47:3260
rdevlin.cronin6ae04a012015-04-03 20:19:4061 // Returns the SiteInstance that the given URL belongs to.
62 // TODO(aa): This only returns correct results for extensions and packaged
63 // apps, not hosted apps.
64 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
65 const GURL& url);
[email protected]d1fe1352012-04-26 00:47:3266
rdevlin.cronin6ae04a012015-04-03 20:19:4067 using FrameSet = std::set<content::RenderFrameHost*>;
68 const FrameSet GetAllFrames() const;
69
70 // Returns all RenderFrameHosts that are registered for the specified
71 // extension.
72 ProcessManager::FrameSet GetRenderFrameHostsForExtension(
73 const std::string& extension_id);
74
robcdcc4b82015-12-06 12:39:4575 bool IsRenderFrameHostRegistered(content::RenderFrameHost* render_frame_host);
76
[email protected]08d469b22014-03-31 00:51:2477 void AddObserver(ProcessManagerObserver* observer);
78 void RemoveObserver(ProcessManagerObserver* observer);
79
[email protected]029ad372011-05-20 17:12:5680 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4181 // displayed anywhere. Returns false if no background host can be created,
82 // for example for hosted apps and extensions that aren't enabled in
83 // Incognito.
84 virtual bool CreateBackgroundHost(const Extension* extension,
85 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3286
rdevlin.cronin6ae04a012015-04-03 20:19:4087 // Creates background hosts if the embedder is ready and they are not already
88 // loaded.
89 void MaybeCreateStartupBackgroundHosts();
90
91 // Gets the ExtensionHost for the background page for an extension, or null if
[email protected]4814b512009-11-07 00:12:2992 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0793 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2994
rdevlin.croninb48a98e2015-05-01 00:00:2895 // Returns the ExtensionHost for the given |render_frame_host|, if there is
96 // one.
97 ExtensionHost* GetExtensionHostForRenderFrameHost(
98 content::RenderFrameHost* render_frame_host);
99
[email protected]7042b682012-04-19 22:57:51100 // Returns true if the (lazy) background host for the given extension has
101 // already been sent the unload event and is shutting down.
102 bool IsBackgroundHostClosing(const std::string& extension_id);
103
rdevlin.cronin6ae04a012015-04-03 20:19:40104 // Returns the extension associated with the specified RenderFrameHost/
105 // WebContents, or null.
106 const Extension* GetExtensionForRenderFrameHost(
107 content::RenderFrameHost* render_frame_host);
108 const Extension* GetExtensionForWebContents(
lazyboy9db2523f2015-05-06 14:56:55109 const content::WebContents* web_contents);
rdevlin.cronin6ae04a012015-04-03 20:19:40110
[email protected]720ad1312012-02-27 23:07:36111 // Getter and setter for the lazy background page's keepalive count. This is
112 // the count of how many outstanding "things" are keeping the page alive.
113 // When this reaches 0, we will begin the process of shutting down the page.
114 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:07115 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:32116 void IncrementLazyKeepaliveCount(const Extension* extension);
117 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36118
[email protected]388770152013-12-03 01:25:32119 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
120 // impulses will only keep the page alive for a limited amount of time unless
121 // called regularly.
122 void KeepaliveImpulse(const Extension* extension);
123
tommyclie86b2982015-03-16 20:16:45124 // Triggers a keepalive impulse for a plugin (e.g NaCl).
[email protected]e0ff1aa2014-08-16 01:38:11125 static void OnKeepaliveFromPlugin(int render_process_id,
126 int render_frame_id,
127 const std::string& extension_id);
128
[email protected]584e6572013-02-16 07:02:33129 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36130 // pages.
[email protected]77a538f2014-07-14 22:25:37131 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12132
[email protected]584e6572013-02-16 07:02:33133 // Same as above, for the Suspend message.
134 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12135
[email protected]9dfed872013-12-30 23:08:56136 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18137 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25138 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
139 uint64 request_id);
140 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
141 uint64 request_id);
[email protected]6baff0b52012-03-06 01:30:18142
[email protected]0d475e072012-07-26 02:30:42143 // Prevents |extension|'s background page from being closed and sends the
144 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07145 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42146
[email protected]9602db42014-07-25 05:24:37147 // Called on shutdown to close our extension hosts.
148 void CloseBackgroundHosts();
149
[email protected]b6cd7202014-01-07 17:46:55150 // Sets callbacks for testing keepalive impulse behavior.
rdevlin.cronin6ae04a012015-04-03 20:19:40151 using ImpulseCallbackForTesting =
152 base::Callback<void(const std::string& extension_id)>;
[email protected]b6cd7202014-01-07 17:46:55153 void SetKeepaliveImpulseCallbackForTesting(
154 const ImpulseCallbackForTesting& callback);
155 void SetKeepaliveImpulseDecrementCallbackForTesting(
156 const ImpulseCallbackForTesting& callback);
157
kmarshall24b29b22015-04-29 01:41:47158 // EventPageTracker implementation.
159 bool IsEventPageSuspended(const std::string& extension_id) override;
160 bool WakeEventPage(const std::string& extension_id,
161 const base::Callback<void(bool)>& callback) override;
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
rdevlin.cronin6ae04a012015-04-03 20:19:40185 content::BrowserContext* browser_context() const { return browser_context_; }
186
187 const ExtensionHostSet& background_hosts() const {
188 return background_hosts_;
189 }
190
[email protected]6b54fda2014-07-22 02:13:47191 bool startup_background_hosts_created_for_test() const {
192 return startup_background_hosts_created_;
193 }
[email protected]b9f6ba32014-03-10 18:34:08194
[email protected]bc535ee52010-08-31 18:40:32195 protected:
reillyg0ea3fa902014-10-28 15:30:23196 static ProcessManager* Create(content::BrowserContext* context);
197
rdevlin.cronin6ae04a012015-04-03 20:19:40198 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47199 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
200 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07201 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47202 content::BrowserContext* original_context,
203 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32204
rdevlin.cronin6ae04a012015-04-03 20:19:40205 // Not owned. Also used by IncognitoProcessManager.
206 ExtensionRegistry* extension_registry_;
207
208 private:
209 friend class ProcessManagerFactory;
210 friend class ProcessManagerTest;
211
[email protected]6c2381d2011-10-19 02:52:53212 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24213 void Observe(int type,
214 const content::NotificationSource& source,
215 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32216
limasdfe0f061b2015-02-27 00:35:06217 // ExtensionRegistryObserver:
218 void OnExtensionLoaded(content::BrowserContext* browser_context,
219 const Extension* extension) override;
220 void OnExtensionUnloaded(content::BrowserContext* browser_context,
221 const Extension* extension,
222 UnloadedExtensionInfo::Reason reason) override;
223
[email protected]103f19f2012-04-02 19:30:12224 // Extra information we keep for each extension's background page.
225 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40226 struct ExtensionRenderFrameData;
227 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
228 using ExtensionRenderFrames =
229 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10230
[email protected]6b54fda2014-07-22 02:13:47231 // Load all background pages once the profile data is ready and the pages
232 // should be loaded.
233 void CreateStartupBackgroundHosts();
234
[email protected]94de8cb2013-11-07 06:29:21235 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07236 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21237
[email protected]06024c62011-10-20 20:57:12238 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07239 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12240
rdevlin.cronin6ae04a012015-04-03 20:19:40241 // If the frame isn't keeping the lazy background page alive, increments the
242 // keepalive count to do so.
243 void AcquireLazyKeepaliveCountForFrame(
244 content::RenderFrameHost* render_frame_host);
245
246 // If the frame is keeping the lazy background page alive, decrements the
247 // keepalive count to stop doing it.
248 void ReleaseLazyKeepaliveCountForFrame(
249 content::RenderFrameHost* render_frame_host);
250
[email protected]388770152013-12-03 01:25:32251 // Internal implementation of DecrementLazyKeepaliveCount with an
252 // |extension_id| known to have a lazy background page.
253 void DecrementLazyKeepaliveCount(const std::string& extension_id);
254
255 // Checks if keepalive impulses have occured, and adjusts keep alive count.
256 void OnKeepaliveImpulseCheck();
257
[email protected]6baff0b52012-03-06 01:30:18258 // These are called when the extension transitions between idle and active.
259 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20260 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37261 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18262 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42263 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37264 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18265
rdevlin.cronin6ae04a012015-04-03 20:19:40266 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
[email protected]06024c62011-10-20 20:57:12267
rdevlin.cronin6ae04a012015-04-03 20:19:40268 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10269 // which has been unloaded.
270 void UnregisterExtension(const std::string& extension_id);
271
[email protected]caffe702012-05-03 05:30:17272 // Clears background page data for this extension.
273 void ClearBackgroundPageData(const std::string& extension_id);
274
rdevlin.cronin6ae04a012015-04-03 20:19:40275 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02276
rdevlin.cronin6ae04a012015-04-03 20:19:40277 // The set of ExtensionHosts running viewless background extensions.
278 ExtensionHostSet background_hosts_;
279
280 // A SiteInstance related to the SiteInstance for all extensions in
281 // this profile. We create it in such a way that a new
282 // browsing instance is created. This controls process grouping.
283 scoped_refptr<content::SiteInstance> site_instance_;
284
285 // The browser context associated with the |site_instance_|.
286 content::BrowserContext* browser_context_;
287
288 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48289 // extensions. We also keep a cache of the host's view type, because that
290 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40291 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48292
[email protected]103f19f2012-04-02 19:30:12293 BackgroundPageDataMap background_page_data_;
294
[email protected]aa3dd492013-11-05 17:09:09295 // True if we have created the startup set of background hosts.
296 bool startup_background_hosts_created_;
297
[email protected]75e7bac2013-11-01 23:33:42298 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
299
[email protected]b6cd7202014-01-07 17:46:55300 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
301 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
302
brettw236d3172015-06-03 16:31:43303 base::ObserverList<ProcessManagerObserver> observer_list_;
[email protected]08d469b22014-03-31 00:51:24304
[email protected]77a538f2014-07-14 22:25:37305 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
306 // members. These IDs are tracked per extension in background_page_data_ and
307 // are used to verify that nothing has interrupted the process of closing a
308 // lazy background process.
309 //
310 // Any interruption obtains a new ID by incrementing
311 // last_background_close_sequence_id_ and storing it in background_page_data_
312 // for a particular extension. Callbacks and round-trip IPC messages store the
313 // value of the extension's close_sequence_id at the beginning of the process.
314 // Thus comparisons can be done to halt when IDs no longer match.
315 //
316 // This counter provides unique IDs even when BackgroundPageData objects are
317 // reset.
318 uint64 last_background_close_sequence_id_;
319
rockote5703292015-09-28 23:23:09320 // Tracks pending network requests by opaque ID. This is used to ensure proper
321 // keepalive counting in response to request status updates; e.g., if an
322 // extension URLRequest is constructed and then destroyed without ever
323 // starting, we can receive a completion notification without a corresponding
324 // start notification. In that case we want to avoid decrementing keepalive.
325 std::set<int> pending_network_requests_;
326
[email protected]77a538f2014-07-14 22:25:37327 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07328 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16329
[email protected]98b6d942013-11-10 00:34:07330 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05331};
332
[email protected]98b6d942013-11-10 00:34:07333} // namespace extensions
334
335#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_