blob: 586a090b3c79c9aa0dca7c69507e37b023b7584b [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"
[email protected]816939d2013-11-13 00:57:3911#include "chrome/browser/android/tab_android.h"
[email protected]5365e52c2013-07-31 23:07:1712#include "chrome/browser/chrome_notification_types.h"
[email protected]5365e52c2013-07-31 23:07:1713#include "chrome/browser/profiles/profile_android.h"
[email protected]816939d2013-11-13 00:57:3914#include "chrome/browser/sessions/session_restore.h"
[email protected]5365e52c2013-07-31 23:07:1715#include "chrome/browser/sync/profile_sync_service_factory.h"
Mikel Astiz0dd1e66b2018-11-12 10:08:5716#include "chrome/browser/sync/session_sync_service_factory.h"
[email protected]5365e52c2013-07-31 23:07:1717#include "chrome/browser/ui/android/tab_model/tab_model.h"
18#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
19#include "chrome/common/pref_names.h"
20#include "chrome/common/url_constants.h"
maxbogue26f40222016-09-16 20:22:1821#include "components/browser_sync/profile_sync_service.h"
brettwb1fc1b82016-02-02 00:19:0822#include "components/prefs/pref_service.h"
23#include "components/prefs/scoped_user_pref_update.h"
maxbogue8ef25082015-11-16 19:09:5824#include "components/sync_sessions/open_tabs_ui_delegate.h"
Mikel Astiz0dd1e66b2018-11-12 10:08:5725#include "components/sync_sessions/session_sync_service.h"
maxbogue5c8d0e22014-11-06 01:00:0126#include "content/public/browser/notification_details.h"
27#include "content/public/browser/notification_service.h"
[email protected]5365e52c2013-07-31 23:07:1728#include "content/public/browser/notification_source.h"
29#include "content/public/browser/web_contents.h"
30#include "jni/ForeignSessionHelper_jni.h"
31
torne86560112016-08-04 15:59:0432using base::android::JavaParamRef;
[email protected]5365e52c2013-07-31 23:07:1733using base::android::ScopedJavaGlobalRef;
34using base::android::ScopedJavaLocalRef;
35using base::android::AttachCurrentThread;
36using base::android::ConvertUTF16ToJavaString;
37using base::android::ConvertUTF8ToJavaString;
38using base::android::ConvertJavaStringToUTF8;
maxboguea79d99b72016-09-15 15:59:1639using sync_sessions::OpenTabsUIDelegate;
40using sync_sessions::SyncedSession;
[email protected]5365e52c2013-07-31 23:07:1741
42namespace {
43
[email protected]a9f56622013-11-21 19:37:0644OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) {
Mikel Astiz0dd1e66b2018-11-12 10:08:5745 sync_sessions::SessionSyncService* service =
46 SessionSyncServiceFactory::GetInstance()->GetForProfile(profile);
[email protected]5365e52c2013-07-31 23:07:1747
Mikel Astiz0dd1e66b2018-11-12 10:08:5748 // Only return the delegate if it exists.
49 if (!service)
[email protected]5365e52c2013-07-31 23:07:1750 return NULL;
51
[email protected]a9f56622013-11-21 19:37:0652 return service->GetOpenTabsUIDelegate();
[email protected]5365e52c2013-07-31 23:07:1753}
54
skuhneb7409dcf2014-11-14 04:06:5555bool ShouldSkipTab(const sessions::SessionTab& session_tab) {
[email protected]48bf1572014-08-12 01:31:0956 if (session_tab.navigations.empty())
57 return true;
58
maxbogue4157d2b2014-08-27 21:56:4859 int selected_index = session_tab.normalized_navigation_index();
zeae8f0c131d2015-08-04 23:20:2760 const sessions::SerializedNavigationEntry& current_navigation =
[email protected]48bf1572014-08-12 01:31:0961 session_tab.navigations.at(selected_index);
62
63 if (current_navigation.virtual_url().is_empty())
64 return true;
65
66 return false;
67}
68
skuhneb7409dcf2014-11-14 04:06:5569bool ShouldSkipWindow(const sessions::SessionWindow& window) {
avi498cabca2016-09-21 19:03:5070 for (const auto& tab_ptr : window.tabs) {
71 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]48bf1572014-08-12 01:31:0972 if (!ShouldSkipTab(session_tab))
73 return false;
74 }
75 return true;
76}
77
maxboguea79d99b72016-09-15 15:59:1678bool ShouldSkipSession(const SyncedSession& session) {
avi498cabca2016-09-21 19:03:5079 for (const auto& window_pair : session.windows) {
zea325508e2017-03-29 20:11:2280 const sessions::SessionWindow& window = window_pair.second->wrapped_window;
[email protected]48bf1572014-08-12 01:31:0981 if (!ShouldSkipWindow(window))
82 return false;
83 }
84 return true;
85}
86
Daniel Bratell7aacf952017-11-21 17:51:2587void JNI_ForeignSessionHelper_CopyTabToJava(
zeae8f0c131d2015-08-04 23:20:2788 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(
torne948f3662016-08-16 15:10:44101 env, j_window, ConvertUTF8ToJavaString(env, tab_url.spec()),
102 ConvertUTF16ToJavaString(env, current_navigation.title()),
103 tab.timestamp.ToJavaTime(), tab.tab_id.id());
zeae8f0c131d2015-08-04 23:20:27104}
105
Daniel Bratell7aacf952017-11-21 17:51:25106void JNI_ForeignSessionHelper_CopyWindowToJava(
[email protected]5365e52c2013-07-31 23:07:17107 JNIEnv* env,
skuhneb7409dcf2014-11-14 04:06:55108 const sessions::SessionWindow& window,
[email protected]5365e52c2013-07-31 23:07:17109 ScopedJavaLocalRef<jobject>& j_window) {
avi498cabca2016-09-21 19:03:50110 for (const auto& tab_ptr : window.tabs) {
111 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]5365e52c2013-07-31 23:07:17112
[email protected]48bf1572014-08-12 01:31:09113 if (ShouldSkipTab(session_tab))
zeae8f0c131d2015-08-04 23:20:27114 return;
[email protected]48bf1572014-08-12 01:31:09115
Daniel Bratell7aacf952017-11-21 17:51:25116 JNI_ForeignSessionHelper_CopyTabToJava(env, session_tab, j_window);
[email protected]5365e52c2013-07-31 23:07:17117 }
118}
119
Daniel Bratell7aacf952017-11-21 17:51:25120void JNI_ForeignSessionHelper_CopySessionToJava(
[email protected]5365e52c2013-07-31 23:07:17121 JNIEnv* env,
[email protected]9744a15e2013-09-23 20:59:08122 const SyncedSession& session,
[email protected]5365e52c2013-07-31 23:07:17123 ScopedJavaLocalRef<jobject>& j_session) {
avi498cabca2016-09-21 19:03:50124 for (const auto& window_pair : session.windows) {
zea325508e2017-03-29 20:11:22125 const sessions::SessionWindow& window = window_pair.second->wrapped_window;
[email protected]9744a15e2013-09-23 20:59:08126
[email protected]48bf1572014-08-12 01:31:09127 if (ShouldSkipWindow(window))
128 continue;
129
[email protected]5365e52c2013-07-31 23:07:17130 ScopedJavaLocalRef<jobject> last_pushed_window;
torne948f3662016-08-16 15:10:44131 last_pushed_window.Reset(Java_ForeignSessionHelper_pushWindow(
132 env, j_session, window.timestamp.ToJavaTime(), window.window_id.id()));
[email protected]5365e52c2013-07-31 23:07:17133
Daniel Bratell7aacf952017-11-21 17:51:25134 JNI_ForeignSessionHelper_CopyWindowToJava(env, window, last_pushed_window);
[email protected]5365e52c2013-07-31 23:07:17135 }
136}
137
138} // namespace
139
Daniel Bratell7aacf952017-11-21 17:51:25140static jlong JNI_ForeignSessionHelper_Init(
141 JNIEnv* env,
142 const JavaParamRef<jclass>& clazz,
143 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) {
maxbogue0a379452016-09-22 21:35:05184 browser_sync::ProfileSyncService* service =
185 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
blundell0631ab032015-10-29 09:28:02186 if (!service)
187 return;
188
maxbogue5c8d0e22014-11-06 01:00:01189 const syncer::ModelTypeSet types(syncer::SESSIONS);
blundell0631ab032015-10-29 09:28:02190 service->TriggerRefresh(types);
maxbogue5c8d0e22014-11-06 01:00:01191}
192
tornee621a272015-11-30 18:40:53193void ForeignSessionHelper::SetOnForeignSessionCallback(
194 JNIEnv* env,
195 const JavaParamRef<jobject>& obj,
196 const JavaParamRef<jobject>& callback) {
[email protected]5365e52c2013-07-31 23:07:17197 callback_.Reset(env, callback);
198}
199
blundellbbdc8a82015-10-28 15:47:20200void ForeignSessionHelper::FireForeignSessionCallback() {
[email protected]5365e52c2013-07-31 23:07:17201 if (callback_.is_null())
202 return;
203
204 JNIEnv* env = AttachCurrentThread();
torne948f3662016-08-16 15:10:44205 Java_ForeignSessionCallback_onUpdated(env, callback_);
blundellbbdc8a82015-10-28 15:47:20206}
[email protected]5365e52c2013-07-31 23:07:17207
tornee621a272015-11-30 18:40:53208jboolean ForeignSessionHelper::GetForeignSessions(
209 JNIEnv* env,
210 const JavaParamRef<jobject>& obj,
211 const JavaParamRef<jobject>& result) {
[email protected]a9f56622013-11-21 19:37:06212 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
213 if (!open_tabs)
[email protected]5365e52c2013-07-31 23:07:17214 return false;
215
maxboguea79d99b72016-09-15 15:59:16216 std::vector<const SyncedSession*> sessions;
[email protected]a9f56622013-11-21 19:37:06217 if (!open_tabs->GetAllForeignSessions(&sessions))
[email protected]5365e52c2013-07-31 23:07:17218 return false;
219
220 // Use a pref to keep track of sessions that were collapsed by the user.
221 // To prevent the pref from accumulating stale sessions, clear it each time
222 // and only add back sessions that are still current.
223 DictionaryPrefUpdate pref_update(profile_->GetPrefs(),
224 prefs::kNtpCollapsedForeignSessions);
[email protected]5bcdd99d2013-12-23 18:28:30225 base::DictionaryValue* pref_collapsed_sessions = pref_update.Get();
dchengaeceb05e2016-04-07 23:41:10226 std::unique_ptr<base::DictionaryValue> collapsed_sessions(
[email protected]5365e52c2013-07-31 23:07:17227 pref_collapsed_sessions->DeepCopy());
228 pref_collapsed_sessions->Clear();
229
230 ScopedJavaLocalRef<jobject> last_pushed_session;
[email protected]5365e52c2013-07-31 23:07:17231
232 // Note: we don't own the SyncedSessions themselves.
233 for (size_t i = 0; i < sessions.size(); ++i) {
maxboguea79d99b72016-09-15 15:59:16234 const SyncedSession& session = *(sessions[i]);
[email protected]48bf1572014-08-12 01:31:09235 if (ShouldSkipSession(session))
236 continue;
[email protected]5365e52c2013-07-31 23:07:17237
[email protected]9744a15e2013-09-23 20:59:08238 const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag);
[email protected]5365e52c2013-07-31 23:07:17239
240 if (is_collapsed)
[email protected]9744a15e2013-09-23 20:59:08241 pref_collapsed_sessions->SetBoolean(session.session_tag, true);
[email protected]5365e52c2013-07-31 23:07:17242
torne948f3662016-08-16 15:10:44243 last_pushed_session.Reset(Java_ForeignSessionHelper_pushSession(
244 env, result, ConvertUTF8ToJavaString(env, session.session_tag),
245 ConvertUTF8ToJavaString(env, session.session_name), session.device_type,
246 session.modified_time.ToJavaTime()));
[email protected]5365e52c2013-07-31 23:07:17247
Mikel Astizb1513e62018-04-05 16:38:13248 // Push the full session, with tabs ordered by visual position.
249 JNI_ForeignSessionHelper_CopySessionToJava(env, session,
250 last_pushed_session);
[email protected]5365e52c2013-07-31 23:07:17251 }
252
253 return true;
254}
255
tornee621a272015-11-30 18:40:53256jboolean ForeignSessionHelper::OpenForeignSessionTab(
257 JNIEnv* env,
258 const JavaParamRef<jobject>& obj,
259 const JavaParamRef<jobject>& j_tab,
260 const JavaParamRef<jstring>& session_tag,
261 jint session_tab_id,
262 jint j_disposition) {
[email protected]a9f56622013-11-21 19:37:06263 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
264 if (!open_tabs) {
265 LOG(ERROR) << "Null OpenTabsUIDelegate returned.";
[email protected]816939d2013-11-13 00:57:39266 return false;
267 }
268
skuhneb7409dcf2014-11-14 04:06:55269 const sessions::SessionTab* session_tab;
[email protected]816939d2013-11-13 00:57:39270
[email protected]a9f56622013-11-21 19:37:06271 if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag),
Mikel Astize023c572018-03-28 07:56:56272 SessionID::FromSerializedValue(session_tab_id),
[email protected]a9f56622013-11-21 19:37:06273 &session_tab)) {
[email protected]816939d2013-11-13 00:57:39274 LOG(ERROR) << "Failed to load foreign tab.";
275 return false;
276 }
277
278 if (session_tab->navigations.empty()) {
279 LOG(ERROR) << "Foreign tab no longer has valid navigations.";
280 return false;
281 }
282
283 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab);
284 if (!tab_android)
285 return false;
286 content::WebContents* web_contents = tab_android->web_contents();
287 if (!web_contents)
288 return false;
289
290 WindowOpenDisposition disposition =
291 static_cast<WindowOpenDisposition>(j_disposition);
292 SessionRestore::RestoreForeignSessionTab(web_contents,
293 *session_tab,
294 disposition);
295
296 return true;
297}
298
tornee621a272015-11-30 18:40:53299void ForeignSessionHelper::DeleteForeignSession(
300 JNIEnv* env,
301 const JavaParamRef<jobject>& obj,
302 const JavaParamRef<jstring>& session_tag) {
[email protected]a9f56622013-11-21 19:37:06303 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
304 if (open_tabs)
305 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag));
[email protected]5365e52c2013-07-31 23:07:17306}