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 | |
62 // Returns true if there is at least one listener for the given event. | 53 // Returns true if there is at least one listener for the given event. |
63 bool HasEventListener(const std::string& event_name); | 54 bool HasEventListener(const std::string& event_name); |
64 | 55 |
65 // Returns true if the extension is listening to the given event. | 56 // Returns true if the extension is listening to the given event. |
66 bool ExtensionHasEventListener(const std::string& extension_id, | 57 bool ExtensionHasEventListener(const std::string& extension_id, |
67 const std::string& event_name); | 58 const std::string& event_name); |
68 | 59 |
69 // Send an event to every registered extension renderer. If | 60 // Send an event to every registered extension renderer. If |
70 // |restrict_to_profile| is non-NULL, then the event will not be sent to other | 61 // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
71 // profiles unless the extension has permission (e.g. incognito tab update -> | 62 // profiles unless the extension has permission (e.g. incognito tab update -> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // the renderer. | 98 // the renderer. |
108 bool HasInFlightEvents(const std::string& extension_id); | 99 bool HasInFlightEvents(const std::string& extension_id); |
109 | 100 |
110 protected: | 101 protected: |
111 // The details of an event to be dispatched. | 102 // The details of an event to be dispatched. |
112 struct ExtensionEvent; | 103 struct ExtensionEvent; |
113 | 104 |
114 // Shared by DispatchEvent*. If |extension_id| is empty, the event is | 105 // Shared by DispatchEvent*. If |extension_id| is empty, the event is |
115 // broadcast. | 106 // broadcast. |
116 // An event that just came off the pending list may not be delayed again. | 107 // An event that just came off the pending list may not be delayed again. |
117 void DispatchEventImpl(const std::string& extension_id, | 108 void DispatchEventImpl(const linked_ptr<ExtensionEvent>& event, |
118 const linked_ptr<ExtensionEvent>& event, | |
119 bool was_pending); | 109 bool was_pending); |
120 | 110 |
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 | |
129 // Dispatch may be delayed if the extension has a lazy background page. | 111 // Dispatch may be delayed if the extension has a lazy background page. |
130 bool CanDispatchEventNow(const Extension* extension); | 112 bool CanDispatchEventNow(const std::string& extension_id); |
131 | 113 |
132 // Store the event so that it can be dispatched (in order received) | 114 // Store the event so that it can be dispatched (in order received) |
133 // when the background page is done loading. | 115 // when the background page is done loading. |
134 void AppendEvent(const std::string& extension_id, | 116 void AppendEvent(const linked_ptr<ExtensionEvent>& event); |
135 const linked_ptr<ExtensionEvent>& event); | |
136 void DispatchPendingEvents(const std::string& extension_id); | 117 void DispatchPendingEvents(const std::string& extension_id); |
137 | 118 |
138 private: | 119 private: |
139 // An extension listening to an event. | 120 // An extension listening to an event. |
140 struct EventListener; | 121 struct EventListener; |
141 | 122 |
142 virtual void Observe(int type, | 123 virtual void Observe(int type, |
143 const content::NotificationSource& source, | 124 const content::NotificationSource& source, |
144 const content::NotificationDetails& details) OVERRIDE; | 125 const content::NotificationDetails& details) OVERRIDE; |
145 | 126 |
146 Profile* profile_; | 127 Profile* profile_; |
147 | 128 |
148 content::NotificationRegistrar registrar_; | 129 content::NotificationRegistrar registrar_; |
149 | 130 |
150 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 131 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
151 | 132 |
152 // A map between an event name and a set of extensions that are listening | 133 // A map between an event name and a set of extensions that are listening |
153 // to that event. | 134 // to that event. |
154 typedef std::map<std::string, std::set<EventListener> > ListenerMap; | 135 typedef std::map<std::string, std::set<EventListener> > ListenerMap; |
155 ListenerMap listeners_; | 136 ListenerMap listeners_; |
156 | 137 |
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 | |
162 // A map between an extension id and the queue of events pending | 138 // A map between an extension id and the queue of events pending |
163 // the load of it's background page. | 139 // the load of it's background page. |
164 typedef std::vector<linked_ptr<ExtensionEvent> > PendingEventsList; | 140 typedef std::vector<linked_ptr<ExtensionEvent> > PendingEventsList; |
165 typedef std::map<std::string, | 141 typedef std::map<std::string, |
166 linked_ptr<PendingEventsList> > PendingEventsPerExtMap; | 142 linked_ptr<PendingEventsList> > PendingEventsPerExtMap; |
167 PendingEventsPerExtMap pending_events_; | 143 PendingEventsPerExtMap pending_events_; |
168 | 144 |
169 // Track of the number of dispatched events that have not yet sent an | 145 // Track of the number of dispatched events that have not yet sent an |
170 // ACK from the renderer. | 146 // ACK from the renderer. |
171 void IncrementInFlightEvents(const Extension* extension); | 147 void IncrementInFlightEvents(const Extension* extension); |
172 std::map<std::string, int> in_flight_events_; | 148 std::map<std::string, int> in_flight_events_; |
173 | 149 |
174 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); | 150 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); |
175 }; | 151 }; |
176 | 152 |
177 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 153 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
OLD | NEW |