blob: 0f2dd33a39cecf4edf9c50c96631b503afed3494 [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);
82 ~ServiceWorkerTaskQueue() override;
83
84 // Convenience method to return the ServiceWorkerTaskQueue for a given
85 // |context|.
86 static ServiceWorkerTaskQueue* Get(content::BrowserContext* context);
87
88 bool ShouldEnqueueTask(content::BrowserContext* context,
89 const Extension* extension) override;
David Bertoni8269a092018-12-19 15:55:4290 void AddPendingTask(const LazyContextId& context_id,
91 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2392
Istiaque Ahmedccb444022018-06-19 02:11:1293 // Performs Service Worker related tasks upon |extension| activation,
94 // e.g. registering |extension|'s worker, executing any pending tasks.
95 void ActivateExtension(const Extension* extension);
96 // Performs Service Worker related tasks upon |extension| deactivation,
97 // e.g. unregistering |extension|'s worker.
98 void DeactivateExtension(const Extension* extension);
99
Istiaque Ahmedd4b67ee2019-03-02 10:53:20100 // Called once an extension Service Worker context was initialized but not
101 // necessarily started executing its JavaScript.
102 void DidInitializeServiceWorkerContext(int render_process_id,
103 const ExtensionId& extension_id,
104 int64_t service_worker_version_id,
105 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25106 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57107 // This can be thought as "loadstop", i.e. the global JS script of the worker
108 // has completed executing.
109 void DidStartServiceWorkerContext(int render_process_id,
110 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17111 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57112 const GURL& service_worker_scope,
113 int64_t service_worker_version_id,
114 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25115 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57116 void DidStopServiceWorkerContext(int render_process_id,
117 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17118 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57119 const GURL& service_worker_scope,
120 int64_t service_worker_version_id,
121 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25122
Istiaque Ahmedc92b1ea2019-12-31 00:32:49123 // Returns the current ActivationSequence for an extension, if the extension
Anton Bikineev6d678472021-05-15 18:48:51124 // is currently activated. Returns absl::nullopt if the extension isn't
Istiaque Ahmedc92b1ea2019-12-31 00:32:49125 // activated.
Anton Bikineev6d678472021-05-15 18:48:51126 absl::optional<ActivationSequence> GetCurrentSequence(
Istiaque Ahmedc92b1ea2019-12-31 00:32:49127 const ExtensionId& extension_id) const;
128
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59129 // content::ServiceWorkerContextObserver:
130 void OnReportConsoleMessage(int64_t version_id,
131 const GURL& scope,
132 const content::ConsoleMessage& message) override;
133 void OnDestruct(content::ServiceWorkerContext* context) override;
134
Istiaque Ahmed70f76ac2018-11-02 02:59:55135 class TestObserver {
136 public:
David Bertoni77615d22019-05-29 23:10:13137 TestObserver();
138 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:55139
140 // Called when an extension with id |extension_id| is going to be activated.
141 // |will_register_service_worker| is true if a Service Worker will be
142 // registered.
143 virtual void OnActivateExtension(const ExtensionId& extension_id,
Istiaque Ahmed43949bf72020-03-20 04:42:08144 bool will_register_service_worker) {}
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22145 virtual void DidStartWorkerFail(
146 const ExtensionId& extension_id,
147 size_t num_pending_tasks,
148 blink::ServiceWorkerStatusCode status_code) {}
Istiaque Ahmed70f76ac2018-11-02 02:59:55149
150 private:
151 DISALLOW_COPY_AND_ASSIGN(TestObserver);
152 };
153
154 static void SetObserverForTest(TestObserver* observer);
155
Istiaque Ahmed43949bf72020-03-20 04:42:08156 size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id);
157
lazyboy63b994a2017-06-30 21:20:23158 private:
Istiaque Ahmed92ad7fc2019-11-18 19:02:26159 using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
160
Istiaque Ahmedc92b1ea2019-12-31 00:32:49161 class WorkerState;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26162
Istiaque Ahmed92ad7fc2019-11-18 19:02:26163 void RunTasksAfterStartWorker(const SequencedContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12164
Istiaque Ahmed92ad7fc2019-11-18 19:02:26165 void DidRegisterServiceWorker(const SequencedContextId& context_id,
Istiaque Ahmed78da8dc2020-09-30 21:39:37166 base::Time start_time,
Ghazale Hosseinabadi43f92c72021-02-03 20:23:21167 blink::ServiceWorkerStatusCode status);
Istiaque Ahmedccb444022018-06-19 02:11:12168 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
Istiaque Ahmed4cff0ce2020-06-25 23:44:43169 ActivationSequence sequence,
Istiaque Ahmedccb444022018-06-19 02:11:12170 bool success);
171
Istiaque Ahmed92ad7fc2019-11-18 19:02:26172 void DidStartWorkerForScope(const SequencedContextId& context_id,
Istiaque Ahmed78da8dc2020-09-30 21:39:37173 base::Time start_time,
Zhuoyu Qian2266251f2018-10-13 02:59:00174 int64_t version_id,
175 int process_id,
176 int thread_id);
Ghazale Hosseinabadi77dcc352020-12-21 09:18:22177 void DidStartWorkerFail(const SequencedContextId& context_id,
178 blink::ServiceWorkerStatusCode status_code);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25179
Istiaque Ahmed70f76ac2018-11-02 02:59:55180 // The following three methods retrieve, store, and remove information
181 // about Service Worker registration of SW based background pages:
182 //
183 // Retrieves the last version of the extension, returns invalid version if
184 // there isn't any such extension.
185 base::Version RetrieveRegisteredServiceWorkerVersion(
186 const ExtensionId& extension_id);
187 // Records that the extension with |extension_id| and |version| successfully
188 // registered a Service Worker.
189 void SetRegisteredServiceWorkerInfo(const ExtensionId& extension_id,
190 const base::Version& version);
191 // Clears any record of registered Service Worker for the given extension with
192 // |extension_id|.
193 void RemoveRegisteredServiceWorkerInfo(const ExtensionId& extension_id);
194
Istiaque Ahmed4b70a70d2019-02-28 01:36:57195 // If the worker with |context_id| has seen worker start
196 // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs
197 // all pending tasks for that worker.
Istiaque Ahmedc92b1ea2019-12-31 00:32:49198 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26199
200 // Returns true if |sequence| is the current activation sequence for
201 // |extension_id|.
202 bool IsCurrentSequence(const ExtensionId& extension_id,
203 ActivationSequence sequence) const;
204
Istiaque Ahmedc92b1ea2019-12-31 00:32:49205 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26206
Istiaque Ahmed9987ca892020-01-09 22:47:17207 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26208
Ghazale Hosseinabadi5ca84a7b2021-06-14 11:08:59209 std::multiset<content::ServiceWorkerContext*> observing_worker_contexts_;
210
Istiaque Ahmedc92b1ea2019-12-31 00:32:49211 // The state of worker of each activated extension.
212 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25213
Istiaque Ahmedccb444022018-06-19 02:11:12214 content::BrowserContext* const browser_context_ = nullptr;
215
David Bertoniac01b942019-08-26 23:48:17216 // A map of Service Worker registrations if this instance is for an
217 // off-the-record BrowserContext. These are stored in the ExtensionPrefs
218 // for a regular profile.
219 // TODO(crbug.com/939664): Make this better by passing in something that
220 // will manage storing and retrieving this data.
221 std::unordered_map<ExtensionId, base::Version> off_the_record_registrations_;
222
Istiaque Ahmed92ad7fc2019-11-18 19:02:26223 // Current ActivationSequence for each activated extensions.
224 std::map<ExtensionId, ActivationSequence> activation_sequences_;
225
Jeremy Roman9fc2de62019-07-12 14:15:03226 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
Istiaque Ahmedccb444022018-06-19 02:11:12227
lazyboy63b994a2017-06-30 21:20:23228 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
229};
230
231} // namespace extensions
232
233#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_