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

Unified Diff: chrome/browser/sync/sessions/sync_sessions_web_contents_router.cc

Issue 2753753005: [sync] WebContentsObserver based sessions notifications (Closed)
Patch Set: [sync] WebContentsObserver based sessions notifications Created 3 years, 9 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/sync/sessions/sync_sessions_web_contents_router.cc
diff --git a/chrome/browser/sync/sessions/sync_sessions_web_contents_router.cc b/chrome/browser/sync/sessions/sync_sessions_web_contents_router.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ccd99496f65777b546df17b9ab4e99ee103e99f9
--- /dev/null
+++ b/chrome/browser/sync/sessions/sync_sessions_web_contents_router.cc
@@ -0,0 +1,111 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h"
+
+#include "chrome/browser/history/history_service_factory.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/browser_list.h"
+#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "components/history/core/browser/history_service.h"
+#include "components/sync_sessions/synced_tab_delegate.h"
+
+namespace sync_sessions {
+
+SyncSessionsWebContentsRouter::SyncSessionsWebContentsRouter(Profile* profile) {
+ handler_ = nullptr;
Nicolas Zea 2017/03/16 19:57:43 can remove if you initialize in the header
Patrick Noland 2017/03/17 00:20:10 Done.
+
+ history::HistoryService* history_service =
+ HistoryServiceFactory::GetForProfile(profile,
+ ServiceAccessType::EXPLICIT_ACCESS);
+ if (history_service) {
+ favicon_changed_subscription_ = history_service->AddFaviconsChangedCallback(
+ base::Bind(&SyncSessionsWebContentsRouter::OnFaviconsChanged,
+ base::Unretained(this)));
+ }
+// Android has no BrowserList or TabStripModel, so we exclude code that refers
Nicolas Zea 2017/03/16 19:57:43 nit: newline above
Patrick Noland 2017/03/17 00:20:10 Done.
+// to those two things. For non-android platforms, this code is used to
+// ensure we are notified of tabs being added and moved between tab strips.
+#if !defined(OS_ANDROID)
+ BrowserList* browser_list = BrowserList::GetInstance();
+ for (Browser* browser : *browser_list)
+ browser->tab_strip_model()->AddObserver(this);
+ browser_list->AddObserver(this);
+#endif // !defined(OS_ANDROID)
+}
+
+SyncSessionsWebContentsRouter::~SyncSessionsWebContentsRouter() {}
+
+void SyncSessionsWebContentsRouter::NotifyTabModified(
+ content::WebContents* web_contents) {
+ if (handler_ && web_contents) {
+ SyncedTabDelegate* delegate =
+ TabContentsSyncedTabDelegate::FromWebContents(web_contents);
+ if (delegate)
+ handler_->OnLocalTabModified(delegate);
+ }
+
+ if (!flare_.is_null()) {
+ flare_.Run(syncer::SESSIONS);
+ flare_.Reset();
+ }
+}
+
+void SyncSessionsWebContentsRouter::InjectStartSyncFlare(
+ syncer::SyncableService::StartSyncFlare flare) {
+ flare_ = flare;
+}
+
+void SyncSessionsWebContentsRouter::StartRoutingTo(
+ LocalSessionEventHandler* handler) {
+ handler_ = handler;
+}
+
+void SyncSessionsWebContentsRouter::Stop() {
+ handler_ = nullptr;
+}
+
+void SyncSessionsWebContentsRouter::OnFaviconsChanged(
+ const std::set<GURL>& page_urls,
+ const GURL& icon_url) {
+ if (handler_)
+ handler_->OnFaviconsChanged(page_urls, icon_url);
+}
+
+void SyncSessionsWebContentsRouter::Shutdown() {
+ favicon_changed_subscription_.reset();
+#if !defined(OS_ANDROID)
+ BrowserList* browser_list = BrowserList::GetInstance();
+ for (Browser* browser : *browser_list)
+ browser->tab_strip_model()->RemoveObserver(this);
+ BrowserList::GetInstance()->RemoveObserver(this);
+#endif
+}
+
+#if !defined(OS_ANDROID)
+void SyncSessionsWebContentsRouter::OnBrowserAdded(Browser* browser) {
+ browser->tab_strip_model()->AddObserver(this);
+}
+
+void SyncSessionsWebContentsRouter::OnBrowserRemoved(Browser* browser) {
+ browser->tab_strip_model()->RemoveObserver(this);
+}
+
+void SyncSessionsWebContentsRouter::TabInsertedAt(
+ TabStripModel* model,
+ content::WebContents* web_contents,
+ int index,
+ bool foreground) {
+ if (handler_ && web_contents) {
+ SyncedTabDelegate* delegate =
+ TabContentsSyncedTabDelegate::FromWebContents(web_contents);
+ if (delegate)
+ handler_->OnLocalTabModified(delegate);
+ }
+}
+#endif // !defined(OS_ANDROID)
+
+} // namespace sync_sessions

Powered by Google App Engine
This is Rietveld 408576698