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