blob: bd196f40348bc8c08603eb7bf3e426becf3f1142 [file] [log] [blame]
lazyboy63b994a2017-06-30 21:20:231// 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 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
13#include "base/memory/weak_ptr.h"
Hans Wennborgb3e433a2020-04-21 11:21:4014#include "base/strings/string_util.h"
Istiaque Ahmed70f76ac2018-11-02 02:59:5515#include "base/version.h"
lazyboy63b994a2017-06-30 21:20:2316#include "components/keyed_service/core/keyed_service.h"
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:5917#include "content/public/browser/service_worker_context_observer.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2618#include "extensions/browser/lazy_context_id.h"
lazyboy63b994a2017-06-30 21:20:2319#include "extensions/browser/lazy_context_task_queue.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2620#include "extensions/browser/service_worker/worker_id.h"
Istiaque Ahmed9987ca892020-01-09 22:47:1721#include "extensions/common/activation_sequence.h"
lazyboy63b994a2017-06-30 21:20:2322#include "extensions/common/extension_id.h"
Lei Zhang698df03c2021-05-21 04:23:3423#include "third_party/abseil-cpp/absl/types/optional.h"
Ghazale Hosseinabadi77dcc352020-12-21 09:18:2224#include "third_party/blink/public/common/service_worker/service_worker_status_code.h"
lazyboy63b994a2017-06-30 21:20:2325#include "url/gurl.h"
26
27namespace content {
28class BrowserContext;
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:5929class ServiceWorkerContext;
lazyboy63b994a2017-06-30 21:20:2330}
31
32namespace extensions {
33class Extension;
lazyboy63b994a2017-06-30 21:20:2334
Istiaque Ahmed92ad7fc2019-11-18 19:02:2635// 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//
lazyboy63b994a2017-06-30 21:20:2376// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
77class ServiceWorkerTaskQueue : public KeyedService,
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:5978 public LazyContextTaskQueue,
79 public content::ServiceWorkerContextObserver {
lazyboy63b994a2017-06-30 21:20:2380 public:
81 explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context);
Peter Boström951cf77e2021-09-22 00:02:5982
83 ServiceWorkerTaskQueue(const ServiceWorkerTaskQueue&) = delete;
84 ServiceWorkerTaskQueue& operator=(const ServiceWorkerTaskQueue&) = delete;
85
lazyboy63b994a2017-06-30 21:20:2386 ~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 Bertoni8269a092018-12-19 15:55:4294 void AddPendingTask(const LazyContextId& context_id,
95 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2396
Istiaque Ahmedccb444022018-06-19 02:11:1297 // 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 Ahmedd4b67ee2019-03-02 10:53:20104 // 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 Ahmedb8e24bdb2018-09-13 15:17:25110 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57111 // 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 Ahmed9987ca892020-01-09 22:47:17115 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57116 const GURL& service_worker_scope,
117 int64_t service_worker_version_id,
118 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25119 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57120 void DidStopServiceWorkerContext(int render_process_id,
121 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17122 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57123 const GURL& service_worker_scope,
124 int64_t service_worker_version_id,
125 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25126
Istiaque Ahmedc92b1ea2019-12-31 00:32:49127 // Returns the current ActivationSequence for an extension, if the extension
Anton Bikineev6d678472021-05-15 18:48:51128 // is currently activated. Returns absl::nullopt if the extension isn't
Istiaque Ahmedc92b1ea2019-12-31 00:32:49129 // activated.
Anton Bikineev6d678472021-05-15 18:48:51130 absl::optional<ActivationSequence> GetCurrentSequence(
Istiaque Ahmedc92b1ea2019-12-31 00:32:49131 const ExtensionId& extension_id) const;
132
Istiaque Ahmed6e5a0832021-08-26 23:41:14133 // Activates incognito split mode extensions that are activated in |other|
134 // task queue.
135 void ActivateIncognitoSplitModeExtensions(ServiceWorkerTaskQueue* other);
136
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59137 // 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 Ahmed70f76ac2018-11-02 02:59:55143 class TestObserver {
144 public:
David Bertoni77615d22019-05-29 23:10:13145 TestObserver();
Peter Boström951cf77e2021-09-22 00:02:59146
147 TestObserver(const TestObserver&) = delete;
148 TestObserver& operator=(const TestObserver&) = delete;
149
David Bertoni77615d22019-05-29 23:10:13150 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:55151
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 Ahmed43949bf72020-03-20 04:42:08156 bool will_register_service_worker) {}
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22157 virtual void DidStartWorkerFail(
158 const ExtensionId& extension_id,
159 size_t num_pending_tasks,
160 blink::ServiceWorkerStatusCode status_code) {}
Istiaque Ahmed70f76ac2018-11-02 02:59:55161 };
162
163 static void SetObserverForTest(TestObserver* observer);
164
Istiaque Ahmed43949bf72020-03-20 04:42:08165 size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id);
166
lazyboy63b994a2017-06-30 21:20:23167 private:
Istiaque Ahmed92ad7fc2019-11-18 19:02:26168 using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
169
Istiaque Ahmedc92b1ea2019-12-31 00:32:49170 class WorkerState;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26171
Istiaque Ahmed92ad7fc2019-11-18 19:02:26172 void RunTasksAfterStartWorker(const SequencedContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12173
Istiaque Ahmed92ad7fc2019-11-18 19:02:26174 void DidRegisterServiceWorker(const SequencedContextId& context_id,
Istiaque Ahmed78da8dc2020-09-30 21:39:37175 base::Time start_time,
Ghazale Hosseinabadi43f92c72021-02-03 20:23:21176 blink::ServiceWorkerStatusCode status);
Istiaque Ahmedccb444022018-06-19 02:11:12177 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
Istiaque Ahmed4cff0ce2020-06-25 23:44:43178 ActivationSequence sequence,
Istiaque Ahmedccb444022018-06-19 02:11:12179 bool success);
180
Istiaque Ahmed92ad7fc2019-11-18 19:02:26181 void DidStartWorkerForScope(const SequencedContextId& context_id,
Istiaque Ahmed78da8dc2020-09-30 21:39:37182 base::Time start_time,
Zhuoyu Qian2266251f2018-10-13 02:59:00183 int64_t version_id,
184 int process_id,
185 int thread_id);
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22186 void DidStartWorkerFail(const SequencedContextId& context_id,
187 blink::ServiceWorkerStatusCode status_code);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25188
Istiaque Ahmed70f76ac2018-11-02 02:59:55189 // 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 Ahmed4b70a70d2019-02-28 01:36:57204 // 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 Ahmedc92b1ea2019-12-31 00:32:49207 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26208
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 Ahmedc92b1ea2019-12-31 00:32:49214 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26215
Ghazale Hosseinabadi1ccaf8f2021-06-22 23:12:29216 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 Ahmed9987ca892020-01-09 22:47:17226 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26227
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59228 std::multiset<content::ServiceWorkerContext*> observing_worker_contexts_;
229
Istiaque Ahmedc92b1ea2019-12-31 00:32:49230 // The state of worker of each activated extension.
231 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25232
Istiaque Ahmedccb444022018-06-19 02:11:12233 content::BrowserContext* const browser_context_ = nullptr;
234
David Bertoniac01b942019-08-26 23:48:17235 // 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 Ahmed92ad7fc2019-11-18 19:02:26242 // Current ActivationSequence for each activated extensions.
243 std::map<ExtensionId, ActivationSequence> activation_sequences_;
244
Jeremy Roman9fc2de62019-07-12 14:15:03245 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
lazyboy63b994a2017-06-30 21:20:23246};
247
248} // namespace extensions
249
250#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_