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 "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 36 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
37 GetForProfile(profile); | 37 GetForProfile(profile); |
38 | 38 |
39 // Only return the associator if it exists and it is done syncing sessions. | 39 // Only return the associator if it exists and it is done syncing sessions. |
40 if (!service || !service->ShouldPushChanges()) | 40 if (!service || !service->ShouldPushChanges()) |
41 return NULL; | 41 return NULL; |
42 | 42 |
43 return service->GetSessionModelAssociator(); | 43 return service->GetSessionModelAssociator(); |
44 } | 44 } |
45 | 45 |
| 46 bool ShouldSkipTab(const SessionTab& tab) { |
| 47 if (tab.navigations.empty()) |
| 48 return true; |
| 49 |
| 50 int selected_index = tab.current_navigation_index; |
| 51 if (selected_index < 0 || |
| 52 selected_index >= static_cast<int>(tab.navigations.size())) |
| 53 return true; |
| 54 |
| 55 return false; |
| 56 } |
| 57 |
| 58 bool ShouldSkipWindow(const SessionWindow& window) { |
| 59 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
| 60 tab_it != window.tabs.end(); ++tab_it) { |
| 61 const SessionTab &tab = **tab_it; |
| 62 if (!ShouldSkipTab(tab)) |
| 63 return false; |
| 64 } |
| 65 return true; |
| 66 } |
| 67 |
| 68 bool ShouldSkipSession(const browser_sync::SyncedSession& session) { |
| 69 for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 70 session.windows.begin(); it != session.windows.end(); ++it) { |
| 71 const SessionWindow &window = *(it->second); |
| 72 if (!ShouldSkipWindow(window)) |
| 73 return false; |
| 74 } |
| 75 return true; |
| 76 } |
| 77 |
46 void CopyTabsToJava( | 78 void CopyTabsToJava( |
47 JNIEnv* env, | 79 JNIEnv* env, |
48 const SessionWindow* window, | 80 const SessionWindow& window, |
49 ScopedJavaLocalRef<jobject>& j_window) { | 81 ScopedJavaLocalRef<jobject>& j_window) { |
50 for (std::vector<SessionTab*>::const_iterator tab_it = window->tabs.begin(); | 82 for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
51 tab_it != window->tabs.end(); ++tab_it) { | 83 tab_it != window.tabs.end(); ++tab_it) { |
52 const SessionTab &tab = **tab_it; | 84 const SessionTab &tab = **tab_it; |
53 | 85 |
54 if (tab.navigations.empty()) | 86 if (ShouldSkipTab(tab)) |
55 continue; | 87 continue; |
56 | 88 |
| 89 int selected_index = tab.current_navigation_index; |
| 90 DCHECK(selected_index >= 0); |
| 91 DCHECK(selected_index < static_cast<int>(tab.navigations.size())); |
| 92 |
57 const ::sessions::SerializedNavigationEntry& current_navigation = | 93 const ::sessions::SerializedNavigationEntry& current_navigation = |
58 tab.navigations.at(tab.current_navigation_index); | 94 tab.navigations.at(selected_index); |
59 | 95 |
60 GURL tab_url = current_navigation.virtual_url(); | 96 GURL tab_url = current_navigation.virtual_url(); |
61 if (tab_url.SchemeIs(chrome::kChromeNativeScheme) || | |
62 (tab_url.SchemeIs(chrome::kChromeUIScheme) && | |
63 tab_url.host() == chrome::kChromeUINewTabHost)) | |
64 continue; | |
65 | 97 |
66 Java_ForeignSessionHelper_pushTab( | 98 Java_ForeignSessionHelper_pushTab( |
67 env, j_window.obj(), | 99 env, j_window.obj(), |
68 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), | 100 ConvertUTF8ToJavaString(env, tab_url.spec()).Release(), |
69 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), | 101 ConvertUTF16ToJavaString(env, current_navigation.title()).Release(), |
70 tab.timestamp.ToInternalValue(), tab.tab_id.id()); | 102 tab.timestamp.ToJavaTime(), |
| 103 tab.tab_id.id()); |
71 } | 104 } |
72 } | 105 } |
73 | 106 |
74 void CopyWindowsToJava( | 107 void CopyWindowsToJava( |
75 JNIEnv* env, | 108 JNIEnv* env, |
76 const SyncedSession* session, | 109 const SyncedSession& session, |
77 ScopedJavaLocalRef<jobject>& j_session) { | 110 ScopedJavaLocalRef<jobject>& j_session) { |
78 for (SyncedSession::SyncedWindowMap::const_iterator it = | 111 for (SyncedSession::SyncedWindowMap::const_iterator it = |
79 session->windows.begin(); it != session->windows.end(); ++it) { | 112 session.windows.begin(); it != session.windows.end(); ++it) { |
80 const SessionWindow* window = it->second; | 113 const SessionWindow &window = *(it->second); |
| 114 |
| 115 if (ShouldSkipWindow(window)) |
| 116 continue; |
81 | 117 |
82 ScopedJavaLocalRef<jobject> last_pushed_window; | 118 ScopedJavaLocalRef<jobject> last_pushed_window; |
83 last_pushed_window.Reset( | 119 last_pushed_window.Reset( |
84 Java_ForeignSessionHelper_pushWindow( | 120 Java_ForeignSessionHelper_pushWindow( |
85 env, j_session.obj(), window->timestamp.ToInternalValue(), | 121 env, j_session.obj(), |
86 window->window_id.id())); | 122 window.timestamp.ToJavaTime(), |
| 123 window.window_id.id())); |
87 | 124 |
88 CopyTabsToJava(env, window, last_pushed_window); | 125 CopyTabsToJava(env, window, last_pushed_window); |
89 } | 126 } |
90 } | 127 } |
91 | 128 |
92 } // namespace | 129 } // namespace |
93 | 130 |
94 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { | 131 static jint Init(JNIEnv* env, jclass clazz, jobject profile) { |
95 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( | 132 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
96 ProfileAndroid::FromProfileAndroid(profile)); | 133 ProfileAndroid::FromProfileAndroid(profile)); |
97 return reinterpret_cast<jint>(foreign_session_helper); | 134 return reinterpret_cast<jint>(foreign_session_helper); |
98 } | 135 } |
99 | 136 |
100 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) | 137 ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
101 : profile_(profile) { | 138 : profile_(profile) { |
102 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> | 139 ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
103 GetForProfile(profile); | 140 GetForProfile(profile); |
104 | |
105 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 141 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
106 content::Source<ProfileSyncService>(service)); | 142 content::Source<ProfileSyncService>(service)); |
107 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, | 143 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, |
108 content::Source<Profile>(profile)); | 144 content::Source<Profile>(profile)); |
109 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, | 145 registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
110 content::Source<Profile>(profile)); | 146 content::Source<Profile>(profile)); |
111 } | 147 } |
112 | 148 |
113 ForeignSessionHelper::~ForeignSessionHelper() { | 149 ForeignSessionHelper::~ForeignSessionHelper() { |
114 } | 150 } |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 DictionaryValue* pref_collapsed_sessions = pref_update.Get(); | 207 DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
172 scoped_ptr<DictionaryValue> collapsed_sessions( | 208 scoped_ptr<DictionaryValue> collapsed_sessions( |
173 pref_collapsed_sessions->DeepCopy()); | 209 pref_collapsed_sessions->DeepCopy()); |
174 pref_collapsed_sessions->Clear(); | 210 pref_collapsed_sessions->Clear(); |
175 | 211 |
176 ScopedJavaLocalRef<jobject> last_pushed_session; | 212 ScopedJavaLocalRef<jobject> last_pushed_session; |
177 ScopedJavaLocalRef<jobject> last_pushed_window; | 213 ScopedJavaLocalRef<jobject> last_pushed_window; |
178 | 214 |
179 // Note: we don't own the SyncedSessions themselves. | 215 // Note: we don't own the SyncedSessions themselves. |
180 for (size_t i = 0; i < sessions.size(); ++i) { | 216 for (size_t i = 0; i < sessions.size(); ++i) { |
181 const browser_sync::SyncedSession* session = sessions[i]; | 217 const browser_sync::SyncedSession &session = *(sessions[i]); |
| 218 if (ShouldSkipSession(session)) |
| 219 continue; |
182 | 220 |
183 const bool is_collapsed = collapsed_sessions->HasKey(session->session_tag); | 221 const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); |
184 | 222 |
185 if (is_collapsed) | 223 if (is_collapsed) |
186 pref_collapsed_sessions->SetBoolean(session->session_tag, true); | 224 pref_collapsed_sessions->SetBoolean(session.session_tag, true); |
187 | 225 |
188 last_pushed_session.Reset( | 226 last_pushed_session.Reset( |
189 Java_ForeignSessionHelper_pushSession( | 227 Java_ForeignSessionHelper_pushSession( |
190 env, | 228 env, |
191 result, | 229 result, |
192 ConvertUTF8ToJavaString(env, session->session_tag).Release(), | 230 ConvertUTF8ToJavaString(env, session.session_tag).Release(), |
193 ConvertUTF8ToJavaString(env, session->session_name).Release(), | 231 ConvertUTF8ToJavaString(env, session.session_name).Release(), |
194 ConvertUTF8ToJavaString(env, | 232 session.device_type, |
195 session->DeviceTypeAsString()).Release(), | 233 session.modified_time.ToJavaTime())); |
196 session->modified_time.ToInternalValue())); | |
197 | 234 |
198 CopyWindowsToJava(env, session, last_pushed_session); | 235 CopyWindowsToJava(env, session, last_pushed_session); |
199 } | 236 } |
200 | 237 |
201 return true; | 238 return true; |
202 } | 239 } |
203 | 240 |
204 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, | 241 jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
205 jobject obj, | 242 jobject obj, |
206 jstring session_tag, | 243 jstring session_tag, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // Store session tags for collapsed sessions in a preference so that the | 287 // Store session tags for collapsed sessions in a preference so that the |
251 // collapsed state persists. | 288 // collapsed state persists. |
252 PrefService* prefs = profile_->GetPrefs(); | 289 PrefService* prefs = profile_->GetPrefs(); |
253 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); | 290 DictionaryPrefUpdate update(prefs, prefs::kNtpCollapsedForeignSessions); |
254 if (is_collapsed) | 291 if (is_collapsed) |
255 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); | 292 update.Get()->SetBoolean(ConvertJavaStringToUTF8(env, session_tag), true); |
256 else | 293 else |
257 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); | 294 update.Get()->Remove(ConvertJavaStringToUTF8(env, session_tag), NULL); |
258 } | 295 } |
259 | 296 |
| 297 jboolean ForeignSessionHelper::GetForeignSessionCollapsed(JNIEnv* env, |
| 298 jobject obj, |
| 299 jstring session_tag) { |
| 300 const DictionaryValue* dict = profile_->GetPrefs()->GetDictionary( |
| 301 prefs::kNtpCollapsedForeignSessions); |
| 302 return dict && dict->HasKey(ConvertJavaStringToUTF8(env, session_tag)); |
| 303 } |
| 304 |
260 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, | 305 void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
261 jstring session_tag) { | 306 jstring session_tag) { |
262 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); | 307 SessionModelAssociator* associator = GetSessionModelAssociator(profile_); |
263 if (associator) | 308 if (associator) |
264 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); | 309 associator->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
265 } | 310 } |
266 | 311 |
267 // static | 312 // static |
268 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { | 313 bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
269 return RegisterNativesImpl(env); | 314 return RegisterNativesImpl(env); |
270 } | 315 } |
OLD | NEW |