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) { | |
Yaron
2013/09/20 22:18:08
All of these should start with Caps.
apiccion
2013/09/20 23:02:57
Done.
| |
47 return (url.SchemeIs(chrome::kChromeNativeScheme) || | |
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) { | |
Yaron
2013/09/20 22:18:08
use a const&
apiccion
2013/09/20 23:02:57
Done.
| |
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) { | |
Yaron
2013/09/20 22:18:08
use a const&
apiccion
2013/09/20 23:02:57
Done.
| |
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; | |
103 DCHECK(selected_index >= 0 | |
Yaron
2013/09/20 22:18:08
Split to 2 DCHECKs (otherwise you can't tell which
apiccion
2013/09/20 23:02:57
Done.
| |
104 && selected_index < static_cast<int>(tab.navigation.size())); | |
105 | |
57 const ::sessions::SerializedNavigationEntry& current_navigation = | 106 const ::sessions::SerializedNavigationEntry& current_navigation = |
58 tab.navigations.at(tab.current_navigation_index); | 107 tab.navigations.at(selected_index); |
59 | 108 |
60 GURL tab_url = current_navigation.virtual_url(); | 109 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 | 110 |
66 Java_ForeignSessionHelper_pushTab( | 111 Java_ForeignSessionHelper_pushTab( |
67 env, j_window.obj(), | 112 env, j_window.obj(), |
68 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), | 113 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), |
69 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), | 114 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), |
70 tab.timestamp.ToInternalValue(), tab.tab_id.id()); | 115 tab.timestamp.ToJavaTime(), |
116 tab.tab_id.id()); | |
71 } | 117 } |
72 } | 118 } |
73 | 119 |
74 void CopyWindowsToJava( | 120 void CopyWindowsToJava( |
75 JNIEnv* env, | 121 JNIEnv* env, |
76 const SyncedSession* session, | 122 const SyncedSession* session, |
77 ScopedJavaLocalRef<jobject>& j_session) { | 123 ScopedJavaLocalRef<jobject>& j_session) { |
78 for (SyncedSession::SyncedWindowMap::const_iterator it = | 124 for (SyncedSession::SyncedWindowMap::const_iterator it = |
79 session->windows.begin(); it != session->windows.end(); ++it) { | 125 session->windows.begin(); it != session->windows.end(); ++it) { |
80 const SessionWindow* window = it->second; | 126 const SessionWindow* window = it->second; |
81 | 127 |
128 if (shouldSkipWindow(window)) | |
129 continue; | |
130 | |
82 ScopedJavaLocalRef<jobject> last_pushed_window; | 131 ScopedJavaLocalRef<jobject> last_pushed_window; |
83 last_pushed_window.Reset( | 132 last_pushed_window.Reset( |
84 Java_ForeignSessionHelper_pushWindow( | 133 Java_ForeignSessionHelper_pushWindow( |
85 env, j_session.obj(), window->timestamp.ToInternalValue(), | 134 env, j_session.obj(), |
135 window->timestamp.ToJavaTime(), | |
86 window->window_id.id())); | 136 window->window_id.id())); |
87 | 137 |
88 CopyTabsToJava(env, window, last_pushed_window); | 138 CopyTabsToJava(env, window, last_pushed_window); |
89 } | 139 } |
90 } | 140 } |
91 | 141 |
92 } // namespace | 142 } // namespace |
93 | 143 |
94 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { | 144 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { |
95 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( | 145 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
96 ProfileAndroid::FromProfileAndroid(profile)); | 146 ProfileAndroid::FromProfileAndroid(profile)); |
97 return reinterpret_cast<jint>(foreign_session_helper); | 147 return reinterpret_cast<jint>(foreign_session_helper); |
98 } | 148 } |
99 | 149 |
100 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) | 150 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
101 : profile_(profile) { | 151 : profile_(profile) { |
102 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 152 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
103 GetForProfile(profile); | 153 GetForProfile(profile); |
104 | |
105 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 154 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
106 content::Source<ProfileSyncService>(service)); | 155 content::Source<ProfileSyncService>(service)); |
107 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, | 156 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, |
108 content::Source<Profile>(profile)); | 157 content::Source<Profile>(profile)); |
109 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 158 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
110 content::Source<Profile>(profile)); | 159 content::Source<Profile>(profile)); |
111 } | 160 } |
112 | 161 |
113 ForeignSessionHelper::~ForeignSessionHelper() { | 162 ForeignSessionHelper::~ForeignSessionHelper() { |
114 } | 163 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
172 scoped_ptr<DictionaryValue> collapsed_sessions( | 221 scoped_ptr<DictionaryValue> collapsed_sessions( |
173 pref_collapsed_sessions->DeepCopy()); | 222 pref_collapsed_sessions->DeepCopy()); |
174 pref_collapsed_sessions->Clear(); | 223 pref_collapsed_sessions->Clear(); |
175 | 224 |
176 ScopedJavaLocalRef<jobject> last_pushed_session; | 225 ScopedJavaLocalRef<jobject> last_pushed_session; |
177 ScopedJavaLocalRef<jobject> last_pushed_window; | 226 ScopedJavaLocalRef<jobject> last_pushed_window; |
178 | 227 |
179 // Note: we don't own the SyncedSessions themselves. | 228 // Note: we don't own the SyncedSessions themselves. |
180 for (size_t i = 0; i < sessions.size(); ++i) { | 229 for (size_t i = 0; i < sessions.size(); ++i) { |
181 const browser_sync::SyncedSession* session = sessions[i]; | 230 const browser_sync::SyncedSession* session = sessions[i]; |
231 if (shouldSkipSession(session)) | |
232 continue; | |
182 | 233 |
183 const bool is_collapsed = collapsed_sessions->HasKey(session->session_tag); | 234 const bool is_collapsed = collapsed_sessions->HasKey(session->session_tag); |
184 | 235 |
185 if (is_collapsed) | 236 if (is_collapsed) |
186 pref_collapsed_sessions->SetBoolean(session->session_tag, true); | 237 pref_collapsed_sessions->SetBoolean(session->session_tag, true); |
187 | 238 |
188 last_pushed_session.Reset( | 239 last_pushed_session.Reset( |
189 Java_ForeignSessionHelper_pushSession( | 240 Java_ForeignSessionHelper_pushSession( |
190 env, | 241 env, |
191 result, | 242 result, |
192 ConvertUTF8ToJavaString(env, session->session_tag).Release(), | 243 ConvertUTF8ToJavaString(env, session->session_tag).Release(), |
193 ConvertUTF8ToJavaString(env, session->session_name).Release(), | 244 ConvertUTF8ToJavaString(env, session->session_name).Release(), |
194 ConvertUTF8ToJavaString(env, | 245 session->device_type, |
195 session->DeviceTypeAsString()).Release(), | 246 session->modified_time.ToJavaTime())); |
196 session->modified_time.ToInternalValue())); | |
197 | 247 |
198 CopyWindowsToJava(env, session, last_pushed_session); | 248 CopyWindowsToJava(env, session, last_pushed_session); |
199 } | 249 } |
200 | 250 |
201 return true; | 251 return true; |
202 } | 252 } |
203 | 253 |
204 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 254 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
205 jobject obj, | 255 jobject obj, |
206 jstring session_tag, | 256 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 | 300 // Store session tags for collapsed sessions in a preference so that the |
251 // collapsed state persists. | 301 // collapsed state persists. |
252 PrefService* prefs = profile_->GetPrefs(); | 302 PrefService* prefs = profile_->GetPrefs(); |
253 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 303 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
254 if (is_collapsed) | 304 if (is_collapsed) |
255 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); | 305 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); |
256 else | 306 else |
257 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); | 307 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); |
258 } | 308 } |
259 | 309 |
310 jboolean ForeignSessionHelper::GetForeignSessionCollapsed(JNIEnv* env, | |
311 jobject obj, | |
312 jstring session_tag) { | |
313 const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary( | |
314 prefs::kNtpCollapsedForeignSessions); | |
315 return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag)); | |
316 } | |
317 | |
260 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, | 318 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
261 jstring session_tag) { | 319 jstring session_tag) { |
262 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 320 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
263 if (associator) | 321 if (associator) |
264 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 322 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
265 } | 323 } |
266 | 324 |
267 // static | 325 // static |
268 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 326 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
269 return RegisterNativesImpl(env); | 327 return RegisterNativesImpl(env); |
270 } | 328 } |
OLD | NEW |