lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 1 | // 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 Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <set> |
David Bertoni | ac01b94 | 2019-08-26 23:48:17 | [diff] [blame] | 10 | #include <unordered_map> |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 11 | #include <vector> |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 12 | |
| 13 | #include "base/memory/weak_ptr.h" |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 14 | #include "base/version.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 15 | #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 | |
| 20 | namespace content { |
| 21 | class BrowserContext; |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 22 | class ServiceWorkerContext; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | namespace extensions { |
| 26 | class Extension; |
| 27 | class LazyContextId; |
| 28 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 29 | // TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled. |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 30 | // 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. |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 34 | class 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 Bertoni | 8269a09 | 2018-12-19 15:55:42 | [diff] [blame] | 46 | void AddPendingTask(const LazyContextId& context_id, |
| 47 | PendingTask task) override; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 48 | |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 49 | // 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 Ahmed | d4b67ee | 2019-03-02 10:53:20 | [diff] [blame] | 56 | // 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 Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 62 | // Called once an extension Service Worker started running. |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 63 | // 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 Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 70 | // Called once an extension Service Worker was destroyed. |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 71 | 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 Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 76 | |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 77 | class TestObserver { |
| 78 | public: |
David Bertoni | 77615d2 | 2019-05-29 23:10:13 | [diff] [blame] | 79 | TestObserver(); |
| 80 | virtual ~TestObserver(); |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 81 | |
| 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 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 94 | private: |
Matt Falkenhagen | d55b928 | 2019-09-10 23:53:35 | [diff] [blame^] | 95 | static void DidStartWorkerForScopeOnCoreThread( |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 96 | const LazyContextId& context_id, |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 97 | base::WeakPtr<ServiceWorkerTaskQueue> task_queue, |
| 98 | int64_t version_id, |
| 99 | int process_id, |
| 100 | int thread_id); |
Matt Falkenhagen | d55b928 | 2019-09-10 23:53:35 | [diff] [blame^] | 101 | static void StartServiceWorkerOnCoreThreadToRunTasks( |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 102 | base::WeakPtr<ServiceWorkerTaskQueue> task_queue_weak, |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 103 | const LazyContextId& context_id, |
| 104 | content::ServiceWorkerContext* service_worker_context); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 105 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 106 | void RunTasksAfterStartWorker(const LazyContextId& context_id); |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 107 | |
| 108 | void DidRegisterServiceWorker(const ExtensionId& extension_id, bool success); |
| 109 | void DidUnregisterServiceWorker(const ExtensionId& extension_id, |
| 110 | bool success); |
| 111 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 112 | void DidStartWorkerForScope(const LazyContextId& context_id, |
Zhuoyu Qian | 2266251f | 2018-10-13 02:59:00 | [diff] [blame] | 113 | int64_t version_id, |
| 114 | int process_id, |
| 115 | int thread_id); |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 116 | |
Istiaque Ahmed | 70f76ac | 2018-11-02 02:59:55 | [diff] [blame] | 117 | // 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 Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 132 | // 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 Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 140 | // Set of extension ids that hasn't completed Service Worker registration. |
| 141 | std::set<ExtensionId> pending_registrations_; |
| 142 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 143 | // Set of workers that has seen DidStartWorkerForScope. |
| 144 | std::set<LazyContextId> started_workers_; |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 145 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 146 | // Set of workers that has seen DidStartServiceWorkerContext. |
| 147 | std::set<LazyContextId> loaded_workers_; |
Istiaque Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 148 | |
Istiaque Ahmed | 4b70a70d | 2019-02-28 01:36:57 | [diff] [blame] | 149 | // 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 Ahmed | b8e24bdb | 2018-09-13 15:17:25 | [diff] [blame] | 152 | |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 153 | content::BrowserContext* const browser_context_ = nullptr; |
| 154 | |
David Bertoni | ac01b94 | 2019-08-26 23:48:17 | [diff] [blame] | 155 | // 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 Roman | 9fc2de6 | 2019-07-12 14:15:03 | [diff] [blame] | 162 | base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_{this}; |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame] | 163 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 164 | DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue); |
| 165 | }; |
| 166 | |
| 167 | } // namespace extensions |
| 168 | |
| 169 | #endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_ |