Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [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 | |
| 5 | #ifndef EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_ |
| 6 | #define EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_ |
| 7 | |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <set> |
David Bertoni | ac01b94 | 2019-08-26 23:48:17 | [diff] [blame] | 10 | #include <unordered_map> |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 11 | #include <vector> |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 12 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 13 | #include "base/memory/raw_ptr.h" |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
Hans Wennborg | b3e433a | 2020-04-21 11:21:40 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 16 | #include "base/version.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 17 | #include "components/keyed_service/core/keyed_service.h" |
Istiaque Ahmed | 5eef463 | 2022-04-11 20:06:39 | [diff] [blame] | 18 | #include "content/public/browser/service_worker_context.h" |
Ghazale Hosseinabadi | 5ca84a7b | 2021-06-14 11:08:59 | [diff] [blame] | 19 | #include "content/public/browser/service_worker_context_observer.h" |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 20 | #include "extensions/browser/lazy_context_id.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 21 | #include "extensions/browser/lazy_context_task_queue.h" |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 22 | #include "extensions/browser/service_worker/worker_id.h" |
Istiaque Ahmed | 9987ca89 | 2020-01-09 22:47:17 | [diff] [blame] | 23 | #include "extensions/common/activation_sequence.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 24 | #include "extensions/common/extension_id.h" |
Lei Zhang | 698df03c | 2021-05-21 04:23:34 | [diff] [blame] | 25 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Ghazale Hosseinabadi | 77dcc35 | 2020-12-21 09:18:22 | [diff] [blame] | 26 | #include "third_party/blink/public/common/service_worker/service_worker_status_code.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 27 | #include "url/gurl.h" |
| 28 | |
| 29 | namespace content { |
| 30 | class BrowserContext; |
| 31 | } |
| 32 | |
| 33 | namespace extensions { |
| 34 | class Extension; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 35 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 36 | // A service worker based background specific LazyContextTaskQueue. |
| 37 | // |
| 38 | // This class queues up and runs tasks added through AddPendingTask, after |
| 39 | // registering and starting extension's background Service Worker script if |
| 40 | // necessary. |
| 41 | // |
| 42 | // There are two sets of concepts/events that are important to this class: |
| 43 | // |
| 44 | // C1) Registering and starting a background worker: |
| 45 | // Upon extension activation, this class registers the extension's |
| 46 | // background worker if necessary. After that, if it has queued up tasks |
| 47 | // in |pending_tasks_|, then it moves on to starting the worker. Registration |
| 48 | // and start are initiated from this class. Once started, the worker is |
| 49 | // considered browser process ready. These workers are stored in |
| 50 | // |worker_state_map_| with |browser_ready| = false until we run tasks. |
| 51 | // |
| 52 | // C2) Listening for worker's state update from the renderer: |
| 53 | // - Init (DidInitializeServiceWorkerContext) when the worker is initialized, |
| 54 | // JavaScript starts running after this. |
| 55 | // - Start (DidStartServiceWorkerContext) when the worker has reached |
| 56 | // loadstop. The worker is considered ready to run tasks from this task |
| 57 | // queue. The worker's entry in |worker_state_map_| will carry |
| 58 | // |renderer_ready| = true. |
| 59 | // - Stop (DidStopServiceWorkerContext) when the worker is destroyed, we clear |
| 60 | // its |renderer_ready| status from |worker_state_map_|. |
| 61 | // |
| 62 | // Once a worker reaches readiness in both browser process |
| 63 | // (DidStartWorkerForScope) and worker process (DidStartServiceWorkerContext), |
| 64 | // we consider the worker to be ready to run tasks from |pending_tasks_|. |
| 65 | // Note that events from #C1 and #C2 are somewhat independent, e.g. it is |
| 66 | // possible to see an Init state update from #C2 before #C1 has seen a start |
| 67 | // worker completion. |
| 68 | // |
| 69 | // Sequences of extension activation: |
| 70 | // This class also assigns a unique sequence id to an extension activation so |
| 71 | // that it can differentiate between two activations of a particular extension |
| 72 | // (e.g. reloading an extension can cause two activations). |pending_tasks_|, |
| 73 | // worker registration and start (#C1) have sequence ids attached to them. |
| 74 | // The sequence is expired upon extension deactivation, and tasks are dropped |
| 75 | // from |pending_tasks_|. |
| 76 | // |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 77 | // TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled. |
| 78 | class ServiceWorkerTaskQueue : public KeyedService, |
Ghazale Hosseinabadi | 5ca84a7b | 2021-06-14 11:08:59 | [diff] [blame] | 79 | public LazyContextTaskQueue, |
| 80 | public content::ServiceWorkerContextObserver { |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 81 | public: |
| 82 | explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 83 | |
| 84 | ServiceWorkerTaskQueue(const ServiceWorkerTaskQueue&) = delete; |
| 85 | ServiceWorkerTaskQueue& operator=(const ServiceWorkerTaskQueue&) = delete; |
| 86 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 87 | ~ServiceWorkerTaskQueue() override; |
| 88 | |
| 89 | // Convenience method to return the ServiceWorkerTaskQueue for a given |
| 90 | // |context|. |
| 91 | static ServiceWorkerTaskQueue* Get(content::BrowserContext* context); |
| 92 | |
| 93 | bool ShouldEnqueueTask(content::BrowserContext* context, |
| 94 | const Extension* extension) override; |
David Bertoni | 8269a09 | 2018-12-19 15:55:42 | [diff] [blame] | 95 | void AddPendingTask(const LazyContextId& context_id, |
| 96 | PendingTask task) override; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 97 | |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 98 | // Performs Service Worker related tasks upon |extension| activation, |
| 99 | // e.g. registering |extension|'s worker, executing any pending tasks. |
| 100 | void ActivateExtension(const Extension* extension); |
| 101 | // Performs Service Worker related tasks upon |extension| deactivation, |
| 102 | // e.g. unregistering |extension|'s worker. |
| 103 | void DeactivateExtension(const Extension* extension); |
| 104 | |
Istiaque Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 105 | // Called once an extension Service Worker context was initialized but not |
| 106 | // necessarily started executing its JavaScript. |
| 107 | void DidInitializeServiceWorkerContext(int render_process_id, |
| 108 | const ExtensionId& extension_id, |
| 109 | int64_t service_worker_version_id, |
| 110 | int thread_id); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 111 | // Called once an extension Service Worker started running. |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 112 | // This can be thought as "loadstop", i.e. the global JS script of the worker |
| 113 | // has completed executing. |
| 114 | void DidStartServiceWorkerContext(int render_process_id, |
| 115 | const ExtensionId& extension_id, |
Istiaque Ahmed | 9987ca89 | 2020-01-09 22:47:17 | [diff] [blame] | 116 | ActivationSequence activation_sequence, |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 117 | const GURL& service_worker_scope, |
| 118 | int64_t service_worker_version_id, |
| 119 | int thread_id); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 120 | // Called once an extension Service Worker was destroyed. |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 121 | void DidStopServiceWorkerContext(int render_process_id, |
| 122 | const ExtensionId& extension_id, |
Istiaque Ahmed | 9987ca89 | 2020-01-09 22:47:17 | [diff] [blame] | 123 | ActivationSequence activation_sequence, |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 124 | const GURL& service_worker_scope, |
| 125 | int64_t service_worker_version_id, |
| 126 | int thread_id); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 127 | |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 128 | // Returns the current ActivationSequence for an extension, if the extension |
Anton Bikineev | 6d67847 | 2021-05-15 18:48:51 | [diff] [blame] | 129 | // is currently activated. Returns absl::nullopt if the extension isn't |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 130 | // activated. |
Anton Bikineev | 6d67847 | 2021-05-15 18:48:51 | [diff] [blame] | 131 | absl::optional<ActivationSequence> GetCurrentSequence( |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 132 | const ExtensionId& extension_id) const; |
| 133 | |
Istiaque Ahmed | 6e5a083 | 2021-08-26 23:41:14 | [diff] [blame] | 134 | // Activates incognito split mode extensions that are activated in |other| |
| 135 | // task queue. |
| 136 | void ActivateIncognitoSplitModeExtensions(ServiceWorkerTaskQueue* other); |
| 137 | |
Ghazale Hosseinabadi | 5ca84a7b | 2021-06-14 11:08:59 | [diff] [blame] | 138 | // content::ServiceWorkerContextObserver: |
| 139 | void OnReportConsoleMessage(int64_t version_id, |
| 140 | const GURL& scope, |
| 141 | const content::ConsoleMessage& message) override; |
| 142 | void OnDestruct(content::ServiceWorkerContext* context) override; |
| 143 | |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 144 | class TestObserver { |
| 145 | public: |
David Bertoni | 77615d2 | 2019-05-29 23:10:13 | [diff] [blame] | 146 | TestObserver(); |
Peter Boström | 951cf77e | 2021-09-22 00:02:59 | [diff] [blame] | 147 | |
| 148 | TestObserver(const TestObserver&) = delete; |
| 149 | TestObserver& operator=(const TestObserver&) = delete; |
| 150 | |
David Bertoni | 77615d2 | 2019-05-29 23:10:13 | [diff] [blame] | 151 | virtual ~TestObserver(); |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 152 | |
| 153 | // Called when an extension with id |extension_id| is going to be activated. |
| 154 | // |will_register_service_worker| is true if a Service Worker will be |
| 155 | // registered. |
| 156 | virtual void OnActivateExtension(const ExtensionId& extension_id, |
Istiaque Ahmed | 43949bf7 | 2020-03-20 04:42:08 | [diff] [blame] | 157 | bool will_register_service_worker) {} |
Ghazale Hosseinabadi | 77dcc35 | 2020-12-21 09:18:22 | [diff] [blame] | 158 | virtual void DidStartWorkerFail( |
| 159 | const ExtensionId& extension_id, |
| 160 | size_t num_pending_tasks, |
| 161 | blink::ServiceWorkerStatusCode status_code) {} |
Istiaque Ahmed | 5eef463 | 2022-04-11 20:06:39 | [diff] [blame] | 162 | |
| 163 | // Called when SW was re-registered to fix missing registration, and that |
| 164 | // step finished to mitigate the problem. |
| 165 | virtual void RegistrationMismatchMitigated(bool mitigation_succeeded) {} |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | static void SetObserverForTest(TestObserver* observer); |
| 169 | |
Istiaque Ahmed | 43949bf7 | 2020-03-20 04:42:08 | [diff] [blame] | 170 | size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id); |
| 171 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 172 | private: |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 173 | using SequencedContextId = std::pair<LazyContextId, ActivationSequence>; |
| 174 | |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 175 | class WorkerState; |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 176 | |
Istiaque Ahmed | 5eef463 | 2022-04-11 20:06:39 | [diff] [blame] | 177 | enum class RegistrationReason { |
| 178 | REGISTER_ON_EXTENSION_LOAD, |
| 179 | RE_REGISTER_ON_STATE_MISMATCH, |
| 180 | }; |
| 181 | |
| 182 | void RegisterServiceWorker(RegistrationReason reason, |
| 183 | const SequencedContextId& context_id, |
| 184 | const Extension& extension); |
| 185 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 186 | void RunTasksAfterStartWorker(const SequencedContextId& context_id); |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 187 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 188 | void DidRegisterServiceWorker(const SequencedContextId& context_id, |
Istiaque Ahmed | 5eef463 | 2022-04-11 20:06:39 | [diff] [blame] | 189 | RegistrationReason reason, |
Istiaque Ahmed | 78da8dc | 2020-09-30 21:39:37 | [diff] [blame] | 190 | base::Time start_time, |
Ghazale Hosseinabadi | 43f92c7 | 2021-02-03 20:23:21 | [diff] [blame] | 191 | blink::ServiceWorkerStatusCode status); |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 192 | void DidUnregisterServiceWorker(const ExtensionId& extension_id, |
Istiaque Ahmed | 4cff0ce | 2020-06-25 23:44:43 | [diff] [blame] | 193 | ActivationSequence sequence, |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 194 | bool success); |
| 195 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 196 | void DidStartWorkerForScope(const SequencedContextId& context_id, |
Istiaque Ahmed | 78da8dc | 2020-09-30 21:39:37 | [diff] [blame] | 197 | base::Time start_time, |
Zhuoyu Qian | 2266251f | 2018-10-13 02:59:00 | [diff] [blame] | 198 | int64_t version_id, |
| 199 | int process_id, |
| 200 | int thread_id); |
Ghazale Hosseinabadi | 77dcc35 | 2020-12-21 09:18:22 | [diff] [blame] | 201 | void DidStartWorkerFail(const SequencedContextId& context_id, |
| 202 | blink::ServiceWorkerStatusCode status_code); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 203 | |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 204 | // The following three methods retrieve, store, and remove information |
| 205 | // about Service Worker registration of SW based background pages: |
| 206 | // |
| 207 | // Retrieves the last version of the extension, returns invalid version if |
| 208 | // there isn't any such extension. |
| 209 | base::Version RetrieveRegisteredServiceWorkerVersion( |
| 210 | const ExtensionId& extension_id); |
| 211 | // Records that the extension with |extension_id| and |version| successfully |
| 212 | // registered a Service Worker. |
| 213 | void SetRegisteredServiceWorkerInfo(const ExtensionId& extension_id, |
| 214 | const base::Version& version); |
| 215 | // Clears any record of registered Service Worker for the given extension with |
| 216 | // |extension_id|. |
| 217 | void RemoveRegisteredServiceWorkerInfo(const ExtensionId& extension_id); |
| 218 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 219 | // If the worker with |context_id| has seen worker start |
| 220 | // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs |
| 221 | // all pending tasks for that worker. |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 222 | void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id); |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 223 | |
| 224 | // Returns true if |sequence| is the current activation sequence for |
| 225 | // |extension_id|. |
| 226 | bool IsCurrentSequence(const ExtensionId& extension_id, |
| 227 | ActivationSequence sequence) const; |
| 228 | |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 229 | WorkerState* GetWorkerState(const SequencedContextId& context_id); |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 230 | |
Ghazale Hosseinabadi | 1ccaf8f | 2021-06-22 23:12:29 | [diff] [blame] | 231 | content::ServiceWorkerContext* GetServiceWorkerContext( |
| 232 | const ExtensionId& extension_id); |
| 233 | |
| 234 | // Starts and stops observing |service_worker_context|. |
| 235 | // |
| 236 | // The methods ensure that many:1 relationship of SWContext:SWContextObserver |
| 237 | // is preserved correctly. |
| 238 | void StartObserving(content::ServiceWorkerContext* service_worker_context); |
| 239 | void StopObserving(content::ServiceWorkerContext* service_worker_context); |
| 240 | |
Istiaque Ahmed | 5eef463 | 2022-04-11 20:06:39 | [diff] [blame] | 241 | // Asynchronously verifies whether an expected SW registration (denoted by |
| 242 | // |scope|) is there. |
| 243 | void VerifyRegistration(content::ServiceWorkerContext* service_worker_context, |
| 244 | const SequencedContextId& context_id, |
| 245 | const GURL& scope); |
| 246 | void DidVerifyRegistration(const SequencedContextId& context_id, |
| 247 | content::ServiceWorkerCapability capability); |
| 248 | |
Istiaque Ahmed | 9987ca89 | 2020-01-09 22:47:17 | [diff] [blame] | 249 | int next_activation_sequence_ = 0; |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 250 | |
Ghazale Hosseinabadi | 5ca84a7b | 2021-06-14 11:08:59 | [diff] [blame] | 251 | std::multiset<content::ServiceWorkerContext*> observing_worker_contexts_; |
| 252 | |
Istiaque Ahmed | c92b1ea | 2019-12-31 00:32:49 | [diff] [blame] | 253 | // The state of worker of each activated extension. |
| 254 | std::map<SequencedContextId, WorkerState> worker_state_map_; |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 255 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 256 | const raw_ptr<content::BrowserContext> browser_context_ = nullptr; |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 257 | |
David Bertoni | ac01b94 | 2019-08-26 23:48:17 | [diff] [blame] | 258 | // A map of Service Worker registrations if this instance is for an |
| 259 | // off-the-record BrowserContext. These are stored in the ExtensionPrefs |
| 260 | // for a regular profile. |
| 261 | // TODO(crbug.com/939664): Make this better by passing in something that |
| 262 | // will manage storing and retrieving this data. |
| 263 | std::unordered_map<ExtensionId, base::Version> off_the_record_registrations_; |
| 264 | |
Istiaque Ahmed | 92ad7fc | 2019-11-18 19:02:26 | [diff] [blame] | 265 | // Current ActivationSequence for each activated extensions. |
| 266 | std::map<ExtensionId, ActivationSequence> activation_sequences_; |
| 267 | |
Jeremy Roman | 9fc2de6 | 2019-07-12 14:15:03 | [diff] [blame] | 268 | base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this}; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | } // namespace extensions |
| 272 | |
| 273 | #endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_ |