OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/android/foreign_session_helper.h" | 5 #include "chrome/browser/android/foreign_session_helper.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
36 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 36 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
37 GetForProfile(profile); | 37 GetForProfile(profile); |
38 | 38 |
39 // Only return the associator if it exists and it is done syncing sessions. | 39 // Only return the associator if it exists and it is done syncing sessions. |
40 if (!service || !service->ShouldPushChanges()) | 40 if (!service || !service->ShouldPushChanges()) |
41 return NULL; | 41 return NULL; |
42 | 42 |
43 return service->GetSessionModelAssociator(); | 43 return service->GetSessionModelAssociator(); |
44 } | 44 } |
45 | 45 |
46 bool shouldSkipUrl(const GURL& url) { | |
47 return (url.SchemeIs(chrome::kChromeNativeScheme) || | |
newt (away)
2013/09/20 17:15:03
do we want to show other chrome:// pages? chrome:/
apiccion
2013/09/20 20:47:57
I can confirm these aren't being shown.
newt (away)
2013/09/20 21:14:35
why not? are they not being synced in the first pl
| |
48 (url.SchemeIs(chrome::kChromeUIScheme) && | |
49 url.host() == chrome::kChromeUINewTabHost)); | |
50 } | |
51 | |
52 bool shouldSkipTab(const SessionTab& tab) { | |
53 if (tab.navigations.empty()) | |
54 return true; | |
55 | |
56 int selected_index = tab.current_navigation_index; | |
57 if (selected_index < 0 || | |
58 selected_index >= static_cast<int>(tab.navigations.size())) { | |
59 return true; | |
60 } | |
61 | |
62 const ::sessions::SerializedNavigationEntry& current_navigation = | |
63 tab.navigations.at(selected_index); | |
64 | |
65 GURL tab_url = current_navigation.virtual_url(); | |
66 if (shouldSkipUrl(tab_url)) | |
67 return true; | |
68 return false; | |
69 } | |
70 | |
71 bool shouldSkipWindow(const SessionWindow* window) { | |
72 for (std::vector<SessionTab*>::const_iterator tab_it = window->tabs.begin(); | |
73 tab_it != window->tabs.end(); ++tab_it) { | |
74 const SessionTab &tab = **tab_it; | |
75 if (!shouldSkipTab(tab)) | |
76 return false; | |
77 } | |
78 return true; | |
79 } | |
80 | |
81 bool shouldSkipSession(const browser_sync::SyncedSession* session) { | |
82 for (SyncedSession::SyncedWindowMap::const_iterator it = | |
83 session->windows.begin(); it != session->windows.end(); ++it) { | |
84 const SessionWindow* window = it->second; | |
85 if (!shouldSkipWindow(window)) | |
86 return false; | |
87 } | |
88 return true; | |
89 } | |
90 | |
46 void CopyTabsToJava( | 91 void CopyTabsToJava( |
47 JNIEnv* env, | 92 JNIEnv* env, |
48 const SessionWindow* window, | 93 const SessionWindow* window, |
49 ScopedJavaLocalRef<jobject>& j_window) { | 94 ScopedJavaLocalRef<jobject>& j_window) { |
50 for (std::vector<SessionTab*>::const_iterator tab_it = window->tabs.begin(); | 95 for (std::vector<SessionTab*>::const_iterator tab_it = window->tabs.begin(); |
51 tab_it != window->tabs.end(); ++tab_it) { | 96 tab_it != window->tabs.end(); ++tab_it) { |
52 const SessionTab &tab = **tab_it; | 97 const SessionTab &tab = **tab_it; |
53 | 98 |
54 if (tab.navigations.empty()) | 99 if (shouldSkipTab(tab)) |
55 continue; | 100 continue; |
56 | 101 |
102 int selected_index = tab.current_navigation_index; | |
newt (away)
2013/09/20 17:15:03
you just verified in shouldSkipTab(), that the cur
apiccion
2013/09/20 20:47:57
IS it okay if I put a DCHECK? I'd hate if we chang
newt (away)
2013/09/20 21:14:35
sure.
| |
103 selected_index = std::max( | |
104 0, | |
105 std::min(selected_index, | |
106 static_cast<int>(tab.navigations.size() - 1))); | |
107 | |
57 const ::sessions::SerializedNavigationEntry& current_navigation = | 108 const ::sessions::SerializedNavigationEntry& current_navigation = |
58 tab.navigations.at(tab.current_navigation_index); | 109 tab.navigations.at(selected_index); |
59 | 110 |
60 GURL tab_url = current_navigation.virtual_url(); | 111 GURL tab_url = current_navigation.virtual_url(); |
61 if (tab_url.SchemeIs(chrome::kChromeNativeScheme) || | |
62 (tab_url.SchemeIs(chrome::kChromeUIScheme) && | |
63 tab_url.host() == chrome::kChromeUINewTabHost)) | |
64 continue; | |
65 | 112 |
66 Java_ForeignSessionHelper_pushTab( | 113 Java_ForeignSessionHelper_pushTab( |
67 env, j_window.obj(), | 114 env, j_window.obj(), |
68 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), | 115 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), |
69 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), | 116 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), |
70 tab.timestamp.ToInternalValue(), tab.tab_id.id()); | 117 tab.timestamp.ToJavaTime(), |
118 tab.tab_id.id()); | |
71 } | 119 } |
72 } | 120 } |
73 | 121 |
74 void CopyWindowsToJava( | 122 void CopyWindowsToJava( |
75 JNIEnv* env, | 123 JNIEnv* env, |
76 const SyncedSession* session, | 124 const SyncedSession* session, |
77 ScopedJavaLocalRef<jobject>& j_session) { | 125 ScopedJavaLocalRef<jobject>& j_session) { |
78 for (SyncedSession::SyncedWindowMap::const_iterator it = | 126 for (SyncedSession::SyncedWindowMap::const_iterator it = |
79 session->windows.begin(); it != session->windows.end(); ++it) { | 127 session->windows.begin(); it != session->windows.end(); ++it) { |
80 const SessionWindow* window = it->second; | 128 const SessionWindow* window = it->second; |
81 | 129 |
130 if (shouldSkipWindow(window)) | |
131 continue; | |
132 | |
82 ScopedJavaLocalRef<jobject> last_pushed_window; | 133 ScopedJavaLocalRef<jobject> last_pushed_window; |
83 last_pushed_window.Reset( | 134 last_pushed_window.Reset( |
84 Java_ForeignSessionHelper_pushWindow( | 135 Java_ForeignSessionHelper_pushWindow( |
85 env, j_session.obj(), window->timestamp.ToInternalValue(), | 136 env, j_session.obj(), |
137 window->timestamp.ToJavaTime(), | |
86 window->window_id.id())); | 138 window->window_id.id())); |
87 | 139 |
88 CopyTabsToJava(env, window, last_pushed_window); | 140 CopyTabsToJava(env, window, last_pushed_window); |
89 } | 141 } |
90 } | 142 } |
91 | 143 |
92 } // namespace | 144 } // namespace |
93 | 145 |
94 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { | 146 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { |
95 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( | 147 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
96 ProfileAndroid::FromProfileAndroid(profile)); | 148 ProfileAndroid::FromProfileAndroid(profile)); |
97 return reinterpret_cast<jint>(foreign_session_helper); | 149 return reinterpret_cast<jint>(foreign_session_helper); |
98 } | 150 } |
99 | 151 |
100 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) | 152 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
101 : profile_(profile) { | 153 : profile_(profile) { |
102 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 154 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
103 GetForProfile(profile); | 155 GetForProfile(profile); |
104 | |
105 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 156 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
106 content::Source<ProfileSyncService>(service)); | 157 content::Source<ProfileSyncService>(service)); |
107 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, | 158 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, |
108 content::Source<Profile>(profile)); | 159 content::Source<Profile>(profile)); |
109 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 160 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
110 content::Source<Profile>(profile)); | 161 content::Source<Profile>(profile)); |
111 } | 162 } |
112 | 163 |
113 ForeignSessionHelper::~ForeignSessionHelper() { | 164 ForeignSessionHelper::~ForeignSessionHelper() { |
114 } | 165 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 scoped_ptr<DictionaryValue> collapsed_sessions( | 223 scoped_ptr<DictionaryValue> collapsed_sessions( |
173 pref_collapsed_sessions->DeepCopy()); | 224 pref_collapsed_sessions->DeepCopy()); |
174 pref_collapsed_sessions->Clear(); | 225 pref_collapsed_sessions->Clear(); |
175 | 226 |
176 ScopedJavaLocalRef<jobject> last_pushed_session; | 227 ScopedJavaLocalRef<jobject> last_pushed_session; |
177 ScopedJavaLocalRef<jobject> last_pushed_window; | 228 ScopedJavaLocalRef<jobject> last_pushed_window; |
178 | 229 |
179 // Note: we don't own the SyncedSessions themselves. | 230 // Note: we don't own the SyncedSessions themselves. |
180 for (size_t i = 0; i < sessions.size(); ++i) { | 231 for (size_t i = 0; i < sessions.size(); ++i) { |
181 const browser_sync::SyncedSession* session = sessions[i]; | 232 const browser_sync::SyncedSession* session = sessions[i]; |
233 if (shouldSkipSession(session)) | |
234 continue; | |
182 | 235 |
183 const bool is_collapsed = collapsed_sessions->HasKey(session->session_tag); | 236 const bool is_collapsed = collapsed_sessions->HasKey(session->session_tag); |
184 | 237 |
185 if (is_collapsed) | 238 if (is_collapsed) |
186 pref_collapsed_sessions->SetBoolean(session->session_tag, true); | 239 pref_collapsed_sessions->SetBoolean(session->session_tag, true); |
187 | 240 |
188 last_pushed_session.Reset( | 241 last_pushed_session.Reset( |
189 Java_ForeignSessionHelper_pushSession( | 242 Java_ForeignSessionHelper_pushSession( |
190 env, | 243 env, |
191 result, | 244 result, |
192 ConvertUTF8ToJavaString(env, session->session_tag).Release(), | 245 ConvertUTF8ToJavaString(env, session->session_tag).Release(), |
193 ConvertUTF8ToJavaString(env, session->session_name).Release(), | 246 ConvertUTF8ToJavaString(env, session->session_name).Release(), |
194 ConvertUTF8ToJavaString(env, | 247 session->device_type, |
195 session->DeviceTypeAsString()).Release(), | 248 session->modified_time.ToJavaTime())); |
196 session->modified_time.ToInternalValue())); | |
197 | 249 |
198 CopyWindowsToJava(env, session, last_pushed_session); | 250 CopyWindowsToJava(env, session, last_pushed_session); |
199 } | 251 } |
200 | 252 |
201 return true; | 253 return true; |
202 } | 254 } |
203 | 255 |
204 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 256 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
205 jobject obj, | 257 jobject obj, |
206 jstring session_tag, | 258 jstring session_tag, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 // Store session tags for collapsed sessions in a preference so that the | 302 // Store session tags for collapsed sessions in a preference so that the |
251 // collapsed state persists. | 303 // collapsed state persists. |
252 PrefService* prefs = profile_->GetPrefs(); | 304 PrefService* prefs = profile_->GetPrefs(); |
253 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 305 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
254 if (is_collapsed) | 306 if (is_collapsed) |
255 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); | 307 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); |
256 else | 308 else |
257 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); | 309 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); |
258 } | 310 } |
259 | 311 |
312 jboolean ForeignSessionHelper::GetForeignSessionCollapsed(JNIEnv* env, | |
313 jobject obj, | |
314 jstring session_tag) { | |
315 const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary( | |
316 prefs::kNtpCollapsedForeignSessions); | |
317 return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag)); | |
318 } | |
319 | |
260 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, | 320 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
261 jstring session_tag) { | 321 jstring session_tag) { |
262 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 322 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
263 if (associator) | 323 if (associator) |
264 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 324 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
265 } | 325 } |
266 | 326 |
267 // static | 327 // static |
268 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 328 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
269 return RegisterNativesImpl(env); | 329 return RegisterNativesImpl(env); |
270 } | 330 } |
OLD | NEW |