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

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

Issue 10909256: Always send the full tab object in ExtensionAction click event. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Realized that any API outside of tabs.json and windows.json that referenced tabs.Tab would have sam… Created 8 years, 3 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 | Annotate | Revision Log
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/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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 if (service) { 159 if (service) {
160 service->window_event_router()->OnActiveWindowChanged( 160 service->window_event_router()->OnActiveWindowChanged(
161 browser ? browser->extension_window_controller() : NULL); 161 browser ? browser->extension_window_controller() : NULL);
162 } 162 }
163 } 163 }
164 164
165 void BrowserEventRouter::TabCreatedAt(WebContents* contents, 165 void BrowserEventRouter::TabCreatedAt(WebContents* contents,
166 int index, 166 int index,
167 bool active) { 167 bool active) {
168 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 168 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
169 DispatchEventWithTab(profile, "", events::kOnTabCreated, contents, active, 169 const EventListenerMap::ListenerList& listeners(
170 EventRouter::USER_GESTURE_NOT_ENABLED); 170 ExtensionSystem::Get(profile)->event_router()->
171 listeners().GetEventListenersByName(events::kOnTabCreated));
172 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin();
173 it != listeners.end();
174 ++it) {
175 scoped_ptr<ListValue> args(new ListValue());
176 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue(
177 contents,
178 profile->GetExtensionService()->extensions()->GetByID(
179 (*it)->extension_id));
180 args->Append(tab_value);
181 tab_value->SetBoolean(tab_keys::kSelectedKey, active);
182 DispatchEventToExtension(profile, (*it)->extension_id,
183 events::kOnTabCreated, args.Pass(),
184 EventRouter::USER_GESTURE_NOT_ENABLED);
185 }
171 186
172 RegisterForTabNotifications(contents); 187 RegisterForTabNotifications(contents);
173 } 188 }
174 189
175 void BrowserEventRouter::TabInsertedAt(TabContents* contents, 190 void BrowserEventRouter::TabInsertedAt(TabContents* contents,
176 int index, 191 int index,
177 bool active) { 192 bool active) {
178 // If tab is new, send created event. 193 // If tab is new, send created event.
179 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents()); 194 int tab_id = ExtensionTabUtil::GetTabId(contents->web_contents());
180 if (!GetTabEntry(contents->web_contents())) { 195 if (!GetTabEntry(contents->web_contents())) {
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 scoped_ptr<ListValue> event_args, 386 scoped_ptr<ListValue> event_args,
372 scoped_ptr<ListValue> cross_incognito_args) { 387 scoped_ptr<ListValue> cross_incognito_args) {
373 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter()) 388 if (!profile_->IsSameProfile(profile) || !profile->GetExtensionEventRouter())
374 return; 389 return;
375 390
376 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito( 391 profile->GetExtensionEventRouter()->DispatchEventsToRenderersAcrossIncognito(
377 event_name, event_args.Pass(), profile, cross_incognito_args.Pass(), 392 event_name, event_args.Pass(), profile, cross_incognito_args.Pass(),
378 GURL()); 393 GURL());
379 } 394 }
380 395
381 void BrowserEventRouter::DispatchEventWithTab(
382 Profile* profile,
383 const std::string& extension_id,
384 const char* event_name,
385 const WebContents* web_contents,
386 bool active,
387 EventRouter::UserGestureState user_gesture,
388 scoped_ptr<ListValue> event_args) {
389 if (!profile_->IsSameProfile(profile))
390 return;
391
392 if (!extension_id.empty()) {
393 event_args->Append(ExtensionTabUtil::CreateTabValueActive(
394 web_contents,
395 active,
396 profile->GetExtensionService()->extensions()->GetByID(extension_id)));
397 DispatchEventToExtension(profile, extension_id, event_name,
398 event_args.Pass(), user_gesture);
399 } else {
400 const EventListenerMap::ListenerList& listeners(
401 ExtensionSystem::Get(profile)->event_router()->
402 listeners().GetEventListenersByName(event_name));
403
404 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin();
405 it != listeners.end();
406 ++it) {
407 scoped_ptr<ListValue> args(event_args->DeepCopy());
408 args->Append(ExtensionTabUtil::CreateTabValueActive(
409 web_contents,
410 active,
411 profile->GetExtensionService()->extensions()->GetByID(
412 (*it)->extension_id)));
413 DispatchEventToExtension(profile, (*it)->extension_id, event_name,
414 args.Pass(), user_gesture);
415 }
416 }
417 }
418
419 void BrowserEventRouter::DispatchSimpleBrowserEvent( 396 void BrowserEventRouter::DispatchSimpleBrowserEvent(
420 Profile* profile, const int window_id, const char* event_name) { 397 Profile* profile, const int window_id, const char* event_name) {
421 if (!profile_->IsSameProfile(profile)) 398 if (!profile_->IsSameProfile(profile))
422 return; 399 return;
423 400
424 scoped_ptr<ListValue> args(new ListValue()); 401 scoped_ptr<ListValue> args(new ListValue());
425 args->Append(Value::CreateIntegerValue(window_id)); 402 args->Append(Value::CreateIntegerValue(window_id));
426 403
427 DispatchEvent(profile, event_name, args.Pass(), 404 DispatchEvent(profile, event_name, args.Pass(),
428 EventRouter::USER_GESTURE_UNKNOWN); 405 EventRouter::USER_GESTURE_UNKNOWN);
429 } 406 }
430 407
431 void BrowserEventRouter::DispatchTabUpdatedEvent( 408 void BrowserEventRouter::DispatchTabUpdatedEvent(
432 WebContents* contents, DictionaryValue* changed_properties) { 409 WebContents* contents, DictionaryValue* changed_properties) {
433 DCHECK(changed_properties); 410 DCHECK(changed_properties);
434 DCHECK(contents); 411 DCHECK(contents);
435 412
436 // The state of the tab (as seen from the extension point of view) has 413 // The state of the tab (as seen from the extension point of view) has
437 // changed. Send a notification to the extension. 414 // changed. Send a notification to the extension.
438 scoped_ptr<ListValue> args(new ListValue()); 415 scoped_ptr<ListValue> args_base(new ListValue());
439 416
440 // First arg: The id of the tab that changed. 417 // First arg: The id of the tab that changed.
441 args->Append(Value::CreateIntegerValue(ExtensionTabUtil::GetTabId(contents))); 418 args_base->Append(Value::CreateIntegerValue(
419 ExtensionTabUtil::GetTabId(contents)));
442 420
443 // Second arg: An object containing the changes to the tab state. 421 // Second arg: An object containing the changes to the tab state.
444 args->Append(changed_properties); 422 args_base->Append(changed_properties);
445 423
446 // Third arg: An object containing the state of the tab. 424 // Third arg: An object containing the state of the tab.
447 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext()); 425 Profile* profile = Profile::FromBrowserContext(contents->GetBrowserContext());
448 426
449 DispatchEventWithTab(profile, "", events::kOnTabUpdated, contents, true, 427 const EventListenerMap::ListenerList& listeners(
450 EventRouter::USER_GESTURE_UNKNOWN, args.Pass()); 428 ExtensionSystem::Get(profile)->event_router()->
429 listeners().GetEventListenersByName(events::kOnTabUpdated));
430 for (EventListenerMap::ListenerList::const_iterator it = listeners.begin();
431 it != listeners.end();
432 ++it) {
433 scoped_ptr<ListValue> args(args_base->DeepCopy());
434 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue(
435 contents,
436 profile->GetExtensionService()->extensions()->GetByID(
437 (*it)->extension_id));
438 args->Append(tab_value);
439 DispatchEventToExtension(profile, (*it)->extension_id,
440 events::kOnTabUpdated, args.Pass(),
441 EventRouter::USER_GESTURE_UNKNOWN);
442 }
451 } 443 }
452 444
453 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry( 445 BrowserEventRouter::TabEntry* BrowserEventRouter::GetTabEntry(
454 const WebContents* contents) { 446 const WebContents* contents) {
455 int tab_id = ExtensionTabUtil::GetTabId(contents); 447 int tab_id = ExtensionTabUtil::GetTabId(contents);
456 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id); 448 std::map<int, TabEntry>::iterator i = tab_entries_.find(tab_id);
457 if (tab_entries_.end() == i) 449 if (tab_entries_.end() == i)
458 return NULL; 450 return NULL;
459 return &i->second; 451 return &i->second;
460 } 452 }
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 break; 582 break;
591 case ExtensionAction::TYPE_PAGE: 583 case ExtensionAction::TYPE_PAGE:
592 event_name = "pageAction.onClicked"; 584 event_name = "pageAction.onClicked";
593 break; 585 break;
594 case ExtensionAction::TYPE_SCRIPT_BADGE: 586 case ExtensionAction::TYPE_SCRIPT_BADGE:
595 event_name = "scriptBadge.onClicked"; 587 event_name = "scriptBadge.onClicked";
596 break; 588 break;
597 } 589 }
598 590
599 if (event_name) { 591 if (event_name) {
600 DispatchEventWithTab(profile, 592 scoped_ptr<ListValue> args(new ListValue());
601 extension_action.extension_id(), 593
602 event_name, 594 // We always include all privacy sensitive fields, regardless of
603 tab_contents->web_contents(), 595 // permissions for backward compatibility. See crbug.com/149020.
604 true, 596 // Since the user has to take action in order to fire this event, it's not
605 EventRouter::USER_GESTURE_ENABLED); 597 // that big a deal.
598 DictionaryValue* tab_value = ExtensionTabUtil::CreateTabValue(
599 tab_contents->web_contents(), NULL, -1,
600 ExtensionTabUtil::INCLUDE_PRIVACY_SENSITIVE_FIELDS);
601 args->Append(tab_value);
602
603 DispatchEventToExtension(profile,
604 extension_action.extension_id(),
605 event_name,
606 args.Pass(),
607 EventRouter::USER_GESTURE_ENABLED);
606 } 608 }
607 } 609 }
608 610
609 } // namespace extensions 611 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698