| 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/scoped_user_pref_update.h" | 11 #include "base/prefs/scoped_user_pref_update.h" |
| 12 #include "chrome/browser/android/tab_android.h" |
| 11 #include "chrome/browser/chrome_notification_types.h" | 13 #include "chrome/browser/chrome_notification_types.h" |
| 12 #include "chrome/browser/profiles/profile_android.h" | 14 #include "chrome/browser/profiles/profile_android.h" |
| 15 #include "chrome/browser/sessions/session_restore.h" |
| 13 #include "chrome/browser/sync/glue/session_model_associator.h" | 16 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | 17 #include "chrome/browser/sync/profile_sync_service.h" |
| 15 #include "chrome/browser/sync/profile_sync_service_factory.h" | 18 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 19 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 17 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 20 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 18 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 20 #include "content/public/browser/notification_source.h" | 23 #include "content/public/browser/notification_source.h" |
| 21 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 22 #include "jni/ForeignSessionHelper_jni.h" | 25 #include "jni/ForeignSessionHelper_jni.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 36 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 39 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 37 GetForProfile(profile); | 40 GetForProfile(profile); |
| 38 | 41 |
| 39 // Only return the associator if it exists and it is done syncing sessions. | 42 // Only return the associator if it exists and it is done syncing sessions. |
| 40 if (!service || !service->ShouldPushChanges()) | 43 if (!service || !service->ShouldPushChanges()) |
| 41 return NULL; | 44 return NULL; |
| 42 | 45 |
| 43 return service->GetSessionModelAssociator(); | 46 return service->GetSessionModelAssociator(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 bool ShouldSkipTab(const SessionTab& tab) { | 49 bool ShouldSkipTab(const SessionTab& session_tab) { |
| 47 if (tab.navigations.empty()) | 50 if (session_tab.navigations.empty()) |
| 48 return true; | 51 return true; |
| 49 | 52 |
| 50 int selected_index = tab.current_navigation_index; | 53 int selected_index = session_tab.current_navigation_index; |
| 51 if (selected_index < 0 || | 54 if (selected_index < 0 || |
| 52 selected_index >= static_cast<int>(tab.navigations.size())) | 55 selected_index >= static_cast<int>(session_tab.navigations.size())) |
| 53 return true; | 56 return true; |
| 54 | 57 |
| 55 const ::sessions::SerializedNavigationEntry& current_navigation = | 58 const ::sessions::SerializedNavigationEntry& current_navigation = |
| 56 tab.navigations.at(selected_index); | 59 session_tab.navigations.at(selected_index); |
| 57 | 60 |
| 58 if (current_navigation.virtual_url().is_empty()) | 61 if (current_navigation.virtual_url().is_empty()) |
| 59 return true; | 62 return true; |
| 60 | 63 |
| 61 return false; | 64 return false; |
| 62 } | 65 } |
| 63 | 66 |
| 64 bool ShouldSkipWindow(const SessionWindow& window) { | 67 bool ShouldSkipWindow(const SessionWindow& window) { |
| 65 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); | 68 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
| 66 tab_it != window.tabs.end(); ++tab_it) { | 69 tab_it != window.tabs.end(); ++tab_it) { |
| 67 const SessionTab &tab = **tab_it; | 70 const SessionTab &session_tab = **tab_it; |
| 68 if (!ShouldSkipTab(tab)) | 71 if (!ShouldSkipTab(session_tab)) |
| 69 return false; | 72 return false; |
| 70 } | 73 } |
| 71 return true; | 74 return true; |
| 72 } | 75 } |
| 73 | 76 |
| 74 bool ShouldSkipSession(const browser_sync::SyncedSession& session) { | 77 bool ShouldSkipSession(const browser_sync::SyncedSession& session) { |
| 75 for (SyncedSession::SyncedWindowMap::const_iterator it = | 78 for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 76 session.windows.begin(); it != session.windows.end(); ++it) { | 79 session.windows.begin(); it != session.windows.end(); ++it) { |
| 77 const SessionWindow &window = *(it->second); | 80 const SessionWindow &window = *(it->second); |
| 78 if (!ShouldSkipWindow(window)) | 81 if (!ShouldSkipWindow(window)) |
| 79 return false; | 82 return false; |
| 80 } | 83 } |
| 81 return true; | 84 return true; |
| 82 } | 85 } |
| 83 | 86 |
| 84 void CopyTabsToJava( | 87 void CopyTabsToJava( |
| 85 JNIEnv* env, | 88 JNIEnv* env, |
| 86 const SessionWindow& window, | 89 const SessionWindow& window, |
| 87 ScopedJavaLocalRef<jobject>& j_window) { | 90 ScopedJavaLocalRef<jobject>& j_window) { |
| 88 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); | 91 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
| 89 tab_it != window.tabs.end(); ++tab_it) { | 92 tab_it != window.tabs.end(); ++tab_it) { |
| 90 const SessionTab &tab = **tab_it; | 93 const SessionTab &session_tab = **tab_it; |
| 91 | 94 |
| 92 if (ShouldSkipTab(tab)) | 95 if (ShouldSkipTab(session_tab)) |
| 93 continue; | 96 continue; |
| 94 | 97 |
| 95 int selected_index = tab.current_navigation_index; | 98 int selected_index = session_tab.current_navigation_index; |
| 96 DCHECK(selected_index >= 0); | 99 DCHECK(selected_index >= 0); |
| 97 DCHECK(selected_index < static_cast<int>(tab.navigations.size())); | 100 DCHECK(selected_index < static_cast<int>(session_tab.navigations.size())); |
| 98 | 101 |
| 99 const ::sessions::SerializedNavigationEntry& current_navigation = | 102 const ::sessions::SerializedNavigationEntry& current_navigation = |
| 100 tab.navigations.at(selected_index); | 103 session_tab.navigations.at(selected_index); |
| 101 | 104 |
| 102 GURL tab_url = current_navigation.virtual_url(); | 105 GURL tab_url = current_navigation.virtual_url(); |
| 103 | 106 |
| 104 Java_ForeignSessionHelper_pushTab( | 107 Java_ForeignSessionHelper_pushTab( |
| 105 env, j_window.obj(), | 108 env, j_window.obj(), |
| 106 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), | 109 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), |
| 107 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), | 110 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), |
| 108 tab.timestamp.ToJavaTime(), | 111 session_tab.timestamp.ToJavaTime(), |
| 109 tab.tab_id.id()); | 112 session_tab.tab_id.id()); |
| 110 } | 113 } |
| 111 } | 114 } |
| 112 | 115 |
| 113 void CopyWindowsToJava( | 116 void CopyWindowsToJava( |
| 114 JNIEnv* env, | 117 JNIEnv* env, |
| 115 const SyncedSession& session, | 118 const SyncedSession& session, |
| 116 ScopedJavaLocalRef<jobject>& j_session) { | 119 ScopedJavaLocalRef<jobject>& j_session) { |
| 117 for (SyncedSession::SyncedWindowMap::const_iterator it = | 120 for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 118 session.windows.begin(); it != session.windows.end(); ++it) { | 121 session.windows.begin(); it != session.windows.end(); ++it) { |
| 119 const SessionWindow &window = *(it->second); | 122 const SessionWindow &window = *(it->second); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 ConvertUTF8ToJavaString(env, session.session_name).Release(), | 240 ConvertUTF8ToJavaString(env, session.session_name).Release(), |
| 238 session.device_type, | 241 session.device_type, |
| 239 session.modified_time.ToJavaTime())); | 242 session.modified_time.ToJavaTime())); |
| 240 | 243 |
| 241 CopyWindowsToJava(env, session, last_pushed_session); | 244 CopyWindowsToJava(env, session, last_pushed_session); |
| 242 } | 245 } |
| 243 | 246 |
| 244 return true; | 247 return true; |
| 245 } | 248 } |
| 246 | 249 |
| 247 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 250 // TODO(apiccion): Remvoe this method once downstream CL Lands. |
| 248 jobject obj, | 251 // See: http://crbug.com/257102 |
| 249 jstring session_tag, | 252 jboolean ForeignSessionHelper::OpenForeignSessionTabOld(JNIEnv* env, |
| 250 jint tab_id) { | 253 jobject obj, |
| 254 jstring session_tag, |
| 255 jint session_tab_id) { |
| 251 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 256 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
| 252 if (!associator) { | 257 if (!associator) { |
| 253 LOG(ERROR) << "Null SessionModelAssociator returned."; | 258 LOG(ERROR) << "Null SessionModelAssociator returned."; |
| 254 return false; | 259 return false; |
| 255 } | 260 } |
| 256 | 261 |
| 257 const SessionTab* tab; | 262 const SessionTab* session_tab; |
| 258 | 263 |
| 259 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), | 264 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
| 260 tab_id, &tab)) { | 265 session_tab_id, &session_tab)) { |
| 261 LOG(ERROR) << "Failed to load foreign tab."; | 266 LOG(ERROR) << "Failed to load foreign tab."; |
| 262 return false; | 267 return false; |
| 263 } | 268 } |
| 264 | 269 |
| 265 if (tab->navigations.empty()) { | 270 if (session_tab->navigations.empty()) { |
| 266 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 271 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 267 return false; | 272 return false; |
| 268 } | 273 } |
| 269 | 274 |
| 270 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); | 275 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); |
| 271 DCHECK(tab_model); | 276 DCHECK(tab_model); |
| 272 if (!tab_model) | 277 if (!tab_model) |
| 273 return false; | 278 return false; |
| 274 | 279 |
| 275 std::vector<content::NavigationEntry*> entries = | 280 std::vector<content::NavigationEntry*> entries = |
| 276 sessions::SerializedNavigationEntry::ToNavigationEntries( | 281 sessions::SerializedNavigationEntry::ToNavigationEntries( |
| 277 tab->navigations, profile_); | 282 session_tab->navigations, profile_); |
| 278 content::WebContents* new_web_contents = content::WebContents::Create( | 283 content::WebContents* new_web_contents = content::WebContents::Create( |
| 279 content::WebContents::CreateParams(profile_)); | 284 content::WebContents::CreateParams(profile_)); |
| 280 int selected_index = tab->normalized_navigation_index(); | 285 int selected_index = session_tab->normalized_navigation_index(); |
| 281 new_web_contents->GetController().Restore( | 286 new_web_contents->GetController().Restore( |
| 282 selected_index, | 287 selected_index, |
| 283 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, | 288 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, |
| 284 &entries); | 289 &entries); |
| 285 tab_model->CreateTab(new_web_contents); | 290 tab_model->CreateTab(new_web_contents); |
| 286 | 291 |
| 287 return true; | 292 return true; |
| 288 } | 293 } |
| 289 | 294 |
| 295 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
| 296 jobject obj, |
| 297 jobject j_tab, |
| 298 jstring session_tag, |
| 299 jint session_tab_id, |
| 300 jint j_disposition) { |
| 301 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
| 302 if (!associator) { |
| 303 LOG(ERROR) << "Null SessionModelAssociator returned."; |
| 304 return false; |
| 305 } |
| 306 |
| 307 const SessionTab* session_tab; |
| 308 |
| 309 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
| 310 session_tab_id, |
| 311 &session_tab)) { |
| 312 LOG(ERROR) << "Failed to load foreign tab."; |
| 313 return false; |
| 314 } |
| 315 |
| 316 if (session_tab->navigations.empty()) { |
| 317 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 318 return false; |
| 319 } |
| 320 |
| 321 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
| 322 if (!tab_android) |
| 323 return false; |
| 324 content::WebContents* web_contents = tab_android->web_contents(); |
| 325 if (!web_contents) |
| 326 return false; |
| 327 |
| 328 WindowOpenDisposition disposition = |
| 329 static_cast<WindowOpenDisposition>(j_disposition); |
| 330 SessionRestore::RestoreForeignSessionTab(web_contents, |
| 331 *session_tab, |
| 332 disposition); |
| 333 |
| 334 return true; |
| 335 } |
| 336 |
| 290 void ForeignSessionHelper::SetForeignSessionCollapsed(JNIEnv* env, jobject obj, | 337 void ForeignSessionHelper::SetForeignSessionCollapsed(JNIEnv* env, jobject obj, |
| 291 jstring session_tag, | 338 jstring session_tag, |
| 292 jboolean is_collapsed) { | 339 jboolean is_collapsed) { |
| 293 // Store session tags for collapsed sessions in a preference so that the | 340 // Store session tags for collapsed sessions in a preference so that the |
| 294 // collapsed state persists. | 341 // collapsed state persists. |
| 295 PrefService* prefs = profile_->GetPrefs(); | 342 PrefService* prefs = profile_->GetPrefs(); |
| 296 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 343 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
| 297 if (is_collapsed) | 344 if (is_collapsed) |
| 298 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); | 345 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); |
| 299 else | 346 else |
| (...skipping 12 matching lines...) Expand all Loading... |
| 312 jstring session_tag) { | 359 jstring session_tag) { |
| 313 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 360 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
| 314 if (associator) | 361 if (associator) |
| 315 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 362 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
| 316 } | 363 } |
| 317 | 364 |
| 318 // static | 365 // static |
| 319 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 366 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 320 return RegisterNativesImpl(env); | 367 return RegisterNativesImpl(env); |
| 321 } | 368 } |
| OLD | NEW |