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

Unified Diff: chrome/browser/extensions/api/web_navigation/web_navigation_api.cc

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
Patch Set: updates 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 side-by-side diff with in-line comments
Download patch
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..b17dd6a14a8a4941702c0ac4d8e519720edd18b0 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"
@@ -229,6 +230,23 @@ 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::kReplacedTabIdKey,
+ 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);
+
+ DispatchEvent(browser_context, keys::kOnTabReplaced, args, GURL());
+}
+
} // namespace
@@ -397,22 +415,71 @@ WebNavigationEventRouter::PendingWebContents::~PendingWebContents() {}
WebNavigationEventRouter::WebNavigationEventRouter(Profile* profile)
: profile_(profile) {}
-WebNavigationEventRouter::~WebNavigationEventRouter() {}
+WebNavigationEventRouter::~WebNavigationEventRouter() {
+ BrowserList::RemoveObserver(this);
+}
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,

Powered by Google App Engine
This is Rietveld 408576698