Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(643)

Side by Side Diff: chrome/browser/extensions/browser_event_router.cc

Issue 10696208: Move ExtensionEventRouter and related into extensions namespace (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Fixed bug + latest master Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "chrome/browser/extensions/extension_browser_event_router.h" 5 #include "chrome/browser/extensions/browser_event_router.h"
6 6
7 #include "base/json/json_writer.h" 7 #include "base/json/json_writer.h"
8 #include "base/values.h" 8 #include "base/values.h"
9 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h" 9 #include "chrome/browser/extensions/api/extension_action/extension_page_actions_ api_constants.h"
10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 10 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
11 #include "chrome/browser/extensions/extension_event_names.h" 11 #include "chrome/browser/extensions/event_names.h"
12 #include "chrome/browser/extensions/extension_event_router.h" 12 #include "chrome/browser/extensions/event_router.h"
13 #include "chrome/browser/extensions/extension_service.h" 13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_tab_util.h" 14 #include "chrome/browser/extensions/extension_tab_util.h"
15 #include "chrome/browser/extensions/window_controller.h" 15 #include "chrome/browser/extensions/window_controller.h"
16 #include "chrome/browser/profiles/profile.h" 16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h" 17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_list.h"
19 #include "chrome/browser/ui/browser_tabstrip.h" 19 #include "chrome/browser/ui/browser_tabstrip.h"
20 #include "chrome/browser/ui/tab_contents/tab_contents.h" 20 #include "chrome/browser/ui/tab_contents/tab_contents.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/common/chrome_notification_types.h" 22 #include "chrome/common/chrome_notification_types.h"
23 #include "chrome/common/extensions/extension.h" 23 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/extensions/extension_constants.h" 24 #include "chrome/common/extensions/extension_constants.h"
25 #include "content/public/browser/navigation_controller.h" 25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/notification_service.h" 26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
28 28
29 #if defined(TOOLKIT_GTK) 29 #if defined(TOOLKIT_GTK)
30 #include "ui/base/x/active_window_watcher_x.h" 30 #include "ui/base/x/active_window_watcher_x.h"
31 #endif 31 #endif
32 32
33 namespace events = extension_event_names; 33 namespace events = extensions::event_names;
34 namespace tab_keys = extensions::tabs_constants; 34 namespace tab_keys = extensions::tabs_constants;
35 namespace page_action_keys = extension_page_actions_api_constants; 35 namespace page_action_keys = extension_page_actions_api_constants;
36 36
37 using content::NavigationController; 37 using content::NavigationController;
38 using content::WebContents; 38 using content::WebContents;
39 39
40 ExtensionBrowserEventRouter::TabEntry::TabEntry() 40 namespace extensions {
41
42 BrowserEventRouter::TabEntry::TabEntry()
41 : complete_waiting_on_load_(false), 43 : complete_waiting_on_load_(false),
42 url_() { 44 url_() {
43 } 45 }
44 46
45 DictionaryValue* ExtensionBrowserEventRouter::TabEntry::UpdateLoadState( 47 DictionaryValue* BrowserEventRouter::TabEntry::UpdateLoadState(
46 const WebContents* contents) { 48 const WebContents* contents) {
47 // The tab may go in & out of loading (for instance if iframes navigate). 49 // The tab may go in & out of loading (for instance if iframes navigate).
48 // We only want to respond to the first change from loading to !loading after 50 // We only want to respond to the first change from loading to !loading after
49 // the NAV_ENTRY_COMMITTED was fired. 51 // the NAV_ENTRY_COMMITTED was fired.
50 if (!complete_waiting_on_load_ || contents->IsLoading()) 52 if (!complete_waiting_on_load_ || contents->IsLoading())
51 return NULL; 53 return NULL;
52 54
53 // Send "complete" state change. 55 // Send "complete" state change.
54 complete_waiting_on_load_ = false; 56 complete_waiting_on_load_ = false;
55 DictionaryValue* changed_properties = new DictionaryValue(); 57 DictionaryValue* changed_properties = new DictionaryValue();
56 changed_properties->SetString(tab_keys::kStatusKey, 58 changed_properties->SetString(tab_keys::kStatusKey,
57 tab_keys::kStatusValueComplete); 59 tab_keys::kStatusValueComplete);
58 return changed_properties; 60 return changed_properties;
59 } 61 }
60 62
61 DictionaryValue* ExtensionBrowserEventRouter::TabEntry::DidNavigate( 63 DictionaryValue* BrowserEventRouter::TabEntry::DidNavigate(
62 const WebContents* contents) { 64 const WebContents* contents) {
63 // Send "loading" state change. 65 // Send "loading" state change.
64 complete_waiting_on_load_ = true; 66 complete_waiting_on_load_ = true;
65 DictionaryValue* changed_properties = new DictionaryValue(); 67 DictionaryValue* changed_properties = new DictionaryValue();
66 changed_properties->SetString(tab_keys::kStatusKey, 68 changed_properties->SetString(tab_keys::kStatusKey,
67 tab_keys::kStatusValueLoading); 69 tab_keys::kStatusValueLoading);
68 70
69 if (contents->GetURL() != url_) { 71 if (contents->GetURL() != url_) {
70 url_ = contents->GetURL(); 72 url_ = contents->GetURL();
71 changed_properties->SetString(tab_keys::kUrlKey, url_.spec()); 73 changed_properties->SetString(tab_keys::kUrlKey, url_.spec());
72 } 74 }
73 75
74 return changed_properties; 76 return changed_properties;
75 } 77 }
76 78
77 void ExtensionBrowserEventRouter::Init() { 79 void BrowserEventRouter::Init() {
78 if (initialized_) 80 if (initialized_)
79 return; 81 return;
80 BrowserList::AddObserver(this); 82 BrowserList::AddObserver(this);
81 #if defined(TOOLKIT_VIEWS) 83 #if defined(TOOLKIT_VIEWS)
82 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this); 84 views::WidgetFocusManager::GetInstance()->AddFocusChangeListener(this);
83 #elif defined(TOOLKIT_GTK) 85 #elif defined(TOOLKIT_GTK)
84 ui::ActiveWindowWatcherX::AddObserver(this); 86 ui::ActiveWindowWatcherX::AddObserver(this);
85 #elif defined(OS_MACOSX) 87 #elif defined(OS_MACOSX)
86 // Needed for when no suitable window can be passed to an extension as the 88 // Needed for when no suitable window can be passed to an extension as the
87 // currently focused window. 89 // currently focused window.
(...skipping 15 matching lines...) Expand all
103 chrome::GetTabContentsAt(browser, i)->web_contents(); 105 chrome::GetTabContentsAt(browser, i)->web_contents();
104 int tab_id = ExtensionTabUtil::GetTabId(contents); 106 int tab_id = ExtensionTabUtil::GetTabId(contents);
105 tab_entries_[tab_id] = TabEntry(); 107 tab_entries_[tab_id] = TabEntry();
106 } 108 }
107 } 109 }
108 } 110 }
109 111
110 initialized_ = true; 112 initialized_ = true;
111 } 113 }
112 114
113 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter(Profile* profile) 115 BrowserEventRouter::BrowserEventRouter(Profile* profile)
114 : initialized_(false), 116 : initialized_(false),
115 profile_(profile), 117 profile_(profile),
116 focused_profile_(NULL), 118 focused_profile_(NULL),
117 focused_window_id_(extension_misc::kUnknownWindowId) { 119 focused_window_id_(extension_misc::kUnknownWindowId) {
118 DCHECK(!profile->IsOffTheRecord()); 120 DCHECK(!profile->IsOffTheRecord());
119 } 121 }
120 122
121 ExtensionBrowserEventRouter::~ExtensionBrowserEventRouter() { 123 BrowserEventRouter::~BrowserEventRouter() {
122 BrowserList::RemoveObserver(this); 124 BrowserList::RemoveObserver(this);
123 #if defined(TOOLKIT_VIEWS) 125 #if defined(TOOLKIT_VIEWS)
124 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this); 126 views::WidgetFocusManager::GetInstance()->RemoveFocusChangeListener(this);
125 #elif defined(TOOLKIT_GTK) 127 #elif defined(TOOLKIT_GTK)
126 ui::ActiveWindowWatcherX::RemoveObserver(this); 128 ui::ActiveWindowWatcherX::RemoveObserver(this);
127 #endif 129 #endif
128 } 130 }
129 131
130 void ExtensionBrowserEventRouter::OnBrowserAdded(Browser* browser) { 132 void BrowserEventRouter::OnBrowserAdded(Browser* browser) {
131 RegisterForBrowserNotifications(browser); 133 RegisterForBrowserNotifications(browser);
132 } 134 }
133 135
134 void ExtensionBrowserEventRouter::RegisterForBrowserNotifications( 136 void BrowserEventRouter::RegisterForBrowserNotifications(Browser* browser) {
135 Browser* browser) {
136 if (!profile_->IsSameProfile(browser->profile())) 137 if (!profile_->IsSameProfile(browser->profile()))
137 return; 138 return;
138 // Start listening to TabStripModel events for this browser. 139 // Start listening to TabStripModel events for this browser.
139 browser->tab_strip_model()->AddObserver(this); 140 browser->tab_strip_model()->AddObserver(this);
140 141
141 // If this is a new window, it isn't ready at this point, so we register to be 142 // If this is a new window, it isn't ready at this point, so we register to be
142 // notified when it is. If this is an existing window, this is a no-op that we 143 // notified when it is. If this is an existing window, this is a no-op that we
143 // just do to reduce code complexity. 144 // just do to reduce code complexity.
144 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, 145 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
145 content::Source<Browser>(browser)); 146 content::Source<Browser>(browser));
146 147
147 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { 148 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
148 RegisterForTabNotifications( 149 RegisterForTabNotifications(
149 chrome::GetTabContentsAt(browser, i)->web_contents()); 150 chrome::GetTabContentsAt(browser, i)->web_contents());
150 } 151 }
151 } 152 }
152 153
153 void ExtensionBrowserEventRouter::RegisterForTabNotifications( 154 void BrowserEventRouter::RegisterForTabNotifications(WebContents* contents) {
154 WebContents* contents) {
155 registrar_.Add( 155 registrar_.Add(
156 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 156 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
157 content::Source<NavigationController>(&contents->GetController())); 157 content::Source<NavigationController>(&contents->GetController()));
158 158
159 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's 159 // Observing NOTIFICATION_WEB_CONTENTS_DESTROYED is necessary because it's
160 // possible for tabs to be created, detached and then destroyed without 160 // possible for tabs to be created, detached and then destroyed without
161 // ever having been re-attached and closed. This happens in the case of 161 // ever having been re-attached and closed. This happens in the case of
162 // a devtools WebContents that is opened in window, docked, then closed. 162 // a devtools WebContents that is opened in window, docked, then closed.
163 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 163 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
164 content::Source<WebContents>(contents)); 164 content::Source<WebContents>(contents));
165 } 165 }
166 166
167 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( 167 void BrowserEventRouter::UnregisterForTabNotifications(WebContents* contents) {
168 WebContents* contents) {
169 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
170 content::Source<NavigationController>(&contents->GetController())); 169 content::Source<NavigationController>(&contents->GetController()));
171 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
172 content::Source<WebContents>(contents)); 171 content::Source<WebContents>(contents));
173 } 172 }
174 173
175 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { 174 void BrowserEventRouter::OnBrowserWindowReady(Browser* browser) {
176 ListValue args; 175 ListValue args;
177 176
178 DCHECK(browser->extension_window_controller()); 177 DCHECK(browser->extension_window_controller());
179 DictionaryValue* window_dictionary = 178 DictionaryValue* window_dictionary =
180 browser->extension_window_controller()->CreateWindowValue(); 179 browser->extension_window_controller()->CreateWindowValue();
181 args.Append(window_dictionary); 180 args.Append(window_dictionary);
182 181
183 std::string json_args; 182 std::string json_args;
184 base::JSONWriter::Write(&args, &json_args); 183 base::JSONWriter::Write(&args, &json_args);
185 184
186 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); 185 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args);
187 } 186 }
188 187
189 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { 188 void BrowserEventRouter::OnBrowserRemoved(Browser* browser) {
190 if (!profile_->IsSameProfile(browser->profile())) 189 if (!profile_->IsSameProfile(browser->profile()))
191 return; 190 return;
192 191
193 // Stop listening to TabStripModel events for this browser. 192 // Stop listening to TabStripModel events for this browser.
194 browser->tab_strip_model()->RemoveObserver(this); 193 browser->tab_strip_model()->RemoveObserver(this);
195 194
196 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, 195 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY,
197 content::Source<Browser>(browser)); 196 content::Source<Browser>(browser));
198 197
199 DispatchSimpleBrowserEvent(browser->profile(), 198 DispatchSimpleBrowserEvent(browser->profile(),
200 ExtensionTabUtil::GetWindowId(browser), 199 ExtensionTabUtil::GetWindowId(browser),
201 events::kOnWindowRemoved); 200 events::kOnWindowRemoved);
202 } 201 }
203 202
204 #if defined(TOOLKIT_VIEWS) 203 #if defined(TOOLKIT_VIEWS)
205 void ExtensionBrowserEventRouter::OnNativeFocusChange( 204 void BrowserEventRouter::OnNativeFocusChange(gfx::NativeView focused_before,
206 gfx::NativeView focused_before, 205 gfx::NativeView focused_now) {
207 gfx::NativeView focused_now) {
208 if (!focused_now) 206 if (!focused_now)
209 OnBrowserSetLastActive(NULL); 207 OnBrowserSetLastActive(NULL);
210 } 208 }
211 #elif defined(TOOLKIT_GTK) 209 #elif defined(TOOLKIT_GTK)
212 void ExtensionBrowserEventRouter::ActiveWindowChanged( 210 void BrowserEventRouter::ActiveWindowChanged(GdkWindow* active_window) {
213 GdkWindow* active_window) {
214 if (!active_window) 211 if (!active_window)
215 OnBrowserSetLastActive(NULL); 212 OnBrowserSetLastActive(NULL);
216 } 213 }
217 #endif 214 #endif
218 215
219 void ExtensionBrowserEventRouter::OnBrowserSetLastActive( 216 void BrowserEventRouter::OnBrowserSetLastActive(Browser* browser) {
220 Browser* browser) {
221 Profile* window_profile = NULL; 217 Profile* window_profile = NULL;
222 int window_id = extension_misc::kUnknownWindowId; 218 int window_id = extension_misc::kUnknownWindowId;
223 if (browser && profile_->IsSameProfile(browser->profile())) { 219 if (browser && profile_->IsSameProfile(browser->profile())) {
224 window_profile = browser->profile(); 220 window_profile = browser->profile();
225 window_id = ExtensionTabUtil::GetWindowId(browser); 221 window_id = ExtensionTabUtil::GetWindowId(browser);
226 } 222 }
227 223
228 if (focused_window_id_ == window_id) 224 if (focused_window_id_ == window_id)
229 return; 225 return;
230 226
(...skipping 21 matching lines...) Expand all
252 base::JSONWriter::Write(&none_args, &none_json_args); 248 base::JSONWriter::Write(&none_args, &none_json_args);
253 } 249 }
254 250
255 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : 251 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ :
256 previous_focused_profile), 252 previous_focused_profile),
257 events::kOnWindowFocusedChanged, 253 events::kOnWindowFocusedChanged,
258 real_json_args, 254 real_json_args,
259 none_json_args); 255 none_json_args);
260 } 256 }
261 257
262 void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, 258 void BrowserEventRouter::TabCreatedAt(WebContents* contents,
263 int index, 259 int index,
264 bool active) { 260 bool active) {
265 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 261 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
266 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active); 262 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active);
267 263
268 RegisterForTabNotifications(contents); 264 RegisterForTabNotifications(contents);
269 } 265 }
270 266
271 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, 267 void BrowserEventRouter::TabInsertedAt(TabContents* contents,
272 int index, 268 int index,
273 bool active) { 269 bool active) {
274 // If tab is new, send created event. 270 // If tab is new, send created event.
275 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); 271 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents());
276 if (!GetTabEntry(contents->web_contents())) { 272 if (!GetTabEntry(contents->web_contents())) {
277 tab_entries_[tab_id] = TabEntry(); 273 tab_entries_[tab_id] = TabEntry();
278 274
279 TabCreatedAt(contents->web_contents(), index, active); 275 TabCreatedAt(contents->web_contents(), index, active);
280 return; 276 return;
281 } 277 }
282 278
283 ListValue args; 279 ListValue args;
284 args.Append(Value::CreateIntegerValue(tab_id)); 280 args.Append(Value::CreateIntegerValue(tab_id));
285 281
286 DictionaryValue* object_args = new DictionaryValue(); 282 DictionaryValue* object_args = new DictionaryValue();
287 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( 283 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue(
288 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); 284 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents())));
289 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( 285 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue(
290 index)); 286 index));
291 args.Append(object_args); 287 args.Append(object_args);
292 288
293 std::string json_args; 289 std::string json_args;
294 base::JSONWriter::Write(&args, &json_args); 290 base::JSONWriter::Write(&args, &json_args);
295 291
296 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); 292 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args);
297 } 293 }
298 294
299 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, 295 void BrowserEventRouter::TabDetachedAt(TabContents* contents, int index) {
300 int index) {
301 if (!GetTabEntry(contents->web_contents())) { 296 if (!GetTabEntry(contents->web_contents())) {
302 // The tab was removed. Don't send detach event. 297 // The tab was removed. Don't send detach event.
303 return; 298 return;
304 } 299 }
305 300
306 ListValue args; 301 ListValue args;
307 args.Append(Value::CreateIntegerValue( 302 args.Append(Value::CreateIntegerValue(
308 ExtensionTabUtil::GetTabId(contents->web_contents()))); 303 ExtensionTabUtil::GetTabId(contents->web_contents())));
309 304
310 DictionaryValue* object_args = new DictionaryValue(); 305 DictionaryValue* object_args = new DictionaryValue();
311 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( 306 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue(
312 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); 307 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents())));
313 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( 308 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue(
314 index)); 309 index));
315 args.Append(object_args); 310 args.Append(object_args);
316 311
317 std::string json_args; 312 std::string json_args;
318 base::JSONWriter::Write(&args, &json_args); 313 base::JSONWriter::Write(&args, &json_args);
319 314
320 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); 315 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args);
321 } 316 }
322 317
323 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, 318 void BrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model,
324 TabContents* contents, 319 TabContents* contents,
325 int index) { 320 int index) {
326 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); 321 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents());
327 322
328 ListValue args; 323 ListValue args;
329 args.Append(Value::CreateIntegerValue(tab_id)); 324 args.Append(Value::CreateIntegerValue(tab_id));
330 325
331 DictionaryValue* object_args = new DictionaryValue(); 326 DictionaryValue* object_args = new DictionaryValue();
332 object_args->SetBoolean(tab_keys::kWindowClosing, 327 object_args->SetBoolean(tab_keys::kWindowClosing,
333 tab_strip_model->closing_all()); 328 tab_strip_model->closing_all());
334 args.Append(object_args); 329 args.Append(object_args);
335 330
336 std::string json_args; 331 std::string json_args;
337 base::JSONWriter::Write(&args, &json_args); 332 base::JSONWriter::Write(&args, &json_args);
338 333
339 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); 334 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args);
340 335
341 int removed_count = tab_entries_.erase(tab_id); 336 int removed_count = tab_entries_.erase(tab_id);
342 DCHECK_GT(removed_count, 0); 337 DCHECK_GT(removed_count, 0);
343 338
344 UnregisterForTabNotifications(contents->web_contents()); 339 UnregisterForTabNotifications(contents->web_contents());
345 } 340 }
346 341
347 void ExtensionBrowserEventRouter::ActiveTabChanged( 342 void BrowserEventRouter::ActiveTabChanged(TabContents* old_contents,
348 TabContents* old_contents, 343 TabContents* new_contents,
349 TabContents* new_contents, 344 int index,
350 int index, 345 bool user_gesture) {
351 bool user_gesture) {
352 ListValue args; 346 ListValue args;
353 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); 347 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents());
354 args.Append(Value::CreateIntegerValue(tab_id)); 348 args.Append(Value::CreateIntegerValue(tab_id));
355 349
356 DictionaryValue* object_args = new DictionaryValue(); 350 DictionaryValue* object_args = new DictionaryValue();
357 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( 351 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue(
358 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); 352 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents())));
359 args.Append(object_args); 353 args.Append(object_args);
360 354
361 // The onActivated event replaced onActiveChanged and onSelectionChanged. The 355 // The onActivated event replaced onActiveChanged and onSelectionChanged. The
362 // deprecated events take two arguments: tabId, {windowId}. 356 // deprecated events take two arguments: tabId, {windowId}.
363 std::string old_json_args; 357 std::string old_json_args;
364 base::JSONWriter::Write(&args, &old_json_args); 358 base::JSONWriter::Write(&args, &old_json_args);
365 359
366 // The onActivated event takes one argument: {windowId, tabId}. 360 // The onActivated event takes one argument: {windowId, tabId}.
367 std::string new_json_args; 361 std::string new_json_args;
368 args.Remove(0, NULL); 362 args.Remove(0, NULL);
369 object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); 363 object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id));
370 base::JSONWriter::Write(&args, &new_json_args); 364 base::JSONWriter::Write(&args, &new_json_args);
371 365
372 Profile* profile = new_contents->profile(); 366 Profile* profile = new_contents->profile();
373 DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args); 367 DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args);
374 DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args); 368 DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args);
375 DispatchEvent(profile, events::kOnTabActivated, new_json_args); 369 DispatchEvent(profile, events::kOnTabActivated, new_json_args);
376 } 370 }
377 371
378 void ExtensionBrowserEventRouter::TabSelectionChanged( 372 void BrowserEventRouter::TabSelectionChanged(
379 TabStripModel* tab_strip_model, 373 TabStripModel* tab_strip_model,
380 const TabStripSelectionModel& old_model) { 374 const TabStripSelectionModel& old_model) {
381 TabStripSelectionModel::SelectedIndices new_selection = 375 TabStripSelectionModel::SelectedIndices new_selection =
382 tab_strip_model->selection_model().selected_indices(); 376 tab_strip_model->selection_model().selected_indices();
383 ListValue* all = new ListValue(); 377 ListValue* all = new ListValue();
384 378
385 for (size_t i = 0; i < new_selection.size(); ++i) { 379 for (size_t i = 0; i < new_selection.size(); ++i) {
386 int index = new_selection[i]; 380 int index = new_selection[i];
387 TabContents* contents = tab_strip_model->GetTabContentsAt(index); 381 TabContents* contents = tab_strip_model->GetTabContentsAt(index);
388 if (!contents) 382 if (!contents)
(...skipping 13 matching lines...) Expand all
402 396
403 std::string json_args; 397 std::string json_args;
404 base::JSONWriter::Write(&args, &json_args); 398 base::JSONWriter::Write(&args, &json_args);
405 399
406 // The onHighlighted event replaced onHighlightChanged. 400 // The onHighlighted event replaced onHighlightChanged.
407 Profile* profile = tab_strip_model->profile(); 401 Profile* profile = tab_strip_model->profile();
408 DispatchEvent(profile, events::kOnTabHighlightChanged, json_args); 402 DispatchEvent(profile, events::kOnTabHighlightChanged, json_args);
409 DispatchEvent(profile, events::kOnTabHighlighted, json_args); 403 DispatchEvent(profile, events::kOnTabHighlighted, json_args);
410 } 404 }
411 405
412 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, 406 void BrowserEventRouter::TabMoved(TabContents* contents,
413 int from_index, 407 int from_index,
414 int to_index) { 408 int to_index) {
415 ListValue args; 409 ListValue args;
416 args.Append(Value::CreateIntegerValue( 410 args.Append(Value::CreateIntegerValue(
417 ExtensionTabUtil::GetTabId(contents->web_contents()))); 411 ExtensionTabUtil::GetTabId(contents->web_contents())));
418 412
419 DictionaryValue* object_args = new DictionaryValue(); 413 DictionaryValue* object_args = new DictionaryValue();
420 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( 414 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue(
421 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); 415 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents())));
422 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( 416 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue(
423 from_index)); 417 from_index));
424 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( 418 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue(
425 to_index)); 419 to_index));
426 args.Append(object_args); 420 args.Append(object_args);
427 421
428 std::string json_args; 422 std::string json_args;
429 base::JSONWriter::Write(&args, &json_args); 423 base::JSONWriter::Write(&args, &json_args);
430 424
431 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); 425 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args);
432 } 426 }
433 427
434 void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, 428 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) {
435 bool did_navigate) {
436 TabEntry* entry = GetTabEntry(contents); 429 TabEntry* entry = GetTabEntry(contents);
437 DictionaryValue* changed_properties = NULL; 430 DictionaryValue* changed_properties = NULL;
438 431
439 DCHECK(entry); 432 DCHECK(entry);
440 433
441 if (did_navigate) 434 if (did_navigate)
442 changed_properties = entry->DidNavigate(contents); 435 changed_properties = entry->DidNavigate(contents);
443 else 436 else
444 changed_properties = entry->UpdateLoadState(contents); 437 changed_properties = entry->UpdateLoadState(contents);
445 438
446 if (changed_properties) 439 if (changed_properties)
447 DispatchTabUpdatedEvent(contents, changed_properties); 440 DispatchTabUpdatedEvent(contents, changed_properties);
448 } 441 }
449 442
450 void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, 443 void BrowserEventRouter::DispatchEvent(Profile* profile,
451 const char* event_name, 444 const char* event_name,
452 const std::string& json_args) { 445 const std::string& json_args) {
453 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) 446 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter())
454 return; 447 return;
455 448
456 profile->GetExtensionEventRouter()->DispatchEventToRenderers( 449 profile->GetExtensionEventRouter()->DispatchEventToRenderers(
457 event_name, json_args, profile, GURL(), extensions::EventFilteringInfo()); 450 event_name, json_args, profile, GURL(), EventFilteringInfo());
458 } 451 }
459 452
460 void ExtensionBrowserEventRouter::DispatchEventToExtension( 453 void BrowserEventRouter::DispatchEventToExtension(
461 Profile* profile, 454 Profile* profile,
462 const std::string& extension_id, 455 const std::string& extension_id,
463 const char* event_name, 456 const char* event_name,
464 const std::string& json_args) { 457 const std::string& json_args) {
465 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) 458 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter())
466 return; 459 return;
467 460
468 profile->GetExtensionEventRouter()->DispatchEventToExtension( 461 profile->GetExtensionEventRouter()->DispatchEventToExtension(
469 extension_id, event_name, json_args, profile, GURL()); 462 extension_id, event_name, json_args, profile, GURL());
470 } 463 }
471 464
472 void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( 465 void BrowserEventRouter::DispatchEventsAcrossIncognito(
473 Profile* profile, 466 Profile* profile,
474 const char* event_name, 467 const char* event_name,
475 const std::string& json_args, 468 const std::string& json_args,
476 const std::string& cross_incognito_args) { 469 const std::string& cross_incognito_args) {
477 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) 470 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter())
478 return; 471 return;
479 472
480 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( 473 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito(
481 event_name, json_args, profile, cross_incognito_args, GURL()); 474 event_name, json_args, profile, cross_incognito_args, GURL());
482 } 475 }
483 476
484 void ExtensionBrowserEventRouter::DispatchEventWithTab( 477 void BrowserEventRouter::DispatchEventWithTab(Profile* profile,
485 Profile* profile, 478 const std::string& extension_id,
486 const std::string& extension_id, 479 const char* event_name,
487 const char* event_name, 480 const WebContents* web_contents,
488 const WebContents* web_contents, 481 bool active) {
489 bool active) {
490 if (!profile_->IsSameProfile(profile)) 482 if (!profile_->IsSameProfile(profile))
491 return; 483 return;
492 484
493 ListValue args; 485 ListValue args;
494 args.Append(ExtensionTabUtil::CreateTabValueActive( 486 args.Append(ExtensionTabUtil::CreateTabValueActive(
495 web_contents, active)); 487 web_contents, active));
496 std::string json_args; 488 std::string json_args;
497 base::JSONWriter::Write(&args, &json_args); 489 base::JSONWriter::Write(&args, &json_args);
498 if (!extension_id.empty()) { 490 if (!extension_id.empty()) {
499 DispatchEventToExtension(profile, extension_id, event_name, json_args); 491 DispatchEventToExtension(profile, extension_id, event_name, json_args);
500 } else { 492 } else {
501 DispatchEvent(profile, event_name, json_args); 493 DispatchEvent(profile, event_name, json_args);
502 } 494 }
503 } 495 }
504 496
505 void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( 497 void BrowserEventRouter::DispatchSimpleBrowserEvent(
506 Profile* profile, const int window_id, const char* event_name) { 498 Profile* profile, const int window_id, const char* event_name) {
507 if (!profile_->IsSameProfile(profile)) 499 if (!profile_->IsSameProfile(profile))
508 return; 500 return;
509 501
510 ListValue args; 502 ListValue args;
511 args.Append(Value::CreateIntegerValue(window_id)); 503 args.Append(Value::CreateIntegerValue(window_id));
512 504
513 std::string json_args; 505 std::string json_args;
514 base::JSONWriter::Write(&args, &json_args); 506 base::JSONWriter::Write(&args, &json_args);
515 507
516 DispatchEvent(profile, event_name, json_args); 508 DispatchEvent(profile, event_name, json_args);
517 } 509 }
518 510
519 void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( 511 void BrowserEventRouter::DispatchTabUpdatedEvent(
520 WebContents* contents, DictionaryValue* changed_properties) { 512 WebContents* contents, DictionaryValue* changed_properties) {
521 DCHECK(changed_properties); 513 DCHECK(changed_properties);
522 DCHECK(contents); 514 DCHECK(contents);
523 515
524 // The state of the tab (as seen from the extension point of view) has 516 // The state of the tab (as seen from the extension point of view) has
525 // changed. Send a notification to the extension. 517 // changed. Send a notification to the extension.
526 ListValue args; 518 ListValue args;
527 519
528 // First arg: The id of the tab that changed. 520 // First arg: The id of the tab that changed.
529 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); 521 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents)));
530 522
531 // Second arg: An object containing the changes to the tab state. 523 // Second arg: An object containing the changes to the tab state.
532 args.Append(changed_properties); 524 args.Append(changed_properties);
533 525
534 // Third arg: An object containing the state of the tab. 526 // Third arg: An object containing the state of the tab.
535 args.Append(ExtensionTabUtil::CreateTabValue(contents)); 527 args.Append(ExtensionTabUtil::CreateTabValue(contents));
536 528
537 std::string json_args; 529 std::string json_args;
538 base::JSONWriter::Write(&args, &json_args); 530 base::JSONWriter::Write(&args, &json_args);
539 531
540 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 532 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
541 DispatchEvent(profile, events::kOnTabUpdated, json_args); 533 DispatchEvent(profile, events::kOnTabUpdated, json_args);
542 } 534 }
543 535
544 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( 536 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry(
545 const WebContents* contents) { 537 const WebContents* contents) {
546 int tab_id = ExtensionTabUtil::GetTabId(contents); 538 int tab_id = ExtensionTabUtil::GetTabId(contents);
547 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); 539 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id);
548 if (tab_entries_.end() == i) 540 if (tab_entries_.end() == i)
549 return NULL; 541 return NULL;
550 return &i->second; 542 return &i->second;
551 } 543 }
552 544
553 void ExtensionBrowserEventRouter::Observe( 545 void BrowserEventRouter::Observe(int type,
554 int type, 546 const content::NotificationSource& source,
555 const content::NotificationSource& source, 547 const content::NotificationDetails& details) {
556 const content::NotificationDetails& details) {
557 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { 548 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
558 NavigationController* source_controller = 549 NavigationController* source_controller =
559 content::Source<NavigationController>(source).ptr(); 550 content::Source<NavigationController>(source).ptr();
560 TabUpdated(source_controller->GetWebContents(), true); 551 TabUpdated(source_controller->GetWebContents(), true);
561 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { 552 } else if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) {
562 // Tab was destroyed after being detached (without being re-attached). 553 // Tab was destroyed after being detached (without being re-attached).
563 WebContents* contents = content::Source<WebContents>(source).ptr(); 554 WebContents* contents = content::Source<WebContents>(source).ptr();
564 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 555 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
565 content::Source<NavigationController>(&contents->GetController())); 556 content::Source<NavigationController>(&contents->GetController()));
566 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 557 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
567 content::Source<WebContents>(contents)); 558 content::Source<WebContents>(contents));
568 } else if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) { 559 } else if (type == chrome::NOTIFICATION_BROWSER_WINDOW_READY) {
569 Browser* browser = content::Source<Browser>(source).ptr(); 560 Browser* browser = content::Source<Browser>(source).ptr();
570 OnBrowserWindowReady(browser); 561 OnBrowserWindowReady(browser);
571 #if defined(OS_MACOSX) 562 #if defined(OS_MACOSX)
572 } else if (type == chrome::NOTIFICATION_NO_KEY_WINDOW) { 563 } else if (type == chrome::NOTIFICATION_NO_KEY_WINDOW) {
573 OnBrowserSetLastActive(NULL); 564 OnBrowserSetLastActive(NULL);
574 #endif 565 #endif
575 } else { 566 } else {
576 NOTREACHED(); 567 NOTREACHED();
577 } 568 }
578 } 569 }
579 570
580 void ExtensionBrowserEventRouter::TabChangedAt(TabContents* contents, 571 void BrowserEventRouter::TabChangedAt(TabContents* contents,
581 int index, 572 int index,
582 TabChangeType change_type) { 573 TabChangeType change_type) {
583 TabUpdated(contents->web_contents(), false); 574 TabUpdated(contents->web_contents(), false);
584 } 575 }
585 576
586 void ExtensionBrowserEventRouter::TabReplacedAt( 577 void BrowserEventRouter::TabReplacedAt(TabStripModel* tab_strip_model,
587 TabStripModel* tab_strip_model, 578 TabContents* old_contents,
588 TabContents* old_contents, 579 TabContents* new_contents,
589 TabContents* new_contents, 580 int index) {
590 int index) {
591 TabClosingAt(tab_strip_model, old_contents, index); 581 TabClosingAt(tab_strip_model, old_contents, index);
592 TabInsertedAt(new_contents, index, tab_strip_model->active_index() == index); 582 TabInsertedAt(new_contents, index, tab_strip_model->active_index() == index);
593 } 583 }
594 584
595 void ExtensionBrowserEventRouter::TabPinnedStateChanged( 585 void BrowserEventRouter::TabPinnedStateChanged(TabContents* contents,
596 TabContents* contents, 586 int index) {
597 int index) {
598 TabStripModel* tab_strip = NULL; 587 TabStripModel* tab_strip = NULL;
599 int tab_index; 588 int tab_index;
600 589
601 if (ExtensionTabUtil::GetTabStripModel( 590 if (ExtensionTabUtil::GetTabStripModel(
602 contents->web_contents(), &tab_strip, &tab_index)) { 591 contents->web_contents(), &tab_strip, &tab_index)) {
603 DictionaryValue* changed_properties = new DictionaryValue(); 592 DictionaryValue* changed_properties = new DictionaryValue();
604 changed_properties->SetBoolean(tab_keys::kPinnedKey, 593 changed_properties->SetBoolean(tab_keys::kPinnedKey,
605 tab_strip->IsTabPinned(tab_index)); 594 tab_strip->IsTabPinned(tab_index));
606 DispatchTabUpdatedEvent(contents->web_contents(), changed_properties); 595 DispatchTabUpdatedEvent(contents->web_contents(), changed_properties);
607 } 596 }
608 } 597 }
609 598
610 void ExtensionBrowserEventRouter::TabStripEmpty() {} 599 void BrowserEventRouter::TabStripEmpty() {}
611 600
612 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( 601 void BrowserEventRouter::DispatchOldPageActionEvent(
613 Profile* profile, 602 Profile* profile,
614 const std::string& extension_id, 603 const std::string& extension_id,
615 const std::string& page_action_id, 604 const std::string& page_action_id,
616 int tab_id, 605 int tab_id,
617 const std::string& url, 606 const std::string& url,
618 int button) { 607 int button) {
619 ListValue args; 608 ListValue args;
620 args.Append(Value::CreateStringValue(page_action_id)); 609 args.Append(Value::CreateStringValue(page_action_id));
621 610
622 DictionaryValue* data = new DictionaryValue(); 611 DictionaryValue* data = new DictionaryValue();
623 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); 612 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id));
624 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); 613 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url));
625 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); 614 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button));
626 args.Append(data); 615 args.Append(data);
627 616
628 std::string json_args; 617 std::string json_args;
629 base::JSONWriter::Write(&args, &json_args); 618 base::JSONWriter::Write(&args, &json_args);
630 619
631 DispatchEventToExtension(profile, extension_id, "pageActions", json_args); 620 DispatchEventToExtension(profile, extension_id, "pageActions", json_args);
632 } 621 }
633 622
634 void ExtensionBrowserEventRouter::BrowserActionExecuted( 623 void BrowserEventRouter::BrowserActionExecuted(
635 const ExtensionAction& browser_action, 624 const ExtensionAction& browser_action,
636 Browser* browser) { 625 Browser* browser) {
637 Profile* profile = browser->profile(); 626 Profile* profile = browser->profile();
638 TabContents* tab_contents = NULL; 627 TabContents* tab_contents = NULL;
639 int tab_id = 0; 628 int tab_id = 0;
640 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) 629 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id))
641 return; 630 return;
642 ExtensionActionExecuted(profile, browser_action, tab_contents); 631 ExtensionActionExecuted(profile, browser_action, tab_contents);
643 } 632 }
644 633
645 void ExtensionBrowserEventRouter::PageActionExecuted( 634 void BrowserEventRouter::PageActionExecuted(Profile* profile,
646 Profile* profile, 635 const ExtensionAction& page_action,
647 const ExtensionAction& page_action, 636 int tab_id,
648 int tab_id, 637 const std::string& url,
649 const std::string& url, 638 int button) {
650 int button) {
651 DispatchOldPageActionEvent(profile, page_action.extension_id(), 639 DispatchOldPageActionEvent(profile, page_action.extension_id(),
652 page_action.id(), tab_id, url, button); 640 page_action.id(), tab_id, url, button);
653 TabContents* tab_contents = NULL; 641 TabContents* tab_contents = NULL;
654 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), 642 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(),
655 NULL, NULL, &tab_contents, NULL)) { 643 NULL, NULL, &tab_contents, NULL)) {
656 return; 644 return;
657 } 645 }
658 ExtensionActionExecuted(profile, page_action, tab_contents); 646 ExtensionActionExecuted(profile, page_action, tab_contents);
659 } 647 }
660 648
661 void ExtensionBrowserEventRouter::ScriptBadgeExecuted( 649 void BrowserEventRouter::ScriptBadgeExecuted(
662 Profile* profile, 650 Profile* profile,
663 const ExtensionAction& script_badge, 651 const ExtensionAction& script_badge,
664 int tab_id) { 652 int tab_id) {
665 TabContents* tab_contents = NULL; 653 TabContents* tab_contents = NULL;
666 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(), 654 if (!ExtensionTabUtil::GetTabById(tab_id, profile, profile->IsOffTheRecord(),
667 NULL, NULL, &tab_contents, NULL)) { 655 NULL, NULL, &tab_contents, NULL)) {
668 return; 656 return;
669 } 657 }
670 ExtensionActionExecuted(profile, script_badge, tab_contents); 658 ExtensionActionExecuted(profile, script_badge, tab_contents);
671 } 659 }
672 660
673 void ExtensionBrowserEventRouter::CommandExecuted( 661 void BrowserEventRouter::CommandExecuted(Profile* profile,
674 Profile* profile, 662 const std::string& extension_id,
675 const std::string& extension_id, 663 const std::string& command) {
676 const std::string& command) {
677 ListValue args; 664 ListValue args;
678 args.Append(Value::CreateStringValue(command)); 665 args.Append(Value::CreateStringValue(command));
679 std::string json_args; 666 std::string json_args;
680 base::JSONWriter::Write(&args, &json_args); 667 base::JSONWriter::Write(&args, &json_args);
681 668
682 DispatchEventToExtension(profile, 669 DispatchEventToExtension(profile,
683 extension_id, 670 extension_id,
684 "experimental.keybinding.onCommand", 671 "experimental.keybinding.onCommand",
685 json_args); 672 json_args);
686 } 673 }
687 674
688 void ExtensionBrowserEventRouter::ExtensionActionExecuted( 675 void BrowserEventRouter::ExtensionActionExecuted(
689 Profile* profile, 676 Profile* profile,
690 const ExtensionAction& extension_action, 677 const ExtensionAction& extension_action,
691 TabContents* tab_contents) { 678 TabContents* tab_contents) {
692 const char* event_name = NULL; 679 const char* event_name = NULL;
693 switch (extension_action.action_type()) { 680 switch (extension_action.action_type()) {
694 case ExtensionAction::TYPE_BROWSER: 681 case ExtensionAction::TYPE_BROWSER:
695 event_name = "browserAction.onClicked"; 682 event_name = "browserAction.onClicked";
696 break; 683 break;
697 case ExtensionAction::TYPE_PAGE: 684 case ExtensionAction::TYPE_PAGE:
698 event_name = "pageAction.onClicked"; 685 event_name = "pageAction.onClicked";
699 break; 686 break;
700 case ExtensionAction::TYPE_SCRIPT_BADGE: 687 case ExtensionAction::TYPE_SCRIPT_BADGE:
701 event_name = "scriptBadge.onClicked"; 688 event_name = "scriptBadge.onClicked";
702 break; 689 break;
703 } 690 }
704 691
705 if (event_name) { 692 if (event_name) {
706 DispatchEventWithTab(profile, 693 DispatchEventWithTab(profile,
707 extension_action.extension_id(), 694 extension_action.extension_id(),
708 event_name, 695 event_name,
709 tab_contents->web_contents(), 696 tab_contents->web_contents(),
710 true); 697 true);
711 } 698 }
712 } 699 }
700
701 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/browser_event_router.h ('k') | chrome/browser/extensions/event_listener_map.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698