blob: 2b4147ecd6d8d61ea2dbe220eb3a3b92cef72c75 [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"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2617#include "extensions/browser/lazy_context_id.h"
lazyboy63b994a2017-06-30 21:20:2318#include "extensions/browser/lazy_context_task_queue.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2619#include "extensions/browser/service_worker/worker_id.h"
Istiaque Ahmed9987ca892020-01-09 22:47:1720#include "extensions/common/activation_sequence.h"
lazyboy63b994a2017-06-30 21:20:2321#include "extensions/common/extension_id.h"
22#include "url/gurl.h"
23
24namespace content {
25class BrowserContext;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2526class ServiceWorkerContext;
lazyboy63b994a2017-06-30 21:20:2327}
28
29namespace extensions {
30class Extension;
lazyboy63b994a2017-06-30 21:20:2331
Istiaque Ahmed92ad7fc2019-11-18 19:02:2632// 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//
lazyboy63b994a2017-06-30 21:20:2373// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
74class 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 Bertoni8269a092018-12-19 15:55:4286 void AddPendingTask(const LazyContextId& context_id,
87 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2388
Istiaque Ahmedccb444022018-06-19 02:11:1289 // 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 Ahmedd4b67ee2019-03-02 10:53:2096 // 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 Ahmedb8e24bdb2018-09-13 15:17:25102 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57103 // 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 Ahmed9987ca892020-01-09 22:47:17107 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57108 const GURL& service_worker_scope,
109 int64_t service_worker_version_id,
110 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25111 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57112 void DidStopServiceWorkerContext(int render_process_id,
113 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17114 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57115 const GURL& service_worker_scope,
116 int64_t service_worker_version_id,
117 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25118
Istiaque Ahmedc92b1ea2019-12-31 00:32:49119 // 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 Ahmed70f76ac2018-11-02 02:59:55125 class TestObserver {
126 public:
David Bertoni77615d22019-05-29 23:10:13127 TestObserver();
128 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:55129
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 Ahmed43949bf72020-03-20 04:42:08134 bool will_register_service_worker) {}
135 virtual void DidStartWorkerFail(const ExtensionId& extension_id,
136 size_t num_pending_tasks) {}
Istiaque Ahmed70f76ac2018-11-02 02:59:55137
138 private:
139 DISALLOW_COPY_AND_ASSIGN(TestObserver);
140 };
141
142 static void SetObserverForTest(TestObserver* observer);
143
Istiaque Ahmed43949bf72020-03-20 04:42:08144 size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id);
145
lazyboy63b994a2017-06-30 21:20:23146 private:
Istiaque Ahmed92ad7fc2019-11-18 19:02:26147 using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
148
Istiaque Ahmedc92b1ea2019-12-31 00:32:49149 class WorkerState;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26150
Matt Falkenhagend55b9282019-09-10 23:53:35151 static void DidStartWorkerForScopeOnCoreThread(
Istiaque Ahmed92ad7fc2019-11-18 19:02:26152 const SequencedContextId& context_id,
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25153 base::WeakPtr<ServiceWorkerTaskQueue> task_queue,
154 int64_t version_id,
155 int process_id,
156 int thread_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26157 static void DidStartWorkerFailOnCoreThread(
158 const SequencedContextId& context_id,
159 base::WeakPtr<ServiceWorkerTaskQueue> task_queue);
Matt Falkenhagend55b9282019-09-10 23:53:35160 static void StartServiceWorkerOnCoreThreadToRunTasks(
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25161 base::WeakPtr<ServiceWorkerTaskQueue> task_queue_weak,
Istiaque Ahmed92ad7fc2019-11-18 19:02:26162 const SequencedContextId& context_id,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57163 content::ServiceWorkerContext* service_worker_context);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25164
Istiaque Ahmed92ad7fc2019-11-18 19:02:26165 void RunTasksAfterStartWorker(const SequencedContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12166
Istiaque Ahmed92ad7fc2019-11-18 19:02:26167 void DidRegisterServiceWorker(const SequencedContextId& context_id,
168 bool success);
Istiaque Ahmedccb444022018-06-19 02:11:12169 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
Istiaque Ahmed4cff0ce2020-06-25 23:44:43170 ActivationSequence sequence,
Istiaque Ahmedccb444022018-06-19 02:11:12171 bool success);
172
Istiaque Ahmed92ad7fc2019-11-18 19:02:26173 void DidStartWorkerForScope(const SequencedContextId& context_id,
Zhuoyu Qian2266251f2018-10-13 02:59:00174 int64_t version_id,
175 int process_id,
176 int thread_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26177 void DidStartWorkerFail(const SequencedContextId& context_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25178
Istiaque Ahmed70f76ac2018-11-02 02:59:55179 // The following three methods retrieve, store, and remove information
180 // about Service Worker registration of SW based background pages:
181 //
182 // Retrieves the last version of the extension, returns invalid version if
183 // there isn't any such extension.
184 base::Version RetrieveRegisteredServiceWorkerVersion(
185 const ExtensionId& extension_id);
186 // Records that the extension with |extension_id| and |version| successfully
187 // registered a Service Worker.
188 void SetRegisteredServiceWorkerInfo(const ExtensionId& extension_id,
189 const base::Version& version);
190 // Clears any record of registered Service Worker for the given extension with
191 // |extension_id|.
192 void RemoveRegisteredServiceWorkerInfo(const ExtensionId& extension_id);
193
Istiaque Ahmed4b70a70d2019-02-28 01:36:57194 // If the worker with |context_id| has seen worker start
195 // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs
196 // all pending tasks for that worker.
Istiaque Ahmedc92b1ea2019-12-31 00:32:49197 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26198
199 // Returns true if |sequence| is the current activation sequence for
200 // |extension_id|.
201 bool IsCurrentSequence(const ExtensionId& extension_id,
202 ActivationSequence sequence) const;
203
Istiaque Ahmedc92b1ea2019-12-31 00:32:49204 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26205
Istiaque Ahmed9987ca892020-01-09 22:47:17206 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26207
Istiaque Ahmedc92b1ea2019-12-31 00:32:49208 // The state of worker of each activated extension.
209 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25210
Istiaque Ahmedccb444022018-06-19 02:11:12211 content::BrowserContext* const browser_context_ = nullptr;
212
David Bertoniac01b942019-08-26 23:48:17213 // A map of Service Worker registrations if this instance is for an
214 // off-the-record BrowserContext. These are stored in the ExtensionPrefs
215 // for a regular profile.
216 // TODO(crbug.com/939664): Make this better by passing in something that
217 // will manage storing and retrieving this data.
218 std::unordered_map<ExtensionId, base::Version> off_the_record_registrations_;
219
Istiaque Ahmed92ad7fc2019-11-18 19:02:26220 // Current ActivationSequence for each activated extensions.
221 std::map<ExtensionId, ActivationSequence> activation_sequences_;
222
Jeremy Roman9fc2de62019-07-12 14:15:03223 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
Istiaque Ahmedccb444022018-06-19 02:11:12224
lazyboy63b994a2017-06-30 21:20:23225 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
226};
227
228} // namespace extensions
229
230#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_