blob: c22a747e1beab632a573783122c61898f6646a73 [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>
10
11#include "base/memory/weak_ptr.h"
lazyboy63b994a2017-06-30 21:20:2312#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
17namespace content {
18class BrowserContext;
19}
20
21namespace extensions {
22class Extension;
23class LazyContextId;
24
lazyboy63b994a2017-06-30 21:20:2325// TODO(lazyboy): Clean up queue when extension is unloaded/uninstalled.
26class 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 Ahmedb7f0e2fd92017-10-11 17:37:4240 LazyContextTaskQueue::PendingTask task) override;
lazyboy63b994a2017-06-30 21:20:2341
Istiaque Ahmedccb444022018-06-19 02:11:1242 // 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
lazyboy63b994a2017-06-30 21:20:2349 private:
Istiaque Ahmedccb444022018-06-19 02:11:1250 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
lazyboy63b994a2017-06-30 21:20:2370 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerTaskQueue);
71};
72
73} // namespace extensions
74
75#endif // EXTENSIONS_BROWSER_SERVICE_WORKER_TASK_QUEUE_H_