OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ | 5 #ifndef EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ |
6 #define EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ | 6 #define EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "extensions/browser/lazy_context_task_queue.h" |
13 #include "extensions/common/extension_id.h" | 14 #include "extensions/common/extension_id.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DictionaryValue; | 17 class DictionaryValue; |
17 } | 18 } |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 class BrowserContext; | 21 class BrowserContext; |
21 } | 22 } |
22 | 23 |
23 namespace extensions { | 24 namespace extensions { |
24 class EventListener; | 25 class EventListener; |
25 class Extension; | 26 class Extension; |
26 class ExtensionHost; | |
27 class LazyContextId; | 27 class LazyContextId; |
28 struct Event; | 28 struct Event; |
29 | 29 |
30 // Helper class for EventRouter to dispatch lazy events to lazy contexts. | 30 // Helper class for EventRouter to dispatch lazy events to lazy contexts. |
31 // | 31 // |
32 // Manages waking up lazy contexts if they are stopped. | 32 // Manages waking up lazy contexts if they are stopped. |
33 class LazyEventDispatcher { | 33 class LazyEventDispatcher { |
34 public: | 34 public: |
35 // TODO(lazyboy): ExtensionHost is specific to events pages, provide a generic | |
36 // context info that works for both event pages and service workers. | |
37 using DispatchFunction = | 35 using DispatchFunction = |
38 base::Callback<void(const linked_ptr<Event>&, ExtensionHost*)>; | 36 base::Callback<void(const linked_ptr<Event>&, |
| 37 std::unique_ptr<LazyContextTaskQueue::ContextInfo>)>; |
39 | 38 |
40 LazyEventDispatcher(content::BrowserContext* browser_context, | 39 LazyEventDispatcher(content::BrowserContext* browser_context, |
41 const linked_ptr<Event>& event, | 40 const linked_ptr<Event>& event, |
42 const DispatchFunction& dispatch_function); | 41 const DispatchFunction& dispatch_function); |
43 ~LazyEventDispatcher(); | 42 ~LazyEventDispatcher(); |
44 | 43 |
45 // Dispatches a lazy event to |extension_id|. | 44 // Dispatches the lazy |event_| to |extension_id|. |
46 // | 45 // |
47 // Ensures that all lazy background pages that are interested in the given | 46 // Ensures that all lazy background pages that are interested in the given |
48 // event are loaded, and queues the event if the page is not ready yet. | 47 // event are loaded, and queues the event if the page is not ready yet. |
49 void DispatchToEventPage(const ExtensionId& extension_id, | 48 void DispatchToEventPage(const ExtensionId& extension_id, |
50 const base::DictionaryValue* listener_filter); | 49 const base::DictionaryValue* listener_filter); |
| 50 // Dispatches the lazy |event_| to |extension_id|'s service worker. |
| 51 // |
| 52 // Service workers are started if they were stopped, before dispatching the |
| 53 // event. |
| 54 void DispatchToServiceWorker(const ExtensionId& extension_id, |
| 55 const GURL& service_worker_scope, |
| 56 const base::DictionaryValue* listener_filter); |
51 | 57 |
52 // Returns whether or not an event listener identical to |listener| is queued | 58 // Returns whether or not an event listener identical to |listener| is queued |
53 // for dispatch already. | 59 // for dispatch already. |
54 bool HasAlreadyDispatched(content::BrowserContext* context, | 60 bool HasAlreadyDispatched(content::BrowserContext* context, |
55 const EventListener* listener) const; | 61 const EventListener* listener) const; |
56 | 62 |
57 private: | 63 private: |
58 using EventPageDispatchIdentifier = | 64 using EventPageDispatchIdentifier = |
59 std::pair<const content::BrowserContext*, std::string>; | 65 std::pair<const content::BrowserContext*, std::string>; |
| 66 using ServiceWorkerDispatchIdentifier = |
| 67 std::pair<const content::BrowserContext*, GURL>; |
60 | 68 |
61 void DispatchToLazyContext(LazyContextId* dispatch_context, | 69 void DispatchToLazyContext(LazyContextId* dispatch_context, |
62 const base::DictionaryValue* listener_filter); | 70 const base::DictionaryValue* listener_filter); |
63 | 71 |
64 // Possibly loads given extension's background page or extension Service | 72 // Possibly loads given extension's background page or extension Service |
65 // Worker in preparation to dispatch an event. Returns true if the event was | 73 // Worker in preparation to dispatch an event. Returns true if the event was |
66 // queued for subsequent dispatch, false otherwise. | 74 // queued for subsequent dispatch, false otherwise. |
67 bool QueueEventDispatch(LazyContextId* dispatch_context, | 75 bool QueueEventDispatch(LazyContextId* dispatch_context, |
68 const Extension* extension, | 76 const Extension* extension, |
69 const base::DictionaryValue* listener_filter); | 77 const base::DictionaryValue* listener_filter); |
70 | 78 |
71 bool HasAlreadyDispatchedImpl(const LazyContextId* dispatch_context) const; | 79 bool HasAlreadyDispatchedImpl(const LazyContextId* dispatch_context) const; |
72 | 80 |
73 void RecordAlreadyDispatched(LazyContextId* dispatch_context); | 81 void RecordAlreadyDispatched(LazyContextId* dispatch_context); |
74 | 82 |
75 content::BrowserContext* GetIncognitoContext(const Extension* extension); | 83 content::BrowserContext* GetIncognitoContext(const Extension* extension); |
76 | 84 |
77 content::BrowserContext* const browser_context_; | 85 content::BrowserContext* const browser_context_; |
78 linked_ptr<Event> event_; | 86 linked_ptr<Event> event_; |
79 DispatchFunction dispatch_function_; | 87 DispatchFunction dispatch_function_; |
80 | 88 |
| 89 // TODO(lazyboy): Instead of keeping these two std::sets, compbine them using |
| 90 // LazyContextId key when service worker event listeners are more common. |
81 std::set<EventPageDispatchIdentifier> dispatched_ids_for_event_page_; | 91 std::set<EventPageDispatchIdentifier> dispatched_ids_for_event_page_; |
| 92 std::set<ServiceWorkerDispatchIdentifier> dispatched_ids_for_service_worker_; |
82 | 93 |
83 DISALLOW_COPY_AND_ASSIGN(LazyEventDispatcher); | 94 DISALLOW_COPY_AND_ASSIGN(LazyEventDispatcher); |
84 }; | 95 }; |
85 | 96 |
86 } // namespace extensions | 97 } // namespace extensions |
87 | 98 |
88 #endif // EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ | 99 #endif // EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCHER_H_ |
OLD | NEW |