[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) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 43 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 44 | GetForProfile(profile); |
| 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) { |
| 68 | for (std::vector<sessions::SessionTab*>::const_iterator tab_it = |
| 69 | window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) { |
| 70 | const sessions::SessionTab &session_tab = **tab_it; |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 71 | if (!ShouldSkipTab(session_tab)) |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 77 | bool ShouldSkipSession(const SyncedSession& session) { |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 78 | for (SyncedSession::SyncedWindowMap::const_iterator it = |
| 79 | session.windows.begin(); it != session.windows.end(); ++it) { |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 80 | const sessions::SessionWindow &window = *(it->second); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 81 | if (!ShouldSkipWindow(window)) |
| 82 | return false; |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 87 | void CopyTabToJava( |
| 88 | JNIEnv* env, |
| 89 | const sessions::SessionTab& tab, |
| 90 | ScopedJavaLocalRef<jobject>& j_window) { |
| 91 | int selected_index = tab.normalized_navigation_index(); |
| 92 | DCHECK_GE(selected_index, 0); |
| 93 | DCHECK_LT(selected_index, static_cast<int>(tab.navigations.size())); |
| 94 | |
| 95 | const sessions::SerializedNavigationEntry& current_navigation = |
| 96 | tab.navigations.at(selected_index); |
| 97 | |
| 98 | GURL tab_url = current_navigation.virtual_url(); |
| 99 | |
| 100 | Java_ForeignSessionHelper_pushTab( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 101 | env, j_window, ConvertUTF8ToJavaString(env, tab_url.spec()), |
| 102 | ConvertUTF16ToJavaString(env, current_navigation.title()), |
| 103 | tab.timestamp.ToJavaTime(), tab.tab_id.id()); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void CopyWindowToJava( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 107 | JNIEnv* env, |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 108 | const sessions::SessionWindow& window, |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 109 | ScopedJavaLocalRef<jobject>& j_window) { |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 110 | for (std::vector<sessions::SessionTab*>::const_iterator tab_it = |
| 111 | window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) { |
| 112 | const sessions::SessionTab &session_tab = **tab_it; |
[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 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 117 | CopyTabToJava(env, session_tab, j_window); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 121 | void 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) { |
| 125 | for (SyncedSession::SyncedWindowMap::const_iterator it = |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 126 | session.windows.begin(); it != session.windows.end(); ++it) { |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 127 | const sessions::SessionWindow &window = *(it->second); |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 128 | |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 129 | if (ShouldSkipWindow(window)) |
| 130 | continue; |
| 131 | |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 132 | ScopedJavaLocalRef<jobject> last_pushed_window; |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 133 | last_pushed_window.Reset(Java_ForeignSessionHelper_pushWindow( |
| 134 | env, j_session, window.timestamp.ToJavaTime(), window.window_id.id())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 135 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 136 | CopyWindowToJava(env, window, last_pushed_window); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | } // namespace |
| 141 | |
torne | 89cc5d9 | 2015-09-04 11:16:35 | [diff] [blame] | 142 | static jlong Init(JNIEnv* env, |
| 143 | const JavaParamRef<jclass>& clazz, |
| 144 | const JavaParamRef<jobject>& profile) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 145 | ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper( |
| 146 | ProfileAndroid::FromProfileAndroid(profile)); |
[email protected] | d557b604 | 2013-11-21 16:34:31 | [diff] [blame] | 147 | return reinterpret_cast<intptr_t>(foreign_session_helper); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | ForeignSessionHelper::ForeignSessionHelper(Profile* profile) |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 151 | : profile_(profile), scoped_observer_(this) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 152 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 153 | GetForProfile(profile); |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 154 | |
| 155 | // NOTE: The ProfileSyncService can be null in tests. |
| 156 | if (service) |
| 157 | scoped_observer_.Add(service); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | ForeignSessionHelper::~ForeignSessionHelper() { |
| 161 | } |
| 162 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 163 | void ForeignSessionHelper::Destroy(JNIEnv* env, |
| 164 | const JavaParamRef<jobject>& obj) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 165 | delete this; |
| 166 | } |
| 167 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 168 | jboolean ForeignSessionHelper::IsTabSyncEnabled( |
| 169 | JNIEnv* env, |
| 170 | const JavaParamRef<jobject>& obj) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 171 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 172 | GetForProfile(profile_); |
| 173 | return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS); |
| 174 | } |
| 175 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 176 | void ForeignSessionHelper::TriggerSessionSync( |
| 177 | JNIEnv* env, |
| 178 | const JavaParamRef<jobject>& obj) { |
blundell | 0631ab03 | 2015-10-29 09:28:02 | [diff] [blame] | 179 | ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()-> |
| 180 | GetForProfile(profile_); |
| 181 | if (!service) |
| 182 | return; |
| 183 | |
maxbogue | 5c8d0e2 | 2014-11-06 01:00:01 | [diff] [blame] | 184 | const syncer::ModelTypeSet types(syncer::SESSIONS); |
blundell | 0631ab03 | 2015-10-29 09:28:02 | [diff] [blame] | 185 | service->TriggerRefresh(types); |
maxbogue | 5c8d0e2 | 2014-11-06 01:00:01 | [diff] [blame] | 186 | } |
| 187 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 188 | void ForeignSessionHelper::SetOnForeignSessionCallback( |
| 189 | JNIEnv* env, |
| 190 | const JavaParamRef<jobject>& obj, |
| 191 | const JavaParamRef<jobject>& callback) { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 192 | callback_.Reset(env, callback); |
| 193 | } |
| 194 | |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 195 | void ForeignSessionHelper::FireForeignSessionCallback() { |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 196 | if (callback_.is_null()) |
| 197 | return; |
| 198 | |
| 199 | JNIEnv* env = AttachCurrentThread(); |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 200 | Java_ForeignSessionCallback_onUpdated(env, callback_); |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 201 | } |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 202 | |
blundell | fc39f9aa | 2015-10-30 08:25:54 | [diff] [blame] | 203 | void ForeignSessionHelper::OnSyncConfigurationCompleted() { |
| 204 | FireForeignSessionCallback(); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 205 | } |
| 206 | |
blundell | fc39f9aa | 2015-10-30 08:25:54 | [diff] [blame] | 207 | void ForeignSessionHelper::OnForeignSessionUpdated() { |
blundell | bbdc8a8 | 2015-10-28 15:47:20 | [diff] [blame] | 208 | FireForeignSessionCallback(); |
| 209 | } |
| 210 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 211 | jboolean ForeignSessionHelper::GetForeignSessions( |
| 212 | JNIEnv* env, |
| 213 | const JavaParamRef<jobject>& obj, |
| 214 | const JavaParamRef<jobject>& result) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 215 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 216 | if (!open_tabs) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 217 | return false; |
| 218 | |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 219 | std::vector<const SyncedSession*> sessions; |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 220 | if (!open_tabs->GetAllForeignSessions(&sessions)) |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 221 | return false; |
| 222 | |
| 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 |
| 225 | // and only add back sessions that are still current. |
| 226 | DictionaryPrefUpdate pref_update(profile_->GetPrefs(), |
| 227 | prefs::kNtpCollapsedForeignSessions); |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 228 | base::DictionaryValue* pref_collapsed_sessions = pref_update.Get(); |
dcheng | aeceb05e | 2016-04-07 23:41:10 | [diff] [blame] | 229 | std::unique_ptr<base::DictionaryValue> collapsed_sessions( |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 230 | pref_collapsed_sessions->DeepCopy()); |
| 231 | pref_collapsed_sessions->Clear(); |
| 232 | |
| 233 | ScopedJavaLocalRef<jobject> last_pushed_session; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 234 | |
| 235 | // Note: we don't own the SyncedSessions themselves. |
| 236 | for (size_t i = 0; i < sessions.size(); ++i) { |
maxbogue | a79d99b7 | 2016-09-15 15:59:16 | [diff] [blame] | 237 | const SyncedSession& session = *(sessions[i]); |
[email protected] | 48bf157 | 2014-08-12 01:31:09 | [diff] [blame] | 238 | if (ShouldSkipSession(session)) |
| 239 | continue; |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 240 | |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 241 | const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 242 | |
| 243 | if (is_collapsed) |
[email protected] | 9744a15e | 2013-09-23 20:59:08 | [diff] [blame] | 244 | pref_collapsed_sessions->SetBoolean(session.session_tag, true); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 245 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 246 | last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession( |
| 247 | env, result, ConvertUTF8ToJavaString(env, session.session_tag), |
| 248 | ConvertUTF8ToJavaString(env, session.session_name), session.device_type, |
| 249 | session.modified_time.ToJavaTime())); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 250 | |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 251 | const std::string group_name = |
| 252 | base::FieldTrialList::FindFullName("TabSyncByRecency"); |
| 253 | if (group_name == "Enabled") { |
| 254 | // Create a custom window with tabs from all windows included and ordered |
| 255 | // by recency (GetForeignSessionTabs will do ordering automatically). |
| 256 | std::vector<const sessions::SessionTab*> tabs; |
| 257 | open_tabs->GetForeignSessionTabs(session.session_tag, &tabs); |
| 258 | ScopedJavaLocalRef<jobject> last_pushed_window( |
| 259 | Java_ForeignSessionHelper_pushWindow( |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 260 | env, last_pushed_session, session.modified_time.ToJavaTime(), 0)); |
zea | e8f0c131d | 2015-08-04 23:20:27 | [diff] [blame] | 261 | for (const sessions::SessionTab* tab : tabs) { |
| 262 | if (ShouldSkipTab(*tab)) |
| 263 | continue; |
| 264 | CopyTabToJava(env, *tab, last_pushed_window); |
| 265 | } |
| 266 | } else { |
| 267 | // Push the full session, with tabs ordered by visual position. |
| 268 | CopySessionToJava(env, session, last_pushed_session); |
| 269 | } |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 275 | jboolean ForeignSessionHelper::OpenForeignSessionTab( |
| 276 | JNIEnv* env, |
| 277 | const JavaParamRef<jobject>& obj, |
| 278 | const JavaParamRef<jobject>& j_tab, |
| 279 | const JavaParamRef<jstring>& session_tag, |
| 280 | jint session_tab_id, |
| 281 | jint j_disposition) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 282 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 283 | if (!open_tabs) { |
| 284 | LOG(ERROR) << "Null OpenTabsUIDelegate returned."; |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | |
skuhne | b7409dcf | 2014-11-14 04:06:55 | [diff] [blame] | 288 | const sessions::SessionTab* session_tab; |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 289 | |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 290 | if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag), |
| 291 | session_tab_id, |
| 292 | &session_tab)) { |
[email protected] | 816939d | 2013-11-13 00:57:39 | [diff] [blame] | 293 | LOG(ERROR) << "Failed to load foreign tab."; |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | if (session_tab->navigations.empty()) { |
| 298 | LOG(ERROR) << "Foreign tab no longer has valid navigations."; |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab); |
| 303 | if (!tab_android) |
| 304 | return false; |
| 305 | content::WebContents* web_contents = tab_android->web_contents(); |
| 306 | if (!web_contents) |
| 307 | return false; |
| 308 | |
| 309 | WindowOpenDisposition disposition = |
| 310 | static_cast<WindowOpenDisposition>(j_disposition); |
| 311 | SessionRestore::RestoreForeignSessionTab(web_contents, |
| 312 | *session_tab, |
| 313 | disposition); |
| 314 | |
| 315 | return true; |
| 316 | } |
| 317 | |
torne | e621a27 | 2015-11-30 18:40:53 | [diff] [blame] | 318 | void ForeignSessionHelper::DeleteForeignSession( |
| 319 | JNIEnv* env, |
| 320 | const JavaParamRef<jobject>& obj, |
| 321 | const JavaParamRef<jstring>& session_tag) { |
[email protected] | a9f5662 | 2013-11-21 19:37:06 | [diff] [blame] | 322 | OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_); |
| 323 | if (open_tabs) |
| 324 | open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag)); |
[email protected] | 5365e52c | 2013-07-31 23:07:17 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // static |
| 328 | bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) { |
| 329 | return RegisterNativesImpl(env); |
| 330 | } |