blob: 446fbfb8e4d7a7ea5997120b17f73e923c27c362 [file] [log] [blame]
Avi Drissman60039d42022-09-13 21:49:051// Copyright 2017 The Chromium Authors
lazyboy63b994a2017-06-30 21:20:232// 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 Ahmedccb444022018-06-19 02:11:128#include <map>
9#include <set>
David Bertoniac01b942019-08-26 23:48:1710#include <unordered_map>
Istiaque Ahmed4b70a70d2019-02-28 01:36:5711#include <vector>
Istiaque Ahmedccb444022018-06-19 02:11:1212
Keishi Hattori0e45c022021-11-27 09:25:5213#include "base/memory/raw_ptr.h"
Istiaque Ahmedccb444022018-06-19 02:11:1214#include "base/memory/weak_ptr.h"
Hans Wennborgb3e433a2020-04-21 11:21:4015#include "base/strings/string_util.h"
Istiaque Ahmed70f76ac2018-11-02 02:59:5516#include "base/version.h"
lazyboy63b994a2017-06-30 21:20:2317#include "components/keyed_service/core/keyed_service.h"
Istiaque Ahmed5eef4632022-04-11 20:06:3918#include "content/public/browser/service_worker_context.h"
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:5919#include "content/public/browser/service_worker_context_observer.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2620#include "extensions/browser/lazy_context_id.h"
lazyboy63b994a2017-06-30 21:20:2321#include "extensions/browser/lazy_context_task_queue.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2622#include "extensions/browser/service_worker/worker_id.h"
Istiaque Ahmed9987ca892020-01-09 22:47:1723#include "extensions/common/activation_sequence.h"
lazyboy63b994a2017-06-30 21:20:2324#include "extensions/common/extension_id.h"
Lei Zhang698df03c2021-05-21 04:23:3425#include "third_party/abseil-cpp/absl/types/optional.h"
Ghazale Hosseinabadi77dcc352020-12-21 09:18:2226#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
lazyboy63b994a2017-06-30 21:20:2327#include "url/gurl.h"
28
29namespace content {
30class BrowserContext;
31}
32
33namespace extensions {
34class Extension;
lazyboy63b994a2017-06-30 21:20:2335
Istiaque Ahmed92ad7fc2019-11-18 19:02:2636// 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//
lazyboy63b994a2017-06-30 21:20:2377// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
78class ServiceWorkerTaskQueue : public KeyedService,
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:5979 public LazyContextTaskQueue,
80 public content::ServiceWorkerContextObserver {
lazyboy63b994a2017-06-30 21:20:2381 public:
82 explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context);
Peter Boström951cf77e2021-09-22 00:02:5983
84 ServiceWorkerTaskQueue(const ServiceWorkerTaskQueue&) = delete;
85 ServiceWorkerTaskQueue& operator=(const ServiceWorkerTaskQueue&) = delete;
86
lazyboy63b994a2017-06-30 21:20:2387 ~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 Bertoni8269a092018-12-19 15:55:4295 void AddPendingTask(const LazyContextId& context_id,
96 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2397
Istiaque Ahmedccb444022018-06-19 02:11:1298 // 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 Ahmedd4b67ee2019-03-02 10:53:20105 // 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 Ahmedb8e24bdb2018-09-13 15:17:25111 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57112 // 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 Ahmed9987ca892020-01-09 22:47:17116 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57117 const GURL& service_worker_scope,
118 int64_t service_worker_version_id,
119 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25120 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57121 void DidStopServiceWorkerContext(int render_process_id,
122 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17123 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57124 const GURL& service_worker_scope,
125 int64_t service_worker_version_id,
126 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25127
Istiaque Ahmedc92b1ea2019-12-31 00:32:49128 // Returns the current ActivationSequence for an extension, if the extension
Anton Bikineev6d678472021-05-15 18:48:51129 // is currently activated. Returns absl::nullopt if the extension isn't
Istiaque Ahmedc92b1ea2019-12-31 00:32:49130 // activated.
Anton Bikineev6d678472021-05-15 18:48:51131 absl::optional<ActivationSequence> GetCurrentSequence(
Istiaque Ahmedc92b1ea2019-12-31 00:32:49132 const ExtensionId& extension_id) const;
133
Istiaque Ahmed6e5a0832021-08-26 23:41:14134 // Activates incognito split mode extensions that are activated in |other|
135 // task queue.
136 void ActivateIncognitoSplitModeExtensions(ServiceWorkerTaskQueue* other);
137
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59138 // 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 Ahmed70f76ac2018-11-02 02:59:55144 class TestObserver {
145 public:
David Bertoni77615d22019-05-29 23:10:13146 TestObserver();
Peter Boström951cf77e2021-09-22 00:02:59147
148 TestObserver(const TestObserver&) = delete;
149 TestObserver& operator=(const TestObserver&) = delete;
150
David Bertoni77615d22019-05-29 23:10:13151 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:55152
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 Ahmed43949bf72020-03-20 04:42:08157 bool will_register_service_worker) {}
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22158 virtual void DidStartWorkerFail(
159 const ExtensionId& extension_id,
160 size_t num_pending_tasks,
161 blink::ServiceWorkerStatusCode status_code) {}
Istiaque Ahmed5eef4632022-04-11 20:06:39162
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 Ahmed70f76ac2018-11-02 02:59:55166 };
167
168 static void SetObserverForTest(TestObserver* observer);
169
Istiaque Ahmed43949bf72020-03-20 04:42:08170 size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id);
171
lazyboy63b994a2017-06-30 21:20:23172 private:
Istiaque Ahmed92ad7fc2019-11-18 19:02:26173 using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
174
Istiaque Ahmedc92b1ea2019-12-31 00:32:49175 class WorkerState;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26176
Istiaque Ahmed5eef4632022-04-11 20:06:39177 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 Ahmed92ad7fc2019-11-18 19:02:26186 void RunTasksAfterStartWorker(const SequencedContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12187
Istiaque Ahmed92ad7fc2019-11-18 19:02:26188 void DidRegisterServiceWorker(const SequencedContextId& context_id,
Istiaque Ahmed5eef4632022-04-11 20:06:39189 RegistrationReason reason,
Istiaque Ahmed78da8dc2020-09-30 21:39:37190 base::Time start_time,
Ghazale Hosseinabadi43f92c72021-02-03 20:23:21191 blink::ServiceWorkerStatusCode status);
Istiaque Ahmedccb444022018-06-19 02:11:12192 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
Istiaque Ahmed4cff0ce2020-06-25 23:44:43193 ActivationSequence sequence,
Istiaque Ahmedccb444022018-06-19 02:11:12194 bool success);
195
Istiaque Ahmed92ad7fc2019-11-18 19:02:26196 void DidStartWorkerForScope(const SequencedContextId& context_id,
Istiaque Ahmed78da8dc2020-09-30 21:39:37197 base::Time start_time,
Zhuoyu Qian2266251f2018-10-13 02:59:00198 int64_t version_id,
199 int process_id,
200 int thread_id);
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22201 void DidStartWorkerFail(const SequencedContextId& context_id,
202 blink::ServiceWorkerStatusCode status_code);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25203
Istiaque Ahmed70f76ac2018-11-02 02:59:55204 // 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 Ahmed4b70a70d2019-02-28 01:36:57219 // 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 Ahmedc92b1ea2019-12-31 00:32:49222 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26223
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 Ahmedc92b1ea2019-12-31 00:32:49229 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26230
Ghazale Hosseinabadi1ccaf8f2021-06-22 23:12:29231 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 Ahmed5eef4632022-04-11 20:06:39241 // 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 Ahmed9987ca892020-01-09 22:47:17249 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26250
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59251 std::multiset<content::ServiceWorkerContext*> observing_worker_contexts_;
252
Istiaque Ahmedc92b1ea2019-12-31 00:32:49253 // The state of worker of each activated extension.
254 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25255
Keishi Hattori0e45c022021-11-27 09:25:52256 const raw_ptr<content::BrowserContext> browser_context_ = nullptr;
Istiaque Ahmedccb444022018-06-19 02:11:12257
David Bertoniac01b942019-08-26 23:48:17258 // 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 Ahmed92ad7fc2019-11-18 19:02:26265 // Current ActivationSequence for each activated extensions.
266 std::map<ExtensionId, ActivationSequence> activation_sequences_;
267
Jeremy Roman9fc2de62019-07-12 14:15:03268 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
lazyboy63b994a2017-06-30 21:20:23269};
270
271} // namespace extensions
272
273#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_