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/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" |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 to_index)); | 332 to_index)); |
333 args->Append(object_args); | 333 args->Append(object_args); |
334 | 334 |
335 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 335 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
336 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), | 336 DispatchEvent(profile, events::kOnTabMoved, args.Pass(), |
337 EventRouter::USER_GESTURE_UNKNOWN); | 337 EventRouter::USER_GESTURE_UNKNOWN); |
338 } | 338 } |
339 | 339 |
340 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { | 340 void BrowserEventRouter::TabUpdated(WebContents* contents, bool did_navigate) { |
341 TabEntry* entry = GetTabEntry(contents); | 341 TabEntry* entry = GetTabEntry(contents); |
342 DictionaryValue* changed_properties = NULL; | 342 scoped_ptr<DictionaryValue> changed_properties; |
343 | 343 |
344 DCHECK(entry); | 344 DCHECK(entry); |
345 | 345 |
346 if (did_navigate) | 346 if (did_navigate) |
347 changed_properties = entry->DidNavigate(contents); | 347 changed_properties.reset(entry->DidNavigate(contents)); |
348 else | 348 else |
349 changed_properties = entry->UpdateLoadState(contents); | 349 changed_properties.reset(entry->UpdateLoadState(contents)); |
350 | 350 |
351 if (changed_properties) | 351 if (changed_properties) |
352 DispatchTabUpdatedEvent(contents, changed_properties); | 352 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
353 } | 353 } |
354 | 354 |
355 void BrowserEventRouter::DispatchEvent( | 355 void BrowserEventRouter::DispatchEvent( |
356 Profile* profile, | 356 Profile* profile, |
357 const char* event_name, | 357 const char* event_name, |
358 scoped_ptr<ListValue> args, | 358 scoped_ptr<ListValue> args, |
359 EventRouter::UserGestureState user_gesture) { | 359 EventRouter::UserGestureState user_gesture) { |
360 if (!profile_->IsSameProfile(profile) || | 360 if (!profile_->IsSameProfile(profile) || |
361 !extensions::ExtensionSystem::Get(profile)->event_router()) | 361 !extensions::ExtensionSystem::Get(profile)->event_router()) |
362 return; | 362 return; |
(...skipping 26 matching lines...) Expand all Loading... |
389 if (!profile_->IsSameProfile(profile)) | 389 if (!profile_->IsSameProfile(profile)) |
390 return; | 390 return; |
391 | 391 |
392 scoped_ptr<ListValue> args(new ListValue()); | 392 scoped_ptr<ListValue> args(new ListValue()); |
393 args->Append(Value::CreateIntegerValue(window_id)); | 393 args->Append(Value::CreateIntegerValue(window_id)); |
394 | 394 |
395 DispatchEvent(profile, event_name, args.Pass(), | 395 DispatchEvent(profile, event_name, args.Pass(), |
396 EventRouter::USER_GESTURE_UNKNOWN); | 396 EventRouter::USER_GESTURE_UNKNOWN); |
397 } | 397 } |
398 | 398 |
399 static void WillDispatchTabUpdatedEvent(WebContents* contents, | 399 static void WillDispatchTabUpdatedEvent( |
400 Profile* profile, | 400 WebContents* contents, |
401 const Extension* extension, | 401 const DictionaryValue* changed_properties, |
402 ListValue* event_args) { | 402 Profile* profile, |
| 403 const Extension* extension, |
| 404 ListValue* event_args) { |
| 405 // Overwrite the second argument with the appropriate properties dictionary, |
| 406 // depending on extension permissions. |
| 407 DictionaryValue* properties_value = changed_properties->DeepCopy(); |
| 408 ExtensionTabUtil::ScrubTabValueForExtension(contents, extension, |
| 409 properties_value); |
| 410 event_args->Set(1, properties_value); |
| 411 |
| 412 // Overwrite the third arg with our tab value as seen by this extension. |
403 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 413 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
404 contents, extension); | 414 contents, extension); |
405 // Overwrite the third arg with our tab value as seen by this extension. | |
406 event_args->Set(2, tab_value); | 415 event_args->Set(2, tab_value); |
407 } | 416 } |
408 | 417 |
409 void BrowserEventRouter::DispatchTabUpdatedEvent( | 418 void BrowserEventRouter::DispatchTabUpdatedEvent( |
410 WebContents* contents, DictionaryValue* changed_properties) { | 419 WebContents* contents, scoped_ptr<DictionaryValue> changed_properties) { |
411 DCHECK(changed_properties); | 420 DCHECK(changed_properties); |
412 DCHECK(contents); | 421 DCHECK(contents); |
413 | 422 |
414 // The state of the tab (as seen from the extension point of view) has | 423 // The state of the tab (as seen from the extension point of view) has |
415 // changed. Send a notification to the extension. | 424 // changed. Send a notification to the extension. |
416 scoped_ptr<ListValue> args_base(new ListValue()); | 425 scoped_ptr<ListValue> args_base(new ListValue()); |
417 | 426 |
418 // First arg: The id of the tab that changed. | 427 // First arg: The id of the tab that changed. |
419 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); | 428 args_base->AppendInteger(ExtensionTabUtil::GetTabId(contents)); |
420 | 429 |
421 // Second arg: An object containing the changes to the tab state. | 430 // Second arg: An object containing the changes to the tab state. Filled in |
422 args_base->Append(changed_properties); | 431 // by WillDispatchTabUpdatedEvent as a copy of changed_properties, if the |
| 432 // extension has the tabs permission. |
423 | 433 |
424 // Third arg: An object containing the state of the tab. Filled in by | 434 // Third arg: An object containing the state of the tab. Filled in by |
425 // WillDispatchTabUpdatedEvent. | 435 // WillDispatchTabUpdatedEvent. |
426 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); | 436 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); |
427 | 437 |
428 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); | 438 scoped_ptr<Event> event(new Event(events::kOnTabUpdated, args_base.Pass())); |
429 event->restrict_to_profile = profile; | 439 event->restrict_to_profile = profile; |
430 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; | 440 event->user_gesture = EventRouter::USER_GESTURE_NOT_ENABLED; |
431 event->will_dispatch_callback = | 441 event->will_dispatch_callback = |
432 base::Bind(&WillDispatchTabUpdatedEvent, contents); | 442 base::Bind(&WillDispatchTabUpdatedEvent, |
| 443 contents, changed_properties.get()); |
433 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); | 444 ExtensionSystem::Get(profile)->event_router()->BroadcastEvent(event.Pass()); |
434 } | 445 } |
435 | 446 |
436 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( | 447 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( |
437 const WebContents* contents) { | 448 const WebContents* contents) { |
438 int tab_id = ExtensionTabUtil::GetTabId(contents); | 449 int tab_id = ExtensionTabUtil::GetTabId(contents); |
439 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); | 450 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); |
440 if (tab_entries_.end() == i) | 451 if (tab_entries_.end() == i) |
441 return NULL; | 452 return NULL; |
442 return &i->second; | 453 return &i->second; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 RegisterForTabNotifications(new_contents); | 505 RegisterForTabNotifications(new_contents); |
495 } | 506 } |
496 } | 507 } |
497 | 508 |
498 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, | 509 void BrowserEventRouter::TabPinnedStateChanged(WebContents* contents, |
499 int index) { | 510 int index) { |
500 TabStripModel* tab_strip = NULL; | 511 TabStripModel* tab_strip = NULL; |
501 int tab_index; | 512 int tab_index; |
502 | 513 |
503 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { | 514 if (ExtensionTabUtil::GetTabStripModel(contents, &tab_strip, &tab_index)) { |
504 DictionaryValue* changed_properties = new DictionaryValue(); | 515 scoped_ptr<DictionaryValue> changed_properties(new DictionaryValue()); |
505 changed_properties->SetBoolean(tab_keys::kPinnedKey, | 516 changed_properties->SetBoolean(tab_keys::kPinnedKey, |
506 tab_strip->IsTabPinned(tab_index)); | 517 tab_strip->IsTabPinned(tab_index)); |
507 DispatchTabUpdatedEvent(contents, changed_properties); | 518 DispatchTabUpdatedEvent(contents, changed_properties.Pass()); |
508 } | 519 } |
509 } | 520 } |
510 | 521 |
511 void BrowserEventRouter::TabStripEmpty() {} | 522 void BrowserEventRouter::TabStripEmpty() {} |
512 | 523 |
513 void BrowserEventRouter::DispatchOldPageActionEvent( | 524 void BrowserEventRouter::DispatchOldPageActionEvent( |
514 Profile* profile, | 525 Profile* profile, |
515 const std::string& extension_id, | 526 const std::string& extension_id, |
516 const std::string& page_action_id, | 527 const std::string& page_action_id, |
517 int tab_id, | 528 int tab_id, |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
584 event_name = "scriptBadge.onClicked"; | 595 event_name = "scriptBadge.onClicked"; |
585 break; | 596 break; |
586 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: | 597 case Extension::ActionInfo::TYPE_SYSTEM_INDICATOR: |
587 // The System Indicator handles its own clicks. | 598 // The System Indicator handles its own clicks. |
588 break; | 599 break; |
589 } | 600 } |
590 | 601 |
591 if (event_name) { | 602 if (event_name) { |
592 scoped_ptr<ListValue> args(new ListValue()); | 603 scoped_ptr<ListValue> args(new ListValue()); |
593 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( | 604 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue( |
594 web_contents, | 605 web_contents); |
595 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS); | |
596 args->Append(tab_value); | 606 args->Append(tab_value); |
597 | 607 |
598 DispatchEventToExtension(profile, | 608 DispatchEventToExtension(profile, |
599 extension_action.extension_id(), | 609 extension_action.extension_id(), |
600 event_name, | 610 event_name, |
601 args.Pass(), | 611 args.Pass(), |
602 EventRouter::USER_GESTURE_ENABLED); | 612 EventRouter::USER_GESTURE_ENABLED); |
603 } | 613 } |
604 } | 614 } |
605 | 615 |
606 } // namespace extensions | 616 } // namespace extensions |
OLD | NEW |