| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/sync/profile_sync_service_android.h" | 5 #include "chrome/browser/sync/profile_sync_service_android.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/android/jni_android.h" | 12 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_array.h" | 13 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 14 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 15 #include "base/bind.h" |
| 14 #include "base/i18n/time_formatting.h" | 16 #include "base/i18n/time_formatting.h" |
| 15 #include "base/json/json_writer.h" | 17 #include "base/json/json_writer.h" |
| 16 #include "base/logging.h" | 18 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/ptr_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 20 #include "chrome/browser/browser_process.h" | 22 #include "chrome/browser/browser_process.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/browser/signin/signin_manager_factory.h" | 24 #include "chrome/browser/signin/signin_manager_factory.h" |
| 23 #include "chrome/browser/sync/profile_sync_service_factory.h" | 25 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 24 #include "chrome/browser/sync/sync_ui_util.h" | 26 #include "chrome/browser/sync/sync_ui_util.h" |
| 25 #include "chrome/common/channel_info.h" | 27 #include "chrome/common/channel_info.h" |
| 26 #include "chrome/grit/generated_resources.h" | 28 #include "chrome/grit/generated_resources.h" |
| 27 #include "components/browser_sync/browser/profile_sync_service.h" | 29 #include "components/browser_sync/browser/profile_sync_service.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 using base::android::ScopedJavaLocalRef; | 49 using base::android::ScopedJavaLocalRef; |
| 48 using content::BrowserThread; | 50 using content::BrowserThread; |
| 49 | 51 |
| 50 namespace { | 52 namespace { |
| 51 | 53 |
| 52 // Native callback for the JNI GetAllNodes method. When | 54 // Native callback for the JNI GetAllNodes method. When |
| 53 // ProfileSyncService::GetAllNodes completes, this method is called and the | 55 // ProfileSyncService::GetAllNodes completes, this method is called and the |
| 54 // results are sent to the Java callback. | 56 // results are sent to the Java callback. |
| 55 void NativeGetAllNodesCallback( | 57 void NativeGetAllNodesCallback( |
| 56 const base::android::ScopedJavaGlobalRef<jobject>& callback, | 58 const base::android::ScopedJavaGlobalRef<jobject>& callback, |
| 57 scoped_ptr<base::ListValue> result) { | 59 std::unique_ptr<base::ListValue> result) { |
| 58 JNIEnv* env = base::android::AttachCurrentThread(); | 60 JNIEnv* env = base::android::AttachCurrentThread(); |
| 59 std::string json_string; | 61 std::string json_string; |
| 60 if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { | 62 if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) { |
| 61 DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; | 63 DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code."; |
| 62 json_string = std::string(); | 64 json_string = std::string(); |
| 63 } | 65 } |
| 64 | 66 |
| 65 ScopedJavaLocalRef<jstring> java_json_string = | 67 ScopedJavaLocalRef<jstring> java_json_string = |
| 66 ConvertUTF8ToJavaString(env, json_string); | 68 ConvertUTF8ToJavaString(env, json_string); |
| 67 Java_ProfileSyncService_onGetAllNodesResult(env, | 69 Java_ProfileSyncService_onGetAllNodesResult(env, |
| (...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 return ConvertUTF8ToJavaString(env, status); | 339 return ConvertUTF8ToJavaString(env, status); |
| 338 } | 340 } |
| 339 | 341 |
| 340 void ProfileSyncServiceAndroid::GetAllNodes( | 342 void ProfileSyncServiceAndroid::GetAllNodes( |
| 341 JNIEnv* env, | 343 JNIEnv* env, |
| 342 const JavaParamRef<jobject>& obj, | 344 const JavaParamRef<jobject>& obj, |
| 343 const JavaParamRef<jobject>& callback) { | 345 const JavaParamRef<jobject>& callback) { |
| 344 base::android::ScopedJavaGlobalRef<jobject> java_callback; | 346 base::android::ScopedJavaGlobalRef<jobject> java_callback; |
| 345 java_callback.Reset(env, callback); | 347 java_callback.Reset(env, callback); |
| 346 | 348 |
| 347 base::Callback<void(scoped_ptr<base::ListValue>)> native_callback = | 349 base::Callback<void(std::unique_ptr<base::ListValue>)> native_callback = |
| 348 base::Bind(&NativeGetAllNodesCallback, java_callback); | 350 base::Bind(&NativeGetAllNodesCallback, java_callback); |
| 349 sync_service_->GetAllNodes(native_callback); | 351 sync_service_->GetAllNodes(native_callback); |
| 350 } | 352 } |
| 351 | 353 |
| 352 jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, | 354 jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, |
| 353 const JavaParamRef<jobject>&) { | 355 const JavaParamRef<jobject>&) { |
| 354 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 356 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 355 return sync_service_->GetAuthError().state(); | 357 return sync_service_->GetAuthError().state(); |
| 356 } | 358 } |
| 357 | 359 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); | 449 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); |
| 448 } | 450 } |
| 449 | 451 |
| 450 // Functionality only available for testing purposes. | 452 // Functionality only available for testing purposes. |
| 451 | 453 |
| 452 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( | 454 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( |
| 453 JNIEnv* env, | 455 JNIEnv* env, |
| 454 const JavaParamRef<jobject>&) { | 456 const JavaParamRef<jobject>&) { |
| 455 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 457 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 456 | 458 |
| 457 scoped_ptr<base::DictionaryValue> about_info = | 459 std::unique_ptr<base::DictionaryValue> about_info = |
| 458 sync_driver::sync_ui_util::ConstructAboutInformation( | 460 sync_driver::sync_ui_util::ConstructAboutInformation( |
| 459 sync_service_, sync_service_->signin(), chrome::GetChannel()); | 461 sync_service_, sync_service_->signin(), chrome::GetChannel()); |
| 460 std::string about_info_json; | 462 std::string about_info_json; |
| 461 base::JSONWriter::Write(*about_info, &about_info_json); | 463 base::JSONWriter::Write(*about_info, &about_info_json); |
| 462 | 464 |
| 463 return ConvertUTF8ToJavaString(env, about_info_json); | 465 return ConvertUTF8ToJavaString(env, about_info_json); |
| 464 } | 466 } |
| 465 | 467 |
| 466 jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( | 468 jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest( |
| 467 JNIEnv* env, | 469 JNIEnv* env, |
| 468 const JavaParamRef<jobject>& obj) { | 470 const JavaParamRef<jobject>& obj) { |
| 469 // Use profile preferences here instead of SyncPrefs to avoid an extra | 471 // Use profile preferences here instead of SyncPrefs to avoid an extra |
| 470 // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value | 472 // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value |
| 471 // to to base::Time. | 473 // to to base::Time. |
| 472 return static_cast<jlong>( | 474 return static_cast<jlong>( |
| 473 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime)); | 475 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime)); |
| 474 } | 476 } |
| 475 | 477 |
| 476 void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( | 478 void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest( |
| 477 JNIEnv* env, | 479 JNIEnv* env, |
| 478 const JavaParamRef<jobject>& obj, | 480 const JavaParamRef<jobject>& obj, |
| 479 jlong network_resources) { | 481 jlong network_resources) { |
| 480 syncer::NetworkResources* resources = | 482 syncer::NetworkResources* resources = |
| 481 reinterpret_cast<syncer::NetworkResources*>(network_resources); | 483 reinterpret_cast<syncer::NetworkResources*>(network_resources); |
| 482 sync_service_->OverrideNetworkResourcesForTest( | 484 sync_service_->OverrideNetworkResourcesForTest( |
| 483 make_scoped_ptr<syncer::NetworkResources>(resources)); | 485 base::WrapUnique<syncer::NetworkResources>(resources)); |
| 484 } | 486 } |
| 485 | 487 |
| 486 // static | 488 // static |
| 487 ProfileSyncServiceAndroid* | 489 ProfileSyncServiceAndroid* |
| 488 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { | 490 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() { |
| 489 return reinterpret_cast<ProfileSyncServiceAndroid*>( | 491 return reinterpret_cast<ProfileSyncServiceAndroid*>( |
| 490 Java_ProfileSyncService_getProfileSyncServiceAndroid( | 492 Java_ProfileSyncService_getProfileSyncServiceAndroid( |
| 491 AttachCurrentThread())); | 493 AttachCurrentThread())); |
| 492 } | 494 } |
| 493 | 495 |
| 494 // static | 496 // static |
| 495 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | 497 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { |
| 496 return RegisterNativesImpl(env); | 498 return RegisterNativesImpl(env); |
| 497 } | 499 } |
| 498 | 500 |
| 499 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { | 501 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) { |
| 500 ProfileSyncServiceAndroid* profile_sync_service_android = | 502 ProfileSyncServiceAndroid* profile_sync_service_android = |
| 501 new ProfileSyncServiceAndroid(env, obj); | 503 new ProfileSyncServiceAndroid(env, obj); |
| 502 if (profile_sync_service_android->Init()) { | 504 if (profile_sync_service_android->Init()) { |
| 503 return reinterpret_cast<intptr_t>(profile_sync_service_android); | 505 return reinterpret_cast<intptr_t>(profile_sync_service_android); |
| 504 } else { | 506 } else { |
| 505 delete profile_sync_service_android; | 507 delete profile_sync_service_android; |
| 506 return 0; | 508 return 0; |
| 507 } | 509 } |
| 508 } | 510 } |
| OLD | NEW |