Commit 598bf6cf authored by nyquist@chromium.org's avatar nyquist@chromium.org

Depend on stored sync session GUID for Android.

This changes the Android port of Chrome to depend on the pref
kSyncSessionsGUID to be set before the SessionModelAssociator is
initialized.

We now also force-removes any foreign sessions that are using the
current client's GUID as a session tag.

This also lead to the removal of the Android specific session utils.

BUG=157419


Review URL: https://chromiumcodereview.appspot.com/11414013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171109 0039d316-1c4b-4281-b951-d872f2087c98
parent 3ded155c
...@@ -48,8 +48,6 @@ ...@@ -48,8 +48,6 @@
#include "base/linux_util.h" #include "base/linux_util.h"
#elif defined(OS_WIN) #elif defined(OS_WIN)
#include <windows.h> #include <windows.h>
#elif defined(OS_ANDROID)
#include "sync/util/session_utils_android.h"
#endif #endif
using content::BrowserThread; using content::BrowserThread;
...@@ -57,6 +55,19 @@ using content::NavigationEntry; ...@@ -57,6 +55,19 @@ using content::NavigationEntry;
using prefs::kSyncSessionsGUID; using prefs::kSyncSessionsGUID;
using syncer::SESSIONS; using syncer::SESSIONS;
namespace {
// Given a transaction, returns the GUID-based string that should be used for
// |current_machine_tag_|.
std::string GetMachineTagFromTransaction(
syncer::WriteTransaction* trans) {
syncer::syncable::Directory* dir = trans->GetWrappedWriteTrans()->directory();
std::string machine_tag = "session_sync";
machine_tag.append(dir->cache_guid());
return machine_tag;
}
} // namespace
namespace browser_sync { namespace browser_sync {
namespace { namespace {
...@@ -643,6 +654,11 @@ syncer::SyncError SessionModelAssociator::AssociateModels( ...@@ -643,6 +654,11 @@ syncer::SyncError SessionModelAssociator::AssociateModels(
local_session_syncid_ = write_node.GetId(); local_session_syncid_ = write_node.GetId();
} }
#if defined(OS_ANDROID)
std::string transaction_tag = GetMachineTagFromTransaction(&trans);
if (current_machine_tag_.compare(transaction_tag) != 0)
DeleteForeignSession(transaction_tag);
#endif
} }
// Check if anything has changed on the client side. // Check if anything has changed on the client side.
...@@ -690,19 +706,7 @@ void SessionModelAssociator::InitializeCurrentMachineTag( ...@@ -690,19 +706,7 @@ void SessionModelAssociator::InitializeCurrentMachineTag(
DVLOG(1) << "Restoring persisted session sync guid: " DVLOG(1) << "Restoring persisted session sync guid: "
<< persisted_guid; << persisted_guid;
} else { } else {
syncer::syncable::Directory* dir = current_machine_tag_ = GetMachineTagFromTransaction(trans);
trans->GetWrappedWriteTrans()->directory();
current_machine_tag_ = "session_sync";
#if defined(OS_ANDROID)
const std::string android_id = syncer::internal::GetAndroidId();
// There are reports that sometimes the android_id can't be read. Those
// are supposed to be fixed as of Gingerbread, but if it happens we fall
// back to use the same GUID generation as on other platforms.
current_machine_tag_.append(android_id.empty() ?
dir->cache_guid() : android_id);
#else
current_machine_tag_.append(dir->cache_guid());
#endif
DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; DVLOG(1) << "Creating session sync guid: " << current_machine_tag_;
if (pref_service_) if (pref_service_)
pref_service_->SetString(kSyncSessionsGUID, current_machine_tag_); pref_service_->SetString(kSyncSessionsGUID, current_machine_tag_);
......
...@@ -188,8 +188,6 @@ ...@@ -188,8 +188,6 @@
'util/logging.h', 'util/logging.h',
'util/nigori.cc', 'util/nigori.cc',
'util/nigori.h', 'util/nigori.h',
'util/session_utils_android.cc',
'util/session_utils_android.h',
'util/time.cc', 'util/time.cc',
'util/time.h', 'util/time.h',
], ],
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#elif defined(OS_WIN) #elif defined(OS_WIN)
#include "sync/util/get_session_name_win.h" #include "sync/util/get_session_name_win.h"
#elif defined(OS_ANDROID) #elif defined(OS_ANDROID)
#include "sync/util/session_utils_android.h" #include "base/android/build_info.h"
#endif #endif
namespace syncer { namespace syncer {
...@@ -53,7 +53,9 @@ std::string GetSessionNameSynchronously() { ...@@ -53,7 +53,9 @@ std::string GetSessionNameSynchronously() {
#elif defined(OS_WIN) #elif defined(OS_WIN)
session_name = internal::GetComputerName(); session_name = internal::GetComputerName();
#elif defined(OS_ANDROID) #elif defined(OS_ANDROID)
session_name = internal::GetModel(); base::android::BuildInfo* android_build_info =
base::android::BuildInfo::GetInstance();
session_name = android_build_info->model();
#endif #endif
if (session_name == "Unknown" || session_name.empty()) if (session_name == "Unknown" || session_name.empty())
......
// Copyright (c) 2012 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 <jni.h>
#include <sys/system_properties.h>
#include "sync/util/session_utils_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/logging.h"
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::ConvertUTF8ToJavaString;
using base::android::GetApplicationContext;
using base::android::GetClass;
using base::android::JavaRef;
using base::android::MethodID;
using base::android::ScopedJavaLocalRef;
namespace {
ScopedJavaLocalRef<jstring> GetAndroidIdJNI(
JNIEnv* env, const JavaRef<jobject>& content_resolver) {
ScopedJavaLocalRef<jclass> clazz(
GetClass(env, "android/provider/Settings$Secure"));
jmethodID j_get_string = MethodID::Get<MethodID::TYPE_STATIC>(
env, clazz.obj(), "getString",
"(Landroid/content/ContentResolver;Ljava/lang/String;)"
"Ljava/lang/String;");
ScopedJavaLocalRef<jstring> j_android_id =
ConvertUTF8ToJavaString(env, "android_id");
jstring android_id = static_cast<jstring>(
env->CallStaticObjectMethod(
clazz.obj(), j_get_string, content_resolver.obj(),
j_android_id.obj()));
CheckException(env);
return ScopedJavaLocalRef<jstring>(env, android_id);
}
ScopedJavaLocalRef<jobject> GetContentResolver(JNIEnv* env) {
ScopedJavaLocalRef<jclass> clazz(GetClass(env, "android/content/Context"));
jmethodID j_get_content_resolver_method = MethodID::Get<
MethodID::TYPE_INSTANCE>(
env, clazz.obj(), "getContentResolver",
"()Landroid/content/ContentResolver;");
jobject content_resolver = env->CallObjectMethod(
GetApplicationContext(), j_get_content_resolver_method);
CheckException(env);
return ScopedJavaLocalRef<jobject>(env, content_resolver);
}
}
namespace syncer {
namespace internal {
std::string GetAndroidId() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> android_id =
GetAndroidIdJNI(env, GetContentResolver(env));
return ConvertJavaStringToUTF8(android_id);
}
std::string GetModel() {
char model[PROP_VALUE_MAX];
__system_property_get("ro.product.model", model);
return model;
}
} // namespace internal
} // namespace syncer
// Copyright (c) 2012 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.
#ifndef SYNC_UTIL_SESSION_UTILS_ANDROID_H_
#define SYNC_UTIL_SESSION_UTILS_ANDROID_H_
#include <string>
namespace syncer {
namespace internal {
// Return the unique identifier of this device.
std::string GetAndroidId();
// Return the end-user-visible name for this device.
std::string GetModel();
} // namespace internal
} // namespace syncer
#endif // SYNC_UTIL_SESSION_UTILS_ANDROID_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment