blob: 37c440014a70a0c6ca651dfe226d8e38087df344 [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"
16#include "extensions/browser/lazy_context_task_queue.h"
17#include "extensions/common/extension_id.h"
18#include "url/gurl.h"
19
20namespace content {
21class BrowserContext;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2522class ServiceWorkerContext;
lazyboy63b994a2017-06-30 21:20:2323}
24
25namespace extensions {
26class Extension;
27class LazyContextId;
28
lazyboy63b994a2017-06-30 21:20:2329// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
Istiaque Ahmed4b70a70d2019-02-28 01:36:5730// TODO(lazyboy): Describe the flow of a task in this queue: i.e. when
31// a worker receives DidRegisterServiceWorker, DidStartWorkerForScope and
32// DidStartServiceWorkerContext events and how queued tasks react to these
33// events.
lazyboy63b994a2017-06-30 21:20:2334class ServiceWorkerTaskQueue : public KeyedService,
35 public LazyContextTaskQueue {
36 public:
37 explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context);
38 ~ServiceWorkerTaskQueue() override;
39
40 // Convenience method to return the ServiceWorkerTaskQueue for a given
41 // |context|.
42 static ServiceWorkerTaskQueue* Get(content::BrowserContext* context);
43
44 bool ShouldEnqueueTask(content::BrowserContext* context,
45 const Extension* extension) override;
David Bertoni8269a092018-12-19 15:55:4246 void AddPendingTask(const LazyContextId& context_id,
47 PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2348
Istiaque Ahmedccb444022018-06-19 02:11:1249 // Performs Service Worker related tasks upon |extension| activation,
50 // e.g. registering |extension|'s worker, executing any pending tasks.
51 void ActivateExtension(const Extension* extension);
52 // Performs Service Worker related tasks upon |extension| deactivation,
53 // e.g. unregistering |extension|'s worker.
54 void DeactivateExtension(const Extension* extension);
55
Istiaque Ahmedd4b67ee2019-03-02 10:53:2056 // Called once an extension Service Worker context was initialized but not
57 // necessarily started executing its JavaScript.
58 void DidInitializeServiceWorkerContext(int render_process_id,
59 const ExtensionId& extension_id,
60 int64_t service_worker_version_id,
61 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2562 // Called once an extension Service Worker started running.
Istiaque Ahmed4b70a70d2019-02-28 01:36:5763 // This can be thought as "loadstop", i.e. the global JS script of the worker
64 // has completed executing.
65 void DidStartServiceWorkerContext(int render_process_id,
66 const ExtensionId& extension_id,
67 const GURL& service_worker_scope,
68 int64_t service_worker_version_id,
69 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2570 // Called once an extension Service Worker was destroyed.
Istiaque Ahmed4b70a70d2019-02-28 01:36:5771 void DidStopServiceWorkerContext(int render_process_id,
72 const ExtensionId& extension_id,
73 const GURL& service_worker_scope,
74 int64_t service_worker_version_id,
75 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2576
Istiaque Ahmed70f76ac2018-11-02 02:59:5577 class TestObserver {
78 public:
David Bertoni77615d22019-05-29 23:10:1379 TestObserver();
80 virtual ~TestObserver();
Istiaque Ahmed70f76ac2018-11-02 02:59:5581
82 // Called when an extension with id |extension_id| is going to be activated.
83 // |will_register_service_worker| is true if a Service Worker will be
84 // registered.
85 virtual void OnActivateExtension(const ExtensionId& extension_id,
86 bool will_register_service_worker) = 0;
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(TestObserver);
90 };
91
92 static void SetObserverForTest(TestObserver* observer);
93
lazyboy63b994a2017-06-30 21:20:2394 private:
Matt Falkenhagend55b9282019-09-10 23:53:3595 static void DidStartWorkerForScopeOnCoreThread(
Istiaque Ahmed4b70a70d2019-02-28 01:36:5796 const LazyContextId& context_id,
Istiaque Ahmedb8e24bdb2018-09-13 15:17:2597 base::WeakPtr<ServiceWorkerTaskQueue> task_queue,
98 int64_t version_id,
99 int process_id,
100 int thread_id);
Matt Falkenhagend55b9282019-09-10 23:53:35101 static void StartServiceWorkerOnCoreThreadToRunTasks(
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25102 base::WeakPtr<ServiceWorkerTaskQueue> task_queue_weak,
Istiaque Ahmed4b70a70d2019-02-28 01:36:57103 const LazyContextId& context_id,
104 content::ServiceWorkerContext* service_worker_context);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25105
Istiaque Ahmed4b70a70d2019-02-28 01:36:57106 void RunTasksAfterStartWorker(const LazyContextId& context_id);
Istiaque Ahmedccb444022018-06-19 02:11:12107
108 void DidRegisterServiceWorker(const ExtensionId& extension_id, bool success);
109 void DidUnregisterServiceWorker(const ExtensionId& extension_id,
110 bool success);
111
Istiaque Ahmed4b70a70d2019-02-28 01:36:57112 void DidStartWorkerForScope(const LazyContextId& context_id,
Zhuoyu Qian2266251f2018-10-13 02:59:00113 int64_t version_id,
114 int process_id,
115 int thread_id);
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25116
Istiaque Ahmed70f76ac2018-11-02 02:59:55117 // The following three methods retrieve, store, and remove information
118 // about Service Worker registration of SW based background pages:
119 //
120 // Retrieves the last version of the extension, returns invalid version if
121 // there isn't any such extension.
122 base::Version RetrieveRegisteredServiceWorkerVersion(
123 const ExtensionId& extension_id);
124 // Records that the extension with |extension_id| and |version| successfully
125 // registered a Service Worker.
126 void SetRegisteredServiceWorkerInfo(const ExtensionId& extension_id,
127 const base::Version& version);
128 // Clears any record of registered Service Worker for the given extension with
129 // |extension_id|.
130 void RemoveRegisteredServiceWorkerInfo(const ExtensionId& extension_id);
131
Istiaque Ahmed4b70a70d2019-02-28 01:36:57132 // If the worker with |context_id| has seen worker start
133 // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs
134 // all pending tasks for that worker.
135 void RunPendingTasksIfWorkerReady(const LazyContextId& context_id,
136 int64_t version_id,
137 int process_id,
138 int thread_id);
139
Istiaque Ahmedccb444022018-06-19 02:11:12140 // Set of extension ids that hasn't completed Service Worker registration.
141 std::set<ExtensionId> pending_registrations_;
142
Istiaque Ahmed4b70a70d2019-02-28 01:36:57143 // Set of workers that has seen DidStartWorkerForScope.
144 std::set<LazyContextId> started_workers_;
Istiaque Ahmedccb444022018-06-19 02:11:12145
Istiaque Ahmed4b70a70d2019-02-28 01:36:57146 // Set of workers that has seen DidStartServiceWorkerContext.
147 std::set<LazyContextId> loaded_workers_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25148
Istiaque Ahmed4b70a70d2019-02-28 01:36:57149 // Pending tasks for a |LazyContextId|.
150 // These tasks will be run once the corresponding worker becomes ready.
151 std::map<LazyContextId, std::vector<PendingTask>> pending_tasks_;
Istiaque Ahmedb8e24bdb2018-09-13 15:17:25152
Istiaque Ahmedccb444022018-06-19 02:11:12153 content::BrowserContext* const browser_context_ = nullptr;
154
David Bertoniac01b942019-08-26 23:48:17155 // A map of Service Worker registrations if this instance is for an
156 // off-the-record BrowserContext. These are stored in the ExtensionPrefs
157 // for a regular profile.
158 // TODO(crbug.com/939664): Make this better by passing in something that
159 // will manage storing and retrieving this data.
160 std::unordered_map<ExtensionId, base::Version> off_the_record_registrations_;
161
Jeremy Roman9fc2de62019-07-12 14:15:03162 base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this};
Istiaque Ahmedccb444022018-06-19 02:11:12163
lazyboy63b994a2017-06-30 21:20:23164 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
165};
166
167} // namespace extensions
168
169#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_