blob: 2245d7b115dab71603a6f6de14aba58f8d6600dd [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,
170 bool success);
171
Istiaque Ahmed92ad7fc2019-11-18 19:02:26172 void DidStartWorkerForScope(const SequencedContextId& context_id,
Zhuoyu Qian2266251f2018-10-13 02:59:00173 int64_t version_id,
174 int process_id,
175 int thread_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26176 void DidStartWorkerFail(const SequencedContextId& context_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25177
Istiaque Ahmed70f76ac2018-11-02 02:59:55178 // 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 Ahmed4b70a70d2019-02-28 01:36:57193 // 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 Ahmedc92b1ea2019-12-31 00:32:49196 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26197
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 Ahmedc92b1ea2019-12-31 00:32:49203 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26204
Istiaque Ahmed9987ca892020-01-09 22:47:17205 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26206
Istiaque Ahmedc92b1ea2019-12-31 00:32:49207 // The state of worker of each activated extension.
208 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25209
Istiaque Ahmedccb444022018-06-19 02:11:12210 content::BrowserContext* const browser_context_ = nullptr;
211
David Bertoniac01b942019-08-26 23:48:17212 // 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 Ahmed92ad7fc2019-11-18 19:02:26219 // Current ActivationSequence for each activated extensions.
220 std::map<ExtensionId, ActivationSequence> activation_sequences_;
221
Jeremy Roman9fc2de62019-07-12 14:15:03222 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
Istiaque Ahmedccb444022018-06-19 02:11:12223
lazyboy63b994a2017-06-30 21:20:23224 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
225};
226
227} // namespace extensions
228
229#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_