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