blob: 73cf098d25d1dd11e42a6eb2afd86d596d4a5d9b [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
avic9cec102015-12-23 00:39:268#include <stdint.h>
9
[email protected]45776222009-07-15 20:21:5810#include <map>
[email protected]8a17bd52009-06-06 08:19:4911#include <set>
[email protected]45776222009-07-15 20:21:5812#include <string>
David Bertoni3e1e9fa2018-08-29 20:39:3013#include <utility>
[email protected]481e1a42009-05-06 20:56:0514
[email protected]a23f62e2013-04-26 13:13:0215#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5416#include "base/compiler_specific.h"
avic9cec102015-12-23 00:39:2617#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1518#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2019#include "base/memory/weak_ptr.h"
[email protected]08d469b22014-03-31 00:51:2420#include "base/observer_list.h"
reillyg0ea3fa902014-10-28 15:30:2321#include "components/keyed_service/core/keyed_service.h"
dgozman47679eb12016-10-17 17:30:1822#include "content/public/browser/devtools_agent_host_observer.h"
[email protected]6c2381d2011-10-19 02:52:5323#include "content/public/browser/notification_observer.h"
24#include "content/public/browser/notification_registrar.h"
David Bertoni3e1e9fa2018-08-29 20:39:3025#include "extensions/browser/activity.h"
kmarshall24b29b22015-04-29 01:41:4726#include "extensions/browser/event_page_tracker.h"
limasdfe0f061b2015-02-27 00:35:0627#include "extensions/browser/extension_registry_observer.h"
rdevlin.cronin0670b562016-07-02 02:05:4328#include "extensions/common/extension_id.h"
[email protected]cb2edf22013-04-01 20:25:2329#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0530
[email protected]481e1a42009-05-06 20:56:0531class GURL;
[email protected]b6583592012-01-25 19:52:3332
33namespace content {
[email protected]7f474212013-11-05 04:26:1634class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0235class DevToolsAgentHost;
[email protected]9dfed872013-12-30 23:08:5636class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0537class SiteInstance;
rdevlin.cronin6ae04a012015-04-03 20:19:4038class WebContents;
[email protected]b6583592012-01-25 19:52:3339};
[email protected]481e1a42009-05-06 20:56:0540
[email protected]1c321ee2012-05-21 03:02:3441namespace extensions {
[email protected]98b6d942013-11-10 00:34:0742
[email protected]1c321ee2012-05-21 03:02:3443class Extension;
[email protected]3a1dc572012-07-31 22:25:1344class ExtensionHost;
[email protected]6b54fda2014-07-22 02:13:4745class ExtensionRegistry;
[email protected]08d469b22014-03-31 00:51:2446class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3447
[email protected]bc535ee52010-08-31 18:40:3248// Manages dynamic state of running Chromium extensions. There is one instance
49// of this class per Profile. OTR Profiles have a separate instance that keeps
50// track of split-mode extensions only.
reillyg0ea3fa902014-10-28 15:30:2351class ProcessManager : public KeyedService,
limasdfe0f061b2015-02-27 00:35:0652 public content::NotificationObserver,
kmarshall24b29b22015-04-29 01:41:4753 public ExtensionRegistryObserver,
dgozman47679eb12016-10-17 17:30:1854 public EventPageTracker,
55 public content::DevToolsAgentHostObserver {
[email protected]481e1a42009-05-06 20:56:0556 public:
rdevlin.cronin6ae04a012015-04-03 20:19:4057 using ExtensionHostSet = std::set<extensions::ExtensionHost*>;
[email protected]d1fe1352012-04-26 00:47:3258
reillyg0ea3fa902014-10-28 15:30:2359 static ProcessManager* Get(content::BrowserContext* context);
dcheng9168b2f2014-10-21 12:38:2460 ~ProcessManager() override;
[email protected]481e1a42009-05-06 20:56:0561
sense383ce0f2017-03-24 04:06:4362 // KeyedService support:
63 void Shutdown() override;
64
rdevlin.cronin6ae04a012015-04-03 20:19:4065 void RegisterRenderFrameHost(content::WebContents* web_contents,
66 content::RenderFrameHost* render_frame_host,
67 const Extension* extension);
68 void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host);
[email protected]d1fe1352012-04-26 00:47:3269
rdevlin.cronin6ae04a012015-04-03 20:19:4070 // Returns the SiteInstance that the given URL belongs to.
71 // TODO(aa): This only returns correct results for extensions and packaged
72 // apps, not hosted apps.
73 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
74 const GURL& url);
[email protected]d1fe1352012-04-26 00:47:3275
rdevlin.cronin6ae04a012015-04-03 20:19:4076 using FrameSet = std::set<content::RenderFrameHost*>;
77 const FrameSet GetAllFrames() const;
78
79 // Returns all RenderFrameHosts that are registered for the specified
80 // extension.
81 ProcessManager::FrameSet GetRenderFrameHostsForExtension(
82 const std::string& extension_id);
83
robcdcc4b82015-12-06 12:39:4584 bool IsRenderFrameHostRegistered(content::RenderFrameHost* render_frame_host);
85
[email protected]08d469b22014-03-31 00:51:2486 void AddObserver(ProcessManagerObserver* observer);
87 void RemoveObserver(ProcessManagerObserver* observer);
88
[email protected]029ad372011-05-20 17:12:5689 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4190 // displayed anywhere. Returns false if no background host can be created,
91 // for example for hosted apps and extensions that aren't enabled in
92 // Incognito.
93 virtual bool CreateBackgroundHost(const Extension* extension,
94 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3295
rdevlin.cronin6ae04a012015-04-03 20:19:4096 // Creates background hosts if the embedder is ready and they are not already
97 // loaded.
98 void MaybeCreateStartupBackgroundHosts();
99
100 // Gets the ExtensionHost for the background page for an extension, or null if
[email protected]4814b512009-11-07 00:12:29101 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:07102 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:29103
rdevlin.croninb48a98e2015-05-01 00:00:28104 // Returns the ExtensionHost for the given |render_frame_host|, if there is
105 // one.
106 ExtensionHost* GetExtensionHostForRenderFrameHost(
107 content::RenderFrameHost* render_frame_host);
108
[email protected]7042b682012-04-19 22:57:51109 // Returns true if the (lazy) background host for the given extension has
110 // already been sent the unload event and is shutting down.
111 bool IsBackgroundHostClosing(const std::string& extension_id);
112
Devlin Cronin7a282e32017-08-10 01:54:10113 // Returns the extension associated with the specified RenderFrameHost,
114 // or null.
rdevlin.cronin6ae04a012015-04-03 20:19:40115 const Extension* GetExtensionForRenderFrameHost(
116 content::RenderFrameHost* render_frame_host);
Devlin Cronin7a282e32017-08-10 01:54:10117
118 // Returns the extension associated with the main frame of the given
119 // |web_contents|, or null if there isn't one.
rdevlin.cronin6ae04a012015-04-03 20:19:40120 const Extension* GetExtensionForWebContents(
Lucas Furukawa Gadanie1c5dfda2018-11-29 17:57:41121 content::WebContents* web_contents);
rdevlin.cronin6ae04a012015-04-03 20:19:40122
[email protected]720ad1312012-02-27 23:07:36123 // Getter and setter for the lazy background page's keepalive count. This is
124 // the count of how many outstanding "things" are keeping the page alive.
125 // When this reaches 0, we will begin the process of shutting down the page.
126 // "Things" include pending events, resource loads, and API calls.
wez714dde12017-02-14 22:26:03127 // Returns -1 if |extension| does not have a lazy background page.
David Bertoni3e1e9fa2018-08-29 20:39:30128 // The calls to increment and decrement the count also accept a category
129 // of activity and an extra string of data. These are kept so there is
130 // more information for the counts. See the Activity struct definition
131 // for more details regarding the extra data.
[email protected]98b6d942013-11-10 00:34:07132 int GetLazyKeepaliveCount(const Extension* extension);
David Bertoni3e1e9fa2018-08-29 20:39:30133 void IncrementLazyKeepaliveCount(const Extension* extension,
134 Activity::Type activity_type,
135 const std::string& extra_data);
136 void DecrementLazyKeepaliveCount(const Extension* extension,
137 Activity::Type activity_type,
138 const std::string& extra_data);
139
140 using ActivitiesMultisetPair = std::pair<Activity::Type, std::string>;
141 using ActivitiesMultiset = std::multiset<ActivitiesMultisetPair>;
142
143 // Return the current set of keep-alive activities for the extension.
144 ActivitiesMultiset GetLazyKeepaliveActivities(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36145
[email protected]584e6572013-02-16 07:02:33146 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36147 // pages.
avic9cec102015-12-23 00:39:26148 void OnShouldSuspendAck(const std::string& extension_id,
149 uint64_t sequence_id);
[email protected]103f19f2012-04-02 19:30:12150
[email protected]584e6572013-02-16 07:02:33151 // Same as above, for the Suspend message.
152 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12153
[email protected]9dfed872013-12-30 23:08:56154 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18155 // when network activity is idle for lazy background pages.
chirantan79788f62015-02-02 23:57:25156 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26157 uint64_t request_id);
chirantan79788f62015-02-02 23:57:25158 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
avic9cec102015-12-23 00:39:26159 uint64_t request_id);
[email protected]6baff0b52012-03-06 01:30:18160
[email protected]0d475e072012-07-26 02:30:42161 // Prevents |extension|'s background page from being closed and sends the
162 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07163 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42164
[email protected]9602db42014-07-25 05:24:37165 // Called on shutdown to close our extension hosts.
166 void CloseBackgroundHosts();
167
kmarshall24b29b22015-04-29 01:41:47168 // EventPageTracker implementation.
169 bool IsEventPageSuspended(const std::string& extension_id) override;
170 bool WakeEventPage(const std::string& extension_id,
Istiaque Ahmed28edfd22018-08-30 23:40:47171 base::OnceCallback<void(bool)> callback) override;
kmarshall24b29b22015-04-29 01:41:47172
yoza9bf5602014-09-19 02:03:31173 // Sets the time in milliseconds that an extension event page can
174 // be idle before it is shut down; must be > 0.
175 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
176
177 // Sets the time in milliseconds that an extension event page has
178 // between being notified of its impending unload and that unload
179 // happening.
180 static void SetEventPageSuspendingTimeForTesting(
181 unsigned suspending_time_msec);
182
[email protected]6b54fda2014-07-22 02:13:47183 // Creates a non-incognito instance for tests. |registry| allows unit tests
184 // to inject an ExtensionRegistry that is not managed by the usual
185 // BrowserContextKeyedServiceFactory system.
186 static ProcessManager* CreateForTesting(content::BrowserContext* context,
187 ExtensionRegistry* registry);
188
189 // Creates an incognito-context instance for tests.
[email protected]b9f6ba32014-03-10 18:34:08190 static ProcessManager* CreateIncognitoForTesting(
191 content::BrowserContext* incognito_context,
192 content::BrowserContext* original_context,
[email protected]6b54fda2014-07-22 02:13:47193 ExtensionRegistry* registry);
194
rdevlin.cronin6ae04a012015-04-03 20:19:40195 content::BrowserContext* browser_context() const { return browser_context_; }
196
197 const ExtensionHostSet& background_hosts() const {
198 return background_hosts_;
199 }
200
[email protected]6b54fda2014-07-22 02:13:47201 bool startup_background_hosts_created_for_test() const {
202 return startup_background_hosts_created_;
203 }
[email protected]b9f6ba32014-03-10 18:34:08204
[email protected]bc535ee52010-08-31 18:40:32205 protected:
reillyg0ea3fa902014-10-28 15:30:23206 static ProcessManager* Create(content::BrowserContext* context);
207
rdevlin.cronin6ae04a012015-04-03 20:19:40208 // |context| is incognito pass the master context as |original_context|.
[email protected]6b54fda2014-07-22 02:13:47209 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
210 // |context| as |registry|, or override it for testing.
[email protected]98b6d942013-11-10 00:34:07211 ProcessManager(content::BrowserContext* context,
[email protected]6b54fda2014-07-22 02:13:47212 content::BrowserContext* original_context,
213 ExtensionRegistry* registry);
[email protected]bc535ee52010-08-31 18:40:32214
rdevlin.cronin6ae04a012015-04-03 20:19:40215 // Not owned. Also used by IncognitoProcessManager.
216 ExtensionRegistry* extension_registry_;
217
218 private:
219 friend class ProcessManagerFactory;
220 friend class ProcessManagerTest;
221
[email protected]6c2381d2011-10-19 02:52:53222 // content::NotificationObserver:
dcheng9168b2f2014-10-21 12:38:24223 void Observe(int type,
224 const content::NotificationSource& source,
225 const content::NotificationDetails& details) override;
[email protected]bc535ee52010-08-31 18:40:32226
limasdfe0f061b2015-02-27 00:35:06227 // ExtensionRegistryObserver:
228 void OnExtensionLoaded(content::BrowserContext* browser_context,
229 const Extension* extension) override;
230 void OnExtensionUnloaded(content::BrowserContext* browser_context,
231 const Extension* extension,
limasdf0deef2042017-05-03 19:17:17232 UnloadedExtensionReason reason) override;
limasdfe0f061b2015-02-27 00:35:06233
[email protected]103f19f2012-04-02 19:30:12234 // Extra information we keep for each extension's background page.
235 struct BackgroundPageData;
rdevlin.cronin6ae04a012015-04-03 20:19:40236 struct ExtensionRenderFrameData;
237 using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>;
238 using ExtensionRenderFrames =
239 std::map<content::RenderFrameHost*, ExtensionRenderFrameData>;
[email protected]3e194992011-10-20 05:39:10240
[email protected]6b54fda2014-07-22 02:13:47241 // Load all background pages once the profile data is ready and the pages
242 // should be loaded.
243 void CreateStartupBackgroundHosts();
244
[email protected]94de8cb2013-11-07 06:29:21245 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07246 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21247
[email protected]06024c62011-10-20 20:57:12248 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07249 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12250
rdevlin.cronin6ae04a012015-04-03 20:19:40251 // If the frame isn't keeping the lazy background page alive, increments the
252 // keepalive count to do so.
253 void AcquireLazyKeepaliveCountForFrame(
254 content::RenderFrameHost* render_frame_host);
255
256 // If the frame is keeping the lazy background page alive, decrements the
257 // keepalive count to stop doing it.
258 void ReleaseLazyKeepaliveCountForFrame(
259 content::RenderFrameHost* render_frame_host);
260
[email protected]388770152013-12-03 01:25:32261 // Internal implementation of DecrementLazyKeepaliveCount with an
262 // |extension_id| known to have a lazy background page.
263 void DecrementLazyKeepaliveCount(const std::string& extension_id);
David Bertoni3e1e9fa2018-08-29 20:39:30264 void DecrementLazyKeepaliveCount(const std::string& extension_id,
265 Activity::Type activity_type,
266 const std::string& extra_data);
[email protected]388770152013-12-03 01:25:32267
[email protected]6baff0b52012-03-06 01:30:18268 // These are called when the extension transitions between idle and active.
269 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20270 void OnLazyBackgroundPageIdle(const std::string& extension_id,
avic9cec102015-12-23 00:39:26271 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18272 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42273 void CloseLazyBackgroundPageNow(const std::string& extension_id,
avic9cec102015-12-23 00:39:26274 uint64_t sequence_id);
[email protected]6baff0b52012-03-06 01:30:18275
dgozman47679eb12016-10-17 17:30:18276 const Extension* GetExtensionForAgentHost(
277 content::DevToolsAgentHost* agent_host);
278
279 // content::DevToolsAgentHostObserver overrides.
280 void DevToolsAgentHostAttached(
281 content::DevToolsAgentHost* agent_host) override;
282 void DevToolsAgentHostDetached(
283 content::DevToolsAgentHost* agent_host) override;
[email protected]06024c62011-10-20 20:57:12284
rdevlin.cronin6ae04a012015-04-03 20:19:40285 // Unregister RenderFrameHosts and clear background page data for an extension
[email protected]5b3ee852013-09-26 06:33:10286 // which has been unloaded.
287 void UnregisterExtension(const std::string& extension_id);
288
[email protected]caffe702012-05-03 05:30:17289 // Clears background page data for this extension.
290 void ClearBackgroundPageData(const std::string& extension_id);
291
rdevlin.cronin6ae04a012015-04-03 20:19:40292 content::NotificationRegistrar registrar_;
[email protected]a23f62e2013-04-26 13:13:02293
rdevlin.cronin6ae04a012015-04-03 20:19:40294 // The set of ExtensionHosts running viewless background extensions.
295 ExtensionHostSet background_hosts_;
296
297 // A SiteInstance related to the SiteInstance for all extensions in
298 // this profile. We create it in such a way that a new
299 // browsing instance is created. This controls process grouping.
300 scoped_refptr<content::SiteInstance> site_instance_;
301
302 // The browser context associated with the |site_instance_|.
303 content::BrowserContext* browser_context_;
304
305 // Contains all active extension-related RenderFrameHost instances for all
[email protected]35e0e0792012-11-30 02:35:48306 // extensions. We also keep a cache of the host's view type, because that
307 // information is not accessible at registration/deregistration time.
rdevlin.cronin6ae04a012015-04-03 20:19:40308 ExtensionRenderFrames all_extension_frames_;
[email protected]35e0e0792012-11-30 02:35:48309
[email protected]103f19f2012-04-02 19:30:12310 BackgroundPageDataMap background_page_data_;
311
[email protected]aa3dd492013-11-05 17:09:09312 // True if we have created the startup set of background hosts.
313 bool startup_background_hosts_created_;
314
Trent Apteda250ec3ab2018-08-19 08:52:19315 base::ObserverList<ProcessManagerObserver>::Unchecked observer_list_;
[email protected]08d469b22014-03-31 00:51:24316
[email protected]77a538f2014-07-14 22:25:37317 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
318 // members. These IDs are tracked per extension in background_page_data_ and
319 // are used to verify that nothing has interrupted the process of closing a
320 // lazy background process.
321 //
322 // Any interruption obtains a new ID by incrementing
323 // last_background_close_sequence_id_ and storing it in background_page_data_
324 // for a particular extension. Callbacks and round-trip IPC messages store the
325 // value of the extension's close_sequence_id at the beginning of the process.
326 // Thus comparisons can be done to halt when IDs no longer match.
327 //
328 // This counter provides unique IDs even when BackgroundPageData objects are
329 // reset.
avic9cec102015-12-23 00:39:26330 uint64_t last_background_close_sequence_id_;
[email protected]77a538f2014-07-14 22:25:37331
rockote5703292015-09-28 23:23:09332 // Tracks pending network requests by opaque ID. This is used to ensure proper
333 // keepalive counting in response to request status updates; e.g., if an
334 // extension URLRequest is constructed and then destroyed without ever
335 // starting, we can receive a completion notification without a corresponding
336 // start notification. In that case we want to avoid decrementing keepalive.
naskod13fd292016-06-07 20:33:33337 std::map<int, ExtensionHost*> pending_network_requests_;
rockote5703292015-09-28 23:23:09338
[email protected]77a538f2014-07-14 22:25:37339 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07340 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16341
[email protected]98b6d942013-11-10 00:34:07342 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05343};
344
[email protected]98b6d942013-11-10 00:34:07345} // namespace extensions
346
347#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_