| 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 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "jni/ForeignSessionHelper_jni.h" | 28 #include "jni/ForeignSessionHelper_jni.h" |
| 29 | 29 |
| 30 using base::android::JavaParamRef; | 30 using base::android::JavaParamRef; |
| 31 using base::android::ScopedJavaGlobalRef; | 31 using base::android::ScopedJavaGlobalRef; |
| 32 using base::android::ScopedJavaLocalRef; | 32 using base::android::ScopedJavaLocalRef; |
| 33 using base::android::AttachCurrentThread; | 33 using base::android::AttachCurrentThread; |
| 34 using base::android::ConvertUTF16ToJavaString; | 34 using base::android::ConvertUTF16ToJavaString; |
| 35 using base::android::ConvertUTF8ToJavaString; | 35 using base::android::ConvertUTF8ToJavaString; |
| 36 using base::android::ConvertJavaStringToUTF8; | 36 using base::android::ConvertJavaStringToUTF8; |
| 37 using sync_driver::OpenTabsUIDelegate; | 37 using sync_sessions::OpenTabsUIDelegate; |
| 38 using sync_driver::SyncedSession; | 38 using sync_sessions::SyncedSession; |
| 39 | 39 |
| 40 namespace { | 40 namespace { |
| 41 | 41 |
| 42 OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { | 42 OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { |
| 43 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 43 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 44 GetForProfile(profile); | 44 GetForProfile(profile); |
| 45 | 45 |
| 46 // Only return the delegate if it exists and it is done syncing sessions. | 46 // Only return the delegate if it exists and it is done syncing sessions. |
| 47 if (!service || !service->IsSyncActive()) | 47 if (!service || !service->IsSyncActive()) |
| 48 return NULL; | 48 return NULL; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 bool ShouldSkipWindow(const sessions::SessionWindow& window) { | 67 bool ShouldSkipWindow(const sessions::SessionWindow& window) { |
| 68 for (std::vector<sessions::SessionTab*>::const_iterator tab_it = | 68 for (std::vector<sessions::SessionTab*>::const_iterator tab_it = |
| 69 window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) { | 69 window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) { |
| 70 const sessions::SessionTab &session_tab = **tab_it; | 70 const sessions::SessionTab &session_tab = **tab_it; |
| 71 if (!ShouldSkipTab(session_tab)) | 71 if (!ShouldSkipTab(session_tab)) |
| 72 return false; | 72 return false; |
| 73 } | 73 } |
| 74 return true; | 74 return true; |
| 75 } | 75 } |
| 76 | 76 |
| 77 bool ShouldSkipSession(const sync_driver::SyncedSession& session) { | 77 bool ShouldSkipSession(const SyncedSession& session) { |
| 78 for (SyncedSession::SyncedWindowMap::const_iterator it = | 78 for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 79 session.windows.begin(); it != session.windows.end(); ++it) { | 79 session.windows.begin(); it != session.windows.end(); ++it) { |
| 80 const sessions::SessionWindow &window = *(it->second); | 80 const sessions::SessionWindow &window = *(it->second); |
| 81 if (!ShouldSkipWindow(window)) | 81 if (!ShouldSkipWindow(window)) |
| 82 return false; | 82 return false; |
| 83 } | 83 } |
| 84 return true; | 84 return true; |
| 85 } | 85 } |
| 86 | 86 |
| 87 void CopyTabToJava( | 87 void CopyTabToJava( |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 209 } |
| 210 | 210 |
| 211 jboolean ForeignSessionHelper::GetForeignSessions( | 211 jboolean ForeignSessionHelper::GetForeignSessions( |
| 212 JNIEnv* env, | 212 JNIEnv* env, |
| 213 const JavaParamRef<jobject>& obj, | 213 const JavaParamRef<jobject>& obj, |
| 214 const JavaParamRef<jobject>& result) { | 214 const JavaParamRef<jobject>& result) { |
| 215 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); | 215 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 216 if (!open_tabs) | 216 if (!open_tabs) |
| 217 return false; | 217 return false; |
| 218 | 218 |
| 219 std::vector<const sync_driver::SyncedSession*> sessions; | 219 std::vector<const SyncedSession*> sessions; |
| 220 if (!open_tabs->GetAllForeignSessions(&sessions)) | 220 if (!open_tabs->GetAllForeignSessions(&sessions)) |
| 221 return false; | 221 return false; |
| 222 | 222 |
| 223 // Use a pref to keep track of sessions that were collapsed by the user. | 223 // Use a pref to keep track of sessions that were collapsed by the user. |
| 224 // To prevent the pref from accumulating stale sessions, clear it each time | 224 // To prevent the pref from accumulating stale sessions, clear it each time |
| 225 // and only add back sessions that are still current. | 225 // and only add back sessions that are still current. |
| 226 DictionaryPrefUpdate pref_update(profile_->GetPrefs(), | 226 DictionaryPrefUpdate pref_update(profile_->GetPrefs(), |
| 227 prefs::kNtpCollapsedForeignSessions); | 227 prefs::kNtpCollapsedForeignSessions); |
| 228 base::DictionaryValue* pref_collapsed_sessions = pref_update.Get(); | 228 base::DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
| 229 std::unique_ptr<base::DictionaryValue> collapsed_sessions( | 229 std::unique_ptr<base::DictionaryValue> collapsed_sessions( |
| 230 pref_collapsed_sessions->DeepCopy()); | 230 pref_collapsed_sessions->DeepCopy()); |
| 231 pref_collapsed_sessions->Clear(); | 231 pref_collapsed_sessions->Clear(); |
| 232 | 232 |
| 233 ScopedJavaLocalRef<jobject> last_pushed_session; | 233 ScopedJavaLocalRef<jobject> last_pushed_session; |
| 234 | 234 |
| 235 // Note: we don't own the SyncedSessions themselves. | 235 // Note: we don't own the SyncedSessions themselves. |
| 236 for (size_t i = 0; i < sessions.size(); ++i) { | 236 for (size_t i = 0; i < sessions.size(); ++i) { |
| 237 const sync_driver::SyncedSession& session = *(sessions[i]); | 237 const SyncedSession& session = *(sessions[i]); |
| 238 if (ShouldSkipSession(session)) | 238 if (ShouldSkipSession(session)) |
| 239 continue; | 239 continue; |
| 240 | 240 |
| 241 const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); | 241 const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); |
| 242 | 242 |
| 243 if (is_collapsed) | 243 if (is_collapsed) |
| 244 pref_collapsed_sessions->SetBoolean(session.session_tag, true); | 244 pref_collapsed_sessions->SetBoolean(session.session_tag, true); |
| 245 | 245 |
| 246 last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession( | 246 last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession( |
| 247 env, result, ConvertUTF8ToJavaString(env, session.session_tag), | 247 env, result, ConvertUTF8ToJavaString(env, session.session_tag), |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 const JavaParamRef<jstring>& session_tag) { | 321 const JavaParamRef<jstring>& session_tag) { |
| 322 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); | 322 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 323 if (open_tabs) | 323 if (open_tabs) |
| 324 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 324 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
| 325 } | 325 } |
| 326 | 326 |
| 327 // static | 327 // static |
| 328 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 328 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 329 return RegisterNativesImpl(env); | 329 return RegisterNativesImpl(env); |
| 330 } | 330 } |
| OLD | NEW |