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> |
| 10 | |
| 11 | #include "base/memory/weak_ptr.h" |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 12 | #include "components/keyed_service/core/keyed_service.h" |
| 13 | #include "extensions/browser/lazy_context_task_queue.h" |
| 14 | #include "extensions/common/extension_id.h" |
| 15 | #include "url/gurl.h" |
| 16 | |
| 17 | namespace content { |
| 18 | class BrowserContext; |
| 19 | } |
| 20 | |
| 21 | namespace extensions { |
| 22 | class Extension; |
| 23 | class LazyContextId; |
| 24 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 25 | // TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled. |
| 26 | class ServiceWorkerTaskQueue : public KeyedService, |
| 27 | public LazyContextTaskQueue { |
| 28 | public: |
| 29 | explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context); |
| 30 | ~ServiceWorkerTaskQueue() override; |
| 31 | |
| 32 | // Convenience method to return the ServiceWorkerTaskQueue for a given |
| 33 | // |context|. |
| 34 | static ServiceWorkerTaskQueue* Get(content::BrowserContext* context); |
| 35 | |
| 36 | bool ShouldEnqueueTask(content::BrowserContext* context, |
| 37 | const Extension* extension) override; |
| 38 | void AddPendingTaskToDispatchEvent( |
| 39 | LazyContextId* context_id, |
Istiaque Ahmed | b7f0e2fd9 | 2017-10-11 17:37:42 | [diff] [blame] | 40 | LazyContextTaskQueue::PendingTask task) override; |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 41 | |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame^] | 42 | // Performs Service Worker related tasks upon |extension| activation, |
| 43 | // e.g. registering |extension|'s worker, executing any pending tasks. |
| 44 | void ActivateExtension(const Extension* extension); |
| 45 | // Performs Service Worker related tasks upon |extension| deactivation, |
| 46 | // e.g. unregistering |extension|'s worker. |
| 47 | void DeactivateExtension(const Extension* extension); |
| 48 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 49 | private: |
Istiaque Ahmed | ccb44402 | 2018-06-19 02:11:12 | [diff] [blame^] | 50 | struct TaskInfo; |
| 51 | |
| 52 | void RunTaskAfterStartWorker(LazyContextId* context_id, |
| 53 | LazyContextTaskQueue::PendingTask task); |
| 54 | |
| 55 | void DidRegisterServiceWorker(const ExtensionId& extension_id, bool success); |
| 56 | void DidUnregisterServiceWorker(const ExtensionId& extension_id, |
| 57 | bool success); |
| 58 | |
| 59 | // Set of extension ids that hasn't completed Service Worker registration. |
| 60 | std::set<ExtensionId> pending_registrations_; |
| 61 | |
| 62 | // Map of extension id -> pending tasks. These are run once the Service Worker |
| 63 | // registration of the extension completes. |
| 64 | std::map<ExtensionId, std::vector<TaskInfo>> pending_tasks_; |
| 65 | |
| 66 | content::BrowserContext* const browser_context_ = nullptr; |
| 67 | |
| 68 | base::WeakPtrFactory<ServiceWorkerTaskQueue> weak_factory_; |
| 69 | |
lazyboy | 63b994a | 2017-06-30 21:20:23 | [diff] [blame] | 70 | DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue); |
| 71 | }; |
| 72 | |
| 73 | } // namespace extensions |
| 74 | |
| 75 | #endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_ |