1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/foreign_session_helper.h"
#include <jni.h>
#include "base/android/jni_string.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sync/open_tabs_ui_delegate.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/web_contents.h"
#include "jni/ForeignSessionHelper_jni.h"
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
using base::android::AttachCurrentThread;
using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
using base::android::ConvertJavaStringToUTF8;
using browser_sync::OpenTabsUIDelegate;
using browser_sync::SyncedSession;
namespace {
OpenTabsUIDelegate* GetOpenTabsUIDelegate(Profile* profile) {
ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()->
GetForProfile(profile);
// Only return the delegate if it exists and it is done syncing sessions.
if (!service || !service->SyncActive())
return NULL;
return service->GetOpenTabsUIDelegate();
}
bool ShouldSkipTab(const sessions::SessionTab& session_tab) {
if (session_tab.navigations.empty())
return true;
int selected_index = session_tab.normalized_navigation_index();
const ::sessions::SerializedNavigationEntry& current_navigation =
session_tab.navigations.at(selected_index);
if (current_navigation.virtual_url().is_empty())
return true;
return false;
}
bool ShouldSkipWindow(const sessions::SessionWindow& window) {
for (std::vector<sessions::SessionTab*>::const_iterator tab_it =
window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) {
const sessions::SessionTab &session_tab = **tab_it;
if (!ShouldSkipTab(session_tab))
return false;
}
return true;
}
bool ShouldSkipSession(const browser_sync::SyncedSession& session) {
for (SyncedSession::SyncedWindowMap::const_iterator it =
session.windows.begin(); it != session.windows.end(); ++it) {
const sessions::SessionWindow &window = *(it->second);
if (!ShouldSkipWindow(window))
return false;
}
return true;
}
void CopyTabsToJava(
JNIEnv* env,
const sessions::SessionWindow& window,
ScopedJavaLocalRef<jobject>& j_window) {
for (std::vector<sessions::SessionTab*>::const_iterator tab_it =
window.tabs.begin(); tab_it != window.tabs.end(); ++tab_it) {
const sessions::SessionTab &session_tab = **tab_it;
if (ShouldSkipTab(session_tab))
continue;
int selected_index = session_tab.normalized_navigation_index();
DCHECK(selected_index >= 0);
DCHECK(selected_index < static_cast<int>(session_tab.navigations.size()));
const ::sessions::SerializedNavigationEntry& current_navigation =
session_tab.navigations.at(selected_index);
GURL tab_url = current_navigation.virtual_url();
Java_ForeignSessionHelper_pushTab(
env, j_window.obj(),
ConvertUTF8ToJavaString(env, tab_url.spec()).obj(),
ConvertUTF16ToJavaString(env, current_navigation.title()).obj(),
session_tab.timestamp.ToJavaTime(),
session_tab.tab_id.id());
}
}
void CopyWindowsToJava(
JNIEnv* env,
const SyncedSession& session,
ScopedJavaLocalRef<jobject>& j_session) {
for (SyncedSession::SyncedWindowMap::const_iterator it =
session.windows.begin(); it != session.windows.end(); ++it) {
const sessions::SessionWindow &window = *(it->second);
if (ShouldSkipWindow(window))
continue;
ScopedJavaLocalRef<jobject> last_pushed_window;
last_pushed_window.Reset(
Java_ForeignSessionHelper_pushWindow(
env, j_session.obj(),
window.timestamp.ToJavaTime(),
window.window_id.id()));
CopyTabsToJava(env, window, last_pushed_window);
}
}
} // namespace
static jlong Init(JNIEnv* env, jclass clazz, jobject profile) {
ForeignSessionHelper* foreign_session_helper = new ForeignSessionHelper(
ProfileAndroid::FromProfileAndroid(profile));
return reinterpret_cast<intptr_t>(foreign_session_helper);
}
ForeignSessionHelper::ForeignSessionHelper(Profile* profile)
: profile_(profile) {
ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()->
GetForProfile(profile);
registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE,
content::Source<ProfileSyncService>(service));
registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED,
content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED,
content::Source<Profile>(profile));
}
ForeignSessionHelper::~ForeignSessionHelper() {
}
void ForeignSessionHelper::Destroy(JNIEnv* env, jobject obj) {
delete this;
}
jboolean ForeignSessionHelper::IsTabSyncEnabled(JNIEnv* env, jobject obj) {
ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()->
GetForProfile(profile_);
return service && service->GetActiveDataTypes().Has(syncer::PROXY_TABS);
}
void ForeignSessionHelper::TriggerSessionSync(JNIEnv* env, jobject obj) {
const syncer::ModelTypeSet types(syncer::SESSIONS);
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_SYNC_REFRESH_LOCAL,
content::Source<Profile>(profile_),
content::Details<const syncer::ModelTypeSet>(&types));
}
void ForeignSessionHelper::SetOnForeignSessionCallback(JNIEnv* env,
jobject obj,
jobject callback) {
callback_.Reset(env, callback);
}
void ForeignSessionHelper::Observe(
int type, const content::NotificationSource& source,
const content::NotificationDetails& details) {
if (callback_.is_null())
return;
JNIEnv* env = AttachCurrentThread();
switch (type) {
case chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED:
// Tab sync is disabled, so clean up data about collapsed sessions.
profile_->GetPrefs()->ClearPref(
prefs::kNtpCollapsedForeignSessions);
// Purposeful fall through.
case chrome::NOTIFICATION_SYNC_CONFIGURE_DONE:
case chrome::NOTIFICATION_FOREIGN_SESSION_UPDATED:
Java_ForeignSessionCallback_onUpdated(env, callback_.obj());
break;
default:
NOTREACHED();
}
}
jboolean ForeignSessionHelper::GetForeignSessions(JNIEnv* env,
jobject obj,
jobject result) {
OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
if (!open_tabs)
return false;
std::vector<const browser_sync::SyncedSession*> sessions;
if (!open_tabs->GetAllForeignSessions(&sessions))
return false;
// Use a pref to keep track of sessions that were collapsed by the user.
// To prevent the pref from accumulating stale sessions, clear it each time
// and only add back sessions that are still current.
DictionaryPrefUpdate pref_update(profile_->GetPrefs(),
prefs::kNtpCollapsedForeignSessions);
base::DictionaryValue* pref_collapsed_sessions = pref_update.Get();
scoped_ptr<base::DictionaryValue> collapsed_sessions(
pref_collapsed_sessions->DeepCopy());
pref_collapsed_sessions->Clear();
ScopedJavaLocalRef<jobject> last_pushed_session;
ScopedJavaLocalRef<jobject> last_pushed_window;
// Note: we don't own the SyncedSessions themselves.
for (size_t i = 0; i < sessions.size(); ++i) {
const browser_sync::SyncedSession &session = *(sessions[i]);
if (ShouldSkipSession(session))
continue;
const bool is_collapsed = collapsed_sessions->HasKey(session.session_tag);
if (is_collapsed)
pref_collapsed_sessions->SetBoolean(session.session_tag, true);
last_pushed_session.Reset(
Java_ForeignSessionHelper_pushSession(
env,
result,
ConvertUTF8ToJavaString(env, session.session_tag).obj(),
ConvertUTF8ToJavaString(env, session.session_name).obj(),
session.device_type,
session.modified_time.ToJavaTime()));
CopyWindowsToJava(env, session, last_pushed_session);
}
return true;
}
jboolean ForeignSessionHelper::OpenForeignSessionTab(JNIEnv* env,
jobject obj,
jobject j_tab,
jstring session_tag,
jint session_tab_id,
jint j_disposition) {
OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
if (!open_tabs) {
LOG(ERROR) << "Null OpenTabsUIDelegate returned.";
return false;
}
const sessions::SessionTab* session_tab;
if (!open_tabs->GetForeignTab(ConvertJavaStringToUTF8(env, session_tag),
session_tab_id,
&session_tab)) {
LOG(ERROR) << "Failed to load foreign tab.";
return false;
}
if (session_tab->navigations.empty()) {
LOG(ERROR) << "Foreign tab no longer has valid navigations.";
return false;
}
TabAndroid* tab_android = TabAndroid::GetNativeTab(env, j_tab);
if (!tab_android)
return false;
content::WebContents* web_contents = tab_android->web_contents();
if (!web_contents)
return false;
WindowOpenDisposition disposition =
static_cast<WindowOpenDisposition>(j_disposition);
SessionRestore::RestoreForeignSessionTab(web_contents,
*session_tab,
disposition);
return true;
}
void ForeignSessionHelper::DeleteForeignSession(JNIEnv* env, jobject obj,
jstring session_tag) {
OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate(profile_);
if (open_tabs)
open_tabs->DeleteForeignSession(ConvertJavaStringToUTF8(env, session_tag));
}
// static
bool ForeignSessionHelper::RegisterForeignSessionHelper(JNIEnv* env) {
return RegisterNativesImpl(env);
}
|