blob: a2c628650d394f2b1c4de95005b91016048ccfaa [file] [log] [blame]
[email protected]5365e52c2013-07-31 23:07:171// 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>
avie4d7b6f2015-12-26 00:59:188#include <stddef.h>
[email protected]5365e52c2013-07-31 23:07:179
10#include "base/android/jni_string.h"
Sebastien Marchandf1349f52019-01-25 03:16:4111#include "base/bind.h"
[email protected]816939d2013-11-13 00:57:3912#include "chrome/browser/android/tab_android.h"
[email protected]5365e52c2013-07-31 23:07:1713#include "chrome/browser/chrome_notification_types.h"
[email protected]5365e52c2013-07-31 23:07:1714#include "chrome/browser/profiles/profile_android.h"
[email protected]816939d2013-11-13 00:57:3915#include "chrome/browser/sessions/session_restore.h"
[email protected]5365e52c2013-07-31 23:07:1716#include "chrome/browser/sync/profile_sync_service_factory.h"
Mikel Astiz0dd1e66b2018-11-12 10:08:5717#include "chrome/browser/sync/session_sync_service_factory.h"
[email protected]5365e52c2013-07-31 23:07:1718#include "chrome/browser/ui/android/tab_model/tab_model.h"
19#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
20#include "chrome/common/pref_names.h"
21#include "chrome/common/url_constants.h"
brettwb1fc1b82016-02-02 00:19:0822#include "components/prefs/pref_service.h"
23#include "components/prefs/scoped_user_pref_update.h"
Marc Treib32a98792019-02-10 19:26:4624#include "components/sync/driver/sync_service.h"
maxbogue8ef25082015-11-16 19:09:5825#include "components/sync_sessions/open_tabs_ui_delegate.h"
Mikel Astiz0dd1e66b2018-11-12 10:08:5726#include "components/sync_sessions/session_sync_service.h"
maxbogue5c8d0e22014-11-06 01:00:0127#include "content/public/browser/notification_details.h"
28#include "content/public/browser/notification_service.h"
[email protected]5365e52c2013-07-31 23:07:1729#include "content/public/browser/notification_source.h"
30#include "content/public/browser/web_contents.h"
31#include "jni/ForeignSessionHelper_jni.h"
32
torne86560112016-08-04 15:59:0433using base::android::JavaParamRef;
[email protected]5365e52c2013-07-31 23:07:1734using base::android::ScopedJavaGlobalRef;
35using base::android::ScopedJavaLocalRef;
36using base::android::AttachCurrentThread;
37using base::android::ConvertUTF16ToJavaString;
38using base::android::ConvertUTF8ToJavaString;
39using base::android::ConvertJavaStringToUTF8;
maxboguea79d99b72016-09-15 15:59:1640using sync_sessions::OpenTabsUIDelegate;
41using sync_sessions::SyncedSession;
[email protected]5365e52c2013-07-31 23:07:1742
43namespace {
44
[email protected]a9f56622013-11-21 19:37:0645OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) {
Mikel Astiz0dd1e66b2018-11-12 10:08:5746 sync_sessions::SessionSyncService* service =
47 SessionSyncServiceFactory::GetInstance()->GetForProfile(profile);
[email protected]5365e52c2013-07-31 23:07:1748
Mikel Astiz0dd1e66b2018-11-12 10:08:5749 // Only return the delegate if it exists.
50 if (!service)
[email protected]5365e52c2013-07-31 23:07:1751 return NULL;
52
[email protected]a9f56622013-11-21 19:37:0653 return service->GetOpenTabsUIDelegate();
[email protected]5365e52c2013-07-31 23:07:1754}
55
skuhneb7409dcf2014-11-14 04:06:5556bool ShouldSkipTab(const sessions::SessionTab& session_tab) {
[email protected]48bf1572014-08-12 01:31:0957 if (session_tab.navigations.empty())
58 return true;
59
maxbogue4157d2b2014-08-27 21:56:4860 int selected_index = session_tab.normalized_navigation_index();
zeae8f0c131d2015-08-04 23:20:2761 const sessions::SerializedNavigationEntry& current_navigation =
[email protected]48bf1572014-08-12 01:31:0962 session_tab.navigations.at(selected_index);
63
64 if (current_navigation.virtual_url().is_empty())
65 return true;
66
67 return false;
68}
69
skuhneb7409dcf2014-11-14 04:06:5570bool ShouldSkipWindow(const sessions::SessionWindow& window) {
avi498cabca2016-09-21 19:03:5071 for (const auto& tab_ptr : window.tabs) {
72 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]48bf1572014-08-12 01:31:0973 if (!ShouldSkipTab(session_tab))
74 return false;
75 }
76 return true;
77}
78
maxboguea79d99b72016-09-15 15:59:1679bool ShouldSkipSession(const SyncedSession& session) {
avi498cabca2016-09-21 19:03:5080 for (const auto& window_pair : session.windows) {
zea325508e2017-03-29 20:11:2281 const sessions::SessionWindow& window = window_pair.second->wrapped_window;
[email protected]48bf1572014-08-12 01:31:0982 if (!ShouldSkipWindow(window))
83 return false;
84 }
85 return true;
86}
87
Daniel Bratell7aacf952017-11-21 17:51:2588void JNI_ForeignSessionHelper_CopyTabToJava(
zeae8f0c131d2015-08-04 23:20:2789 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(
torne948f3662016-08-16 15:10:44102 env, j_window, ConvertUTF8ToJavaString(env, tab_url.spec()),
103 ConvertUTF16ToJavaString(env, current_navigation.title()),
104 tab.timestamp.ToJavaTime(), tab.tab_id.id());
zeae8f0c131d2015-08-04 23:20:27105}
106
Daniel Bratell7aacf952017-11-21 17:51:25107void JNI_ForeignSessionHelper_CopyWindowToJava(
[email protected]5365e52c2013-07-31 23:07:17108 JNIEnv* env,
skuhneb7409dcf2014-11-14 04:06:55109 const sessions::SessionWindow& window,
[email protected]5365e52c2013-07-31 23:07:17110 ScopedJavaLocalRef<jobject>& j_window) {
avi498cabca2016-09-21 19:03:50111 for (const auto& tab_ptr : window.tabs) {
112 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]5365e52c2013-07-31 23:07:17113
[email protected]48bf1572014-08-12 01:31:09114 if (ShouldSkipTab(session_tab))
zeae8f0c131d2015-08-04 23:20:27115 return;
[email protected]48bf1572014-08-12 01:31:09116
Daniel Bratell7aacf952017-11-21 17:51:25117 JNI_ForeignSessionHelper_CopyTabToJava(env, session_tab, j_window);
[email protected]5365e52c2013-07-31 23:07:17118 }
119}
120
Daniel Bratell7aacf952017-11-21 17:51:25121void JNI_ForeignSessionHelper_CopySessionToJava(
[email protected]5365e52c2013-07-31 23:07:17122 JNIEnv* env,
[email protected]9744a15e2013-09-23 20:59:08123 const SyncedSession& session,
[email protected]5365e52c2013-07-31 23:07:17124 ScopedJavaLocalRef<jobject>& j_session) {
avi498cabca2016-09-21 19:03:50125 for (const auto& window_pair : session.windows) {
zea325508e2017-03-29 20:11:22126 const sessions::SessionWindow& window = window_pair.second->wrapped_window;
[email protected]9744a15e2013-09-23 20:59:08127
[email protected]48bf1572014-08-12 01:31:09128 if (ShouldSkipWindow(window))
129 continue;
130
[email protected]5365e52c2013-07-31 23:07:17131 ScopedJavaLocalRef<jobject> last_pushed_window;
torne948f3662016-08-16 15:10:44132 last_pushed_window.Reset(Java_ForeignSessionHelper_pushWindow(
133 env, j_session, window.timestamp.ToJavaTime(), window.window_id.id()));
[email protected]5365e52c2013-07-31 23:07:17134
Daniel Bratell7aacf952017-11-21 17:51:25135 JNI_ForeignSessionHelper_CopyWindowToJava(env, window, last_pushed_window);
[email protected]5365e52c2013-07-31 23:07:17136 }
137}
138
139} // namespace
140
Daniel Bratell7aacf952017-11-21 17:51:25141static jlong JNI_ForeignSessionHelper_Init(
142 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25143 const JavaParamRef<jobject>& profile) {
[email protected]5365e52c2013-07-31 23:07:17144 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper(
145 ProfileAndroid::FromProfileAndroid(profile));
[email protected]d557b6042013-11-21 16:34:31146 return reinterpret_cast<intptr_t>(foreign_session_helper);
[email protected]5365e52c2013-07-31 23:07:17147}
148
149ForeignSessionHelper::ForeignSessionHelper(Profile* profile)
Mikel Astiz0dd1e66b2018-11-12 10:08:57150 : profile_(profile) {
151 sync_sessions::SessionSyncService* service =
152 SessionSyncServiceFactory::GetInstance()->GetForProfile(profile);
blundellbbdc8a82015-10-28 15:47:20153
Mikel Astiz0dd1e66b2018-11-12 10:08:57154 // 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]5365e52c2013-07-31 23:07:17163}
164
165ForeignSessionHelper::~ForeignSessionHelper() {
166}
167
tornee621a272015-11-30 18:40:53168void ForeignSessionHelper::Destroy(JNIEnv* env,
169 const JavaParamRef<jobject>& obj) {
[email protected]5365e52c2013-07-31 23:07:17170 delete this;
171}
172
tornee621a272015-11-30 18:40:53173jboolean ForeignSessionHelper::IsTabSyncEnabled(
174 JNIEnv* env,
175 const JavaParamRef<jobject>& obj) {
Mikel Astiz0dd1e66b2018-11-12 10:08:57176 sync_sessions::SessionSyncService* service =
177 SessionSyncServiceFactory::GetInstance()->GetForProfile(profile_);
178 return service && service->GetOpenTabsUIDelegate();
[email protected]5365e52c2013-07-31 23:07:17179}
180
tornee621a272015-11-30 18:40:53181void ForeignSessionHelper::TriggerSessionSync(
182 JNIEnv* env,
183 const JavaParamRef<jobject>& obj) {
Marc Treib32a98792019-02-10 19:26:46184 syncer::SyncService* service =
Marc Treib205e9862019-02-27 21:43:00185 ProfileSyncServiceFactory::GetForProfile(profile_);
blundell0631ab032015-10-29 09:28:02186 if (!service)
187 return;
188
Marc Treib32a98792019-02-10 19:26:46189 service->TriggerRefresh({syncer::SESSIONS});
maxbogue5c8d0e22014-11-06 01:00:01190}
191
tornee621a272015-11-30 18:40:53192void ForeignSessionHelper::SetOnForeignSessionCallback(
193 JNIEnv* env,
194 const JavaParamRef<jobject>& obj,
195 const JavaParamRef<jobject>& callback) {
[email protected]5365e52c2013-07-31 23:07:17196 callback_.Reset(env, callback);
197}
198
blundellbbdc8a82015-10-28 15:47:20199void ForeignSessionHelper::FireForeignSessionCallback() {
[email protected]5365e52c2013-07-31 23:07:17200 if (callback_.is_null())
201 return;
202
203 JNIEnv* env = AttachCurrentThread();
torne948f3662016-08-16 15:10:44204 Java_ForeignSessionCallback_onUpdated(env, callback_);
blundellbbdc8a82015-10-28 15:47:20205}
[email protected]5365e52c2013-07-31 23:07:17206
tornee621a272015-11-30 18:40:53207jboolean ForeignSessionHelper::GetForeignSessions(
208 JNIEnv* env,
209 const JavaParamRef<jobject>& obj,
210 const JavaParamRef<jobject>& result) {
[email protected]a9f56622013-11-21 19:37:06211 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
212 if (!open_tabs)
[email protected]5365e52c2013-07-31 23:07:17213 return false;
214
maxboguea79d99b72016-09-15 15:59:16215 std::vector<const SyncedSession*> sessions;
[email protected]a9f56622013-11-21 19:37:06216 if (!open_tabs->GetAllForeignSessions(&sessions))
[email protected]5365e52c2013-07-31 23:07:17217 return false;
218
219 // Use a pref to keep track of sessions that were collapsed by the user.
220 // To prevent the pref from accumulating stale sessions, clear it each time
221 // and only add back sessions that are still current.
222 DictionaryPrefUpdate pref_update(profile_->GetPrefs(),
223 prefs::kNtpCollapsedForeignSessions);
[email protected]5bcdd99d2013-12-23 18:28:30224 base::DictionaryValue* pref_collapsed_sessions = pref_update.Get();
dchengaeceb05e2016-04-07 23:41:10225 std::unique_ptr<base::DictionaryValue> collapsed_sessions(
[email protected]5365e52c2013-07-31 23:07:17226 pref_collapsed_sessions->DeepCopy());
227 pref_collapsed_sessions->Clear();
228
229 ScopedJavaLocalRef<jobject> last_pushed_session;
[email protected]5365e52c2013-07-31 23:07:17230
231 // Note: we don't own the SyncedSessions themselves.
232 for (size_t i = 0; i < sessions.size(); ++i) {
maxboguea79d99b72016-09-15 15:59:16233 const SyncedSession& session = *(sessions[i]);
[email protected]48bf1572014-08-12 01:31:09234 if (ShouldSkipSession(session))
235 continue;
[email protected]5365e52c2013-07-31 23:07:17236
[email protected]9744a15e2013-09-23 20:59:08237 const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag);
[email protected]5365e52c2013-07-31 23:07:17238
239 if (is_collapsed)
[email protected]9744a15e2013-09-23 20:59:08240 pref_collapsed_sessions->SetBoolean(session.session_tag, true);
[email protected]5365e52c2013-07-31 23:07:17241
torne948f3662016-08-16 15:10:44242 last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession(
243 env, result, ConvertUTF8ToJavaString(env, session.session_tag),
244 ConvertUTF8ToJavaString(env, session.session_name), session.device_type,
245 session.modified_time.ToJavaTime()));
[email protected]5365e52c2013-07-31 23:07:17246
Mikel Astizb1513e62018-04-05 16:38:13247 // Push the full session, with tabs ordered by visual position.
248 JNI_ForeignSessionHelper_CopySessionToJava(env, session,
249 last_pushed_session);
[email protected]5365e52c2013-07-31 23:07:17250 }
251
252 return true;
253}
254
tornee621a272015-11-30 18:40:53255jboolean ForeignSessionHelper::OpenForeignSessionTab(
256 JNIEnv* env,
257 const JavaParamRef<jobject>& obj,
258 const JavaParamRef<jobject>& j_tab,
259 const JavaParamRef<jstring>& session_tag,
260 jint session_tab_id,
261 jint j_disposition) {
[email protected]a9f56622013-11-21 19:37:06262 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
263 if (!open_tabs) {
264 LOG(ERROR) << "Null OpenTabsUIDelegate returned.";
[email protected]816939d2013-11-13 00:57:39265 return false;
266 }
267
skuhneb7409dcf2014-11-14 04:06:55268 const sessions::SessionTab* session_tab;
[email protected]816939d2013-11-13 00:57:39269
[email protected]a9f56622013-11-21 19:37:06270 if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag),
Mikel Astize023c572018-03-28 07:56:56271 SessionID::FromSerializedValue(session_tab_id),
[email protected]a9f56622013-11-21 19:37:06272 &session_tab)) {
[email protected]816939d2013-11-13 00:57:39273 LOG(ERROR) << "Failed to load foreign tab.";
274 return false;
275 }
276
277 if (session_tab->navigations.empty()) {
278 LOG(ERROR) << "Foreign tab no longer has valid navigations.";
279 return false;
280 }
281
282 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab);
283 if (!tab_android)
284 return false;
285 content::WebContents* web_contents = tab_android->web_contents();
286 if (!web_contents)
287 return false;
288
289 WindowOpenDisposition disposition =
290 static_cast<WindowOpenDisposition>(j_disposition);
291 SessionRestore::RestoreForeignSessionTab(web_contents,
292 *session_tab,
293 disposition);
294
295 return true;
296}
297
tornee621a272015-11-30 18:40:53298void ForeignSessionHelper::DeleteForeignSession(
299 JNIEnv* env,
300 const JavaParamRef<jobject>& obj,
301 const JavaParamRef<jstring>& session_tag) {
[email protected]a9f56622013-11-21 19:37:06302 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
303 if (open_tabs)
304 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag));
[email protected]5365e52c2013-07-31 23:07:17305}
Tanja Gornak9086f1a2018-12-05 08:23:51306
307void ForeignSessionHelper::SetInvalidationsForSessionsEnabled(
308 JNIEnv* env,
309 const JavaParamRef<jobject>& obj,
310 jboolean enabled) {
Marc Treib32a98792019-02-10 19:26:46311 syncer::SyncService* service =
Marc Treib205e9862019-02-27 21:43:00312 ProfileSyncServiceFactory::GetForProfile(profile_);
Tanja Gornak9086f1a2018-12-05 08:23:51313 if (!service)
314 return;
315
316 service->SetInvalidationsForSessionsEnabled(enabled);
317}