[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/android/foreign_session_helper.h" |
| 6 | |
| 7 | #include <jni.h> |
| 8 | |
| 9 | #include "base/android/jni_string.h" |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 10 | #include "base/prefs/pref_service.h" |
[email protected] | 9eec53fe | 2013-10-30 20:21:17 | [diff] [blame] | 11 | #include "base/prefs/scoped_user_pref_update.h" |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 12 | #include "chrome/browser/android/tab_android.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 13 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile_android.h" |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 15 | #include "chrome/browser/sessions/session_restore.h" |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 16 | #include "chrome/browser/sync/open_tabs_ui_delegate.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 17 | #include "chrome/browser/sync/profile_sync_service.h" |
| 18 | #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 19 | #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 20 | #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 21 | #include "chrome/common/pref_names.h" |
| 22 | #include "chrome/common/url_constants.h" |
| 23 | #include "content/public/browser/notification_source.h" |
| 24 | #include "content/public/browser/web_contents.h" |
| 25 | #include "jni/ForeignSessionHelper_jni.h" |
| 26 | |
| 27 | using base::android::ScopedJavaGlobalRef; |
| 28 | using base::android::ScopedJavaLocalRef; |
| 29 | using base::android::AttachCurrentThread; |
| 30 | using base::android::ConvertUTF16ToJavaString; |
| 31 | using base::android::ConvertUTF8ToJavaString; |
| 32 | using base::android::ConvertJavaStringToUTF8; |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 33 | using browser_sync::OpenTabsUIDelegate; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 34 | using browser_sync::SyncedSession; |
| 35 | |
| 36 | namespace { |
| 37 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 38 | OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 39 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 40 | GetForProfile(profile); |
| 41 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 42 | // Only return the delegate if it exists and it is done syncing sessions. |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 43 | if (!service || !service->ShouldPushChanges()) |
| 44 | return NULL; |
| 45 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 46 | return service->GetOpenTabsUIDelegate(); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 49 | bool ShouldSkipTab(const SessionTab& session_tab) { |
| 50 | if (session_tab.navigations.empty()) |
| 51 | return true; |
| 52 | |
maxbogue | 4157d2b | 2014-08-27 21:56:48 | [diff] [blame^] | 53 | int selected_index = session_tab.normalized_navigation_index(); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 54 | const ::sessions::SerializedNavigationEntry& current_navigation = |
| 55 | session_tab.navigations.at(selected_index); |
| 56 | |
| 57 | if (current_navigation.virtual_url().is_empty()) |
| 58 | return true; |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | bool ShouldSkipWindow(const SessionWindow& window) { |
| 64 | for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
| 65 | tab_it != window.tabs.end(); ++tab_it) { |
| 66 | const SessionTab &session_tab = **tab_it; |
| 67 | if (!ShouldSkipTab(session_tab)) |
| 68 | return false; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool ShouldSkipSession(const browser_sync::SyncedSession& session) { |
| 74 | for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 75 | session.windows.begin(); it != session.windows.end(); ++it) { |
| 76 | const SessionWindow &window = *(it->second); |
| 77 | if (!ShouldSkipWindow(window)) |
| 78 | return false; |
| 79 | } |
| 80 | return true; |
| 81 | } |
| 82 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 83 | void CopyTabsToJava( |
| 84 | JNIEnv* env, |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 85 | const SessionWindow& window, |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 86 | ScopedJavaLocalRef<jobject>& j_window) { |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 87 | for (std::vector<SessionTab*>::const_iterator tab_it = window.tabs.begin(); |
| 88 | tab_it != window.tabs.end(); ++tab_it) { |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 89 | const SessionTab &session_tab = **tab_it; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 90 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 91 | if (ShouldSkipTab(session_tab)) |
| 92 | continue; |
| 93 | |
maxbogue | 4157d2b | 2014-08-27 21:56:48 | [diff] [blame^] | 94 | int selected_index = session_tab.normalized_navigation_index(); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 95 | DCHECK(selected_index >= 0); |
| 96 | DCHECK(selected_index < static_cast<int>(session_tab.navigations.size())); |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 97 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 98 | const ::sessions::SerializedNavigationEntry& current_navigation = |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 99 | session_tab.navigations.at(selected_index); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 100 | |
| 101 | GURL tab_url = current_navigation.virtual_url(); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 102 | |
| 103 | Java_ForeignSessionHelper_pushTab( |
| 104 | env, j_window.obj(), |
[email protected] | c2a6ac3 | 2014-04-27 00:18:09 | [diff] [blame] | 105 | ConvertUTF8ToJavaString(env, tab_url.spec()).obj(), |
| 106 | ConvertUTF16ToJavaString(env, current_navigation.title()).obj(), |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 107 | session_tab.timestamp.ToJavaTime(), |
| 108 | session_tab.tab_id.id()); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | void CopyWindowsToJava( |
| 113 | JNIEnv* env, |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 114 | const SyncedSession& session, |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 115 | ScopedJavaLocalRef<jobject>& j_session) { |
| 116 | for (SyncedSession::SyncedWindowMap::const_iterator it = |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 117 | session.windows.begin(); it != session.windows.end(); ++it) { |
| 118 | const SessionWindow &window = *(it->second); |
| 119 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 120 | if (ShouldSkipWindow(window)) |
| 121 | continue; |
| 122 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 123 | ScopedJavaLocalRef<jobject> last_pushed_window; |
| 124 | last_pushed_window.Reset( |
| 125 | Java_ForeignSessionHelper_pushWindow( |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 126 | env, j_session.obj(), |
| 127 | window.timestamp.ToJavaTime(), |
| 128 | window.window_id.id())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 129 | |
| 130 | CopyTabsToJava(env, window, last_pushed_window); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | } // namespace |
| 135 | |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 136 | static jlong Init(JNIEnv* env, jclass clazz, jobject profile) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 137 | ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
| 138 | ProfileAndroid::FromProfileAndroid(profile)); |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 139 | return reinterpret_cast<intptr_t>(foreign_session_helper); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
| 143 | : profile_(profile) { |
| 144 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 145 | GetForProfile(profile); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 146 | registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
| 147 | content::Source<ProfileSyncService>(service)); |
| 148 | registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED, |
| 149 | content::Source<Profile>(profile)); |
| 150 | registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, |
| 151 | content::Source<Profile>(profile)); |
| 152 | } |
| 153 | |
| 154 | ForeignSessionHelper::~ForeignSessionHelper() { |
| 155 | } |
| 156 | |
| 157 | void ForeignSessionHelper::Destroy(JNIEnv* env, jobject obj) { |
| 158 | delete this; |
| 159 | } |
| 160 | |
| 161 | jboolean ForeignSessionHelper::IsTabSyncEnabled(JNIEnv* env, jobject obj) { |
| 162 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 163 | GetForProfile(profile_); |
| 164 | return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS); |
| 165 | } |
| 166 | |
| 167 | void ForeignSessionHelper::SetOnForeignSessionCallback(JNIEnv* env, |
| 168 | jobject obj, |
| 169 | jobject callback) { |
| 170 | callback_.Reset(env, callback); |
| 171 | } |
| 172 | |
| 173 | void ForeignSessionHelper::Observe( |
| 174 | int type, const content::NotificationSource& source, |
| 175 | const content::NotificationDetails& details) { |
| 176 | if (callback_.is_null()) |
| 177 | return; |
| 178 | |
| 179 | JNIEnv* env = AttachCurrentThread(); |
| 180 | |
| 181 | switch (type) { |
| 182 | case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED: |
| 183 | // Tab sync is disabled, so clean up data about collapsed sessions. |
| 184 | profile_->GetPrefs()->ClearPref( |
| 185 | prefs::kNtpCollapsedForeignSessions); |
| 186 | // Purposeful fall through. |
| 187 | case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE: |
| 188 | case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED: |
| 189 | Java_ForeignSessionCallback_onUpdated(env, callback_.obj()); |
| 190 | break; |
| 191 | default: |
| 192 | NOTREACHED(); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | jboolean ForeignSessionHelper::GetForeignSessions(JNIEnv* env, |
| 197 | jobject obj, |
| 198 | jobject result) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 199 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 200 | if (!open_tabs) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 201 | return false; |
| 202 | |
| 203 | std::vector<const browser_sync::SyncedSession*> sessions; |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 204 | if (!open_tabs->GetAllForeignSessions(&sessions)) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 205 | return false; |
| 206 | |
| 207 | // Use a pref to keep track of sessions that were collapsed by the user. |
| 208 | // To prevent the pref from accumulating stale sessions, clear it each time |
| 209 | // and only add back sessions that are still current. |
| 210 | DictionaryPrefUpdate pref_update(profile_->GetPrefs(), |
| 211 | prefs::kNtpCollapsedForeignSessions); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 212 | base::DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
| 213 | scoped_ptr<base::DictionaryValue> collapsed_sessions( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 214 | pref_collapsed_sessions->DeepCopy()); |
| 215 | pref_collapsed_sessions->Clear(); |
| 216 | |
| 217 | ScopedJavaLocalRef<jobject> last_pushed_session; |
| 218 | ScopedJavaLocalRef<jobject> last_pushed_window; |
| 219 | |
| 220 | // Note: we don't own the SyncedSessions themselves. |
| 221 | for (size_t i = 0; i < sessions.size(); ++i) { |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 222 | const browser_sync::SyncedSession &session = *(sessions[i]); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 223 | if (ShouldSkipSession(session)) |
| 224 | continue; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 225 | |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 226 | const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 227 | |
| 228 | if (is_collapsed) |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 229 | pref_collapsed_sessions->SetBoolean(session.session_tag, true); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 230 | |
| 231 | last_pushed_session.Reset( |
| 232 | Java_ForeignSessionHelper_pushSession( |
| 233 | env, |
| 234 | result, |
[email protected] | c2a6ac3 | 2014-04-27 00:18:09 | [diff] [blame] | 235 | ConvertUTF8ToJavaString(env, session.session_tag).obj(), |
| 236 | ConvertUTF8ToJavaString(env, session.session_name).obj(), |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 237 | session.device_type, |
| 238 | session.modified_time.ToJavaTime())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 239 | |
| 240 | CopyWindowsToJava(env, session, last_pushed_session); |
| 241 | } |
| 242 | |
| 243 | return true; |
| 244 | } |
| 245 | |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 246 | jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env, |
| 247 | jobject obj, |
| 248 | jobject j_tab, |
| 249 | jstring session_tag, |
| 250 | jint session_tab_id, |
| 251 | jint j_disposition) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 252 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 253 | if (!open_tabs) { |
| 254 | LOG(ERROR) << "Null OpenTabsUIDelegate returned."; |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | |
| 258 | const SessionTab* session_tab; |
| 259 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 260 | if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
| 261 | session_tab_id, |
| 262 | &session_tab)) { |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 263 | LOG(ERROR) << "Failed to load foreign tab."; |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | if (session_tab->navigations.empty()) { |
| 268 | LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
| 273 | if (!tab_android) |
| 274 | return false; |
| 275 | content::WebContents* web_contents = tab_android->web_contents(); |
| 276 | if (!web_contents) |
| 277 | return false; |
| 278 | |
| 279 | WindowOpenDisposition disposition = |
| 280 | static_cast<WindowOpenDisposition>(j_disposition); |
| 281 | SessionRestore::RestoreForeignSessionTab(web_contents, |
| 282 | *session_tab, |
| 283 | disposition); |
| 284 | |
| 285 | return true; |
| 286 | } |
| 287 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 288 | void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj, |
| 289 | jstring session_tag) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 290 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 291 | if (open_tabs) |
| 292 | open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // static |
| 296 | bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 297 | return RegisterNativesImpl(env); |
| 298 | } |