Commit 10b132b0 authored by dtrainor@chromium.org's avatar dtrainor@chromium.org

Refactor SyncedWindowDelegateAndroid into TabModel

Pull out a generic TabModel class for Android.  It owns SyncedWindowDelegate
like Browser owns a corresponding instance.  This will allow us to eventually
implement TabModelAndroid.

We no longer need to keep SyncedWindowDelegateRegistry, as we were using that
in non-sync related places to reach the TabModel methods.

BUG=
TEST=


Review URL: https://chromiumcodereview.appspot.com/10701030

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148798 0039d316-1c4b-4281-b951-d872f2087c98
parent 8b611c09
...@@ -179,6 +179,7 @@ ...@@ -179,6 +179,7 @@
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service.h" #include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_otr_state.h"
#include "chrome/common/child_process_logging.h" #include "chrome/common/child_process_logging.h"
#include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_result_codes.h" #include "chrome/common/chrome_result_codes.h"
...@@ -1863,7 +1864,7 @@ bool MetricsService::CanLogNotification( ...@@ -1863,7 +1864,7 @@ bool MetricsService::CanLogNotification(
// We simply don't log anything to UMA if there is a single incognito // We simply don't log anything to UMA if there is a single incognito
// session visible. The problem is that we always notify using the orginal // session visible. The problem is that we always notify using the orginal
// profile in order to simplify notification processing. // profile in order to simplify notification processing.
return !BrowserList::IsOffTheRecordSessionActive(); return !browser::IsOffTheRecordSessionActive();
} }
void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { void MetricsService::RecordBooleanPrefValue(const char* path, bool value) {
......
...@@ -13,7 +13,7 @@ void SessionRestore::RestoreForeignSessionTab( ...@@ -13,7 +13,7 @@ void SessionRestore::RestoreForeignSessionTab(
content::WebContents* source_web_contents, content::WebContents* source_web_contents,
const SessionTab& session_tab, const SessionTab& session_tab,
WindowOpenDisposition disposition) { WindowOpenDisposition disposition) {
NOTIMPLEMENTED() << "TODO(yfriedman): Upstream this."; NOTIMPLEMENTED() << "TODO(yfriedman, dtrainor): Upstream this.";
} }
// static // static
......
...@@ -2,29 +2,90 @@ ...@@ -2,29 +2,90 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/sync/glue/synced_window_delegate.h" #include "chrome/browser/sync/glue/synced_window_delegate_android.h"
#include "chrome/browser/profiles/profile.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/tab_contents/tab_contents.h"
#include "chrome/browser/sessions/session_id.h" #include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/sync/glue/synced_window_delegate_registry.h"
namespace browser_sync { namespace browser_sync {
// SyncedWindowDelegate implementations
const std::set<SyncedWindowDelegate*> const std::set<SyncedWindowDelegate*>
SyncedWindowDelegate::GetSyncedWindowDelegates() { SyncedWindowDelegate::GetSyncedWindowDelegates() {
return SyncedWindowDelegateRegistry::GetSyncedWindowDelegates(); std::set<SyncedWindowDelegate*> synced_window_delegates;
for (TabModelList::const_iterator i = TabModelList::begin();
i != TabModelList::end(); ++i) {
synced_window_delegates.insert((*i)->GetSyncedWindowDelegate());
}
return synced_window_delegates;
} }
const SyncedWindowDelegate* const SyncedWindowDelegate*
SyncedWindowDelegate::FindSyncedWindowDelegateWithId( SyncedWindowDelegate::FindSyncedWindowDelegateWithId(
SessionID::id_type session_id) { SessionID::id_type session_id) {
std::set<SyncedWindowDelegate*> window = TabModel* tab_model = TabModelList::FindTabModelWithId(
SyncedWindowDelegateRegistry::GetSyncedWindowDelegates(); session_id);
for (std::set<SyncedWindowDelegate*>::const_iterator i =
window.begin(); i != window.end(); ++i) { // In case we don't find the browser (e.g. for Developer Tools).
if ((*i)->GetSessionId() == session_id) return tab_model ? tab_model->GetSyncedWindowDelegate() : NULL;
return *i; }
}
return NULL; // SyncedWindowDelegateAndroid implementations
SyncedWindowDelegateAndroid::SyncedWindowDelegateAndroid(
TabModel* tab_model)
: tab_model_(tab_model) {}
SyncedWindowDelegateAndroid::~SyncedWindowDelegateAndroid() {}
bool SyncedWindowDelegateAndroid::HasWindow() const {
return !tab_model_->GetProfile()->IsOffTheRecord();
}
SessionID::id_type SyncedWindowDelegateAndroid::GetSessionId() const {
return tab_model_->GetSessionId();
}
int SyncedWindowDelegateAndroid::GetTabCount() const {
return tab_model_->GetTabCount();
}
int SyncedWindowDelegateAndroid::GetActiveIndex() const {
return tab_model_->GetActiveIndex();
}
bool SyncedWindowDelegateAndroid::IsApp() const {
return false;
}
bool SyncedWindowDelegateAndroid::IsTypeTabbed() const {
return true;
}
bool SyncedWindowDelegateAndroid::IsTypePopup() const {
return false;
}
bool SyncedWindowDelegateAndroid::IsTabPinned(
const SyncedTabDelegate* tab) const {
return false;
}
SyncedTabDelegate* SyncedWindowDelegateAndroid::GetTabAt(int index) const {
TabContents* tab_contents = tab_model_->GetTabContentsAt(index);
return tab_contents ? tab_contents->synced_tab_delegate() : NULL;
}
SessionID::id_type SyncedWindowDelegateAndroid::GetTabIdAt(int index) const {
return tab_model_->GetTabIdAt(index);
}
bool SyncedWindowDelegateAndroid::IsSessionRestoreInProgress() const {
return tab_model_->IsSessionRestoreInProgress();
} }
} // namespace browser_sync } // namespace browser_sync
// Copyright (c) 2012 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_WINDOW_DELEGATE_ANDROID_H_
#define CHROME_BROWSER_SYNC_GLUE_SYNCED_WINDOW_DELEGATE_ANDROID_H_
#include "base/compiler_specific.h"
#include "chrome/browser/sessions/session_id.h"
#include "chrome/browser/sync/glue/synced_window_delegate.h"
class TabModel;
namespace browser_sync {
class SyncedTabDelegate;
class SyncedWindowDelegateAndroid : public browser_sync::SyncedWindowDelegate {
public:
explicit SyncedWindowDelegateAndroid(TabModel* tab_model);
virtual ~SyncedWindowDelegateAndroid();
// browser_sync::SyncedWindowDelegate implementation.
virtual bool HasWindow() const OVERRIDE;
virtual SessionID::id_type GetSessionId() const OVERRIDE;
virtual int GetTabCount() const OVERRIDE;
virtual int GetActiveIndex() const OVERRIDE;
virtual bool IsApp() const OVERRIDE;
virtual bool IsTypeTabbed() const OVERRIDE;
virtual bool IsTypePopup() const OVERRIDE;
virtual bool IsTabPinned(const SyncedTabDelegate* tab) const OVERRIDE;
virtual SyncedTabDelegate* GetTabAt(int index) const OVERRIDE;
virtual SessionID::id_type GetTabIdAt(int index) const OVERRIDE;
virtual bool IsSessionRestoreInProgress() const OVERRIDE;
private:
TabModel* tab_model_;
DISALLOW_COPY_AND_ASSIGN(SyncedWindowDelegateAndroid);
};
} // namespace browser_sync
#endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_WINDOW_DELEGATE_ANDROID_H_
// Copyright (c) 2012 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_window_delegate_registry.h"
#include "base/lazy_instance.h"
#include "chrome/browser/sync/glue/synced_window_delegate.h"
namespace browser_sync {
namespace {
base::LazyInstance<std::set<SyncedWindowDelegate*> >::Leaky g_delegates =
LAZY_INSTANCE_INITIALIZER;
} // namespace
/* static */
void SyncedWindowDelegateRegistry::Register(SyncedWindowDelegate* delegate) {
g_delegates.Pointer()->insert(delegate);
}
/* static */
void SyncedWindowDelegateRegistry::Unregister(SyncedWindowDelegate* delegate) {
g_delegates.Pointer()->erase(delegate);
}
/* static */
const std::set<SyncedWindowDelegate*>&
SyncedWindowDelegateRegistry::GetSyncedWindowDelegates() {
return g_delegates.Get();
}
} // namespace browser_sync
// Copyright (c) 2012 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_WINDOW_DELEGATE_REGISTRY_H_
#define CHROME_BROWSER_SYNC_GLUE_SYNCED_WINDOW_DELEGATE_REGISTRY_H_
#include <set>
namespace browser_sync {
class SyncedWindowDelegate;
// A location to register SyncedWindowDelegates such that Android can provide
// pseudo-browsers for the purposes of Sync without having a dependency from
// sync onto Android's model of a browser.
class SyncedWindowDelegateRegistry {
public:
static const std::set<SyncedWindowDelegate*>& GetSyncedWindowDelegates();
static void Register(SyncedWindowDelegate* delegate);
static void Unregister(SyncedWindowDelegate* delegate);
};
} // namespace browser_sync
#endif // CHROME_BROWSER_SYNC_GLUE_SYNCED_WINDOW_DELEGATE_REGISTRY_H_
// Copyright (c) 2012 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/ui/android/tab_model/tab_model.h"
#include "base/logging.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_service.h"
#include "chrome/browser/sync/glue/synced_window_delegate.h"
#include "chrome/browser/sync/glue/synced_window_delegate_android.h"
using browser_sync::SyncedWindowDelegate;
using browser_sync::SyncedWindowDelegateAndroid;
using content::NotificationService;
TabModel::TabModel() {
}
TabModel::~TabModel() {
}
void TabModel::BroadcastSessionRestoreComplete() {
DCHECK(GetProfile());
NotificationService::current()->Notify(
chrome::NOTIFICATION_SESSION_RESTORE_COMPLETE,
content::Source<Profile>(GetProfile()),
NotificationService::NoDetails());
}
// Copyright (c) 2012 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_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_
#define CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_
#include "chrome/browser/sessions/session_id.h"
namespace browser_sync {
class SyncedWindowDelegate;
}
namespace content {
class WebContents;
}
class Profile;
class TabContents;
// Abstract representation of a Tab Model for Android. Since Android does
// not use Browser/BrowserList, this is required to allow Chrome to interact
// with Android's Tabs and Tab Model.
class TabModel {
public:
TabModel();
virtual ~TabModel();
virtual SessionID::id_type GetSessionId() const = 0;
virtual int GetTabCount() const = 0;
virtual int GetActiveIndex() const = 0;
virtual TabContents* GetTabContentsAt(int index) const = 0;
virtual SessionID::id_type GetTabIdAt(int index) const = 0;
// Used for restoring tabs from synced foreign sessions.
virtual void CreateTab(content::WebContents* web_contents) = 0;
// Return true if we are currently restoring sessions asynchronously.
virtual bool IsSessionRestoreInProgress() const = 0;
virtual void OpenClearBrowsingData() const = 0;
virtual Profile* GetProfile() const = 0;
virtual browser_sync::SyncedWindowDelegate* GetSyncedWindowDelegate() = 0;
protected:
// Instructs the TabModel to broadcast a notification that all tabs are now
// loaded from storage.
void BroadcastSessionRestoreComplete();
private:
DISALLOW_COPY_AND_ASSIGN(TabModel);
};
#endif // CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_H_
// Copyright (c) 2012 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/ui/android/tab_model/tab_model_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
namespace {
// Maintains and gives access to a static list of TabModel instances.
static TabModelList::TabModelVector& tab_models() {
CR_DEFINE_STATIC_LOCAL(TabModelList::TabModelVector,
tab_model_vector, ());
return tab_model_vector;
}
} // namespace
void TabModelList::AddTabModel(TabModel* tab_model) {
DCHECK(tab_model);
tab_models().push_back(tab_model);
}
void TabModelList::RemoveTabModel(TabModel* tab_model) {
DCHECK(tab_model);
TabModelList::iterator remove_tab_model =
std::find(tab_models().begin(), tab_models().end(), tab_model);
if (remove_tab_model != tab_models().end())
tab_models().erase(remove_tab_model);
}
TabModel* TabModelList::GetTabModelWithProfile(
Profile* profile) {
for (TabModelList::const_iterator i = TabModelList::begin();
i != TabModelList::end(); ++i) {
if ((*i)->GetProfile()->IsSameProfile(profile))
return *i;
}
return NULL;
}
TabModel* TabModelList::FindTabModelWithId(
SessionID::id_type desired_id) {
for (TabModelList::const_iterator i = TabModelList::begin();
i != TabModelList::end(); i++) {
if ((*i)->GetSessionId() == desired_id)
return *i;
}
return NULL;
}
bool TabModelList::IsOffTheRecordSessionActive() {
for (TabModelList::const_iterator i = TabModelList::begin();
i != TabModelList::end(); i++) {
if ((*i)->GetProfile()->IsOffTheRecord())
return true;
}
return false;
}
TabModelList::const_iterator TabModelList::begin() {
return tab_models().begin();
}
TabModelList::const_iterator TabModelList::end() {
return tab_models().end();
}
bool TabModelList::empty() {
return tab_models().empty();
}
size_t TabModelList::size() {
return tab_models().size();
}
// Copyright (c) 2012 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_UI_ANDROID_TAB_MODEL_TAB_MODEL_LIST_H_
#define CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_LIST_H_
#include <vector>
#include "chrome/browser/sessions/session_id.h"
class Profile;
class TabModel;
// Stores a list of all TabModel objects.
class TabModelList {
public:
typedef std::vector<TabModel*> TabModelVector;
typedef TabModelVector::iterator iterator;
typedef TabModelVector::const_iterator const_iterator;
static void AddTabModel(TabModel* tab_model);
static void RemoveTabModel(TabModel* tab_model);
static TabModel* GetTabModelWithProfile(Profile* profile);
static TabModel* FindTabModelWithId(SessionID::id_type desired_id);
static bool IsOffTheRecordSessionActive();
static const_iterator begin();
static const_iterator end();
static bool empty();
static size_t size();
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(TabModelList);
};
#endif // CHROME_BROWSER_UI_ANDROID_TAB_MODEL_TAB_MODEL_LIST_H_
// Copyright (c) 2012 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/ui/browser_otr_state.h"
#include "chrome/browser/ui/browser_list.h"
namespace browser {
bool IsOffTheRecordSessionActive() {
return BrowserList::IsOffTheRecordSessionActive();
}
}
// Copyright (c) 2012 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_UI_BROWSER_OTR_STATE_H_
#define CHROME_BROWSER_UI_BROWSER_OTR_STATE_H_
namespace browser {
bool IsOffTheRecordSessionActive();
}
#endif // CHROME_BROWSER_UI_BROWSER_OTR_STATE_H_
// Copyright (c) 2012 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/ui/browser_otr_state.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
namespace browser {
bool IsOffTheRecordSessionActive() {
return TabModelList::IsOffTheRecordSessionActive();
}
}
...@@ -49,6 +49,11 @@ ...@@ -49,6 +49,11 @@
#include "ui/base/layout.h" #include "ui/base/layout.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#if defined(OS_ANDROID)
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#endif
using content::UserMetricsAction; using content::UserMetricsAction;
using content::WebContents; using content::WebContents;
...@@ -283,7 +288,11 @@ void BrowsingHistoryHandler::HandleRemoveURLsOnOneDay(const ListValue* args) { ...@@ -283,7 +288,11 @@ void BrowsingHistoryHandler::HandleRemoveURLsOnOneDay(const ListValue* args) {
void BrowsingHistoryHandler::HandleClearBrowsingData(const ListValue* args) { void BrowsingHistoryHandler::HandleClearBrowsingData(const ListValue* args) {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
NOTIMPLEMENTED() << "TODO(yfriedman): Upstream the Android version."; Profile* profile = Profile::FromWebUI(web_ui());
const TabModel* tab_model =
TabModelList::GetTabModelWithProfile(profile);
if (tab_model)
tab_model->OpenClearBrowsingData();
#else #else
// TODO(beng): This is an improper direct dependency on Browser. Route this // TODO(beng): This is an improper direct dependency on Browser. Route this
// through some sort of delegate. // through some sort of delegate.
......
...@@ -2270,6 +2270,7 @@ ...@@ -2270,6 +2270,7 @@
'browser/sync/glue/synced_session_tracker.cc', 'browser/sync/glue/synced_session_tracker.cc',
'browser/sync/glue/synced_session_tracker.h', 'browser/sync/glue/synced_session_tracker.h',
'browser/sync/glue/synced_window_delegate.h', 'browser/sync/glue/synced_window_delegate.h',
'browser/sync/glue/synced_window_delegate_android.h',
'browser/sync/glue/synced_window_delegate_android.cc', 'browser/sync/glue/synced_window_delegate_android.cc',
'browser/sync/glue/theme_change_processor.cc', 'browser/sync/glue/theme_change_processor.cc',
'browser/sync/glue/theme_change_processor.h', 'browser/sync/glue/theme_change_processor.h',
...@@ -2399,6 +2400,10 @@ ...@@ -2399,6 +2400,10 @@
'browser/ui/android/ssl_client_certificate_selector.cc', 'browser/ui/android/ssl_client_certificate_selector.cc',
'browser/ui/android/tab_contents/chrome_web_contents_view_delegate_android.cc', 'browser/ui/android/tab_contents/chrome_web_contents_view_delegate_android.cc',
'browser/ui/android/tab_contents/chrome_web_contents_view_delegate_android.h', 'browser/ui/android/tab_contents/chrome_web_contents_view_delegate_android.h',
'browser/ui/android/tab_model/tab_model.cc',
'browser/ui/android/tab_model/tab_model.h',
'browser/ui/android/tab_model/tab_model_list.cc',
'browser/ui/android/tab_model/tab_model_list.h',
'browser/ui/android/tab_restore_service_delegate_android.cc', 'browser/ui/android/tab_restore_service_delegate_android.cc',
'browser/ui/app_modal_dialogs/app_modal_dialog.cc', 'browser/ui/app_modal_dialogs/app_modal_dialog.cc',
'browser/ui/app_modal_dialogs/app_modal_dialog.h', 'browser/ui/app_modal_dialogs/app_modal_dialog.h',
...@@ -2471,6 +2476,9 @@ ...@@ -2471,6 +2476,9 @@
'browser/ui/browser_mac.h', 'browser/ui/browser_mac.h',
'browser/ui/browser_navigator.cc', 'browser/ui/browser_navigator.cc',
'browser/ui/browser_navigator.h', 'browser/ui/browser_navigator.h',
'browser/ui/browser_otr_state_android.cc',
'browser/ui/browser_otr_state.cc',
'browser/ui/browser_otr_state.h',
'browser/ui/browser_ui_prefs.cc', 'browser/ui/browser_ui_prefs.cc',
'browser/ui/browser_ui_prefs.h', 'browser/ui/browser_ui_prefs.h',
'browser/ui/browser_tab_restore_service_delegate.cc', 'browser/ui/browser_tab_restore_service_delegate.cc',
...@@ -4882,10 +4890,6 @@ ...@@ -4882,10 +4890,6 @@
'dependencies': [ 'dependencies': [
'chrome_browser_jni_headers', 'chrome_browser_jni_headers',
], ],
'sources': [
'browser/sync/glue/synced_window_delegate_registry.cc',
'browser/sync/glue/synced_window_delegate_registry.h',
],
'sources!': [ 'sources!': [
'browser/bookmarks/bookmark_context_menu_controller.cc', 'browser/bookmarks/bookmark_context_menu_controller.cc',
# Bookmark export/import are handled via the BookmarkColumns # Bookmark export/import are handled via the BookmarkColumns
...@@ -4926,6 +4930,7 @@ ...@@ -4926,6 +4930,7 @@
'browser/ui/browser_finder.cc', 'browser/ui/browser_finder.cc',
'browser/ui/browser_list.cc', 'browser/ui/browser_list.cc',
'browser/ui/browser_navigator.cc', 'browser/ui/browser_navigator.cc',
'browser/ui/browser_otr_state.cc',
'browser/ui/browser_tab_restore_service_delegate.cc', 'browser/ui/browser_tab_restore_service_delegate.cc',
'browser/ui/browser_tabstrip.cc', 'browser/ui/browser_tabstrip.cc',
'browser/ui/browser_tabstrip.h', 'browser/ui/browser_tabstrip.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment