blob: 17006b71dad6c0b7854972b5c587a1d29a19f936 [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"
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"
maxbogue26f40222016-09-16 20:22:1820#include "components/browser_sync/profile_sync_service.h"
brettwb1fc1b82016-02-02 00:19:0821#include "components/prefs/pref_service.h"
22#include "components/prefs/scoped_user_pref_update.h"
maxbogue8ef25082015-11-16 19:09:5823#include "components/sync_sessions/open_tabs_ui_delegate.h"
maxbogue5c8d0e22014-11-06 01:00:0124#include "content/public/browser/notification_details.h"
25#include "content/public/browser/notification_service.h"
[email protected]5365e52c2013-07-31 23:07:1726#include "content/public/browser/notification_source.h"
27#include "content/public/browser/web_contents.h"
28#include "jni/ForeignSessionHelper_jni.h"
29
torne86560112016-08-04 15:59:0430using base::android::JavaParamRef;
[email protected]5365e52c2013-07-31 23:07:1731using base::android::ScopedJavaGlobalRef;
32using base::android::ScopedJavaLocalRef;
33using base::android::AttachCurrentThread;
34using base::android::ConvertUTF16ToJavaString;
35using base::android::ConvertUTF8ToJavaString;
36using base::android::ConvertJavaStringToUTF8;
maxboguea79d99b72016-09-15 15:59:1637using sync_sessions::OpenTabsUIDelegate;
38using sync_sessions::SyncedSession;
[email protected]5365e52c2013-07-31 23:07:1739
40namespace {
41
[email protected]a9f56622013-11-21 19:37:0642OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) {
maxbogue0a379452016-09-22 21:35:0543 browser_sync::ProfileSyncService* service =
44 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
[email protected]5365e52c2013-07-31 23:07:1745
[email protected]a9f56622013-11-21 19:37:0646 // Only return the delegate if it exists and it is done syncing sessions.
maxbogue192b3602015-06-04 00:33:5547 if (!service || !service->IsSyncActive())
[email protected]5365e52c2013-07-31 23:07:1748 return NULL;
49
[email protected]a9f56622013-11-21 19:37:0650 return service->GetOpenTabsUIDelegate();
[email protected]5365e52c2013-07-31 23:07:1751}
52
skuhneb7409dcf2014-11-14 04:06:5553bool ShouldSkipTab(const sessions::SessionTab& session_tab) {
[email protected]48bf1572014-08-12 01:31:0954 if (session_tab.navigations.empty())
55 return true;
56
maxbogue4157d2b2014-08-27 21:56:4857 int selected_index = session_tab.normalized_navigation_index();
zeae8f0c131d2015-08-04 23:20:2758 const sessions::SerializedNavigationEntry& current_navigation =
[email protected]48bf1572014-08-12 01:31:0959 session_tab.navigations.at(selected_index);
60
61 if (current_navigation.virtual_url().is_empty())
62 return true;
63
64 return false;
65}
66
skuhneb7409dcf2014-11-14 04:06:5567bool ShouldSkipWindow(const sessions::SessionWindow& window) {
avi498cabca2016-09-21 19:03:5068 for (const auto& tab_ptr : window.tabs) {
69 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]48bf1572014-08-12 01:31:0970 if (!ShouldSkipTab(session_tab))
71 return false;
72 }
73 return true;
74}
75
maxboguea79d99b72016-09-15 15:59:1676bool ShouldSkipSession(const SyncedSession& session) {
avi498cabca2016-09-21 19:03:5077 for (const auto& window_pair : session.windows) {
78 const sessions::SessionWindow& window = *(window_pair.second.get());
[email protected]48bf1572014-08-12 01:31:0979 if (!ShouldSkipWindow(window))
80 return false;
81 }
82 return true;
83}
84
zeae8f0c131d2015-08-04 23:20:2785void CopyTabToJava(
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(
torne948f3662016-08-16 15:10:4499 env, j_window, ConvertUTF8ToJavaString(env, tab_url.spec()),
100 ConvertUTF16ToJavaString(env, current_navigation.title()),
101 tab.timestamp.ToJavaTime(), tab.tab_id.id());
zeae8f0c131d2015-08-04 23:20:27102}
103
104void CopyWindowToJava(
[email protected]5365e52c2013-07-31 23:07:17105 JNIEnv* env,
skuhneb7409dcf2014-11-14 04:06:55106 const sessions::SessionWindow& window,
[email protected]5365e52c2013-07-31 23:07:17107 ScopedJavaLocalRef<jobject>& j_window) {
avi498cabca2016-09-21 19:03:50108 for (const auto& tab_ptr : window.tabs) {
109 const sessions::SessionTab& session_tab = *(tab_ptr.get());
[email protected]5365e52c2013-07-31 23:07:17110
[email protected]48bf1572014-08-12 01:31:09111 if (ShouldSkipTab(session_tab))
zeae8f0c131d2015-08-04 23:20:27112 return;
[email protected]48bf1572014-08-12 01:31:09113
zeae8f0c131d2015-08-04 23:20:27114 CopyTabToJava(env, session_tab, j_window);
[email protected]5365e52c2013-07-31 23:07:17115 }
116}
117
zeae8f0c131d2015-08-04 23:20:27118void CopySessionToJava(
[email protected]5365e52c2013-07-31 23:07:17119 JNIEnv* env,
[email protected]9744a15e2013-09-23 20:59:08120 const SyncedSession& session,
[email protected]5365e52c2013-07-31 23:07:17121 ScopedJavaLocalRef<jobject>& j_session) {
avi498cabca2016-09-21 19:03:50122 for (const auto& window_pair : session.windows) {
123 const sessions::SessionWindow& window = *(window_pair.second.get());
[email protected]9744a15e2013-09-23 20:59:08124
[email protected]48bf1572014-08-12 01:31:09125 if (ShouldSkipWindow(window))
126 continue;
127
[email protected]5365e52c2013-07-31 23:07:17128 ScopedJavaLocalRef<jobject> last_pushed_window;
torne948f3662016-08-16 15:10:44129 last_pushed_window.Reset(Java_ForeignSessionHelper_pushWindow(
130 env, j_session, window.timestamp.ToJavaTime(), window.window_id.id()));
[email protected]5365e52c2013-07-31 23:07:17131
zeae8f0c131d2015-08-04 23:20:27132 CopyWindowToJava(env, window, last_pushed_window);
[email protected]5365e52c2013-07-31 23:07:17133 }
134}
135
136} // namespace
137
torne89cc5d92015-09-04 11:16:35138static jlong Init(JNIEnv* env,
139 const JavaParamRef<jclass>& clazz,
140 const JavaParamRef<jobject>& profile) {
[email protected]5365e52c2013-07-31 23:07:17141 ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper(
142 ProfileAndroid::FromProfileAndroid(profile));
[email protected]d557b6042013-11-21 16:34:31143 return reinterpret_cast<intptr_t>(foreign_session_helper);
[email protected]5365e52c2013-07-31 23:07:17144}
145
146ForeignSessionHelper::ForeignSessionHelper(Profile* profile)
blundellbbdc8a82015-10-28 15:47:20147 : profile_(profile), scoped_observer_(this) {
maxbogue0a379452016-09-22 21:35:05148 browser_sync::ProfileSyncService* service =
149 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
blundellbbdc8a82015-10-28 15:47:20150
151 // NOTE: The ProfileSyncService can be null in tests.
152 if (service)
153 scoped_observer_.Add(service);
[email protected]5365e52c2013-07-31 23:07:17154}
155
156ForeignSessionHelper::~ForeignSessionHelper() {
157}
158
tornee621a272015-11-30 18:40:53159void ForeignSessionHelper::Destroy(JNIEnv* env,
160 const JavaParamRef<jobject>& obj) {
[email protected]5365e52c2013-07-31 23:07:17161 delete this;
162}
163
tornee621a272015-11-30 18:40:53164jboolean ForeignSessionHelper::IsTabSyncEnabled(
165 JNIEnv* env,
166 const JavaParamRef<jobject>& obj) {
maxbogue0a379452016-09-22 21:35:05167 browser_sync::ProfileSyncService* service =
168 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
[email protected]5365e52c2013-07-31 23:07:17169 return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS);
170}
171
tornee621a272015-11-30 18:40:53172void ForeignSessionHelper::TriggerSessionSync(
173 JNIEnv* env,
174 const JavaParamRef<jobject>& obj) {
maxbogue0a379452016-09-22 21:35:05175 browser_sync::ProfileSyncService* service =
176 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
blundell0631ab032015-10-29 09:28:02177 if (!service)
178 return;
179
maxbogue5c8d0e22014-11-06 01:00:01180 const syncer::ModelTypeSet types(syncer::SESSIONS);
blundell0631ab032015-10-29 09:28:02181 service->TriggerRefresh(types);
maxbogue5c8d0e22014-11-06 01:00:01182}
183
tornee621a272015-11-30 18:40:53184void ForeignSessionHelper::SetOnForeignSessionCallback(
185 JNIEnv* env,
186 const JavaParamRef<jobject>& obj,
187 const JavaParamRef<jobject>& callback) {
[email protected]5365e52c2013-07-31 23:07:17188 callback_.Reset(env, callback);
189}
190
blundellbbdc8a82015-10-28 15:47:20191void ForeignSessionHelper::FireForeignSessionCallback() {
[email protected]5365e52c2013-07-31 23:07:17192 if (callback_.is_null())
193 return;
194
195 JNIEnv* env = AttachCurrentThread();
torne948f3662016-08-16 15:10:44196 Java_ForeignSessionCallback_onUpdated(env, callback_);
blundellbbdc8a82015-10-28 15:47:20197}
[email protected]5365e52c2013-07-31 23:07:17198
holte4ef35702017-02-03 03:30:10199void ForeignSessionHelper::OnSyncConfigurationCompleted(
200 syncer::SyncService* sync) {
blundellfc39f9aa2015-10-30 08:25:54201 FireForeignSessionCallback();
[email protected]5365e52c2013-07-31 23:07:17202}
203
holte4ef35702017-02-03 03:30:10204void ForeignSessionHelper::OnForeignSessionUpdated(syncer::SyncService* sync) {
blundellbbdc8a82015-10-28 15:47:20205 FireForeignSessionCallback();
206}
207
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
zeae8f0c131d2015-08-04 23:20:27248 const std::string group_name =
249 base::FieldTrialList::FindFullName("TabSyncByRecency");
250 if (group_name == "Enabled") {
251 // Create a custom window with tabs from all windows included and ordered
252 // by recency (GetForeignSessionTabs will do ordering automatically).
253 std::vector<const sessions::SessionTab*> tabs;
254 open_tabs->GetForeignSessionTabs(session.session_tag, &tabs);
255 ScopedJavaLocalRef<jobject> last_pushed_window(
256 Java_ForeignSessionHelper_pushWindow(
torne948f3662016-08-16 15:10:44257 env, last_pushed_session, session.modified_time.ToJavaTime(), 0));
zeae8f0c131d2015-08-04 23:20:27258 for (const sessions::SessionTab* tab : tabs) {
259 if (ShouldSkipTab(*tab))
260 continue;
261 CopyTabToJava(env, *tab, last_pushed_window);
262 }
263 } else {
264 // Push the full session, with tabs ordered by visual position.
265 CopySessionToJava(env, session, last_pushed_session);
266 }
[email protected]5365e52c2013-07-31 23:07:17267 }
268
269 return true;
270}
271
tornee621a272015-11-30 18:40:53272jboolean ForeignSessionHelper::OpenForeignSessionTab(
273 JNIEnv* env,
274 const JavaParamRef<jobject>& obj,
275 const JavaParamRef<jobject>& j_tab,
276 const JavaParamRef<jstring>& session_tag,
277 jint session_tab_id,
278 jint j_disposition) {
[email protected]a9f56622013-11-21 19:37:06279 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
280 if (!open_tabs) {
281 LOG(ERROR) << "Null OpenTabsUIDelegate returned.";
[email protected]816939d2013-11-13 00:57:39282 return false;
283 }
284
skuhneb7409dcf2014-11-14 04:06:55285 const sessions::SessionTab* session_tab;
[email protected]816939d2013-11-13 00:57:39286
[email protected]a9f56622013-11-21 19:37:06287 if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag),
288 session_tab_id,
289 &session_tab)) {
[email protected]816939d2013-11-13 00:57:39290 LOG(ERROR) << "Failed to load foreign tab.";
291 return false;
292 }
293
294 if (session_tab->navigations.empty()) {
295 LOG(ERROR) << "Foreign tab no longer has valid navigations.";
296 return false;
297 }
298
299 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab);
300 if (!tab_android)
301 return false;
302 content::WebContents* web_contents = tab_android->web_contents();
303 if (!web_contents)
304 return false;
305
306 WindowOpenDisposition disposition =
307 static_cast<WindowOpenDisposition>(j_disposition);
308 SessionRestore::RestoreForeignSessionTab(web_contents,
309 *session_tab,
310 disposition);
311
312 return true;
313}
314
tornee621a272015-11-30 18:40:53315void ForeignSessionHelper::DeleteForeignSession(
316 JNIEnv* env,
317 const JavaParamRef<jobject>& obj,
318 const JavaParamRef<jstring>& session_tag) {
[email protected]a9f56622013-11-21 19:37:06319 OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
320 if (open_tabs)
321 open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag));
[email protected]5365e52c2013-07-31 23:07:17322}
323
324// static
325bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) {
326 return RegisterNativesImpl(env);
327}