[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> |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 8 | #include <stddef.h> |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 9 | |
| 10 | #include "base/android/jni_string.h" |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 11 | #include "chrome/browser/android/tab_android.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 12 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 13 | #include "chrome/browser/profiles/profile_android.h" |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 14 | #include "chrome/browser/sessions/session_restore.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 15 | #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 16 | #include "chrome/browser/ui/android/tab_model/tab_model.h" |
| 17 | #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 18 | #include "chrome/common/pref_names.h" |
| 19 | #include "chrome/common/url_constants.h" |
maxbogue | 26f4022 | 2016-09-16 20:22:18 | [diff] [blame] | 20 | #include "components/browser_sync/profile_sync_service.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 21 | #include "components/prefs/pref_service.h" |
| 22 | #include "components/prefs/scoped_user_pref_update.h" |
maxbogue | 8ef2508 | 2015-11-16 19:09:58 | [diff] [blame] | 23 | #include "components/sync_sessions/open_tabs_ui_delegate.h" |
maxbogue | 5c8d0e2 | 2014-11-06 01:00:01 | [diff] [blame] | 24 | #include "content/public/browser/notification_details.h" |
| 25 | #include "content/public/browser/notification_service.h" |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 26 | #include "content/public/browser/notification_source.h" |
| 27 | #include "content/public/browser/web_contents.h" |
| 28 | #include "jni/ForeignSessionHelper_jni.h" |
| 29 | |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 30 | using base::android::JavaParamRef; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 31 | using base::android::ScopedJavaGlobalRef; |
| 32 | using base::android::ScopedJavaLocalRef; |
| 33 | using base::android::AttachCurrentThread; |
| 34 | using base::android::ConvertUTF16ToJavaString; |
| 35 | using base::android::ConvertUTF8ToJavaString; |
| 36 | using base::android::ConvertJavaStringToUTF8; |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 37 | using sync_sessions::OpenTabsUIDelegate; |
| 38 | using sync_sessions::SyncedSession; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 39 | |
| 40 | namespace { |
| 41 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 42 | OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) { |
maxbogue | 0a37945 | 2016-09-22 21:35:05 | [diff] [blame] | 43 | browser_sync::ProfileSyncService* service = |
| 44 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 45 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 46 | // Only return the delegate if it exists and it is done syncing sessions. |
maxbogue | 192b360 | 2015-06-04 00:33:55 | [diff] [blame] | 47 | if (!service || !service->IsSyncActive()) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 48 | return NULL; |
| 49 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 50 | return service->GetOpenTabsUIDelegate(); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 51 | } |
| 52 | |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 53 | bool ShouldSkipTab(const sessions::SessionTab& session_tab) { |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 54 | if (session_tab.navigations.empty()) |
| 55 | return true; |
| 56 | |
maxbogue | 4157d2b | 2014-08-27 21:56:48 | [diff] [blame] | 57 | int selected_index = session_tab.normalized_navigation_index(); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 58 | const sessions::SerializedNavigationEntry& current_navigation = |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 59 | session_tab.navigations.at(selected_index); |
| 60 | |
| 61 | if (current_navigation.virtual_url().is_empty()) |
| 62 | return true; |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 67 | bool ShouldSkipWindow(const sessions::SessionWindow& window) { |
avi | 498cabca | 2016-09-21 19:03:50 | [diff] [blame] | 68 | for (const auto& tab_ptr : window.tabs) { |
| 69 | const sessions::SessionTab& session_tab = *(tab_ptr.get()); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 70 | if (!ShouldSkipTab(session_tab)) |
| 71 | return false; |
| 72 | } |
| 73 | return true; |
| 74 | } |
| 75 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 76 | bool ShouldSkipSession(const SyncedSession& session) { |
avi | 498cabca | 2016-09-21 19:03:50 | [diff] [blame] | 77 | for (const auto& window_pair : session.windows) { |
zea | 325508e | 2017-03-29 20:11:22 | [diff] [blame] | 78 | const sessions::SessionWindow& window = window_pair.second->wrapped_window; |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 79 | if (!ShouldSkipWindow(window)) |
| 80 | return false; |
| 81 | } |
| 82 | return true; |
| 83 | } |
| 84 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 85 | void JNI_ForeignSessionHelper_CopyTabToJava( |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 86 | JNIEnv* env, |
| 87 | const sessions::SessionTab& tab, |
| 88 | ScopedJavaLocalRef<jobject>& j_window) { |
| 89 | int selected_index = tab.normalized_navigation_index(); |
| 90 | DCHECK_GE(selected_index, 0); |
| 91 | DCHECK_LT(selected_index, static_cast<int>(tab.navigations.size())); |
| 92 | |
| 93 | const sessions::SerializedNavigationEntry& current_navigation = |
| 94 | tab.navigations.at(selected_index); |
| 95 | |
| 96 | GURL tab_url = current_navigation.virtual_url(); |
| 97 | |
| 98 | Java_ForeignSessionHelper_pushTab( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 99 | env, j_window, ConvertUTF8ToJavaString(env, tab_url.spec()), |
| 100 | ConvertUTF16ToJavaString(env, current_navigation.title()), |
| 101 | tab.timestamp.ToJavaTime(), tab.tab_id.id()); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 102 | } |
| 103 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 104 | void JNI_ForeignSessionHelper_CopyWindowToJava( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 105 | JNIEnv* env, |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 106 | const sessions::SessionWindow& window, |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 107 | ScopedJavaLocalRef<jobject>& j_window) { |
avi | 498cabca | 2016-09-21 19:03:50 | [diff] [blame] | 108 | for (const auto& tab_ptr : window.tabs) { |
| 109 | const sessions::SessionTab& session_tab = *(tab_ptr.get()); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 110 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 111 | if (ShouldSkipTab(session_tab)) |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 112 | return; |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 113 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 114 | JNI_ForeignSessionHelper_CopyTabToJava(env, session_tab, j_window); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 118 | void JNI_ForeignSessionHelper_CopySessionToJava( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 119 | JNIEnv* env, |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 120 | const SyncedSession& session, |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 121 | ScopedJavaLocalRef<jobject>& j_session) { |
avi | 498cabca | 2016-09-21 19:03:50 | [diff] [blame] | 122 | for (const auto& window_pair : session.windows) { |
zea | 325508e | 2017-03-29 20:11:22 | [diff] [blame] | 123 | const sessions::SessionWindow& window = window_pair.second->wrapped_window; |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 124 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 125 | if (ShouldSkipWindow(window)) |
| 126 | continue; |
| 127 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 128 | ScopedJavaLocalRef<jobject> last_pushed_window; |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 129 | last_pushed_window.Reset(Java_ForeignSessionHelper_pushWindow( |
| 130 | env, j_session, window.timestamp.ToJavaTime(), window.window_id.id())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 131 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 132 | JNI_ForeignSessionHelper_CopyWindowToJava(env, window, last_pushed_window); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace |
| 137 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 138 | static jlong JNI_ForeignSessionHelper_Init( |
| 139 | JNIEnv* env, |
| 140 | const JavaParamRef<jclass>& clazz, |
| 141 | const JavaParamRef<jobject>& profile) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 142 | ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
| 143 | ProfileAndroid::FromProfileAndroid(profile)); |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 144 | return reinterpret_cast<intptr_t>(foreign_session_helper); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 148 | : profile_(profile), scoped_observer_(this) { |
maxbogue | 0a37945 | 2016-09-22 21:35:05 | [diff] [blame] | 149 | browser_sync::ProfileSyncService* service = |
| 150 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 151 | |
| 152 | // NOTE: The ProfileSyncService can be null in tests. |
| 153 | if (service) |
| 154 | scoped_observer_.Add(service); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ForeignSessionHelper::~ForeignSessionHelper() { |
| 158 | } |
| 159 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 160 | void ForeignSessionHelper::Destroy(JNIEnv* env, |
| 161 | const JavaParamRef<jobject>& obj) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 162 | delete this; |
| 163 | } |
| 164 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 165 | jboolean ForeignSessionHelper::IsTabSyncEnabled( |
| 166 | JNIEnv* env, |
| 167 | const JavaParamRef<jobject>& obj) { |
maxbogue | 0a37945 | 2016-09-22 21:35:05 | [diff] [blame] | 168 | browser_sync::ProfileSyncService* service = |
| 169 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 170 | return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS); |
| 171 | } |
| 172 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 173 | void ForeignSessionHelper::TriggerSessionSync( |
| 174 | JNIEnv* env, |
| 175 | const JavaParamRef<jobject>& obj) { |
maxbogue | 0a37945 | 2016-09-22 21:35:05 | [diff] [blame] | 176 | browser_sync::ProfileSyncService* service = |
| 177 | ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); |
blundell | 0631ab03 | 2015-10-29 09:28:02 | [diff] [blame] | 178 | if (!service) |
| 179 | return; |
| 180 | |
maxbogue | 5c8d0e2 | 2014-11-06 01:00:01 | [diff] [blame] | 181 | const syncer::ModelTypeSet types(syncer::SESSIONS); |
blundell | 0631ab03 | 2015-10-29 09:28:02 | [diff] [blame] | 182 | service->TriggerRefresh(types); |
maxbogue | 5c8d0e2 | 2014-11-06 01:00:01 | [diff] [blame] | 183 | } |
| 184 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 185 | void ForeignSessionHelper::SetOnForeignSessionCallback( |
| 186 | JNIEnv* env, |
| 187 | const JavaParamRef<jobject>& obj, |
| 188 | const JavaParamRef<jobject>& callback) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 189 | callback_.Reset(env, callback); |
| 190 | } |
| 191 | |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 192 | void ForeignSessionHelper::FireForeignSessionCallback() { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 193 | if (callback_.is_null()) |
| 194 | return; |
| 195 | |
| 196 | JNIEnv* env = AttachCurrentThread(); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 197 | Java_ForeignSessionCallback_onUpdated(env, callback_); |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 198 | } |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 199 | |
holte | 4ef3570 | 2017-02-03 03:30:10 | [diff] [blame] | 200 | void ForeignSessionHelper::OnSyncConfigurationCompleted( |
| 201 | syncer::SyncService* sync) { |
blundell | fc39f9aa | 2015-10-30 08:25:54 | [diff] [blame] | 202 | FireForeignSessionCallback(); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 203 | } |
| 204 | |
holte | 4ef3570 | 2017-02-03 03:30:10 | [diff] [blame] | 205 | void ForeignSessionHelper::OnForeignSessionUpdated(syncer::SyncService* sync) { |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 206 | FireForeignSessionCallback(); |
| 207 | } |
| 208 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 209 | jboolean ForeignSessionHelper::GetForeignSessions( |
| 210 | JNIEnv* env, |
| 211 | const JavaParamRef<jobject>& obj, |
| 212 | const JavaParamRef<jobject>& result) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 213 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 214 | if (!open_tabs) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 215 | return false; |
| 216 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 217 | std::vector<const SyncedSession*> sessions; |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 218 | if (!open_tabs->GetAllForeignSessions(&sessions)) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 219 | return false; |
| 220 | |
| 221 | // Use a pref to keep track of sessions that were collapsed by the user. |
| 222 | // To prevent the pref from accumulating stale sessions, clear it each time |
| 223 | // and only add back sessions that are still current. |
| 224 | DictionaryPrefUpdate pref_update(profile_->GetPrefs(), |
| 225 | prefs::kNtpCollapsedForeignSessions); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 226 | base::DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
dcheng | aeceb05e | 2016-04-07 23:41:10 | [diff] [blame] | 227 | std::unique_ptr<base::DictionaryValue> collapsed_sessions( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 228 | pref_collapsed_sessions->DeepCopy()); |
| 229 | pref_collapsed_sessions->Clear(); |
| 230 | |
| 231 | ScopedJavaLocalRef<jobject> last_pushed_session; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 232 | |
| 233 | // Note: we don't own the SyncedSessions themselves. |
| 234 | for (size_t i = 0; i < sessions.size(); ++i) { |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 235 | const SyncedSession& session = *(sessions[i]); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 236 | if (ShouldSkipSession(session)) |
| 237 | continue; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 238 | |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 239 | const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 240 | |
| 241 | if (is_collapsed) |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 242 | pref_collapsed_sessions->SetBoolean(session.session_tag, true); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 243 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 244 | last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession( |
| 245 | env, result, ConvertUTF8ToJavaString(env, session.session_tag), |
| 246 | ConvertUTF8ToJavaString(env, session.session_name), session.device_type, |
| 247 | session.modified_time.ToJavaTime())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 248 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 249 | const std::string group_name = |
| 250 | base::FieldTrialList::FindFullName("TabSyncByRecency"); |
| 251 | if (group_name == "Enabled") { |
| 252 | // Create a custom window with tabs from all windows included and ordered |
| 253 | // by recency (GetForeignSessionTabs will do ordering automatically). |
| 254 | std::vector<const sessions::SessionTab*> tabs; |
| 255 | open_tabs->GetForeignSessionTabs(session.session_tag, &tabs); |
| 256 | ScopedJavaLocalRef<jobject> last_pushed_window( |
| 257 | Java_ForeignSessionHelper_pushWindow( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 258 | env, last_pushed_session, session.modified_time.ToJavaTime(), 0)); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 259 | for (const sessions::SessionTab* tab : tabs) { |
| 260 | if (ShouldSkipTab(*tab)) |
| 261 | continue; |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 262 | JNI_ForeignSessionHelper_CopyTabToJava(env, *tab, last_pushed_window); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 263 | } |
| 264 | } else { |
| 265 | // Push the full session, with tabs ordered by visual position. |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 266 | JNI_ForeignSessionHelper_CopySessionToJava(env, session, |
| 267 | last_pushed_session); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 268 | } |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return true; |
| 272 | } |
| 273 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 274 | jboolean ForeignSessionHelper::OpenForeignSessionTab( |
| 275 | JNIEnv* env, |
| 276 | const JavaParamRef<jobject>& obj, |
| 277 | const JavaParamRef<jobject>& j_tab, |
| 278 | const JavaParamRef<jstring>& session_tag, |
| 279 | jint session_tab_id, |
| 280 | jint j_disposition) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 281 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 282 | if (!open_tabs) { |
| 283 | LOG(ERROR) << "Null OpenTabsUIDelegate returned."; |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 284 | return false; |
| 285 | } |
| 286 | |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 287 | const sessions::SessionTab* session_tab; |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 288 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 289 | if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
Mikel Astiz | e023c57 | 2018-03-28 07:56:56 | [diff] [blame^] | 290 | SessionID::FromSerializedValue(session_tab_id), |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 291 | &session_tab)) { |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 292 | LOG(ERROR) << "Failed to load foreign tab."; |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | if (session_tab->navigations.empty()) { |
| 297 | LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
| 302 | if (!tab_android) |
| 303 | return false; |
| 304 | content::WebContents* web_contents = tab_android->web_contents(); |
| 305 | if (!web_contents) |
| 306 | return false; |
| 307 | |
| 308 | WindowOpenDisposition disposition = |
| 309 | static_cast<WindowOpenDisposition>(j_disposition); |
| 310 | SessionRestore::RestoreForeignSessionTab(web_contents, |
| 311 | *session_tab, |
| 312 | disposition); |
| 313 | |
| 314 | return true; |
| 315 | } |
| 316 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 317 | void ForeignSessionHelper::DeleteForeignSession( |
| 318 | JNIEnv* env, |
| 319 | const JavaParamRef<jobject>& obj, |
| 320 | const JavaParamRef<jstring>& session_tag) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 321 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 322 | if (open_tabs) |
| 323 | open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 324 | } |