diff options
13 files changed, 199 insertions, 23 deletions
diff --git a/chrome/browser/android/tab_android.cc b/chrome/browser/android/tab_android.cc index 96368a8..f406226 100644 --- a/chrome/browser/android/tab_android.cc +++ b/chrome/browser/android/tab_android.cc @@ -24,7 +24,6 @@ #include "chrome/browser/ui/browser_tab_contents.h" #include "chrome/browser/ui/find_bar/find_tab_helper.h" #include "chrome/browser/ui/prefs/prefs_tab_helper.h" -#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" #include "chrome/browser/ui/tab_contents/core_tab_helper.h" #include "chrome/browser/ui/toolbar/toolbar_model_impl.h" #include "components/autofill/browser/autofill_external_delegate.h" @@ -86,7 +85,6 @@ void BrowserTabContents::AttachTabHelpers(WebContents* contents) { PrefsTabHelper::CreateForWebContents(contents); prerender::PrerenderTabHelper::CreateForWebContents(contents); SSLTabHelper::CreateForWebContents(contents); - TabContentsSyncedTabDelegate::CreateForWebContents(contents); TabSpecificContentSettings::CreateForWebContents(contents); TranslateTabHelper::CreateForWebContents(contents); WindowAndroidHelper::CreateForWebContents(contents); @@ -104,12 +102,16 @@ WebContents* TabAndroid::InitWebContentsFromView(JNIEnv* env, WebContents* web_contents = content_view_core->GetWebContents(); DCHECK(web_contents); InitTabHelpers(web_contents); + // Make sure tab id is same as web contents id. This means tab id can change + // based on when web_contents are attached to tab. + // TODO(shashishekhar): Add a new notification for this, so any + // observers can make appropriate state changes. + tab_id_.set_id(SessionTabHelper::FromWebContents(web_contents) + ->session_id().id()); return web_contents; } -TabAndroid::TabAndroid(JNIEnv* env, jobject obj) - : tab_id_(-1), - weak_java_tab_(env, obj) { +TabAndroid::TabAndroid(JNIEnv* env, jobject obj) : weak_java_tab_(env, obj) { Java_TabBase_setNativePtr(env, obj, reinterpret_cast<jint>(this)); } diff --git a/chrome/browser/android/tab_android.h b/chrome/browser/android/tab_android.h index 46edfc5..ed92df1 100644 --- a/chrome/browser/android/tab_android.h +++ b/chrome/browser/android/tab_android.h @@ -10,6 +10,7 @@ #include "base/android/jni_helper.h" #include "base/callback_forward.h" #include "base/string16.h" +#include "chrome/browser/sessions/session_id.h" #include "chrome/browser/ui/toolbar/toolbar_model.h" class GURL; @@ -41,9 +42,7 @@ class TabAndroid { virtual ToolbarModel::SecurityLevel GetSecurityLevel(); - int id() const { - return tab_id_; - } + const SessionID& id() const { return tab_id_; } virtual void OnReceivedHttpAuthRequest(jobject auth_handler, const string16& host, @@ -78,10 +77,10 @@ class TabAndroid { static void InitTabHelpers(content::WebContents* web_contents); - static content::WebContents* InitWebContentsFromView(JNIEnv* env, - jobject content_view); + content::WebContents* InitWebContentsFromView(JNIEnv* env, + jobject content_view); - int tab_id_; + SessionID tab_id_; private: JavaObjectWeakGlobalRef weak_java_tab_; diff --git a/chrome/browser/sync/glue/session_model_associator.cc b/chrome/browser/sync/glue/session_model_associator.cc index e65c4bd..32b09dd 100644 --- a/chrome/browser/sync/glue/session_model_associator.cc +++ b/chrome/browser/sync/glue/session_model_associator.cc @@ -213,10 +213,10 @@ bool SessionModelAssociator::AssociateWindows(bool reload_tabs, if (reload_tabs) { SyncedTabDelegate* tab = (*i)->GetTabAt(j); - // It's possible for GetTabAt to return a null tab if it's not in - // memory. We can assume this means the tab already existed but hasn't - // changed, so no need to reassociate. - if (tab && !AssociateTab(*tab, error)) { + // It's possible for GetTabAt to return a tab which has no web + // contents. We can assume this means the tab already existed but + // hasn't changed, so no need to reassociate. + if (tab->HasWebContents() && !AssociateTab(*tab, error)) { // Association failed. Either we need to re-associate, or this is an // unrecoverable error. return false; diff --git a/chrome/browser/sync/glue/session_model_associator_unittest.cc b/chrome/browser/sync/glue/session_model_associator_unittest.cc index 12ccd3c..f63e44b 100644 --- a/chrome/browser/sync/glue/session_model_associator_unittest.cc +++ b/chrome/browser/sync/glue/session_model_associator_unittest.cc @@ -189,6 +189,7 @@ class SyncedTabDelegateMock : public SyncedTabDelegate { MOCK_CONST_METHOD0(GetBlockedNavigations, const std::vector<const content::NavigationEntry*>*()); MOCK_CONST_METHOD0(IsPinned, bool()); + MOCK_CONST_METHOD0(HasWebContents, bool()); }; class SyncRefreshListener : public content::NotificationObserver { diff --git a/chrome/browser/sync/glue/synced_tab_delegate.h b/chrome/browser/sync/glue/synced_tab_delegate.h index 346d88c..e35a307 100644 --- a/chrome/browser/sync/glue/synced_tab_delegate.h +++ b/chrome/browser/sync/glue/synced_tab_delegate.h @@ -51,6 +51,7 @@ class SyncedTabDelegate { GetBlockedNavigations() const = 0; virtual bool IsPinned() const = 0; + virtual bool HasWebContents() const = 0; }; } // namespace browser_sync diff --git a/chrome/browser/sync/glue/synced_tab_delegate_android.cc b/chrome/browser/sync/glue/synced_tab_delegate_android.cc new file mode 100644 index 0000000..e8eefd4 --- /dev/null +++ b/chrome/browser/sync/glue/synced_tab_delegate_android.cc @@ -0,0 +1,106 @@ +// Copyright 2013 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/glue/synced_tab_delegate_android.h" + +#include "base/memory/ref_counted.h" +#include "chrome/browser/android/tab_android.h" +#include "chrome/browser/extensions/tab_helper.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/sessions/session_tab_helper.h" +#include "chrome/browser/sync/glue/synced_window_delegate.h" +#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" +#include "chrome/common/extensions/extension.h" +#include "content/public/browser/navigation_controller.h" +#include "content/public/browser/navigation_entry.h" +#include "content/public/browser/web_contents.h" + +using content::NavigationEntry; + +SyncedTabDelegateAndroid::SyncedTabDelegateAndroid(TabAndroid* tab_android) + : web_contents_(NULL), tab_android_(tab_android) {} + +SyncedTabDelegateAndroid::~SyncedTabDelegateAndroid() {} + +SessionID::id_type SyncedTabDelegateAndroid::GetWindowId() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetWindowId(); +} + +SessionID::id_type SyncedTabDelegateAndroid::GetSessionId() const { + return tab_android_->id().id(); +} + +bool SyncedTabDelegateAndroid::IsBeingDestroyed() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->IsBeingDestroyed(); +} + +Profile* SyncedTabDelegateAndroid::profile() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->profile(); +} + +std::string SyncedTabDelegateAndroid::GetExtensionAppId() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetExtensionAppId(); +} + +int SyncedTabDelegateAndroid::GetCurrentEntryIndex() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetCurrentEntryIndex(); +} + +int SyncedTabDelegateAndroid::GetEntryCount() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetEntryCount(); +} + +int SyncedTabDelegateAndroid::GetPendingEntryIndex() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetPendingEntryIndex(); +} + +NavigationEntry* SyncedTabDelegateAndroid::GetPendingEntry() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetPendingEntry(); +} + +NavigationEntry* SyncedTabDelegateAndroid::GetEntryAtIndex(int i) const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetEntryAtIndex(i); +} + +NavigationEntry* SyncedTabDelegateAndroid::GetActiveEntry() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetActiveEntry(); +} + +bool SyncedTabDelegateAndroid::IsPinned() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->IsPinned(); +} + +bool SyncedTabDelegateAndroid::HasWebContents() const { + return web_contents_ != NULL; +} + +void SyncedTabDelegateAndroid::SetWebContents( + content::WebContents* web_contents) { + web_contents_ = web_contents; + TabContentsSyncedTabDelegate::CreateForWebContents(web_contents_); +} + +void SyncedTabDelegateAndroid::ResetWebContents() { web_contents_ = NULL; } + +bool SyncedTabDelegateAndroid::ProfileIsManaged() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->ProfileIsManaged(); +} + +const std::vector<const content::NavigationEntry*>* +SyncedTabDelegateAndroid::GetBlockedNavigations() const { + return TabContentsSyncedTabDelegate::FromWebContents(web_contents_) + ->GetBlockedNavigations(); +} diff --git a/chrome/browser/sync/glue/synced_tab_delegate_android.h b/chrome/browser/sync/glue/synced_tab_delegate_android.h new file mode 100644 index 0000000..ee50882 --- /dev/null +++ b/chrome/browser/sync/glue/synced_tab_delegate_android.h @@ -0,0 +1,60 @@ +// Copyright 2013 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. + +#ifndef CHROME_BROWSER_SYNC_GLUE_SYNCED_TAB_DELEGATE_ANDROID_H_ +#define CHROME_BROWSER_SYNC_GLUE_SYNCED_TAB_DELEGATE_ANDROID_H_ + +#include "base/compiler_specific.h" +#include "chrome/browser/sync/glue/synced_tab_delegate.h" +#include "content/public/browser/web_contents_user_data.h" + +namespace content { +class WebContents; +} + +class TabAndroid; +// On Android a tab can exist even without web contents. + +// SyncedTabDelegateAndroid wraps TabContentsSyncedTabDelegate and provides +// a method to set web contents later when tab is brought to memory. +class SyncedTabDelegateAndroid : public browser_sync::SyncedTabDelegate { + public: + explicit SyncedTabDelegateAndroid(TabAndroid* owning_tab_); + virtual ~SyncedTabDelegateAndroid(); + + // Methods from SyncedTabDelegate. + virtual SessionID::id_type GetWindowId() const OVERRIDE; + virtual SessionID::id_type GetSessionId() const OVERRIDE; + virtual bool IsBeingDestroyed() const OVERRIDE; + virtual Profile* profile() const OVERRIDE; + virtual std::string GetExtensionAppId() const OVERRIDE; + virtual int GetCurrentEntryIndex() const OVERRIDE; + virtual int GetEntryCount() const OVERRIDE; + virtual int GetPendingEntryIndex() const OVERRIDE; + virtual content::NavigationEntry* GetPendingEntry() const OVERRIDE; + virtual content::NavigationEntry* GetEntryAtIndex(int i) const OVERRIDE; + virtual content::NavigationEntry* GetActiveEntry() const OVERRIDE; + virtual bool IsPinned() const OVERRIDE; + virtual bool HasWebContents() const OVERRIDE; + + // Managed user related methods. + + virtual bool ProfileIsManaged() const OVERRIDE; + virtual const std::vector<const content::NavigationEntry*>* + GetBlockedNavigations() const OVERRIDE; + + // Set the web contents for this tab. Also creates + // TabContentsSyncedTabDelegate for this tab. + virtual void SetWebContents(content::WebContents* web_contents); + // Set web contents to null. + virtual void ResetWebContents(); + + private: + content::WebContents* web_contents_; + TabAndroid* tab_android_; + + DISALLOW_COPY_AND_ASSIGN(SyncedTabDelegateAndroid); +}; + +#endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_TAB_DELEGATE_ANDROID_H_ diff --git a/chrome/browser/sync/glue/synced_window_delegate_android.cc b/chrome/browser/sync/glue/synced_window_delegate_android.cc index 4d8f4ba..e66e09b 100644 --- a/chrome/browser/sync/glue/synced_window_delegate_android.cc +++ b/chrome/browser/sync/glue/synced_window_delegate_android.cc @@ -5,10 +5,9 @@ #include "chrome/browser/sync/glue/synced_window_delegate_android.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/sync/glue/synced_tab_delegate_android.h" #include "chrome/browser/ui/android/tab_model/tab_model.h" #include "chrome/browser/ui/android/tab_model/tab_model_list.h" -#include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" -#include "chrome/browser/sessions/session_id.h" #include "content/public/browser/web_contents.h" namespace browser_sync { @@ -77,13 +76,11 @@ bool SyncedWindowDelegateAndroid::IsTabPinned( } SyncedTabDelegate* SyncedWindowDelegateAndroid::GetTabAt(int index) const { - content::WebContents* web_contents = tab_model_->GetWebContentsAt(index); - return web_contents ? TabContentsSyncedTabDelegate::FromWebContents( - web_contents) : NULL; + return tab_model_->GetTabAt(index); } SessionID::id_type SyncedWindowDelegateAndroid::GetTabIdAt(int index) const { - return tab_model_->GetTabIdAt(index); + return GetTabAt(index)->GetSessionId(); } bool SyncedWindowDelegateAndroid::IsSessionRestoreInProgress() const { diff --git a/chrome/browser/ui/android/tab_model/tab_model.h b/chrome/browser/ui/android/tab_model/tab_model.h index 32f8fcf..59e7be5 100644 --- a/chrome/browser/ui/android/tab_model/tab_model.h +++ b/chrome/browser/ui/android/tab_model/tab_model.h @@ -7,6 +7,7 @@ #include "base/memory/scoped_ptr.h" #include "chrome/browser/sessions/session_id.h" +#include "chrome/browser/sync/glue/synced_tab_delegate.h" #include "chrome/browser/ui/toolbar/toolbar_model.h" #include "chrome/browser/ui/toolbar/toolbar_model_delegate.h" #include "content/public/browser/notification_observer.h" @@ -15,6 +16,7 @@ namespace browser_sync { class SyncedWindowDelegate; class SyncedWindowDelegateAndroid; +class SyncedTabDelegate; } namespace content { @@ -46,7 +48,7 @@ class TabModel : public content::NotificationObserver, virtual int GetTabCount() const = 0; virtual int GetActiveIndex() const = 0; virtual content::WebContents* GetWebContentsAt(int index) const = 0; - virtual SessionID::id_type GetTabIdAt(int index) const = 0; + virtual browser_sync::SyncedTabDelegate* GetTabAt(int index) const = 0; // Used for restoring tabs from synced foreign sessions. virtual void CreateTab(content::WebContents* web_contents) = 0; diff --git a/chrome/browser/ui/android/tab_model/tab_model_unittest.cc b/chrome/browser/ui/android/tab_model/tab_model_unittest.cc index 439b2bc..4a5d6d5 100644 --- a/chrome/browser/ui/android/tab_model/tab_model_unittest.cc +++ b/chrome/browser/ui/android/tab_model/tab_model_unittest.cc @@ -33,10 +33,13 @@ class TestTabModel : public TabModel { virtual content::WebContents* GetWebContentsAt(int index) const OVERRIDE { return NULL; } - virtual SessionID::id_type GetTabIdAt(int index) const OVERRIDE { return 0; } virtual void CreateTab(content::WebContents* web_contents) OVERRIDE {} virtual bool IsSessionRestoreInProgress() const OVERRIDE { return false; } virtual void OpenClearBrowsingData() const OVERRIDE {} + virtual browser_sync::SyncedTabDelegate* GetTabAt(int index) const OVERRIDE { + return NULL; + } + }; TEST_F(TabModelTest, TestProfileHandling) { diff --git a/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.cc b/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.cc index 65617f2..ab71b2e 100644 --- a/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.cc +++ b/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.cc @@ -103,3 +103,5 @@ bool TabContentsSyncedTabDelegate::IsPinned() const { // We might not have a parent window, e.g. Developer Tools. return window ? window->IsTabPinned(this) : false; } + +bool TabContentsSyncedTabDelegate::HasWebContents() const { return true; } diff --git a/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h b/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h index 51dfa18..d58b130 100644 --- a/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h +++ b/chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h @@ -36,6 +36,7 @@ class TabContentsSyncedTabDelegate virtual const std::vector<const content::NavigationEntry*>* GetBlockedNavigations() const OVERRIDE; virtual bool IsPinned() const OVERRIDE; + virtual bool HasWebContents() const OVERRIDE; private: explicit TabContentsSyncedTabDelegate(content::WebContents* web_contents); diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 93a1429..367c633 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -2081,6 +2081,8 @@ 'browser/sync/glue/synced_session.h', 'browser/sync/glue/synced_session_tracker.cc', 'browser/sync/glue/synced_session_tracker.h', + 'browser/sync/glue/synced_tab_delegate_android.cc', + 'browser/sync/glue/synced_tab_delegate_android.h', 'browser/sync/glue/synced_window_delegate.h', 'browser/sync/glue/synced_window_delegate_android.cc', 'browser/sync/glue/synced_window_delegate_android.h', |