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 #include "chrome/browser/extensions/extension_browser_event_router.h" | 5 #include "chrome/browser/extensions/extension_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" |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 165 |
166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( | 166 void ExtensionBrowserEventRouter::UnregisterForTabNotifications( |
167 WebContents* contents) { | 167 WebContents* contents) { |
168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 168 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
169 content::Source<NavigationController>(&contents->GetController())); | 169 content::Source<NavigationController>(&contents->GetController())); |
170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 170 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
171 content::Source<WebContents>(contents)); | 171 content::Source<WebContents>(contents)); |
172 } | 172 } |
173 | 173 |
174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { | 174 void ExtensionBrowserEventRouter::OnBrowserWindowReady(Browser* browser) { |
175 ListValue args; | 175 ListValue* args = new ListValue(); |
176 | 176 |
177 DCHECK(browser->extension_window_controller()); | 177 DCHECK(browser->extension_window_controller()); |
178 DictionaryValue* window_dictionary = | 178 DictionaryValue* window_dictionary = |
179 browser->extension_window_controller()->CreateWindowValue(); | 179 browser->extension_window_controller()->CreateWindowValue(); |
180 args.Append(window_dictionary); | 180 args->Append(window_dictionary); |
181 | 181 |
182 std::string json_args; | 182 DispatchEvent(browser->profile(), events::kOnWindowCreated, args); |
183 base::JSONWriter::Write(&args, &json_args); | |
184 | |
185 DispatchEvent(browser->profile(), events::kOnWindowCreated, json_args); | |
186 } | 183 } |
187 | 184 |
188 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { | 185 void ExtensionBrowserEventRouter::OnBrowserRemoved(Browser* browser) { |
189 if (!profile_->IsSameProfile(browser->profile())) | 186 if (!profile_->IsSameProfile(browser->profile())) |
190 return; | 187 return; |
191 | 188 |
192 // Stop listening to TabStripModel events for this browser. | 189 // Stop listening to TabStripModel events for this browser. |
193 browser->tab_strip_model()->RemoveObserver(this); | 190 browser->tab_strip_model()->RemoveObserver(this); |
194 | 191 |
195 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, | 192 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_WINDOW_READY, |
(...skipping 30 matching lines...) Expand all Loading... |
226 | 223 |
227 if (focused_window_id_ == window_id) | 224 if (focused_window_id_ == window_id) |
228 return; | 225 return; |
229 | 226 |
230 // window_profile is either this profile's default profile, its | 227 // window_profile is either this profile's default profile, its |
231 // incognito profile, or NULL iff this profile is losing focus. | 228 // incognito profile, or NULL iff this profile is losing focus. |
232 Profile* previous_focused_profile = focused_profile_; | 229 Profile* previous_focused_profile = focused_profile_; |
233 focused_profile_ = window_profile; | 230 focused_profile_ = window_profile; |
234 focused_window_id_ = window_id; | 231 focused_window_id_ = window_id; |
235 | 232 |
236 ListValue real_args; | 233 ListValue* real_args = new ListValue(); |
237 real_args.Append(Value::CreateIntegerValue(window_id)); | 234 real_args->Append(Value::CreateIntegerValue(window_id)); |
238 std::string real_json_args; | |
239 base::JSONWriter::Write(&real_args, &real_json_args); | |
240 | 235 |
241 // When switching between windows in the default and incognitoi profiles, | 236 // When switching between windows in the default and incognitoi profiles, |
242 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that | 237 // dispatch WINDOW_ID_NONE to extensions whose profile lost focus that |
243 // can't see the new focused window across the incognito boundary. | 238 // can't see the new focused window across the incognito boundary. |
244 // See crbug.com/46610. | 239 // See crbug.com/46610. |
245 std::string none_json_args; | 240 ListValue* none_args = new ListValue(); |
246 if (focused_profile_ != NULL && previous_focused_profile != NULL && | 241 if (focused_profile_ != NULL && previous_focused_profile != NULL && |
247 focused_profile_ != previous_focused_profile) { | 242 focused_profile_ != previous_focused_profile) { |
248 ListValue none_args; | 243 none_args->Append( |
249 none_args.Append( | |
250 Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); | 244 Value::CreateIntegerValue(extension_misc::kUnknownWindowId)); |
251 base::JSONWriter::Write(&none_args, &none_json_args); | |
252 } | 245 } |
253 | 246 |
254 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : | 247 DispatchEventsAcrossIncognito((focused_profile_ ? focused_profile_ : |
255 previous_focused_profile), | 248 previous_focused_profile), |
256 events::kOnWindowFocusedChanged, | 249 events::kOnWindowFocusedChanged, |
257 real_json_args, | 250 real_args, |
258 none_json_args); | 251 none_args); |
259 } | 252 } |
260 | 253 |
261 void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, | 254 void ExtensionBrowserEventRouter::TabCreatedAt(WebContents* contents, |
262 int index, | 255 int index, |
263 bool active) { | 256 bool active) { |
264 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 257 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
265 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active); | 258 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active); |
266 | 259 |
267 RegisterForTabNotifications(contents); | 260 RegisterForTabNotifications(contents); |
268 } | 261 } |
269 | 262 |
270 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, | 263 void ExtensionBrowserEventRouter::TabInsertedAt(TabContents* contents, |
271 int index, | 264 int index, |
272 bool active) { | 265 bool active) { |
273 // If tab is new, send created event. | 266 // If tab is new, send created event. |
274 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 267 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
275 if (!GetTabEntry(contents->web_contents())) { | 268 if (!GetTabEntry(contents->web_contents())) { |
276 tab_entries_[tab_id] = TabEntry(); | 269 tab_entries_[tab_id] = TabEntry(); |
277 | 270 |
278 TabCreatedAt(contents->web_contents(), index, active); | 271 TabCreatedAt(contents->web_contents(), index, active); |
279 return; | 272 return; |
280 } | 273 } |
281 | 274 |
282 ListValue args; | 275 ListValue* args = new ListValue(); |
283 args.Append(Value::CreateIntegerValue(tab_id)); | 276 args->Append(Value::CreateIntegerValue(tab_id)); |
284 | 277 |
285 DictionaryValue* object_args = new DictionaryValue(); | 278 DictionaryValue* object_args = new DictionaryValue(); |
286 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( | 279 object_args->Set(tab_keys::kNewWindowIdKey, Value::CreateIntegerValue( |
287 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 280 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
288 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( | 281 object_args->Set(tab_keys::kNewPositionKey, Value::CreateIntegerValue( |
289 index)); | 282 index)); |
290 args.Append(object_args); | 283 args->Append(object_args); |
291 | 284 |
292 std::string json_args; | 285 DispatchEvent(contents->profile(), events::kOnTabAttached, args); |
293 base::JSONWriter::Write(&args, &json_args); | |
294 | |
295 DispatchEvent(contents->profile(), events::kOnTabAttached, json_args); | |
296 } | 286 } |
297 | 287 |
298 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, | 288 void ExtensionBrowserEventRouter::TabDetachedAt(TabContents* contents, |
299 int index) { | 289 int index) { |
300 if (!GetTabEntry(contents->web_contents())) { | 290 if (!GetTabEntry(contents->web_contents())) { |
301 // The tab was removed. Don't send detach event. | 291 // The tab was removed. Don't send detach event. |
302 return; | 292 return; |
303 } | 293 } |
304 | 294 |
305 ListValue args; | 295 ListValue* args = new ListValue(); |
306 args.Append(Value::CreateIntegerValue( | 296 args->Append(Value::CreateIntegerValue( |
307 ExtensionTabUtil::GetTabId(contents->web_contents()))); | 297 ExtensionTabUtil::GetTabId(contents->web_contents()))); |
308 | 298 |
309 DictionaryValue* object_args = new DictionaryValue(); | 299 DictionaryValue* object_args = new DictionaryValue(); |
310 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( | 300 object_args->Set(tab_keys::kOldWindowIdKey, Value::CreateIntegerValue( |
311 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 301 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
312 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( | 302 object_args->Set(tab_keys::kOldPositionKey, Value::CreateIntegerValue( |
313 index)); | 303 index)); |
314 args.Append(object_args); | 304 args->Append(object_args); |
315 | 305 |
316 std::string json_args; | 306 DispatchEvent(contents->profile(), events::kOnTabDetached, args); |
317 base::JSONWriter::Write(&args, &json_args); | |
318 | |
319 DispatchEvent(contents->profile(), events::kOnTabDetached, json_args); | |
320 } | 307 } |
321 | 308 |
322 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, | 309 void ExtensionBrowserEventRouter::TabClosingAt(TabStripModel* tab_strip_model, |
323 TabContents* contents, | 310 TabContents* contents, |
324 int index) { | 311 int index) { |
325 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 312 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
326 | 313 |
327 ListValue args; | 314 ListValue* args = new ListValue(); |
328 args.Append(Value::CreateIntegerValue(tab_id)); | 315 args->Append(Value::CreateIntegerValue(tab_id)); |
329 | 316 |
330 DictionaryValue* object_args = new DictionaryValue(); | 317 DictionaryValue* object_args = new DictionaryValue(); |
331 object_args->SetBoolean(tab_keys::kWindowClosing, | 318 object_args->SetBoolean(tab_keys::kWindowClosing, |
332 tab_strip_model->closing_all()); | 319 tab_strip_model->closing_all()); |
333 args.Append(object_args); | 320 args->Append(object_args); |
334 | 321 |
335 std::string json_args; | 322 DispatchEvent(contents->profile(), events::kOnTabRemoved, args); |
336 base::JSONWriter::Write(&args, &json_args); | |
337 | |
338 DispatchEvent(contents->profile(), events::kOnTabRemoved, json_args); | |
339 | 323 |
340 int removed_count = tab_entries_.erase(tab_id); | 324 int removed_count = tab_entries_.erase(tab_id); |
341 DCHECK_GT(removed_count, 0); | 325 DCHECK_GT(removed_count, 0); |
342 | 326 |
343 UnregisterForTabNotifications(contents->web_contents()); | 327 UnregisterForTabNotifications(contents->web_contents()); |
344 } | 328 } |
345 | 329 |
346 void ExtensionBrowserEventRouter::ActiveTabChanged( | 330 void ExtensionBrowserEventRouter::ActiveTabChanged( |
347 TabContents* old_contents, | 331 TabContents* old_contents, |
348 TabContents* new_contents, | 332 TabContents* new_contents, |
349 int index, | 333 int index, |
350 bool user_gesture) { | 334 bool user_gesture) { |
351 ListValue args; | 335 ListValue* args = new ListValue(); |
352 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); | 336 int tab_id = ExtensionTabUtil::GetTabId(new_contents->web_contents()); |
353 args.Append(Value::CreateIntegerValue(tab_id)); | 337 args->Append(Value::CreateIntegerValue(tab_id)); |
354 | 338 |
355 DictionaryValue* object_args = new DictionaryValue(); | 339 DictionaryValue* object_args = new DictionaryValue(); |
356 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 340 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
357 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); | 341 ExtensionTabUtil::GetWindowIdOfTab(new_contents->web_contents()))); |
358 args.Append(object_args); | 342 args->Append(object_args); |
359 | 343 |
360 // The onActivated event replaced onActiveChanged and onSelectionChanged. The | 344 // The onActivated event replaced onActiveChanged and onSelectionChanged. The |
361 // deprecated events take two arguments: tabId, {windowId}. | 345 // deprecated events take two arguments: tabId, {windowId}. |
362 std::string old_json_args; | 346 Profile* profile = new_contents->profile(); |
363 base::JSONWriter::Write(&args, &old_json_args); | 347 DispatchEvent(profile, events::kOnTabSelectionChanged, args->DeepCopy()); |
| 348 DispatchEvent(profile, events::kOnTabActiveChanged, args->DeepCopy()); |
364 | 349 |
365 // The onActivated event takes one argument: {windowId, tabId}. | 350 // The onActivated event takes one argument: {windowId, tabId}. |
366 std::string new_json_args; | 351 args->Remove(0, NULL); |
367 args.Remove(0, NULL); | 352 DispatchEvent(profile, events::kOnTabActivated, args); |
368 object_args->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | |
369 base::JSONWriter::Write(&args, &new_json_args); | |
370 | |
371 Profile* profile = new_contents->profile(); | |
372 DispatchEvent(profile, events::kOnTabSelectionChanged, old_json_args); | |
373 DispatchEvent(profile, events::kOnTabActiveChanged, old_json_args); | |
374 DispatchEvent(profile, events::kOnTabActivated, new_json_args); | |
375 } | 353 } |
376 | 354 |
377 void ExtensionBrowserEventRouter::TabSelectionChanged( | 355 void ExtensionBrowserEventRouter::TabSelectionChanged( |
378 TabStripModel* tab_strip_model, | 356 TabStripModel* tab_strip_model, |
379 const TabStripSelectionModel& old_model) { | 357 const TabStripSelectionModel& old_model) { |
380 TabStripSelectionModel::SelectedIndices new_selection = | 358 TabStripSelectionModel::SelectedIndices new_selection = |
381 tab_strip_model->selection_model().selected_indices(); | 359 tab_strip_model->selection_model().selected_indices(); |
382 ListValue* all = new ListValue(); | 360 ListValue* all = new ListValue(); |
383 | 361 |
384 for (size_t i = 0; i < new_selection.size(); ++i) { | 362 for (size_t i = 0; i < new_selection.size(); ++i) { |
385 int index = new_selection[i]; | 363 int index = new_selection[i]; |
386 TabContents* contents = tab_strip_model->GetTabContentsAt(index); | 364 TabContents* contents = tab_strip_model->GetTabContentsAt(index); |
387 if (!contents) | 365 if (!contents) |
388 break; | 366 break; |
389 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); | 367 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); |
390 all->Append(Value::CreateIntegerValue(tab_id)); | 368 all->Append(Value::CreateIntegerValue(tab_id)); |
391 } | 369 } |
392 | 370 |
393 ListValue args; | 371 ListValue* args = new ListValue(); |
394 DictionaryValue* select_info = new DictionaryValue(); | 372 DictionaryValue* select_info = new DictionaryValue(); |
395 | 373 |
396 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 374 select_info->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
397 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); | 375 ExtensionTabUtil::GetWindowIdOfTabStripModel(tab_strip_model))); |
398 | 376 |
399 select_info->Set(tab_keys::kTabIdsKey, all); | 377 select_info->Set(tab_keys::kTabIdsKey, all); |
400 args.Append(select_info); | 378 args->Append(select_info); |
401 | |
402 std::string json_args; | |
403 base::JSONWriter::Write(&args, &json_args); | |
404 | 379 |
405 // The onHighlighted event replaced onHighlightChanged. | 380 // The onHighlighted event replaced onHighlightChanged. |
406 Profile* profile = tab_strip_model->profile(); | 381 Profile* profile = tab_strip_model->profile(); |
407 DispatchEvent(profile, events::kOnTabHighlightChanged, json_args); | 382 DispatchEvent(profile, events::kOnTabHighlightChanged, args->DeepCopy()); |
408 DispatchEvent(profile, events::kOnTabHighlighted, json_args); | 383 DispatchEvent(profile, events::kOnTabHighlighted, args); |
409 } | 384 } |
410 | 385 |
411 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, | 386 void ExtensionBrowserEventRouter::TabMoved(TabContents* contents, |
412 int from_index, | 387 int from_index, |
413 int to_index) { | 388 int to_index) { |
414 ListValue args; | 389 ListValue* args = new ListValue(); |
415 args.Append(Value::CreateIntegerValue( | 390 args->Append(Value::CreateIntegerValue( |
416 ExtensionTabUtil::GetTabId(contents->web_contents()))); | 391 ExtensionTabUtil::GetTabId(contents->web_contents()))); |
417 | 392 |
418 DictionaryValue* object_args = new DictionaryValue(); | 393 DictionaryValue* object_args = new DictionaryValue(); |
419 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( | 394 object_args->Set(tab_keys::kWindowIdKey, Value::CreateIntegerValue( |
420 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); | 395 ExtensionTabUtil::GetWindowIdOfTab(contents->web_contents()))); |
421 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( | 396 object_args->Set(tab_keys::kFromIndexKey, Value::CreateIntegerValue( |
422 from_index)); | 397 from_index)); |
423 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( | 398 object_args->Set(tab_keys::kToIndexKey, Value::CreateIntegerValue( |
424 to_index)); | 399 to_index)); |
425 args.Append(object_args); | 400 args->Append(object_args); |
426 | 401 |
427 std::string json_args; | 402 DispatchEvent(contents->profile(), events::kOnTabMoved, args); |
428 base::JSONWriter::Write(&args, &json_args); | |
429 | |
430 DispatchEvent(contents->profile(), events::kOnTabMoved, json_args); | |
431 } | 403 } |
432 | 404 |
433 void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, | 405 void ExtensionBrowserEventRouter::TabUpdated(WebContents* contents, |
434 bool did_navigate) { | 406 bool did_navigate) { |
435 TabEntry* entry = GetTabEntry(contents); | 407 TabEntry* entry = GetTabEntry(contents); |
436 DictionaryValue* changed_properties = NULL; | 408 DictionaryValue* changed_properties = NULL; |
437 | 409 |
438 DCHECK(entry); | 410 DCHECK(entry); |
439 | 411 |
440 if (did_navigate) | 412 if (did_navigate) |
441 changed_properties = entry->DidNavigate(contents); | 413 changed_properties = entry->DidNavigate(contents); |
442 else | 414 else |
443 changed_properties = entry->UpdateLoadState(contents); | 415 changed_properties = entry->UpdateLoadState(contents); |
444 | 416 |
445 if (changed_properties) | 417 if (changed_properties) |
446 DispatchTabUpdatedEvent(contents, changed_properties); | 418 DispatchTabUpdatedEvent(contents, changed_properties); |
447 } | 419 } |
448 | 420 |
449 void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, | 421 void ExtensionBrowserEventRouter::DispatchEvent(Profile* profile, |
450 const char* event_name, | 422 const char* event_name, |
451 const std::string& json_args) { | 423 base::ListValue* arguments) { |
452 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 424 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
453 return; | 425 return; |
454 | 426 |
455 profile->GetExtensionEventRouter()->DispatchEventToRenderers( | 427 profile->GetExtensionEventRouter()->DispatchEventToRenderers( |
456 event_name, json_args, profile, GURL()); | 428 event_name, arguments, profile, GURL()); |
457 } | 429 } |
458 | 430 |
459 void ExtensionBrowserEventRouter::DispatchEventToExtension( | 431 void ExtensionBrowserEventRouter::DispatchEventToExtension( |
460 Profile* profile, | 432 Profile* profile, |
461 const std::string& extension_id, | 433 const std::string& extension_id, |
462 const char* event_name, | 434 const char* event_name, |
463 const std::string& json_args) { | 435 ListValue* event_args) { |
464 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 436 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
465 return; | 437 return; |
466 | 438 |
467 profile->GetExtensionEventRouter()->DispatchEventToExtension( | 439 profile->GetExtensionEventRouter()->DispatchEventToExtension( |
468 extension_id, event_name, json_args, profile, GURL()); | 440 extension_id, event_name, event_args, profile, GURL()); |
469 } | 441 } |
470 | 442 |
471 void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( | 443 void ExtensionBrowserEventRouter::DispatchEventsAcrossIncognito( |
472 Profile* profile, | 444 Profile* profile, |
473 const char* event_name, | 445 const char* event_name, |
474 const std::string& json_args, | 446 ListValue* event_args, |
475 const std::string& cross_incognito_args) { | 447 ListValue* cross_incognito_args) { |
476 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) | 448 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) |
477 return; | 449 return; |
478 | 450 |
479 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( | 451 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( |
480 event_name, json_args, profile, cross_incognito_args, GURL()); | 452 event_name, event_args, profile, cross_incognito_args, GURL()); |
481 } | 453 } |
482 | 454 |
483 void ExtensionBrowserEventRouter::DispatchEventWithTab( | 455 void ExtensionBrowserEventRouter::DispatchEventWithTab( |
484 Profile* profile, | 456 Profile* profile, |
485 const std::string& extension_id, | 457 const std::string& extension_id, |
486 const char* event_name, | 458 const char* event_name, |
487 const WebContents* web_contents, | 459 const WebContents* web_contents, |
488 bool active) { | 460 bool active) { |
489 if (!profile_->IsSameProfile(profile)) | 461 if (!profile_->IsSameProfile(profile)) |
490 return; | 462 return; |
491 | 463 |
492 ListValue args; | 464 ListValue* args = new ListValue(); |
493 args.Append(ExtensionTabUtil::CreateTabValueActive( | 465 args->Append(ExtensionTabUtil::CreateTabValueActive( |
494 web_contents, active)); | 466 web_contents, active)); |
495 std::string json_args; | |
496 base::JSONWriter::Write(&args, &json_args); | |
497 if (!extension_id.empty()) { | 467 if (!extension_id.empty()) { |
498 DispatchEventToExtension(profile, extension_id, event_name, json_args); | 468 DispatchEventToExtension(profile, extension_id, event_name, args); |
499 } else { | 469 } else { |
500 DispatchEvent(profile, event_name, json_args); | 470 DispatchEvent(profile, event_name, args); |
501 } | 471 } |
502 } | 472 } |
503 | 473 |
504 void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( | 474 void ExtensionBrowserEventRouter::DispatchSimpleBrowserEvent( |
505 Profile* profile, const int window_id, const char* event_name) { | 475 Profile* profile, const int window_id, const char* event_name) { |
506 if (!profile_->IsSameProfile(profile)) | 476 if (!profile_->IsSameProfile(profile)) |
507 return; | 477 return; |
508 | 478 |
509 ListValue args; | 479 ListValue* args = new ListValue(); |
510 args.Append(Value::CreateIntegerValue(window_id)); | 480 args->Append(Value::CreateIntegerValue(window_id)); |
511 | 481 |
512 std::string json_args; | 482 DispatchEvent(profile, event_name, args); |
513 base::JSONWriter::Write(&args, &json_args); | |
514 | |
515 DispatchEvent(profile, event_name, json_args); | |
516 } | 483 } |
517 | 484 |
518 void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( | 485 void ExtensionBrowserEventRouter::DispatchTabUpdatedEvent( |
519 WebContents* contents, DictionaryValue* changed_properties) { | 486 WebContents* contents, DictionaryValue* changed_properties) { |
520 DCHECK(changed_properties); | 487 DCHECK(changed_properties); |
521 DCHECK(contents); | 488 DCHECK(contents); |
522 | 489 |
523 // The state of the tab (as seen from the extension point of view) has | 490 // The state of the tab (as seen from the extension point of view) has |
524 // changed. Send a notification to the extension. | 491 // changed. Send a notification to the extension. |
525 ListValue args; | 492 ListValue* args = new ListValue(); |
526 | 493 |
527 // First arg: The id of the tab that changed. | 494 // First arg: The id of the tab that changed. |
528 args.Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); | 495 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); |
529 | 496 |
530 // Second arg: An object containing the changes to the tab state. | 497 // Second arg: An object containing the changes to the tab state. |
531 args.Append(changed_properties); | 498 args->Append(changed_properties); |
532 | 499 |
533 // Third arg: An object containing the state of the tab. | 500 // Third arg: An object containing the state of the tab. |
534 args.Append(ExtensionTabUtil::CreateTabValue(contents)); | 501 args->Append(ExtensionTabUtil::CreateTabValue(contents)); |
535 | |
536 std::string json_args; | |
537 base::JSONWriter::Write(&args, &json_args); | |
538 | 502 |
539 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 503 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
540 DispatchEvent(profile, events::kOnTabUpdated, json_args); | 504 DispatchEvent(profile, events::kOnTabUpdated, args); |
541 } | 505 } |
542 | 506 |
543 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( | 507 ExtensionBrowserEventRouter::TabEntry* ExtensionBrowserEventRouter::GetTabEntry( |
544 const WebContents* contents) { | 508 const WebContents* contents) { |
545 int tab_id = ExtensionTabUtil::GetTabId(contents); | 509 int tab_id = ExtensionTabUtil::GetTabId(contents); |
546 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 510 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
547 if (tab_entries_.end() == i) | 511 if (tab_entries_.end() == i) |
548 return NULL; | 512 return NULL; |
549 return &i->second; | 513 return &i->second; |
550 } | 514 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 | 572 |
609 void ExtensionBrowserEventRouter::TabStripEmpty() {} | 573 void ExtensionBrowserEventRouter::TabStripEmpty() {} |
610 | 574 |
611 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( | 575 void ExtensionBrowserEventRouter::DispatchOldPageActionEvent( |
612 Profile* profile, | 576 Profile* profile, |
613 const std::string& extension_id, | 577 const std::string& extension_id, |
614 const std::string& page_action_id, | 578 const std::string& page_action_id, |
615 int tab_id, | 579 int tab_id, |
616 const std::string& url, | 580 const std::string& url, |
617 int button) { | 581 int button) { |
618 ListValue args; | 582 ListValue* args = new ListValue(); |
619 args.Append(Value::CreateStringValue(page_action_id)); | 583 args->Append(Value::CreateStringValue(page_action_id)); |
620 | 584 |
621 DictionaryValue* data = new DictionaryValue(); | 585 DictionaryValue* data = new DictionaryValue(); |
622 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); | 586 data->Set(tab_keys::kTabIdKey, Value::CreateIntegerValue(tab_id)); |
623 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); | 587 data->Set(tab_keys::kTabUrlKey, Value::CreateStringValue(url)); |
624 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); | 588 data->Set(page_action_keys::kButtonKey, Value::CreateIntegerValue(button)); |
625 args.Append(data); | 589 args->Append(data); |
626 | 590 |
627 std::string json_args; | 591 DispatchEventToExtension(profile, extension_id, "pageActions", args); |
628 base::JSONWriter::Write(&args, &json_args); | |
629 | |
630 DispatchEventToExtension(profile, extension_id, "pageActions", json_args); | |
631 } | 592 } |
632 | 593 |
633 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 594 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
634 const ExtensionAction& browser_action, | 595 const ExtensionAction& browser_action, |
635 Browser* browser) { | 596 Browser* browser) { |
636 Profile* profile = browser->profile(); | 597 Profile* profile = browser->profile(); |
637 TabContents* tab_contents = NULL; | 598 TabContents* tab_contents = NULL; |
638 int tab_id = 0; | 599 int tab_id = 0; |
639 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 600 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
640 return; | 601 return; |
(...skipping 25 matching lines...) Expand all Loading... |
666 NULL, NULL, &tab_contents, NULL)) { | 627 NULL, NULL, &tab_contents, NULL)) { |
667 return; | 628 return; |
668 } | 629 } |
669 ExtensionActionExecuted(profile, script_badge, tab_contents); | 630 ExtensionActionExecuted(profile, script_badge, tab_contents); |
670 } | 631 } |
671 | 632 |
672 void ExtensionBrowserEventRouter::CommandExecuted( | 633 void ExtensionBrowserEventRouter::CommandExecuted( |
673 Profile* profile, | 634 Profile* profile, |
674 const std::string& extension_id, | 635 const std::string& extension_id, |
675 const std::string& command) { | 636 const std::string& command) { |
676 ListValue args; | 637 ListValue* args = new ListValue(); |
677 args.Append(Value::CreateStringValue(command)); | 638 args->Append(Value::CreateStringValue(command)); |
678 std::string json_args; | |
679 base::JSONWriter::Write(&args, &json_args); | |
680 | 639 |
681 DispatchEventToExtension(profile, | 640 DispatchEventToExtension(profile, |
682 extension_id, | 641 extension_id, |
683 "experimental.keybinding.onCommand", | 642 "experimental.keybinding.onCommand", |
684 json_args); | 643 args); |
685 } | 644 } |
686 | 645 |
687 void ExtensionBrowserEventRouter::ExtensionActionExecuted( | 646 void ExtensionBrowserEventRouter::ExtensionActionExecuted( |
688 Profile* profile, | 647 Profile* profile, |
689 const ExtensionAction& extension_action, | 648 const ExtensionAction& extension_action, |
690 TabContents* tab_contents) { | 649 TabContents* tab_contents) { |
691 const char* event_name = NULL; | 650 const char* event_name = NULL; |
692 switch (extension_action.action_type()) { | 651 switch (extension_action.action_type()) { |
693 case ExtensionAction::TYPE_BROWSER: | 652 case ExtensionAction::TYPE_BROWSER: |
694 event_name = "browserAction.onClicked"; | 653 event_name = "browserAction.onClicked"; |
695 break; | 654 break; |
696 case ExtensionAction::TYPE_PAGE: | 655 case ExtensionAction::TYPE_PAGE: |
697 event_name = "pageAction.onClicked"; | 656 event_name = "pageAction.onClicked"; |
698 break; | 657 break; |
699 case ExtensionAction::TYPE_SCRIPT_BADGE: | 658 case ExtensionAction::TYPE_SCRIPT_BADGE: |
700 event_name = "scriptBadge.onClicked"; | 659 event_name = "scriptBadge.onClicked"; |
701 break; | 660 break; |
702 } | 661 } |
703 | 662 |
704 if (event_name) { | 663 if (event_name) { |
705 DispatchEventWithTab(profile, | 664 DispatchEventWithTab(profile, |
706 extension_action.extension_id(), | 665 extension_action.extension_id(), |
707 event_name, | 666 event_name, |
708 tab_contents->web_contents(), | 667 tab_contents->web_contents(), |
709 true); | 668 true); |
710 } | 669 } |
711 } | 670 } |
OLD | NEW |