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 "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/prefs/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
12 #include "chrome/browser/android/tab_android.h" | 12 #include "chrome/browser/android/tab_android.h" |
13 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
14 #include "chrome/browser/profiles/profile_android.h" | 14 #include "chrome/browser/profiles/profile_android.h" |
15 #include "chrome/browser/sessions/session_restore.h" | 15 #include "chrome/browser/sessions/session_restore.h" |
16 #include "chrome/browser/sync/glue/session_model_associator.h" | 16 #include "chrome/browser/sync/open_tabs_ui_delegate.h" |
17 #include "chrome/browser/sync/profile_sync_service.h" | 17 #include "chrome/browser/sync/profile_sync_service.h" |
18 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
19 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 19 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
20 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 20 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
21 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
22 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
23 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
25 #include "jni/ForeignSessionHelper_jni.h" | 25 #include "jni/ForeignSessionHelper_jni.h" |
26 | 26 |
27 using base::android::ScopedJavaGlobalRef; | 27 using base::android::ScopedJavaGlobalRef; |
28 using base::android::ScopedJavaLocalRef; | 28 using base::android::ScopedJavaLocalRef; |
29 using base::android::AttachCurrentThread; | 29 using base::android::AttachCurrentThread; |
30 using base::android::ConvertUTF16ToJavaString; | 30 using base::android::ConvertUTF16ToJavaString; |
31 using base::android::ConvertUTF8ToJavaString; | 31 using base::android::ConvertUTF8ToJavaString; |
32 using base::android::ConvertJavaStringToUTF8; | 32 using base::android::ConvertJavaStringToUTF8; |
33 using browser_sync::SessionModelAssociator; | 33 using browser_sync::OpenTabsUIDelegate; |
34 using browser_sync::SyncedSession; | 34 using browser_sync::SyncedSession; |
35 | 35 |
36 namespace { | 36 namespace { |
37 | 37 |
38 SessionModelAssociator* GetSessionModelAssociator(Profile* profile) { | 38 OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { |
39 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 39 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
40 GetForProfile(profile); | 40 GetForProfile(profile); |
41 | 41 |
42 // Only return the associator if it exists and it is done syncing sessions. | 42 // Only return the delegate if it exists and it is done syncing sessions. |
43 if (!service || !service->ShouldPushChanges()) | 43 if (!service || !service->ShouldPushChanges()) |
44 return NULL; | 44 return NULL; |
45 | 45 |
46 return service->GetSessionModelAssociator(); | 46 return service->GetOpenTabsUIDelegate(); |
47 } | 47 } |
48 | 48 |
49 bool ShouldSkipTab(const SessionTab& session_tab) { | 49 bool ShouldSkipTab(const SessionTab& session_tab) { |
50 if (session_tab.navigations.empty()) | 50 if (session_tab.navigations.empty()) |
51 return true; | 51 return true; |
52 | 52 |
53 int selected_index = session_tab.current_navigation_index; | 53 int selected_index = session_tab.current_navigation_index; |
54 if (selected_index < 0 || | 54 if (selected_index < 0 || |
55 selected_index >= static_cast<int>(session_tab.navigations.size())) | 55 selected_index >= static_cast<int>(session_tab.navigations.size())) |
56 return true; | 56 return true; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 Java_ForeignSessionCallback_onUpdated(env, callback_.obj()); | 193 Java_ForeignSessionCallback_onUpdated(env, callback_.obj()); |
194 break; | 194 break; |
195 default: | 195 default: |
196 NOTREACHED(); | 196 NOTREACHED(); |
197 } | 197 } |
198 } | 198 } |
199 | 199 |
200 jboolean ForeignSessionHelper::GetForeignSessions(JNIEnv* env, | 200 jboolean ForeignSessionHelper::GetForeignSessions(JNIEnv* env, |
201 jobject obj, | 201 jobject obj, |
202 jobject result) { | 202 jobject result) { |
203 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 203 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
204 if (!associator) | 204 if (!open_tabs) |
205 return false; | 205 return false; |
206 | 206 |
207 std::vector<const browser_sync::SyncedSession*> sessions; | 207 std::vector<const browser_sync::SyncedSession*> sessions; |
208 if (!associator->GetAllForeignSessions(&sessions)) | 208 if (!open_tabs->GetAllForeignSessions(&sessions)) |
209 return false; | 209 return false; |
210 | 210 |
211 // Use a pref to keep track of sessions that were collapsed by the user. | 211 // Use a pref to keep track of sessions that were collapsed by the user. |
212 // To prevent the pref from accumulating stale sessions, clear it each time | 212 // To prevent the pref from accumulating stale sessions, clear it each time |
213 // and only add back sessions that are still current. | 213 // and only add back sessions that are still current. |
214 DictionaryPrefUpdate pref_update(profile_->GetPrefs(), | 214 DictionaryPrefUpdate pref_update(profile_->GetPrefs(), |
215 prefs::kNtpCollapsedForeignSessions); | 215 prefs::kNtpCollapsedForeignSessions); |
216 DictionaryValue* pref_collapsed_sessions = pref_update.Get(); | 216 DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
217 scoped_ptr<DictionaryValue> collapsed_sessions( | 217 scoped_ptr<DictionaryValue> collapsed_sessions( |
218 pref_collapsed_sessions->DeepCopy()); | 218 pref_collapsed_sessions->DeepCopy()); |
(...skipping 27 matching lines...) Expand all Loading... |
246 | 246 |
247 return true; | 247 return true; |
248 } | 248 } |
249 | 249 |
250 // TODO(apiccion): Remvoe this method once downstream CL Lands. | 250 // TODO(apiccion): Remvoe this method once downstream CL Lands. |
251 // See: http://crbug.com/257102 | 251 // See: http://crbug.com/257102 |
252 jboolean ForeignSessionHelper::OpenForeignSessionTabOld(JNIEnv* env, | 252 jboolean ForeignSessionHelper::OpenForeignSessionTabOld(JNIEnv* env, |
253 jobject obj, | 253 jobject obj, |
254 jstring session_tag, | 254 jstring session_tag, |
255 jint session_tab_id) { | 255 jint session_tab_id) { |
256 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 256 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
257 if (!associator) { | 257 if (!open_tabs) { |
258 LOG(ERROR) << "Null SessionModelAssociator returned."; | 258 LOG(ERROR) << "Null OpenTabsUIDelegate returned."; |
259 return false; | 259 return false; |
260 } | 260 } |
261 | 261 |
262 const SessionTab* session_tab; | 262 const SessionTab* session_tab; |
263 | 263 |
264 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), | 264 if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
265 session_tab_id, &session_tab)) { | 265 session_tab_id, &session_tab)) { |
266 LOG(ERROR) << "Failed to load foreign tab."; | 266 LOG(ERROR) << "Failed to load foreign tab."; |
267 return false; | 267 return false; |
268 } | 268 } |
269 | 269 |
270 if (session_tab->navigations.empty()) { | 270 if (session_tab->navigations.empty()) { |
271 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 271 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
272 return false; | 272 return false; |
273 } | 273 } |
274 | 274 |
275 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); | 275 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); |
(...skipping 15 matching lines...) Expand all Loading... |
291 | 291 |
292 return true; | 292 return true; |
293 } | 293 } |
294 | 294 |
295 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 295 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
296 jobject obj, | 296 jobject obj, |
297 jobject j_tab, | 297 jobject j_tab, |
298 jstring session_tag, | 298 jstring session_tag, |
299 jint session_tab_id, | 299 jint session_tab_id, |
300 jint j_disposition) { | 300 jint j_disposition) { |
301 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 301 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
302 if (!associator) { | 302 if (!open_tabs) { |
303 LOG(ERROR) << "Null SessionModelAssociator returned."; | 303 LOG(ERROR) << "Null OpenTabsUIDelegate returned."; |
304 return false; | 304 return false; |
305 } | 305 } |
306 | 306 |
307 const SessionTab* session_tab; | 307 const SessionTab* session_tab; |
308 | 308 |
309 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), | 309 if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
310 session_tab_id, | 310 session_tab_id, |
311 &session_tab)) { | 311 &session_tab)) { |
312 LOG(ERROR) << "Failed to load foreign tab."; | 312 LOG(ERROR) << "Failed to load foreign tab."; |
313 return false; | 313 return false; |
314 } | 314 } |
315 | 315 |
316 if (session_tab->navigations.empty()) { | 316 if (session_tab->navigations.empty()) { |
317 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 317 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
318 return false; | 318 return false; |
319 } | 319 } |
320 | 320 |
321 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); | 321 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
322 if (!tab_android) | 322 if (!tab_android) |
323 return false; | 323 return false; |
324 content::WebContents* web_contents = tab_android->web_contents(); | 324 content::WebContents* web_contents = tab_android->web_contents(); |
325 if (!web_contents) | 325 if (!web_contents) |
326 return false; | 326 return false; |
327 | 327 |
328 WindowOpenDisposition disposition = | 328 WindowOpenDisposition disposition = |
329 static_cast<WindowOpenDisposition>(j_disposition); | 329 static_cast<WindowOpenDisposition>(j_disposition); |
330 SessionRestore::RestoreForeignSessionTab(web_contents, | 330 SessionRestore::RestoreForeignSessionTab(web_contents, |
331 *session_tab, | 331 *session_tab, |
332 disposition); | 332 disposition); |
333 | 333 |
334 return true; | 334 return true; |
335 } | 335 } |
336 | 336 |
337 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, | 337 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
338 jstring session_tag) { | 338 jstring session_tag) { |
339 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 339 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
340 if (associator) | 340 if (open_tabs) |
341 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 341 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
342 } | 342 } |
343 | 343 |
344 // static | 344 // static |
345 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 345 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
346 return RegisterNativesImpl(env); | 346 return RegisterNativesImpl(env); |
347 } | 347 } |
OLD | NEW |