1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/sync/browser_synced_window_delegate.h"
9 #include "chrome/browser/sync/sessions/sessions_util.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_iterator.h"
13 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "components/sessions/session_id.h"
17 // static SyncedWindowDelegate implementations
19 namespace browser_sync
{
22 std::set
<const SyncedWindowDelegate
*> SyncedWindowDelegate::GetAll() {
23 std::set
<const SyncedWindowDelegate
*> synced_window_delegates
;
24 BrowserSyncedWindowDelegate::GetAllHelper(&synced_window_delegates
);
25 return synced_window_delegates
;
29 const SyncedWindowDelegate
* SyncedWindowDelegate::FindById(
30 SessionID::id_type id
) {
31 const SyncedWindowDelegate
* synced_window_delegate
=
32 BrowserSyncedWindowDelegate::FindByIdHelper(id
);
33 return synced_window_delegate
;
36 } // namespace browser_sync
38 // BrowserSyncedWindowDelegate implementations
40 BrowserSyncedWindowDelegate::BrowserSyncedWindowDelegate(Browser
* browser
)
41 : browser_(browser
) {}
43 BrowserSyncedWindowDelegate::~BrowserSyncedWindowDelegate() {}
46 void BrowserSyncedWindowDelegate::GetAllHelper(
47 std::set
<const browser_sync::SyncedWindowDelegate
*>* delegates
) {
48 // Add all the browser windows.
49 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
50 delegates
->insert(it
->synced_window_delegate());
54 const browser_sync::SyncedWindowDelegate
*
55 BrowserSyncedWindowDelegate::FindByIdHelper(SessionID::id_type id
) {
56 Browser
* browser
= chrome::FindBrowserWithID(id
);
57 return (browser
!= NULL
) ? browser
->synced_window_delegate() : NULL
;
60 bool BrowserSyncedWindowDelegate::IsTabPinned(
61 const browser_sync::SyncedTabDelegate
* tab
) const {
62 for (int i
= 0; i
< browser_
->tab_strip_model()->count(); i
++) {
63 browser_sync::SyncedTabDelegate
* current
= GetTabAt(i
);
65 return browser_
->tab_strip_model()->IsTabPinned(i
);
67 // The window and tab are not always updated atomically, so it's possible
68 // one of the values was stale. We'll retry later, just ignore for now.
72 browser_sync::SyncedTabDelegate
* BrowserSyncedWindowDelegate::GetTabAt(
74 return TabContentsSyncedTabDelegate::FromWebContents(
75 browser_
->tab_strip_model()->GetWebContentsAt(index
));
78 SessionID::id_type
BrowserSyncedWindowDelegate::GetTabIdAt(int index
) const {
79 return GetTabAt(index
)->GetSessionId();
82 bool BrowserSyncedWindowDelegate::HasWindow() const {
83 return browser_
->window() != NULL
;
86 SessionID::id_type
BrowserSyncedWindowDelegate::GetSessionId() const {
87 return browser_
->session_id().id();
90 int BrowserSyncedWindowDelegate::GetTabCount() const {
91 return browser_
->tab_strip_model()->count();
94 int BrowserSyncedWindowDelegate::GetActiveIndex() const {
95 return browser_
->tab_strip_model()->active_index();
98 bool BrowserSyncedWindowDelegate::IsApp() const {
99 return browser_
->is_app();
102 bool BrowserSyncedWindowDelegate::IsTypeTabbed() const {
103 return browser_
->is_type_tabbed();
106 bool BrowserSyncedWindowDelegate::IsTypePopup() const {
107 return browser_
->is_type_popup();
110 bool BrowserSyncedWindowDelegate::IsSessionRestoreInProgress() const {
114 bool BrowserSyncedWindowDelegate::ShouldSync() const {
117 return IsTypeTabbed() || IsTypePopup();