OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // Note that multiple extensions can share a process due to process | 43 // Note that multiple extensions can share a process due to process |
44 // collapsing. Also, a single extension can have 2 processes if it is a split | 44 // collapsing. Also, a single extension can have 2 processes if it is a split |
45 // mode extension. | 45 // mode extension. |
46 void AddEventListener(const std::string& event_name, | 46 void AddEventListener(const std::string& event_name, |
47 content::RenderProcessHost* process, | 47 content::RenderProcessHost* process, |
48 const std::string& extension_id); | 48 const std::string& extension_id); |
49 void RemoveEventListener(const std::string& event_name, | 49 void RemoveEventListener(const std::string& event_name, |
50 content::RenderProcessHost* process, | 50 content::RenderProcessHost* process, |
51 const std::string& extension_id); | 51 const std::string& extension_id); |
52 | 52 |
| 53 // Add or remove the extension as having a lazy background page that listens |
| 54 // to the event. The difference from the above methods is that these will be |
| 55 // remembered even after the process goes away. We use this list to decide |
| 56 // which extension pages to load when dispatching an event. |
| 57 void AddLazyEventListener(const std::string& event_name, |
| 58 const std::string& extension_id); |
| 59 void RemoveLazyEventListener(const std::string& event_name, |
| 60 const std::string& extension_id); |
| 61 |
53 // Returns true if there is at least one listener for the given event. | 62 // Returns true if there is at least one listener for the given event. |
54 bool HasEventListener(const std::string& event_name); | 63 bool HasEventListener(const std::string& event_name); |
55 | 64 |
56 // Returns true if the extension is listening to the given event. | 65 // Returns true if the extension is listening to the given event. |
57 bool ExtensionHasEventListener(const std::string& extension_id, | 66 bool ExtensionHasEventListener(const std::string& extension_id, |
58 const std::string& event_name); | 67 const std::string& event_name); |
59 | 68 |
60 // Send an event to every registered extension renderer. If | 69 // Send an event to every registered extension renderer. If |
61 // |restrict_to_profile| is non-NULL, then the event will not be sent to other | 70 // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
62 // profiles unless the extension has permission (e.g. incognito tab update -> | 71 // profiles unless the extension has permission (e.g. incognito tab update -> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 // the renderer. | 107 // the renderer. |
99 bool HasInFlightEvents(const std::string& extension_id); | 108 bool HasInFlightEvents(const std::string& extension_id); |
100 | 109 |
101 protected: | 110 protected: |
102 // The details of an event to be dispatched. | 111 // The details of an event to be dispatched. |
103 struct ExtensionEvent; | 112 struct ExtensionEvent; |
104 | 113 |
105 // Shared by DispatchEvent*. If |extension_id| is empty, the event is | 114 // Shared by DispatchEvent*. If |extension_id| is empty, the event is |
106 // broadcast. | 115 // broadcast. |
107 // An event that just came off the pending list may not be delayed again. | 116 // An event that just came off the pending list may not be delayed again. |
108 void DispatchEventImpl(const linked_ptr<ExtensionEvent>& event, | 117 void DispatchEventImpl(const std::string& extension_id, |
| 118 const linked_ptr<ExtensionEvent>& event, |
109 bool was_pending); | 119 bool was_pending); |
110 | 120 |
| 121 // Ensures that all non-persistent background pages that are interested in the |
| 122 // given event are loaded, and queues the event if the page is not ready yet. |
| 123 // If |extension_id| is non-empty, we load only that extension's page |
| 124 // (assuming it is interested in the event). |
| 125 void LoadLazyBackgroundPagesForEvent( |
| 126 const std::string& extension_id, |
| 127 const linked_ptr<ExtensionEvent>& event); |
| 128 |
111 // Dispatch may be delayed if the extension has a lazy background page. | 129 // Dispatch may be delayed if the extension has a lazy background page. |
112 bool CanDispatchEventNow(const std::string& extension_id); | 130 bool CanDispatchEventNow(const Extension* extension); |
113 | 131 |
114 // Store the event so that it can be dispatched (in order received) | 132 // Store the event so that it can be dispatched (in order received) |
115 // when the background page is done loading. | 133 // when the background page is done loading. |
116 void AppendEvent(const linked_ptr<ExtensionEvent>& event); | 134 void AppendEvent(const std::string& extension_id, |
| 135 const linked_ptr<ExtensionEvent>& event); |
117 void DispatchPendingEvents(const std::string& extension_id); | 136 void DispatchPendingEvents(const std::string& extension_id); |
118 | 137 |
119 private: | 138 private: |
120 // An extension listening to an event. | 139 // An extension listening to an event. |
121 struct EventListener; | 140 struct EventListener; |
122 | 141 |
123 virtual void Observe(int type, | 142 virtual void Observe(int type, |
124 const content::NotificationSource& source, | 143 const content::NotificationSource& source, |
125 const content::NotificationDetails& details) OVERRIDE; | 144 const content::NotificationDetails& details) OVERRIDE; |
126 | 145 |
127 Profile* profile_; | 146 Profile* profile_; |
128 | 147 |
129 content::NotificationRegistrar registrar_; | 148 content::NotificationRegistrar registrar_; |
130 | 149 |
131 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 150 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
132 | 151 |
133 // A map between an event name and a set of extensions that are listening | 152 // A map between an event name and a set of extensions that are listening |
134 // to that event. | 153 // to that event. |
135 typedef std::map<std::string, std::set<EventListener> > ListenerMap; | 154 typedef std::map<std::string, std::set<EventListener> > ListenerMap; |
136 ListenerMap listeners_; | 155 ListenerMap listeners_; |
137 | 156 |
| 157 // Keeps track of all the non-persistent background pages that are listening |
| 158 // to events. |
| 159 // TODO(mpcomplete): save to disk. |
| 160 ListenerMap lazy_listeners_; |
| 161 |
138 // A map between an extension id and the queue of events pending | 162 // A map between an extension id and the queue of events pending |
139 // the load of it's background page. | 163 // the load of it's background page. |
140 typedef std::vector<linked_ptr<ExtensionEvent> > PendingEventsList; | 164 typedef std::vector<linked_ptr<ExtensionEvent> > PendingEventsList; |
141 typedef std::map<std::string, | 165 typedef std::map<std::string, |
142 linked_ptr<PendingEventsList> > PendingEventsPerExtMap; | 166 linked_ptr<PendingEventsList> > PendingEventsPerExtMap; |
143 PendingEventsPerExtMap pending_events_; | 167 PendingEventsPerExtMap pending_events_; |
144 | 168 |
145 // Track of the number of dispatched events that have not yet sent an | 169 // Track of the number of dispatched events that have not yet sent an |
146 // ACK from the renderer. | 170 // ACK from the renderer. |
147 void IncrementInFlightEvents(const Extension* extension); | 171 void IncrementInFlightEvents(const Extension* extension); |
148 std::map<std::string, int> in_flight_events_; | 172 std::map<std::string, int> in_flight_events_; |
149 | 173 |
150 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); | 174 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); |
151 }; | 175 }; |
152 | 176 |
153 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 177 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
OLD | NEW |