| 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 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 250 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
| 251 jobject obj, | 251 jobject obj, |
| 252 jobject j_tab, | 252 jobject j_tab, |
| 253 jstring session_tag, | 253 jstring session_tag, |
| 254 jint session_tab_id, | 254 jint session_tab_id, |
| 255 jint j_disposition) { | 255 jint j_disposition) { |
| 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, | 265 session_tab_id, |
| 266 &session_tab)) { | 266 &session_tab)) { |
| 267 LOG(ERROR) << "Failed to load foreign tab."; | 267 LOG(ERROR) << "Failed to load foreign tab."; |
| 268 return false; | 268 return false; |
| 269 } | 269 } |
| 270 | 270 |
| 271 if (session_tab->navigations.empty()) { | 271 if (session_tab->navigations.empty()) { |
| 272 LOG(ERROR) << "Foreign tab no longer has valid navigations."; | 272 LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 273 return false; | 273 return false; |
| 274 } | 274 } |
| 275 | 275 |
| 276 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); | 276 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
| 277 if (!tab_android) | 277 if (!tab_android) |
| 278 return false; | 278 return false; |
| 279 content::WebContents* web_contents = tab_android->web_contents(); | 279 content::WebContents* web_contents = tab_android->web_contents(); |
| 280 if (!web_contents) | 280 if (!web_contents) |
| 281 return false; | 281 return false; |
| 282 | 282 |
| 283 WindowOpenDisposition disposition = | 283 WindowOpenDisposition disposition = |
| 284 static_cast<WindowOpenDisposition>(j_disposition); | 284 static_cast<WindowOpenDisposition>(j_disposition); |
| 285 SessionRestore::RestoreForeignSessionTab(web_contents, | 285 SessionRestore::RestoreForeignSessionTab(web_contents, |
| 286 *session_tab, | 286 *session_tab, |
| 287 disposition); | 287 disposition); |
| 288 | 288 |
| 289 return true; | 289 return true; |
| 290 } | 290 } |
| 291 | 291 |
| 292 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, | 292 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
| 293 jstring session_tag) { | 293 jstring session_tag) { |
| 294 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 294 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 295 if (associator) | 295 if (open_tabs) |
| 296 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 296 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
| 297 } | 297 } |
| 298 | 298 |
| 299 // static | 299 // static |
| 300 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 300 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 301 return RegisterNativesImpl(env); | 301 return RegisterNativesImpl(env); |
| 302 } | 302 } |
| OLD | NEW |