Chromium Code Reviews| 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" |
| 13 #include "chrome/browser/sync/glue/session_model_associator.h" | 15 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | 16 #include "chrome/browser/sync/profile_sync_service.h" |
| 15 #include "chrome/browser/sync/profile_sync_service_factory.h" | 17 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 16 #include "chrome/browser/ui/android/tab_model/tab_model.h" | 18 #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 17 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | 19 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 18 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 19 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 20 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 ConvertUTF8ToJavaString(env, session.session_name).Release(), | 239 ConvertUTF8ToJavaString(env, session.session_name).Release(), |
| 238 session.device_type, | 240 session.device_type, |
| 239 session.modified_time.ToJavaTime())); | 241 session.modified_time.ToJavaTime())); |
| 240 | 242 |
| 241 CopyWindowsToJava(env, session, last_pushed_session); | 243 CopyWindowsToJava(env, session, last_pushed_session); |
| 242 } | 244 } |
| 243 | 245 |
| 244 return true; | 246 return true; |
| 245 } | 247 } |
| 246 | 248 |
| 249 // TODO (apiccion): Remove this method once the downstream CL lands. | |
| 250 // See: https://code.google.com/p/chromium/issues/detail?id=257102 | |
| 251 jboolean ForeignSessionHelper::OpenForeignSessionTabOld(JNIEnv* env, | |
| 252 jobject obj, | |
|
newt (away)
2013/11/01 23:41:32
don't need this method
apiccion
2013/11/02 01:01:14
Done.
| |
| 253 jstring session_tag, | |
| 254 jint tab_id) { | |
| 255 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | |
| 256 if (!associator) { | |
| 257 LOG(ERROR) << "Null SessionModelAssociator returned."; | |
| 258 return false; | |
| 259 } | |
| 260 | |
| 261 const SessionTab* tab; | |
| 262 | |
| 263 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), | |
| 264 tab_id, &tab)) { | |
| 265 LOG(ERROR) << "Failed to load foreign tab."; | |
| 266 return false; | |
| 267 } | |
| 268 | |
| 269 if (tab->navigations.empty()) { | |
| 270 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | |
| 271 return false; | |
| 272 } | |
| 273 | |
| 274 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); | |
| 275 DCHECK(tab_model); | |
| 276 if (!tab_model) | |
| 277 return false; | |
| 278 | |
| 279 std::vector<content::NavigationEntry*> entries = | |
| 280 sessions::SerializedNavigationEntry::ToNavigationEntries( | |
| 281 tab->navigations, profile_); | |
| 282 content::WebContents* new_web_contents = content::WebContents::Create( | |
| 283 content::WebContents::CreateParams(profile_)); | |
| 284 int selected_index = tab->normalized_navigation_index(); | |
| 285 new_web_contents->GetController().Restore( | |
| 286 selected_index, | |
| 287 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, | |
| 288 &entries); | |
| 289 tab_model->CreateTab(new_web_contents); | |
| 290 | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 247 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 294 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
| 248 jobject obj, | 295 jobject obj, |
| 296 jobject j_tab, | |
| 249 jstring session_tag, | 297 jstring session_tag, |
| 250 jint tab_id) { | 298 jint tab_id) { |
| 251 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 299 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
| 252 if (!associator) { | 300 if (!associator) { |
| 253 LOG(ERROR) << "Null SessionModelAssociator returned."; | 301 LOG(ERROR) << "Null SessionModelAssociator returned."; |
| 254 return false; | 302 return false; |
| 255 } | 303 } |
| 256 | 304 |
| 257 const SessionTab* tab; | 305 const SessionTab* tab; |
| 258 | 306 |
| 259 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), | 307 if (!associator->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
| 260 tab_id, &tab)) { | 308 tab_id, &tab)) { |
| 261 LOG(ERROR) << "Failed to load foreign tab."; | 309 LOG(ERROR) << "Failed to load foreign tab."; |
| 262 return false; | 310 return false; |
| 263 } | 311 } |
| 264 | 312 |
| 265 if (tab->navigations.empty()) { | 313 if (tab->navigations.empty()) { |
|
newt (away)
2013/11/01 23:41:32
you can just replace the rest of this method with
apiccion
2013/11/02 01:01:14
Done.
| |
| 266 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 314 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 267 return false; | 315 return false; |
| 268 } | 316 } |
| 269 | 317 |
| 270 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); | 318 TabModel* tab_model = TabModelList::GetTabModelWithProfile(profile_); |
| 271 DCHECK(tab_model); | 319 DCHECK(tab_model); |
| 272 if (!tab_model) | 320 if (!tab_model) |
| 273 return false; | 321 return false; |
| 274 | 322 |
| 275 std::vector<content::NavigationEntry*> entries = | 323 std::vector<content::NavigationEntry*> entries = |
| 276 sessions::SerializedNavigationEntry::ToNavigationEntries( | 324 sessions::SerializedNavigationEntry::ToNavigationEntries( |
| 277 tab->navigations, profile_); | 325 tab->navigations, profile_); |
| 278 content::WebContents* new_web_contents = content::WebContents::Create( | 326 content::WebContents* new_web_contents = content::WebContents::Create( |
| 279 content::WebContents::CreateParams(profile_)); | 327 content::WebContents::CreateParams(profile_)); |
| 280 int selected_index = tab->normalized_navigation_index(); | 328 int selected_index = tab->normalized_navigation_index(); |
| 281 new_web_contents->GetController().Restore( | 329 new_web_contents->GetController().Restore( |
| 282 selected_index, | 330 selected_index, |
| 283 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, | 331 content::NavigationController::RESTORE_LAST_SESSION_EXITED_CLEANLY, |
| 284 &entries); | 332 &entries); |
| 285 tab_model->CreateTab(new_web_contents); | 333 |
| 334 content::WebContents* old_web_contents = | |
| 335 tab_model->GetWebContentsAt(tab_model->GetActiveIndex()); | |
| 336 | |
| 337 TabAndroid* currentTab = TabAndroid::GetNativeTab(env, j_tab); | |
| 338 currentTab->SwapTabContents(old_web_contents, new_web_contents); | |
| 286 | 339 |
| 287 return true; | 340 return true; |
| 288 } | 341 } |
| 289 | 342 |
| 290 void ForeignSessionHelper::SetForeignSessionCollapsed(JNIEnv* env, jobject obj, | 343 void ForeignSessionHelper::SetForeignSessionCollapsed(JNIEnv* env, jobject obj, |
| 291 jstring session_tag, | 344 jstring session_tag, |
| 292 jboolean is_collapsed) { | 345 jboolean is_collapsed) { |
| 293 // Store session tags for collapsed sessions in a preference so that the | 346 // Store session tags for collapsed sessions in a preference so that the |
| 294 // collapsed state persists. | 347 // collapsed state persists. |
| 295 PrefService* prefs = profile_->GetPrefs(); | 348 PrefService* prefs = profile_->GetPrefs(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 312 jstring session_tag) { | 365 jstring session_tag) { |
| 313 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 366 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
| 314 if (associator) | 367 if (associator) |
| 315 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 368 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
| 316 } | 369 } |
| 317 | 370 |
| 318 // static | 371 // static |
| 319 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 372 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 320 return RegisterNativesImpl(env); | 373 return RegisterNativesImpl(env); |
| 321 } | 374 } |
| OLD | NEW |