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