Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_ |
| 6 | #define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_ |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 7 | |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 10 | #include <map> |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 11 | #include <set> |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 12 | #include <string> |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 13 | #include <utility> |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 14 | #include <vector> |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 15 | |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 16 | #include "base/callback.h" |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 17 | #include "base/compiler_specific.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 18 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 19 | #include "base/memory/ref_counted.h" |
[email protected] | 99c49b5 | 2012-05-02 01:50:20 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
[email protected] | 08d469b2 | 2014-03-31 00:51:24 | [diff] [blame] | 21 | #include "base/observer_list.h" |
Sigurdur Asgeirsson | 834f057 | 2021-03-24 13:24:58 | [diff] [blame] | 22 | #include "base/scoped_multi_source_observation.h" |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 23 | #include "components/keyed_service/core/keyed_service.h" |
dgozman | 47679eb1 | 2016-10-17 17:30:18 | [diff] [blame] | 24 | #include "content/public/browser/devtools_agent_host_observer.h" |
Evan Stade | 922f3f1f | 2019-09-04 21:05:13 | [diff] [blame] | 25 | #include "content/public/browser/render_process_host.h" |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 26 | #include "content/public/browser/render_process_host_observer.h" |
Istiaque Ahmed | fbeff9fb | 2022-02-09 22:15:34 | [diff] [blame] | 27 | #include "content/public/browser/service_worker_external_request_timeout_type.h" |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 28 | #include "extensions/browser/activity.h" |
kmarshall | 24b29b2 | 2015-04-29 01:41:47 | [diff] [blame] | 29 | #include "extensions/browser/event_page_tracker.h" |
Vasiliy Telezhnikov | 9980b40 | 2021-02-18 20:41:54 | [diff] [blame] | 30 | #include "extensions/browser/extension_host_observer.h" |
limasdf | e0f061b | 2015-02-27 00:35:06 | [diff] [blame] | 31 | #include "extensions/browser/extension_registry_observer.h" |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 32 | #include "extensions/browser/service_worker/worker_id.h" |
| 33 | #include "extensions/browser/service_worker/worker_id_set.h" |
rdevlin.cronin | 0670b56 | 2016-07-02 02:05:43 | [diff] [blame] | 34 | #include "extensions/common/extension_id.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 35 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 36 | class GURL; |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 37 | |
| 38 | namespace content { |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 39 | class BrowserContext; |
[email protected] | a23f62e | 2013-04-26 13:13:02 | [diff] [blame] | 40 | class DevToolsAgentHost; |
[email protected] | 9dfed87 | 2013-12-30 23:08:56 | [diff] [blame] | 41 | class RenderFrameHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 42 | class SiteInstance; |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 43 | class WebContents; |
Nico Weber | 210c0bc | 2019-02-11 22:01:09 | [diff] [blame] | 44 | } // namespace content |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 45 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 46 | namespace extensions { |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 47 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 48 | class Extension; |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 49 | class ExtensionHost; |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 50 | class ExtensionRegistry; |
[email protected] | 08d469b2 | 2014-03-31 00:51:24 | [diff] [blame] | 51 | class ProcessManagerObserver; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 52 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 53 | // Manages dynamic state of running Chromium extensions. There is one instance |
| 54 | // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 55 | // track of split-mode extensions only. |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 56 | class ProcessManager : public KeyedService, |
kmarshall | 24b29b2 | 2015-04-29 01:41:47 | [diff] [blame] | 57 | public ExtensionRegistryObserver, |
dgozman | 47679eb1 | 2016-10-17 17:30:18 | [diff] [blame] | 58 | public EventPageTracker, |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 59 | public content::DevToolsAgentHostObserver, |
Vasiliy Telezhnikov | 9980b40 | 2021-02-18 20:41:54 | [diff] [blame] | 60 | public content::RenderProcessHostObserver, |
| 61 | public ExtensionHostObserver { |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 62 | public: |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 63 | using ExtensionHostSet = std::set<extensions::ExtensionHost*>; |
[email protected] | d1fe135 | 2012-04-26 00:47:32 | [diff] [blame] | 64 | |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 65 | static ProcessManager* Get(content::BrowserContext* context); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 66 | |
| 67 | ProcessManager(const ProcessManager&) = delete; |
| 68 | ProcessManager& operator=(const ProcessManager&) = delete; |
| 69 | |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 70 | ~ProcessManager() override; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 71 | |
sense | 383ce0f | 2017-03-24 04:06:43 | [diff] [blame] | 72 | // KeyedService support: |
| 73 | void Shutdown() override; |
| 74 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 75 | void RegisterRenderFrameHost(content::WebContents* web_contents, |
| 76 | content::RenderFrameHost* render_frame_host, |
| 77 | const Extension* extension); |
| 78 | void UnregisterRenderFrameHost(content::RenderFrameHost* render_frame_host); |
[email protected] | d1fe135 | 2012-04-26 00:47:32 | [diff] [blame] | 79 | |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 80 | // Registers or unregisters a running worker state to this process manager. |
| 81 | // Note: This does not create any Service Workers. |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 82 | void RegisterServiceWorker(const WorkerId& worker_id); |
| 83 | void UnregisterServiceWorker(const WorkerId& worker_id); |
| 84 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 85 | // Returns the SiteInstance that the given URL belongs to. |
Karandeep Bhatia | 48d90fdf | 2021-07-09 01:20:51 | [diff] [blame] | 86 | // NOTE: Usage of this method is potentially error-prone. An extension can |
| 87 | // correspond to multiple SiteInstances (e.g. consider a cross origin isolated |
| 88 | // extension with non-cross-origin-isolated contexts). |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 89 | // TODO(aa): This only returns correct results for extensions and packaged |
| 90 | // apps, not hosted apps. |
| 91 | virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL( |
| 92 | const GURL& url); |
[email protected] | d1fe135 | 2012-04-26 00:47:32 | [diff] [blame] | 93 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 94 | using FrameSet = std::set<content::RenderFrameHost*>; |
| 95 | const FrameSet GetAllFrames() const; |
| 96 | |
| 97 | // Returns all RenderFrameHosts that are registered for the specified |
| 98 | // extension. |
| 99 | ProcessManager::FrameSet GetRenderFrameHostsForExtension( |
| 100 | const std::string& extension_id); |
| 101 | |
rob | cdcc4b8 | 2015-12-06 12:39:45 | [diff] [blame] | 102 | bool IsRenderFrameHostRegistered(content::RenderFrameHost* render_frame_host); |
| 103 | |
[email protected] | 08d469b2 | 2014-03-31 00:51:24 | [diff] [blame] | 104 | void AddObserver(ProcessManagerObserver* observer); |
| 105 | void RemoveObserver(ProcessManagerObserver* observer); |
| 106 | |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 107 | // Creates a new UI-less extension instance. Like CreateViewHost, but not |
[email protected] | 6ad9cdf7 | 2014-02-27 13:12:41 | [diff] [blame] | 108 | // displayed anywhere. Returns false if no background host can be created, |
| 109 | // for example for hosted apps and extensions that aren't enabled in |
| 110 | // Incognito. |
| 111 | virtual bool CreateBackgroundHost(const Extension* extension, |
| 112 | const GURL& url); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 113 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 114 | // Creates background hosts if the embedder is ready and they are not already |
| 115 | // loaded. |
| 116 | void MaybeCreateStartupBackgroundHosts(); |
| 117 | |
| 118 | // Gets the ExtensionHost for the background page for an extension, or null if |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 119 | // the extension isn't running or doesn't have a background page. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 120 | ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 121 | |
Devlin Cronin | ccf3b9d | 2022-06-30 02:16:16 | [diff] [blame] | 122 | // Returns the background page ExtensionHost for the given |
| 123 | // |render_frame_host|, if |render_frame_host| is within the extension's |
| 124 | // background. Note that this will return the background page host for |
| 125 | // iframes embedded in the background page, even if they are not extension |
| 126 | // frames. |
| 127 | // TODO(https://crbug.com/1340001): Make these "gotchas" less subtle. |
rdevlin.cronin | b48a98e | 2015-05-01 00:00:28 | [diff] [blame] | 128 | ExtensionHost* GetExtensionHostForRenderFrameHost( |
| 129 | content::RenderFrameHost* render_frame_host); |
| 130 | |
[email protected] | 7042b68 | 2012-04-19 22:57:51 | [diff] [blame] | 131 | // Returns true if the (lazy) background host for the given extension has |
| 132 | // already been sent the unload event and is shutting down. |
| 133 | bool IsBackgroundHostClosing(const std::string& extension_id); |
| 134 | |
Devlin Cronin | 7a282e3 | 2017-08-10 01:54:10 | [diff] [blame] | 135 | // Returns the extension associated with the specified RenderFrameHost, |
| 136 | // or null. |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 137 | const Extension* GetExtensionForRenderFrameHost( |
| 138 | content::RenderFrameHost* render_frame_host); |
Devlin Cronin | 7a282e3 | 2017-08-10 01:54:10 | [diff] [blame] | 139 | |
| 140 | // Returns the extension associated with the main frame of the given |
| 141 | // |web_contents|, or null if there isn't one. |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 142 | const Extension* GetExtensionForWebContents( |
Lucas Furukawa Gadani | e1c5dfda | 2018-11-29 17:57:41 | [diff] [blame] | 143 | content::WebContents* web_contents); |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 144 | |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 145 | // Getter and setter for the lazy background page's keepalive count. This is |
| 146 | // the count of how many outstanding "things" are keeping the page alive. |
| 147 | // When this reaches 0, we will begin the process of shutting down the page. |
| 148 | // "Things" include pending events, resource loads, and API calls. |
wez | 714dde1 | 2017-02-14 22:26:03 | [diff] [blame] | 149 | // Returns -1 if |extension| does not have a lazy background page. |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 150 | // The calls to increment and decrement the count also accept a category |
| 151 | // of activity and an extra string of data. These are kept so there is |
| 152 | // more information for the counts. See the Activity struct definition |
| 153 | // for more details regarding the extra data. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 154 | int GetLazyKeepaliveCount(const Extension* extension); |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 155 | void IncrementLazyKeepaliveCount(const Extension* extension, |
| 156 | Activity::Type activity_type, |
| 157 | const std::string& extra_data); |
| 158 | void DecrementLazyKeepaliveCount(const Extension* extension, |
| 159 | Activity::Type activity_type, |
| 160 | const std::string& extra_data); |
| 161 | |
Vasiliy Telezhnikov | 78b98d0 | 2021-03-16 14:26:28 | [diff] [blame] | 162 | // Sends out notification to observers when the extension process is gone. |
| 163 | void NotifyExtensionProcessTerminated(const Extension* extension); |
| 164 | |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 165 | // Methods to increment or decrement the ref-count of a specified service |
| 166 | // worker with id |worker_id|. |
| 167 | // The increment method returns the guid that needs to be passed to the |
| 168 | // decrement method. |
Istiaque Ahmed | fbeff9fb | 2022-02-09 22:15:34 | [diff] [blame] | 169 | // |timeout_type| is the SW's timeout behavior. |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 170 | std::string IncrementServiceWorkerKeepaliveCount( |
| 171 | const WorkerId& worker_id, |
Istiaque Ahmed | fbeff9fb | 2022-02-09 22:15:34 | [diff] [blame] | 172 | content::ServiceWorkerExternalRequestTimeoutType timeout_type, |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 173 | Activity::Type activity_type, |
| 174 | const std::string& extra_data); |
| 175 | // Decrements the ref-count of the specified worker with |worker_id| that |
| 176 | // had its ref-count incremented with |request_uuid|. |
| 177 | void DecrementServiceWorkerKeepaliveCount(const WorkerId& worker_id, |
| 178 | const std::string& request_uuid, |
| 179 | Activity::Type activity_type, |
| 180 | const std::string& extra_data); |
| 181 | |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 182 | using ActivitiesMultisetPair = std::pair<Activity::Type, std::string>; |
| 183 | using ActivitiesMultiset = std::multiset<ActivitiesMultisetPair>; |
| 184 | |
| 185 | // Return the current set of keep-alive activities for the extension. |
| 186 | ActivitiesMultiset GetLazyKeepaliveActivities(const Extension* extension); |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 187 | |
[email protected] | 584e657 | 2013-02-16 07:02:33 | [diff] [blame] | 188 | // Handles a response to the ShouldSuspend message, used for lazy background |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 189 | // pages. |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 190 | void OnShouldSuspendAck(const std::string& extension_id, |
| 191 | uint64_t sequence_id); |
[email protected] | 103f19f | 2012-04-02 19:30:12 | [diff] [blame] | 192 | |
[email protected] | 9dfed87 | 2013-12-30 23:08:56 | [diff] [blame] | 193 | // Tracks network requests for a given RenderFrameHost, used to know |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 194 | // when network activity is idle for lazy background pages. |
Vasiliy Telezhnikov | 9980b40 | 2021-02-18 20:41:54 | [diff] [blame] | 195 | void NetworkRequestStarted(content::RenderFrameHost* render_frame_host, |
| 196 | uint64_t request_id); |
| 197 | void NetworkRequestDone(content::RenderFrameHost* render_frame_host, |
| 198 | uint64_t request_id); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 199 | |
[email protected] | 0d475e07 | 2012-07-26 02:30:42 | [diff] [blame] | 200 | // Prevents |extension|'s background page from being closed and sends the |
| 201 | // onSuspendCanceled() event to it. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 202 | void CancelSuspend(const Extension* extension); |
[email protected] | 0d475e07 | 2012-07-26 02:30:42 | [diff] [blame] | 203 | |
[email protected] | 9602db4 | 2014-07-25 05:24:37 | [diff] [blame] | 204 | // Called on shutdown to close our extension hosts. |
| 205 | void CloseBackgroundHosts(); |
| 206 | |
kmarshall | 24b29b2 | 2015-04-29 01:41:47 | [diff] [blame] | 207 | // EventPageTracker implementation. |
| 208 | bool IsEventPageSuspended(const std::string& extension_id) override; |
| 209 | bool WakeEventPage(const std::string& extension_id, |
Istiaque Ahmed | 28edfd2 | 2018-08-30 23:40:47 | [diff] [blame] | 210 | base::OnceCallback<void(bool)> callback) override; |
kmarshall | 24b29b2 | 2015-04-29 01:41:47 | [diff] [blame] | 211 | |
yoz | a9bf560 | 2014-09-19 02:03:31 | [diff] [blame] | 212 | // Sets the time in milliseconds that an extension event page can |
| 213 | // be idle before it is shut down; must be > 0. |
| 214 | static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec); |
| 215 | |
| 216 | // Sets the time in milliseconds that an extension event page has |
| 217 | // between being notified of its impending unload and that unload |
| 218 | // happening. |
| 219 | static void SetEventPageSuspendingTimeForTesting( |
| 220 | unsigned suspending_time_msec); |
| 221 | |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 222 | // Creates a non-incognito instance for tests. |registry| allows unit tests |
| 223 | // to inject an ExtensionRegistry that is not managed by the usual |
| 224 | // BrowserContextKeyedServiceFactory system. |
| 225 | static ProcessManager* CreateForTesting(content::BrowserContext* context, |
| 226 | ExtensionRegistry* registry); |
| 227 | |
| 228 | // Creates an incognito-context instance for tests. |
[email protected] | b9f6ba3 | 2014-03-10 18:34:08 | [diff] [blame] | 229 | static ProcessManager* CreateIncognitoForTesting( |
| 230 | content::BrowserContext* incognito_context, |
| 231 | content::BrowserContext* original_context, |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 232 | ExtensionRegistry* registry); |
| 233 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 234 | content::BrowserContext* browser_context() const { return browser_context_; } |
| 235 | |
| 236 | const ExtensionHostSet& background_hosts() const { |
| 237 | return background_hosts_; |
| 238 | } |
| 239 | |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 240 | // Returns true if this ProcessManager has registered any worker with id |
| 241 | // |worker_id|. |
| 242 | bool HasServiceWorker(const WorkerId& worker_id) const; |
| 243 | |
Ghazale Hosseinabadi | b11bca62 | 2020-09-22 21:15:50 | [diff] [blame] | 244 | // Returns all the Service Worker infos that is active for the extension with |
| 245 | // |extension_id|. |
| 246 | std::vector<WorkerId> GetServiceWorkersForExtension( |
| 247 | const ExtensionId& extension_id) const; |
| 248 | |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 249 | bool startup_background_hosts_created_for_test() const { |
| 250 | return startup_background_hosts_created_; |
| 251 | } |
[email protected] | b9f6ba3 | 2014-03-10 18:34:08 | [diff] [blame] | 252 | |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 253 | std::vector<WorkerId> GetAllWorkersIdsForTesting(); |
| 254 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 255 | protected: |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 256 | static ProcessManager* Create(content::BrowserContext* context); |
| 257 | |
Reilly Grant | 58ea383 | 2020-06-24 17:47:55 | [diff] [blame] | 258 | // |context| is incognito pass the original context as |original_context|. |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 259 | // Otherwise pass the same context for both. Pass the ExtensionRegistry for |
| 260 | // |context| as |registry|, or override it for testing. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 261 | ProcessManager(content::BrowserContext* context, |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 262 | content::BrowserContext* original_context, |
| 263 | ExtensionRegistry* registry); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 264 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 265 | // Not owned. Also used by IncognitoProcessManager. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 266 | raw_ptr<ExtensionRegistry> extension_registry_; |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 267 | |
| 268 | private: |
| 269 | friend class ProcessManagerFactory; |
| 270 | friend class ProcessManagerTest; |
| 271 | |
limasdf | e0f061b | 2015-02-27 00:35:06 | [diff] [blame] | 272 | // ExtensionRegistryObserver: |
| 273 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 274 | const Extension* extension) override; |
| 275 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 276 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 277 | UnloadedExtensionReason reason) override; |
limasdf | e0f061b | 2015-02-27 00:35:06 | [diff] [blame] | 278 | |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 279 | // content::RenderProcessHostObserver: |
| 280 | void RenderProcessExited( |
| 281 | content::RenderProcessHost* host, |
| 282 | const content::ChildProcessTerminationInfo& info) override; |
| 283 | |
Vasiliy Telezhnikov | 9980b40 | 2021-02-18 20:41:54 | [diff] [blame] | 284 | // ExtensionHostObserver: |
| 285 | void OnExtensionHostDestroyed(ExtensionHost* host) override; |
Vasiliy Telezhnikov | 9980b40 | 2021-02-18 20:41:54 | [diff] [blame] | 286 | |
[email protected] | 103f19f | 2012-04-02 19:30:12 | [diff] [blame] | 287 | // Extra information we keep for each extension's background page. |
| 288 | struct BackgroundPageData; |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 289 | struct ExtensionRenderFrameData; |
| 290 | using BackgroundPageDataMap = std::map<ExtensionId, BackgroundPageData>; |
| 291 | using ExtensionRenderFrames = |
| 292 | std::map<content::RenderFrameHost*, ExtensionRenderFrameData>; |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 293 | |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 294 | // Load all background pages once the profile data is ready and the pages |
| 295 | // should be loaded. |
| 296 | void CreateStartupBackgroundHosts(); |
| 297 | |
[email protected] | 94de8cb | 2013-11-07 06:29:21 | [diff] [blame] | 298 | // Called just after |host| is created so it can be registered in our lists. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 299 | void OnBackgroundHostCreated(ExtensionHost* host); |
[email protected] | 94de8cb | 2013-11-07 06:29:21 | [diff] [blame] | 300 | |
Devlin Cronin | 36eecdae0 | 2022-07-07 18:15:08 | [diff] [blame] | 301 | // Handles a request from a created extension host to close the contents. |
| 302 | // This happens in cases such as the contents calling `window.close()`. |
| 303 | void HandleCloseExtensionHost(ExtensionHost* host); |
| 304 | |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 305 | // Close the given |host| iff it's a background page. |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 306 | void CloseBackgroundHost(ExtensionHost* host); |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 307 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 308 | // If the frame isn't keeping the lazy background page alive, increments the |
| 309 | // keepalive count to do so. |
| 310 | void AcquireLazyKeepaliveCountForFrame( |
| 311 | content::RenderFrameHost* render_frame_host); |
| 312 | |
| 313 | // If the frame is keeping the lazy background page alive, decrements the |
| 314 | // keepalive count to stop doing it. |
| 315 | void ReleaseLazyKeepaliveCountForFrame( |
| 316 | content::RenderFrameHost* render_frame_host); |
| 317 | |
[email protected] | 38877015 | 2013-12-03 01:25:32 | [diff] [blame] | 318 | // Internal implementation of DecrementLazyKeepaliveCount with an |
| 319 | // |extension_id| known to have a lazy background page. |
| 320 | void DecrementLazyKeepaliveCount(const std::string& extension_id); |
David Bertoni | 3e1e9fa | 2018-08-29 20:39:30 | [diff] [blame] | 321 | void DecrementLazyKeepaliveCount(const std::string& extension_id, |
| 322 | Activity::Type activity_type, |
| 323 | const std::string& extra_data); |
[email protected] | 38877015 | 2013-12-03 01:25:32 | [diff] [blame] | 324 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 325 | // These are called when the extension transitions between idle and active. |
| 326 | // They control the process of closing the background page when idle. |
[email protected] | 99c49b5 | 2012-05-02 01:50:20 | [diff] [blame] | 327 | void OnLazyBackgroundPageIdle(const std::string& extension_id, |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 328 | uint64_t sequence_id); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 329 | void OnLazyBackgroundPageActive(const std::string& extension_id); |
[email protected] | 0d475e07 | 2012-07-26 02:30:42 | [diff] [blame] | 330 | void CloseLazyBackgroundPageNow(const std::string& extension_id, |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 331 | uint64_t sequence_id); |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame] | 332 | |
dgozman | 47679eb1 | 2016-10-17 17:30:18 | [diff] [blame] | 333 | const Extension* GetExtensionForAgentHost( |
| 334 | content::DevToolsAgentHost* agent_host); |
| 335 | |
| 336 | // content::DevToolsAgentHostObserver overrides. |
| 337 | void DevToolsAgentHostAttached( |
| 338 | content::DevToolsAgentHost* agent_host) override; |
| 339 | void DevToolsAgentHostDetached( |
| 340 | content::DevToolsAgentHost* agent_host) override; |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 341 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 342 | // Unregister RenderFrameHosts and clear background page data for an extension |
[email protected] | 5b3ee85 | 2013-09-26 06:33:10 | [diff] [blame] | 343 | // which has been unloaded. |
| 344 | void UnregisterExtension(const std::string& extension_id); |
| 345 | |
[email protected] | caffe70 | 2012-05-03 05:30:17 | [diff] [blame] | 346 | // Clears background page data for this extension. |
| 347 | void ClearBackgroundPageData(const std::string& extension_id); |
| 348 | |
Gyuyoung Kim | f271be4 | 2021-03-03 03:09:37 | [diff] [blame] | 349 | // Handles a response to the SuspendExtension Mojo method, used for lazy |
| 350 | // background pages. |
| 351 | void OnSuspendAck(const std::string& extension_id); |
| 352 | |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 353 | // The set of ExtensionHosts running viewless background extensions. |
| 354 | ExtensionHostSet background_hosts_; |
| 355 | |
| 356 | // A SiteInstance related to the SiteInstance for all extensions in |
| 357 | // this profile. We create it in such a way that a new |
| 358 | // browsing instance is created. This controls process grouping. |
| 359 | scoped_refptr<content::SiteInstance> site_instance_; |
| 360 | |
| 361 | // The browser context associated with the |site_instance_|. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 362 | raw_ptr<content::BrowserContext> browser_context_; |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 363 | |
| 364 | // Contains all active extension-related RenderFrameHost instances for all |
[email protected] | 35e0e079 | 2012-11-30 02:35:48 | [diff] [blame] | 365 | // extensions. We also keep a cache of the host's view type, because that |
| 366 | // information is not accessible at registration/deregistration time. |
rdevlin.cronin | 6ae04a01 | 2015-04-03 20:19:40 | [diff] [blame] | 367 | ExtensionRenderFrames all_extension_frames_; |
[email protected] | 35e0e079 | 2012-11-30 02:35:48 | [diff] [blame] | 368 | |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 369 | // TaskRunner for interacting with ServiceWorkerContexts. |
Matt Falkenhagen | d55b928 | 2019-09-10 23:53:35 | [diff] [blame] | 370 | // TODO(crbug.com/824858): This is unused when ServiceWorkerOnUI is enabled. |
Istiaque Ahmed | b744ecf | 2019-02-06 00:23:32 | [diff] [blame] | 371 | scoped_refptr<base::SequencedTaskRunner> worker_task_runner_; |
| 372 | |
| 373 | // Contains all active extension Service Worker information for all |
| 374 | // extensions. |
| 375 | WorkerIdSet all_extension_workers_; |
| 376 | |
[email protected] | 103f19f | 2012-04-02 19:30:12 | [diff] [blame] | 377 | BackgroundPageDataMap background_page_data_; |
| 378 | |
[email protected] | aa3dd49 | 2013-11-05 17:09:09 | [diff] [blame] | 379 | // True if we have created the startup set of background hosts. |
| 380 | bool startup_background_hosts_created_; |
| 381 | |
Evan Stade | 22e3f9b | 2019-10-01 21:12:17 | [diff] [blame] | 382 | base::ObserverList<ProcessManagerObserver> observer_list_; |
[email protected] | 08d469b2 | 2014-03-31 00:51:24 | [diff] [blame] | 383 | |
[email protected] | 77a538f | 2014-07-14 22:25:37 | [diff] [blame] | 384 | // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id |
| 385 | // members. These IDs are tracked per extension in background_page_data_ and |
| 386 | // are used to verify that nothing has interrupted the process of closing a |
| 387 | // lazy background process. |
| 388 | // |
| 389 | // Any interruption obtains a new ID by incrementing |
| 390 | // last_background_close_sequence_id_ and storing it in background_page_data_ |
| 391 | // for a particular extension. Callbacks and round-trip IPC messages store the |
| 392 | // value of the extension's close_sequence_id at the beginning of the process. |
| 393 | // Thus comparisons can be done to halt when IDs no longer match. |
| 394 | // |
| 395 | // This counter provides unique IDs even when BackgroundPageData objects are |
| 396 | // reset. |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 397 | uint64_t last_background_close_sequence_id_; |
[email protected] | 77a538f | 2014-07-14 22:25:37 | [diff] [blame] | 398 | |
rockot | e570329 | 2015-09-28 23:23:09 | [diff] [blame] | 399 | // Tracks pending network requests by opaque ID. This is used to ensure proper |
| 400 | // keepalive counting in response to request status updates; e.g., if an |
| 401 | // extension URLRequest is constructed and then destroyed without ever |
| 402 | // starting, we can receive a completion notification without a corresponding |
| 403 | // start notification. In that case we want to avoid decrementing keepalive. |
nasko | d13fd29 | 2016-06-07 20:33:33 | [diff] [blame] | 404 | std::map<int, ExtensionHost*> pending_network_requests_; |
rockot | e570329 | 2015-09-28 23:23:09 | [diff] [blame] | 405 | |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 406 | // Observers of Service Worker RPH this ProcessManager manages. |
Sigurdur Asgeirsson | 834f057 | 2021-03-24 13:24:58 | [diff] [blame] | 407 | base::ScopedMultiSourceObservation<content::RenderProcessHost, |
| 408 | content::RenderProcessHostObserver> |
| 409 | process_observations_{this}; |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 410 | // Maps render render_process_id -> extension_id for all Service Workers this |
| 411 | // ProcessManager manages. |
| 412 | std::map<int, std::set<ExtensionId>> worker_process_to_extension_ids_; |
| 413 | |
[email protected] | 77a538f | 2014-07-14 22:25:37 | [diff] [blame] | 414 | // Must be last member, see doc on WeakPtrFactory. |
Jeremy Roman | 9fc2de6 | 2019-07-12 14:15:03 | [diff] [blame] | 415 | base::WeakPtrFactory<ProcessManager> weak_ptr_factory_{this}; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 416 | }; |
| 417 | |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 418 | } // namespace extensions |
| 419 | |
| 420 | #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_ |