OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_API_TABS_TABS_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/memory/linked_ptr.h" |
13 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
14 #include "chrome/browser/extensions/api/tabs/tabs_api.h" | 15 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
15 #include "chrome/browser/ui/browser_list_observer.h" | 16 #include "chrome/browser/ui/browser_list_observer.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" |
17 #include "components/favicon/core/favicon_driver_observer.h" | 18 #include "components/favicon/core/favicon_driver_observer.h" |
18 #include "components/ui/zoom/zoom_observer.h" | 19 #include "components/ui/zoom/zoom_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
21 | 22 |
22 namespace content { | 23 namespace content { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 void OnFaviconAvailable(const gfx::Image& image) override; | 88 void OnFaviconAvailable(const gfx::Image& image) override; |
88 void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver, | 89 void OnFaviconUpdated(favicon::FaviconDriver* favicon_driver, |
89 bool icon_url_changed) override; | 90 bool icon_url_changed) override; |
90 | 91 |
91 private: | 92 private: |
92 // "Synthetic" event. Called from TabInsertedAt if new tab is detected. | 93 // "Synthetic" event. Called from TabInsertedAt if new tab is detected. |
93 void TabCreatedAt(content::WebContents* contents, int index, bool active); | 94 void TabCreatedAt(content::WebContents* contents, int index, bool active); |
94 | 95 |
95 // Internal processing of tab updated events. Is called by both TabChangedAt | 96 // Internal processing of tab updated events. Is called by both TabChangedAt |
96 // and Observe/NAV_ENTRY_COMMITTED. | 97 // and Observe/NAV_ENTRY_COMMITTED. |
97 void TabUpdated(content::WebContents* contents, bool did_navigate); | 98 class TabEntry; |
| 99 void TabUpdated(linked_ptr<TabEntry> entry, |
| 100 scoped_ptr<base::DictionaryValue> changed_properties); |
98 | 101 |
99 // Triggers a tab updated event if the favicon URL changes. | 102 // Triggers a tab updated event if the favicon URL changes. |
100 void FaviconUrlUpdated(content::WebContents* contents); | 103 void FaviconUrlUpdated(content::WebContents* contents); |
101 | 104 |
102 // The DispatchEvent methods forward events to the |profile|'s event router. | 105 // The DispatchEvent methods forward events to the |profile|'s event router. |
103 // The TabsEventRouter listens to events for all profiles, | 106 // The TabsEventRouter listens to events for all profiles, |
104 // so we avoid duplication by dropping events destined for other profiles. | 107 // so we avoid duplication by dropping events destined for other profiles. |
105 void DispatchEvent(Profile* profile, | 108 void DispatchEvent(Profile* profile, |
106 const std::string& event_name, | 109 const std::string& event_name, |
107 scoped_ptr<base::ListValue> args, | 110 scoped_ptr<base::ListValue> args, |
(...skipping 26 matching lines...) Expand all Loading... |
134 // Removes notifications added in RegisterForTabNotifications. | 137 // Removes notifications added in RegisterForTabNotifications. |
135 void UnregisterForTabNotifications(content::WebContents* contents); | 138 void UnregisterForTabNotifications(content::WebContents* contents); |
136 | 139 |
137 content::NotificationRegistrar registrar_; | 140 content::NotificationRegistrar registrar_; |
138 | 141 |
139 // Maintain some information about known tabs, so we can: | 142 // Maintain some information about known tabs, so we can: |
140 // | 143 // |
141 // - distinguish between tab creation and tab insertion | 144 // - distinguish between tab creation and tab insertion |
142 // - not send tab-detached after tab-removed | 145 // - not send tab-detached after tab-removed |
143 // - reduce the "noise" of TabChangedAt() when sending events to extensions | 146 // - reduce the "noise" of TabChangedAt() when sending events to extensions |
| 147 // - remember last muted and audible states to know if there was a change |
144 class TabEntry { | 148 class TabEntry { |
145 public: | 149 public: |
146 // Create a new tab entry whose initial state is TAB_COMPLETE. This | 150 // Create a TabEntry associated with, and tracking state changes to, |
147 // constructor is required because TabEntry objects placed inside an | 151 // |contents|. |
148 // std::map<> by value. | 152 explicit TabEntry(content::WebContents* contents); |
149 TabEntry(); | |
150 | 153 |
151 // Update the load state of the tab based on its WebContents. Returns true | 154 // Indicate via a list of key/value pairs if a tab is loading based on its |
152 // if the state changed, false otherwise. Whether the state has changed or | 155 // WebContents. Whether the state has changed or not is used to determine |
153 // not is used to determine if events needs to be sent to extensions during | 156 // if events needs to be sent to extensions during processing of |
154 // processing of TabChangedAt(). This method will "hold" a state-change | 157 // TabChangedAt(). If this method indicates that a tab should "hold" a |
155 // to "loading", until the DidNavigate() method which should always follow | 158 // state-change to "loading", the DidNavigate() method should eventually |
156 // it. Returns NULL if no updates should be sent. | 159 // send a similar message to undo it. If false, the returned key/value |
157 base::DictionaryValue* UpdateLoadState( | 160 // pairs list is empty. |
158 const content::WebContents* contents); | 161 scoped_ptr<base::DictionaryValue> UpdateLoadState(); |
159 | 162 |
160 // Indicates that a tab load has resulted in a navigation and the | 163 // Indicate via a list of key/value pairs that a tab load has resulted in a |
161 // destination url is available for inspection. Returns NULL if no updates | 164 // navigation and the destination url is available for inspection. The list |
162 // should be sent. | 165 // is empty if no updates should be sent. |
163 base::DictionaryValue* DidNavigate(const content::WebContents* contents); | 166 scoped_ptr<base::DictionaryValue> DidNavigate(); |
| 167 |
| 168 // Update the audible and muted states and return whether they were changed |
| 169 bool SetAudible(bool new_val); |
| 170 bool SetMuted(bool new_val); |
| 171 |
| 172 content::WebContents* web_contents() { return contents_; } |
164 | 173 |
165 private: | 174 private: |
| 175 content::WebContents* contents_; |
| 176 |
166 // Whether we are waiting to fire the 'complete' status change. This will | 177 // Whether we are waiting to fire the 'complete' status change. This will |
167 // occur the first time the WebContents stops loading after the | 178 // occur the first time the WebContents stops loading after the |
168 // NAV_ENTRY_COMMITTED was fired. The tab may go back into and out of the | 179 // NAV_ENTRY_COMMITTED was fired. The tab may go back into and out of the |
169 // loading state subsequently, but we will ignore those changes. | 180 // loading state subsequently, but we will ignore those changes. |
170 bool complete_waiting_on_load_; | 181 bool complete_waiting_on_load_; |
171 | 182 |
| 183 // Previous audible and muted states |
| 184 bool was_audible_; |
| 185 bool was_muted_; |
| 186 |
172 GURL url_; | 187 GURL url_; |
173 }; | 188 }; |
174 | 189 |
175 // Gets the TabEntry for the given |contents|. Returns TabEntry* if | 190 // Gets the TabEntry for the given |contents|. Returns linked_ptr<TabEntry> |
176 // found, NULL if not. | 191 // if found, NULL if not. |
177 TabEntry* GetTabEntry(content::WebContents* contents); | 192 linked_ptr<TabEntry> GetTabEntry(content::WebContents* contents); |
178 | 193 |
179 std::map<int, TabEntry> tab_entries_; | 194 using TabEntryMap = std::map<int, linked_ptr<TabEntry>>; |
| 195 TabEntryMap tab_entries_; |
180 | 196 |
181 // The main profile that owns this event router. | 197 // The main profile that owns this event router. |
182 Profile* profile_; | 198 Profile* profile_; |
183 | 199 |
184 ScopedObserver<favicon::FaviconDriver, TabsEventRouter> | 200 ScopedObserver<favicon::FaviconDriver, TabsEventRouter> |
185 favicon_scoped_observer_; | 201 favicon_scoped_observer_; |
186 | 202 |
187 DISALLOW_COPY_AND_ASSIGN(TabsEventRouter); | 203 DISALLOW_COPY_AND_ASSIGN(TabsEventRouter); |
188 }; | 204 }; |
189 | 205 |
190 } // namespace extensions | 206 } // namespace extensions |
191 | 207 |
192 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_H_ | 208 #endif // CHROME_BROWSER_EXTENSIONS_API_TABS_TABS_EVENT_ROUTER_H_ |
OLD | NEW |