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