 Chromium Code Reviews
 Chromium Code Reviews Issue 10692097:
  Introduce webNavigation.onTabReplaced event that is fired when a tab from instant or prerender repl…  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src
    
  
    Issue 10692097:
  Introduce webNavigation.onTabReplaced event that is fired when a tab from instant or prerender repl…  (Closed) 
  Base URL: svn://svn.chromium.org/chrome/trunk/src| Index: chrome/browser/extensions/api/web_navigation/web_navigation_api.cc | 
| diff --git a/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc b/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc | 
| index 82fe920c80c7217b6126fac03f45f1dc1834f97d..c170362e5786ff8f272fd3dfb8f88ebf8d9c688f 100644 | 
| --- a/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc | 
| +++ b/chrome/browser/extensions/api/web_navigation/web_navigation_api.cc | 
| @@ -16,6 +16,7 @@ | 
| #include "chrome/browser/extensions/extension_tab_util.h" | 
| #include "chrome/browser/profiles/profile.h" | 
| #include "chrome/browser/tab_contents/retargeting_details.h" | 
| +#include "chrome/browser/ui/browser.h" | 
| #include "chrome/browser/ui/tab_contents/tab_contents.h" | 
| #include "chrome/browser/view_type_utils.h" | 
| #include "chrome/common/chrome_notification_types.h" | 
| @@ -206,6 +207,7 @@ void DispatchOnCreatedNavigationTarget( | 
| dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 
| args.Append(dict); | 
| + // TODO(koz): Why not web_contents->GetURL() instead of target_url or both? | 
| DispatchEvent(browser_context, keys::kOnCreatedNavigationTarget, args, | 
| target_url); | 
| } | 
| @@ -229,6 +231,25 @@ void DispatchOnErrorOccurred(WebContents* web_contents, | 
| args, url); | 
| } | 
| +// Constructs and dispatches an onTabReplaced event. | 
| +void DispatchOnTabReplaced( | 
| + WebContents* old_web_contents, | 
| + BrowserContext* browser_context, | 
| + WebContents* new_web_contents) { | 
| + ListValue args; | 
| + DictionaryValue* dict = new DictionaryValue(); | 
| + dict->SetInteger(keys::kSourceTabIdKey, | 
| + ExtensionTabUtil::GetTabId(old_web_contents)); | 
| + dict->SetInteger(keys::kTabIdKey, | 
| + ExtensionTabUtil::GetTabId(new_web_contents)); | 
| + dict->SetDouble(keys::kTimeStampKey, MilliSecondsFromTime(base::Time::Now())); | 
| + args.Append(dict); | 
| + | 
| + // TODO(koz): Why not old_web_contents->GetURL() or both? | 
| + DispatchEvent(browser_context, keys::kOnTabReplaced, args, | 
| + new_web_contents->GetURL()); | 
| +} | 
| + | 
| } // namespace | 
| @@ -400,19 +421,66 @@ WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile) | 
| WebNavigationEventRouter::~WebNavigationEventRouter() {} | 
| 
battre
2012/07/05 15:38:46
The WebNavigationEventRouter is owned by the Exten
 
jochen (gone - plz use gerrit)
2012/07/06 11:13:50
Done.
 | 
| void WebNavigationEventRouter::Init() { | 
| - if (registrar_.IsEmpty()) { | 
| - registrar_.Add(this, | 
| - chrome::NOTIFICATION_RETARGETING, | 
| - content::NotificationService::AllSources()); | 
| - registrar_.Add(this, | 
| - chrome::NOTIFICATION_TAB_ADDED, | 
| - content::NotificationService::AllSources()); | 
| - registrar_.Add(this, | 
| - content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 
| - content::NotificationService::AllSources()); | 
| + if (!registrar_.IsEmpty()) | 
| + return; | 
| + registrar_.Add(this, | 
| + chrome::NOTIFICATION_RETARGETING, | 
| + content::NotificationService::AllSources()); | 
| + registrar_.Add(this, | 
| + chrome::NOTIFICATION_TAB_ADDED, | 
| + content::NotificationService::AllSources()); | 
| + registrar_.Add(this, | 
| + content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 
| + content::NotificationService::AllSources()); | 
| + | 
| + BrowserList::AddObserver(this); | 
| + for (BrowserList::const_iterator iter = BrowserList::begin(); | 
| + iter != BrowserList::end(); ++iter) { | 
| + OnBrowserAdded(*iter); | 
| } | 
| } | 
| +void WebNavigationEventRouter::OnBrowserAdded(Browser* browser) { | 
| + if (!profile_->IsSameProfile(browser->profile())) | 
| + return; | 
| + browser->tab_strip_model()->AddObserver(this); | 
| +} | 
| + | 
| +void WebNavigationEventRouter::OnBrowserRemoved(Browser* browser) { | 
| + if (!profile_->IsSameProfile(browser->profile())) | 
| + return; | 
| + browser->tab_strip_model()->RemoveObserver(this); | 
| +} | 
| + | 
| +void WebNavigationEventRouter::TabReplacedAt( | 
| + TabStripModel* tab_strip_model, | 
| + TabContents* old_contents, | 
| + TabContents* new_contents, | 
| + int index) { | 
| + WebNavigationTabObserver* tab_observer = | 
| + WebNavigationTabObserver::Get(old_contents->web_contents()); | 
| + if (!tab_observer) { | 
| + // If you hit this DCHECK(), please add reproduction steps to | 
| + // http://crbug.com/109464. | 
| + DCHECK(chrome::GetViewType(old_contents->web_contents()) != | 
| + chrome::VIEW_TYPE_TAB_CONTENTS); | 
| + return; | 
| + } | 
| + const FrameNavigationState& frame_navigation_state = | 
| + tab_observer->frame_navigation_state(); | 
| + | 
| + if (!frame_navigation_state.IsValidUrl( | 
| + old_contents->web_contents()->GetURL()) || | 
| + !frame_navigation_state.IsValidUrl( | 
| + new_contents->web_contents()->GetURL())) | 
| + return; | 
| + | 
| + DispatchOnTabReplaced( | 
| + old_contents->web_contents(), | 
| + profile_, | 
| + new_contents->web_contents()); | 
| +} | 
| + | 
| void WebNavigationEventRouter::Observe( | 
| int type, | 
| const content::NotificationSource& source, |