blob: a01d946f995f077b0324d3e51f4072e820119705 [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"
Istiaque Ahmed70f76ac2018-11-02 02:59:5514#include "base/version.h"
lazyboy63b994a2017-06-30 21:20:2315#include "components/keyed_service/core/keyed_service.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2616#include "extensions/browser/lazy_context_id.h"
lazyboy63b994a2017-06-30 21:20:2317#include "extensions/browser/lazy_context_task_queue.h"
Istiaque Ahmed92ad7fc2019-11-18 19:02:2618#include "extensions/browser/service_worker/worker_id.h"
Istiaque Ahmed9987ca892020-01-09 22:47:1719#include "extensions/common/activation_sequence.h"
lazyboy63b994a2017-06-30 21:20:2320#include "extensions/common/extension_id.h"
21#include "url/gurl.h"
22
23namespace content {
24class BrowserContext;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2525class ServiceWorkerContext;
lazyboy63b994a2017-06-30 21:20:2326}
27
28namespace extensions {
29class Extension;
lazyboy63b994a2017-06-30 21:20:2330
Istiaque Ahmed92ad7fc2019-11-18 19:02:2631// A service worker based background specific LazyContextTaskQueue.
32//
33// This class queues up and runs tasks added through AddPendingTask, after
34// registering and starting extension's background Service Worker script if
35// necessary.
36//
37// There are two sets of concepts/events that are important to this class:
38//
39// C1) Registering and starting a background worker:
40// Upon extension activation, this class registers the extension's
41// background worker if necessary. After that, if it has queued up tasks
42// in |pending_tasks_|, then it moves on to starting the worker. Registration
43// and start are initiated from this class. Once started, the worker is
44// considered browser process ready. These workers are stored in
45// |worker_state_map_| with |browser_ready| = false until we run tasks.
46//
47// C2) Listening for worker's state update from the renderer:
48// - Init (DidInitializeServiceWorkerContext) when the worker is initialized,
49// JavaScript starts running after this.
50// - Start (DidStartServiceWorkerContext) when the worker has reached
51// loadstop. The worker is considered ready to run tasks from this task
52// queue. The worker's entry in |worker_state_map_| will carry
53// |renderer_ready| = true.
54// - Stop (DidStopServiceWorkerContext) when the worker is destroyed, we clear
55// its |renderer_ready| status from |worker_state_map_|.
56//
57// Once a worker reaches readiness in both browser process
58// (DidStartWorkerForScope) and worker process (DidStartServiceWorkerContext),
59// we consider the worker to be ready to run tasks from |pending_tasks_|.
60// Note that events from #C1 and #C2 are somewhat independent, e.g. it is
61// possible to see an Init state update from #C2 before #C1 has seen a start
62// worker completion.
63//
64// Sequences of extension activation:
65// This class also assigns a unique sequence id to an extension activation so
66// that it can differentiate between two activations of a particular extension
67// (e.g. reloading an extension can cause two activations). |pending_tasks_|,
68// worker registration and start (#C1) have sequence ids attached to them.
69// The sequence is expired upon extension deactivation, and tasks are dropped
70// from |pending_tasks_|.
71//
lazyboy63b994a2017-06-30 21:20:2372// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
lazyboy63b994a2017-06-30 21:20:2373class ServiceWorkerTaskQueue : public KeyedService,
74 public LazyContextTaskQueue {
75 public:
76 explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context);
77 ~ServiceWorkerTaskQueue() override;
78
79 // Convenience method to return the ServiceWorkerTaskQueue for a given
80 // |context|.
81 static ServiceWorkerTaskQueue* Get(content::BrowserContext* context);
82
83 bool ShouldEnqueueTask(content::BrowserContext* context,
84 const Extension* extension) override;
David Bertoni8269a092018-12-19 15:55:4285 void AddPendingTask(const LazyContextId& context_id,
86 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2387
Istiaque Ahmedccb444022018-06-19 02:11:1288 // Performs Service Worker related tasks upon |extension| activation,
89 // e.g. registering |extension|'s worker, executing any pending tasks.
90 void ActivateExtension(const Extension* extension);
91 // Performs Service Worker related tasks upon |extension| deactivation,
92 // e.g. unregistering |extension|'s worker.
93 void DeactivateExtension(const Extension* extension);
94
Istiaque Ahmedd4b67ee2019-03-02 10:53:2095 // Called once an extension Service Worker context was initialized but not
96 // necessarily started executing its JavaScript.
97 void DidInitializeServiceWorkerContext(int render_process_id,
98 const ExtensionId& extension_id,
99 int64_t service_worker_version_id,
100 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25101 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57102 // This can be thought as "loadstop", i.e. the global JS script of the worker
103 // has completed executing.
104 void DidStartServiceWorkerContext(int render_process_id,
105 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17106 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57107 const GURL& service_worker_scope,
108 int64_t service_worker_version_id,
109 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25110 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:57111 void DidStopServiceWorkerContext(int render_process_id,
112 const ExtensionId& extension_id,
Istiaque Ahmed9987ca892020-01-09 22:47:17113 ActivationSequence activation_sequence,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57114 const GURL& service_worker_scope,
115 int64_t service_worker_version_id,
116 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25117
Istiaque Ahmedc92b1ea2019-12-31 00:32:49118 // Returns the current ActivationSequence for an extension, if the extension
119 // is currently activated. Returns base::nullopt if the extension isn't
120 // activated.
121 base::Optional<ActivationSequence> GetCurrentSequence(
122 const ExtensionId& extension_id) const;
123
Istiaque Ahmed70f76ac2018-11-02 02:59:55124 class TestObserver {
125 public:
David Bertoni77615d22019-05-29 23:10:13126 TestObserver();
127 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:55128
129 // Called when an extension with id |extension_id| is going to be activated.
130 // |will_register_service_worker| is true if a Service Worker will be
131 // registered.
132 virtual void OnActivateExtension(const ExtensionId& extension_id,
Istiaque Ahmed43949bf72020-03-20 04:42:08133 bool will_register_service_worker) {}
134 virtual void DidStartWorkerFail(const ExtensionId& extension_id,
135 size_t num_pending_tasks) {}
Istiaque Ahmed70f76ac2018-11-02 02:59:55136
137 private:
138 DISALLOW_COPY_AND_ASSIGN(TestObserver);
139 };
140
141 static void SetObserverForTest(TestObserver* observer);
142
Istiaque Ahmed43949bf72020-03-20 04:42:08143 size_t GetNumPendingTasksForTest(const LazyContextId& lazy_context_id);
144
lazyboy63b994a2017-06-30 21:20:23145 private:
Istiaque Ahmed92ad7fc2019-11-18 19:02:26146 using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
147
Istiaque Ahmedc92b1ea2019-12-31 00:32:49148 class WorkerState;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26149
Matt Falkenhagend55b9282019-09-10 23:53:35150 static void DidStartWorkerForScopeOnCoreThread(
Istiaque Ahmed92ad7fc2019-11-18 19:02:26151 const SequencedContextId& context_id,
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25152 base::WeakPtr<ServiceWorkerTaskQueue> task_queue,
153 int64_t version_id,
154 int process_id,
155 int thread_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26156 static void DidStartWorkerFailOnCoreThread(
157 const SequencedContextId& context_id,
158 base::WeakPtr<ServiceWorkerTaskQueue> task_queue);
Matt Falkenhagend55b9282019-09-10 23:53:35159 static void StartServiceWorkerOnCoreThreadToRunTasks(
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25160 base::WeakPtr<ServiceWorkerTaskQueue> task_queue_weak,
Istiaque Ahmed92ad7fc2019-11-18 19:02:26161 const SequencedContextId& context_id,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57162 content::ServiceWorkerContext* service_worker_context);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25163
Istiaque Ahmed92ad7fc2019-11-18 19:02:26164 void RunTasksAfterStartWorker(const SequencedContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12165
Istiaque Ahmed92ad7fc2019-11-18 19:02:26166 void DidRegisterServiceWorker(const SequencedContextId& context_id,
167 bool success);
Istiaque Ahmedccb444022018-06-19 02:11:12168 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
169 bool success);
170
Istiaque Ahmed92ad7fc2019-11-18 19:02:26171 void DidStartWorkerForScope(const SequencedContextId& context_id,
Zhuoyu Qian2266251f2018-10-13 02:59:00172 int64_t version_id,
173 int process_id,
174 int thread_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26175 void DidStartWorkerFail(const SequencedContextId& context_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25176
Istiaque Ahmed70f76ac2018-11-02 02:59:55177 // The following three methods retrieve, store, and remove information
178 // about Service Worker registration of SW based background pages:
179 //
180 // Retrieves the last version of the extension, returns invalid version if
181 // there isn't any such extension.
182 base::Version RetrieveRegisteredServiceWorkerVersion(
183 const ExtensionId& extension_id);
184 // Records that the extension with |extension_id| and |version| successfully
185 // registered a Service Worker.
186 void SetRegisteredServiceWorkerInfo(const ExtensionId& extension_id,
187 const base::Version& version);
188 // Clears any record of registered Service Worker for the given extension with
189 // |extension_id|.
190 void RemoveRegisteredServiceWorkerInfo(const ExtensionId& extension_id);
191
Istiaque Ahmed4b70a70d2019-02-28 01:36:57192 // If the worker with |context_id| has seen worker start
193 // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs
194 // all pending tasks for that worker.
Istiaque Ahmedc92b1ea2019-12-31 00:32:49195 void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26196
197 // Returns true if |sequence| is the current activation sequence for
198 // |extension_id|.
199 bool IsCurrentSequence(const ExtensionId& extension_id,
200 ActivationSequence sequence) const;
201
Istiaque Ahmedc92b1ea2019-12-31 00:32:49202 WorkerState* GetWorkerState(const SequencedContextId& context_id);
Istiaque Ahmed92ad7fc2019-11-18 19:02:26203
Istiaque Ahmed9987ca892020-01-09 22:47:17204 int next_activation_sequence_ = 0;
Istiaque Ahmed92ad7fc2019-11-18 19:02:26205
Istiaque Ahmedc92b1ea2019-12-31 00:32:49206 // The state of worker of each activated extension.
207 std::map<SequencedContextId, WorkerState> worker_state_map_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25208
Istiaque Ahmedccb444022018-06-19 02:11:12209 content::BrowserContext* const browser_context_ = nullptr;
210
David Bertoniac01b942019-08-26 23:48:17211 // A map of Service Worker registrations if this instance is for an
212 // off-the-record BrowserContext. These are stored in the ExtensionPrefs
213 // for a regular profile.
214 // TODO(crbug.com/939664): Make this better by passing in something that
215 // will manage storing and retrieving this data.
216 std::unordered_map<ExtensionId, base::Version> off_the_record_registrations_;
217
Istiaque Ahmed92ad7fc2019-11-18 19:02:26218 // Current ActivationSequence for each activated extensions.
219 std::map<ExtensionId, ActivationSequence> activation_sequences_;
220
Jeremy Roman9fc2de62019-07-12 14:15:03221 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
Istiaque Ahmedccb444022018-06-19 02:11:12222
lazyboy63b994a2017-06-30 21:20:23223 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
224};
225
226} // namespace extensions
227
228#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_