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

Side by Side Diff: chrome/browser/ui/sync/tab_contents_synced_tab_delegate.cc

Issue 1408643002: [Sync] Componentize synced_tab_delegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix test broken by rebase Created 5 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" 5 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h"
6 6
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "chrome/browser/profiles/profile.h" 8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/sessions/session_tab_helper.h" 9 #include "chrome/browser/sessions/session_tab_helper.h"
10 #include "components/sessions/content/content_serialized_navigation_builder.h"
10 #include "components/sync_driver/glue/synced_window_delegate.h" 11 #include "components/sync_driver/glue/synced_window_delegate.h"
12 #include "components/sync_driver/sessions/synced_window_delegates_getter.h"
13 #include "components/sync_sessions/sync_sessions_client.h"
14 #include "content/public/browser/favicon_status.h"
11 #include "content/public/browser/navigation_controller.h" 15 #include "content/public/browser/navigation_controller.h"
12 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
13 #include "content/public/browser/web_contents.h" 17 #include "content/public/browser/web_contents.h"
14 18
15 #if defined(ENABLE_EXTENSIONS) 19 #if defined(ENABLE_EXTENSIONS)
16 #include "chrome/browser/extensions/tab_helper.h" 20 #include "chrome/browser/extensions/tab_helper.h"
17 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
18 #endif 22 #endif
19 23
20 #if defined(ENABLE_SUPERVISED_USERS) 24 #if defined(ENABLE_SUPERVISED_USERS)
21 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h" 25 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
22 #endif 26 #endif
23 27
24 using content::NavigationEntry; 28 using content::NavigationEntry;
25 29
26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabContentsSyncedTabDelegate); 30 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabContentsSyncedTabDelegate);
27 31
32 namespace {
33
34 // Helper to access the correct NavigationEntry, accounting for pending entries.
35 NavigationEntry* GetPossiblyPendingEntryAtIndex(
36 content::WebContents* web_contents,
37 int i) {
38 int pending_index = web_contents->GetController().GetPendingEntryIndex();
39 return (pending_index == i)
40 ? web_contents->GetController().GetPendingEntry()
41 : web_contents->GetController().GetEntryAtIndex(i);
42 }
43
44 } // namespace
45
28 TabContentsSyncedTabDelegate::TabContentsSyncedTabDelegate( 46 TabContentsSyncedTabDelegate::TabContentsSyncedTabDelegate(
29 content::WebContents* web_contents) 47 content::WebContents* web_contents)
30 : web_contents_(web_contents), sync_session_id_(0) {} 48 : web_contents_(web_contents), sync_session_id_(0) {}
31 49
32 TabContentsSyncedTabDelegate::~TabContentsSyncedTabDelegate() {} 50 TabContentsSyncedTabDelegate::~TabContentsSyncedTabDelegate() {}
33 51
34 SessionID::id_type TabContentsSyncedTabDelegate::GetWindowId() const { 52 SessionID::id_type TabContentsSyncedTabDelegate::GetWindowId() const {
35 return SessionTabHelper::FromWebContents(web_contents_)->window_id().id(); 53 return SessionTabHelper::FromWebContents(web_contents_)->window_id().id();
36 } 54 }
37 55
38 SessionID::id_type TabContentsSyncedTabDelegate::GetSessionId() const { 56 SessionID::id_type TabContentsSyncedTabDelegate::GetSessionId() const {
39 return SessionTabHelper::FromWebContents(web_contents_)->session_id().id(); 57 return SessionTabHelper::FromWebContents(web_contents_)->session_id().id();
40 } 58 }
41 59
42 bool TabContentsSyncedTabDelegate::IsBeingDestroyed() const { 60 bool TabContentsSyncedTabDelegate::IsBeingDestroyed() const {
43 return web_contents_->IsBeingDestroyed(); 61 return web_contents_->IsBeingDestroyed();
44 } 62 }
45 63
46 Profile* TabContentsSyncedTabDelegate::profile() const {
47 return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
48 }
49
50 std::string TabContentsSyncedTabDelegate::GetExtensionAppId() const { 64 std::string TabContentsSyncedTabDelegate::GetExtensionAppId() const {
51 #if defined(ENABLE_EXTENSIONS) 65 #if defined(ENABLE_EXTENSIONS)
52 const scoped_refptr<const extensions::Extension> extension_app( 66 const scoped_refptr<const extensions::Extension> extension_app(
53 extensions::TabHelper::FromWebContents(web_contents_)->extension_app()); 67 extensions::TabHelper::FromWebContents(web_contents_)->extension_app());
54 if (extension_app.get()) 68 if (extension_app.get())
55 return extension_app->id(); 69 return extension_app->id();
56 #endif 70 #endif
57 return std::string(); 71 return std::string();
58 } 72 }
59 73
60 bool TabContentsSyncedTabDelegate::IsInitialBlankNavigation() const { 74 bool TabContentsSyncedTabDelegate::IsInitialBlankNavigation() const {
61 return web_contents_->GetController().IsInitialBlankNavigation(); 75 return web_contents_->GetController().IsInitialBlankNavigation();
62 } 76 }
63 77
64 int TabContentsSyncedTabDelegate::GetCurrentEntryIndex() const { 78 int TabContentsSyncedTabDelegate::GetCurrentEntryIndex() const {
65 return web_contents_->GetController().GetCurrentEntryIndex(); 79 return web_contents_->GetController().GetCurrentEntryIndex();
66 } 80 }
67 81
68 int TabContentsSyncedTabDelegate::GetEntryCount() const { 82 int TabContentsSyncedTabDelegate::GetEntryCount() const {
69 return web_contents_->GetController().GetEntryCount(); 83 return web_contents_->GetController().GetEntryCount();
70 } 84 }
71 85
72 int TabContentsSyncedTabDelegate::GetPendingEntryIndex() const { 86 GURL TabContentsSyncedTabDelegate::GetVirtualURLAtIndex(int i) const {
73 return web_contents_->GetController().GetPendingEntryIndex(); 87 NavigationEntry* entry = GetPossiblyPendingEntryAtIndex(web_contents_, i);
88 return entry->GetVirtualURL();
74 } 89 }
75 90
76 NavigationEntry* TabContentsSyncedTabDelegate::GetPendingEntry() const { 91 GURL TabContentsSyncedTabDelegate::GetFaviconURLAtIndex(int i) const {
77 return web_contents_->GetController().GetPendingEntry(); 92 NavigationEntry* entry = GetPossiblyPendingEntryAtIndex(web_contents_, i);
93 return (entry->GetFavicon().valid ? entry->GetFavicon().url : GURL());
78 } 94 }
79 95
80 NavigationEntry* TabContentsSyncedTabDelegate::GetEntryAtIndex(int i) const { 96 ui::PageTransition TabContentsSyncedTabDelegate::GetTransitionAtIndex(
81 return web_contents_->GetController().GetEntryAtIndex(i); 97 int i) const {
98 NavigationEntry* entry = GetPossiblyPendingEntryAtIndex(web_contents_, i);
99 return entry->GetTransitionType();
82 } 100 }
83 101
84 NavigationEntry* TabContentsSyncedTabDelegate::GetActiveEntry() const { 102 void TabContentsSyncedTabDelegate::GetSerializedNavigationAtIndex(
85 return web_contents_->GetController().GetVisibleEntry(); 103 int i,
104 sessions::SerializedNavigationEntry* serialized_entry) const {
105 NavigationEntry* entry = GetPossiblyPendingEntryAtIndex(web_contents_, i);
106 *serialized_entry =
107 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry(i,
108 *entry);
86 } 109 }
87 110
88 bool TabContentsSyncedTabDelegate::ProfileIsSupervised() const { 111 bool TabContentsSyncedTabDelegate::ProfileIsSupervised() const {
89 return profile()->IsSupervised(); 112 return Profile::FromBrowserContext(web_contents_->GetBrowserContext())
113 ->IsSupervised();
90 } 114 }
91 115
92 const std::vector<const content::NavigationEntry*>* 116 const std::vector<const sessions::SerializedNavigationEntry*>*
93 TabContentsSyncedTabDelegate::GetBlockedNavigations() const { 117 TabContentsSyncedTabDelegate::GetBlockedNavigations() const {
94 #if defined(ENABLE_SUPERVISED_USERS) 118 #if defined(ENABLE_SUPERVISED_USERS)
95 SupervisedUserNavigationObserver* navigation_observer = 119 SupervisedUserNavigationObserver* navigation_observer =
96 SupervisedUserNavigationObserver::FromWebContents(web_contents_); 120 SupervisedUserNavigationObserver::FromWebContents(web_contents_);
97 DCHECK(navigation_observer); 121 DCHECK(navigation_observer);
98 return navigation_observer->blocked_navigations(); 122 return navigation_observer->blocked_navigations();
99 #else 123 #else
100 NOTREACHED(); 124 NOTREACHED();
101 return NULL; 125 return NULL;
102 #endif 126 #endif
103 } 127 }
104 128
105 bool TabContentsSyncedTabDelegate::IsPinned() const { 129 bool TabContentsSyncedTabDelegate::IsPlaceholderTab() const {
106 const browser_sync::SyncedWindowDelegate* window = GetSyncedWindowDelegate(); 130 return false;
107
108 // We might not have a parent window, e.g. Developer Tools.
109 return window ? window->IsTabPinned(this) : false;
110 }
111
112 bool TabContentsSyncedTabDelegate::HasWebContents() const { return true; }
113
114 content::WebContents* TabContentsSyncedTabDelegate::GetWebContents() const {
115 return web_contents_;
116 } 131 }
117 132
118 int TabContentsSyncedTabDelegate::GetSyncId() const { 133 int TabContentsSyncedTabDelegate::GetSyncId() const {
119 return sync_session_id_; 134 return sync_session_id_;
120 } 135 }
121 136
122 void TabContentsSyncedTabDelegate::SetSyncId(int sync_id) { 137 void TabContentsSyncedTabDelegate::SetSyncId(int sync_id) {
123 sync_session_id_ = sync_id; 138 sync_session_id_ = sync_id;
124 } 139 }
140
141 bool TabContentsSyncedTabDelegate::ShouldSync(
142 sync_sessions::SyncSessionsClient* sessions_client) {
143 if (sessions_client->GetSyncedWindowDelegatesGetter()->FindById(
144 GetWindowId()) == nullptr)
145 return false;
146
147 // Is there a valid NavigationEntry?
148 if (ProfileIsSupervised() && GetBlockedNavigations()->size() > 0)
149 return true;
150
151 if (IsInitialBlankNavigation())
152 return false; // This deliberately ignores a new pending entry.
153
154 int entry_count = GetEntryCount();
155 for (int i = 0; i < entry_count; ++i) {
156 const GURL& virtual_url = GetVirtualURLAtIndex(i);
157 if (!virtual_url.is_valid())
158 continue;
159
160 if (sessions_client->ShouldSyncURL(virtual_url))
161 return true;
162 }
163 return false;
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698