Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_service_android.cc
blobabf87e5f643af3beb330e1f2f2640a81e7237b4f
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
3 // found in the LICENSE file.
5 #include "chrome/browser/sync/profile_sync_service_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/bind.h"
11 #include "base/i18n/time_formatting.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/metrics/field_trial.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/time/time.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/sync/sync_ui_util.h"
25 #include "chrome/common/channel_info.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "components/signin/core/browser/signin_manager.h"
28 #include "components/sync_driver/about_sync_util.h"
29 #include "components/sync_driver/pref_names.h"
30 #include "components/sync_driver/sync_prefs.h"
31 #include "content/public/browser/browser_thread.h"
32 #include "google/cacheinvalidation/types.pb.h"
33 #include "google_apis/gaia/gaia_constants.h"
34 #include "google_apis/gaia/google_service_auth_error.h"
35 #include "jni/ProfileSyncService_jni.h"
36 #include "sync/internal_api/public/network_resources.h"
37 #include "sync/internal_api/public/read_transaction.h"
38 #include "ui/base/l10n/l10n_util.h"
40 using base::android::AttachCurrentThread;
41 using base::android::CheckException;
42 using base::android::ConvertJavaStringToUTF8;
43 using base::android::ConvertUTF8ToJavaString;
44 using base::android::ScopedJavaLocalRef;
45 using content::BrowserThread;
47 namespace {
49 // Native callback for the JNI GetAllNodes method. When
50 // ProfileSyncService::GetAllNodes completes, this method is called and the
51 // results are sent to the Java callback.
52 void NativeGetAllNodesCallback(
53 const base::android::ScopedJavaGlobalRef<jobject>& callback,
54 scoped_ptr<base::ListValue> result) {
55 JNIEnv* env = base::android::AttachCurrentThread();
56 std::string json_string;
57 if (!result.get() || !base::JSONWriter::Write(*result, &json_string)) {
58 DVLOG(1) << "Writing as JSON failed. Passing empty string to Java code.";
59 json_string = std::string();
62 ScopedJavaLocalRef<jstring> java_json_string =
63 ConvertUTF8ToJavaString(env, json_string);
64 Java_ProfileSyncService_onGetAllNodesResult(env,
65 callback.obj(),
66 java_json_string.obj());
69 } // namespace
71 ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj)
72 : profile_(NULL),
73 sync_service_(NULL),
74 weak_java_profile_sync_service_(env, obj) {
75 if (g_browser_process == NULL ||
76 g_browser_process->profile_manager() == NULL) {
77 NOTREACHED() << "Browser process or profile manager not initialized";
78 return;
81 profile_ = ProfileManager::GetActiveUserProfile();
82 if (profile_ == NULL) {
83 NOTREACHED() << "Sync Init: Profile not found.";
84 return;
87 sync_prefs_.reset(new sync_driver::SyncPrefs(profile_->GetPrefs()));
89 sync_service_ =
90 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
91 DCHECK(sync_service_);
94 void ProfileSyncServiceAndroid::Init() {
95 sync_service_->AddObserver(this);
98 void ProfileSyncServiceAndroid::RemoveObserver() {
99 if (sync_service_->HasObserver(this)) {
100 sync_service_->RemoveObserver(this);
104 ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() {
105 RemoveObserver();
108 void ProfileSyncServiceAndroid::OnStateChanged() {
109 // Notify the java world that our sync state has changed.
110 JNIEnv* env = AttachCurrentThread();
111 Java_ProfileSyncService_syncStateChanged(
112 env, weak_java_profile_sync_service_.get(env).obj());
115 jboolean ProfileSyncServiceAndroid::IsPassphrasePrompted(JNIEnv* env,
116 jobject obj) {
117 const std::string group_name =
118 base::FieldTrialList::FindFullName("LimitSyncPassphrasePrompt");
119 if (group_name != "Enabled")
120 return false;
121 return sync_prefs_->IsPassphrasePrompted();
124 void ProfileSyncServiceAndroid::SetPassphrasePrompted(JNIEnv* env,
125 jobject obj,
126 jboolean prompted) {
127 sync_prefs_->SetPassphrasePrompted(prompted);
130 void ProfileSyncServiceAndroid::RequestStart(JNIEnv* env, jobject) {
131 DCHECK_CURRENTLY_ON(BrowserThread::UI);
132 sync_service_->RequestStart();
135 void ProfileSyncServiceAndroid::RequestStop(JNIEnv* env, jobject) {
136 DCHECK_CURRENTLY_ON(BrowserThread::UI);
137 sync_service_->RequestStop(ProfileSyncService::KEEP_DATA);
140 void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) {
141 DCHECK_CURRENTLY_ON(BrowserThread::UI);
142 DCHECK(profile_);
143 sync_service_->RequestStop(ProfileSyncService::CLEAR_DATA);
146 void ProfileSyncServiceAndroid::FlushDirectory(JNIEnv* env, jobject) {
147 DCHECK_CURRENTLY_ON(BrowserThread::UI);
148 sync_service_->FlushDirectory();
151 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary(
152 JNIEnv* env, jobject) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI);
154 DCHECK(profile_);
155 std::string status(sync_service_->QuerySyncStatusSummaryString());
156 return ConvertUTF8ToJavaString(env, status);
159 void ProfileSyncServiceAndroid::GetAllNodes(JNIEnv* env,
160 jobject obj,
161 jobject callback) {
162 base::android::ScopedJavaGlobalRef<jobject> java_callback;
163 java_callback.Reset(env, callback);
165 base::Callback<void(scoped_ptr<base::ListValue>)> native_callback =
166 base::Bind(&NativeGetAllNodesCallback, java_callback);
167 sync_service_->GetAllNodes(native_callback);
170 void ProfileSyncServiceAndroid::SetSyncSessionsId(
171 JNIEnv* env, jobject obj, jstring tag) {
172 DCHECK_CURRENTLY_ON(BrowserThread::UI);
173 DCHECK(profile_);
174 std::string machine_tag = ConvertJavaStringToUTF8(env, tag);
175 sync_prefs_->SetSyncSessionsGUID(machine_tag);
178 jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) {
179 DCHECK_CURRENTLY_ON(BrowserThread::UI);
180 return sync_service_->GetAuthError().state();
183 jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled(
184 JNIEnv* env, jobject) {
185 DCHECK_CURRENTLY_ON(BrowserThread::UI);
186 return sync_service_->EncryptEverythingEnabled();
189 jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) {
190 DCHECK_CURRENTLY_ON(BrowserThread::UI);
191 return sync_service_->backend_initialized();
194 jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress(
195 JNIEnv* env, jobject) {
196 DCHECK_CURRENTLY_ON(BrowserThread::UI);
197 return sync_service_->FirstSetupInProgress();
200 jboolean ProfileSyncServiceAndroid::IsEncryptEverythingAllowed(
201 JNIEnv* env, jobject obj) {
202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
203 return sync_service_->EncryptEverythingAllowed();
206 jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) {
207 DCHECK_CURRENTLY_ON(BrowserThread::UI);
208 return sync_service_->IsPassphraseRequired();
211 jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption(
212 JNIEnv* env, jobject obj) {
213 DCHECK_CURRENTLY_ON(BrowserThread::UI);
214 // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for
215 // a passphrase if cryptographer has any pending keys.
216 if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) {
217 return !IsCryptographerReady(env, obj);
219 if (sync_service_->IsPassphraseRequiredForDecryption()) {
220 // Passwords datatype should never prompt for a passphrase, except when
221 // user is using a custom passphrase. Do not prompt for a passphrase if
222 // passwords are the only encrypted datatype. This prevents a temporary
223 // notification for passphrase when PSS has not completed configuring
224 // DataTypeManager, after configuration password datatype shall be disabled.
225 const syncer::ModelTypeSet encrypted_types =
226 sync_service_->GetEncryptedDataTypes();
227 return !encrypted_types.Equals(syncer::ModelTypeSet(syncer::PASSWORDS));
229 return false;
232 jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase(
233 JNIEnv* env, jobject) {
234 DCHECK_CURRENTLY_ON(BrowserThread::UI);
235 return sync_service_->IsUsingSecondaryPassphrase();
238 jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase(
239 JNIEnv* env, jobject obj, jstring passphrase) {
240 DCHECK_CURRENTLY_ON(BrowserThread::UI);
241 std::string key = ConvertJavaStringToUTF8(env, passphrase);
242 return sync_service_->SetDecryptionPassphrase(key);
245 void ProfileSyncServiceAndroid::SetEncryptionPassphrase(
246 JNIEnv* env, jobject obj, jstring passphrase) {
247 DCHECK_CURRENTLY_ON(BrowserThread::UI);
248 std::string key = ConvertJavaStringToUTF8(env, passphrase);
249 sync_service_->SetEncryptionPassphrase(key, ProfileSyncService::EXPLICIT);
252 jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) {
253 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
254 return sync_service_->IsCryptographerReady(&trans);
257 jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) {
258 DCHECK_CURRENTLY_ON(BrowserThread::UI);
259 return sync_service_->GetPassphraseType();
262 jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime(
263 JNIEnv* env, jobject) {
264 DCHECK_CURRENTLY_ON(BrowserThread::UI);
265 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
266 return !passphrase_time.is_null();
269 jlong ProfileSyncServiceAndroid::GetExplicitPassphraseTime(
270 JNIEnv* env, jobject) {
271 DCHECK_CURRENTLY_ON(BrowserThread::UI);
272 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
273 return passphrase_time.ToJavaTime();
276 ScopedJavaLocalRef<jstring>
277 ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText(
278 JNIEnv* env, jobject) {
279 DCHECK_CURRENTLY_ON(BrowserThread::UI);
280 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
281 base::string16 passphrase_time_str =
282 base::TimeFormatShortDate(passphrase_time);
283 return base::android::ConvertUTF16ToJavaString(env,
284 l10n_util::GetStringFUTF16(
285 IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE,
286 passphrase_time_str));
289 ScopedJavaLocalRef<jstring>
290 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText(
291 JNIEnv* env, jobject) {
292 DCHECK_CURRENTLY_ON(BrowserThread::UI);
293 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime();
294 base::string16 passphrase_time_str =
295 base::TimeFormatShortDate(passphrase_time);
296 return base::android::ConvertUTF16ToJavaString(env,
297 l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE,
298 passphrase_time_str));
301 ScopedJavaLocalRef<jstring>
302 ProfileSyncServiceAndroid::GetCurrentSignedInAccountText(
303 JNIEnv* env, jobject) {
304 DCHECK_CURRENTLY_ON(BrowserThread::UI);
305 const std::string& sync_username =
306 SigninManagerFactory::GetForProfile(profile_)
307 ->GetAuthenticatedAccountInfo()
308 .email;
309 return base::android::ConvertUTF16ToJavaString(env,
310 l10n_util::GetStringFUTF16(
311 IDS_SYNC_ACCOUNT_SYNCING_TO_USER,
312 base::ASCIIToUTF16(sync_username)));
315 ScopedJavaLocalRef<jstring>
316 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText(
317 JNIEnv* env, jobject) {
318 DCHECK_CURRENTLY_ON(BrowserThread::UI);
319 return ConvertUTF8ToJavaString(
320 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY));
323 jboolean ProfileSyncServiceAndroid::IsSyncKeystoreMigrationDone(
324 JNIEnv* env, jobject) {
325 DCHECK_CURRENTLY_ON(BrowserThread::UI);
326 syncer::SyncStatus status;
327 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status);
328 return is_status_valid && !status.keystore_migration_time.is_null();
331 ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetActiveDataTypes(
332 JNIEnv* env, jobject obj) {
333 syncer::ModelTypeSet types = sync_service_->GetActiveDataTypes();
334 types.PutAll(syncer::ControlTypes());
335 return ModelTypeSetToJavaIntArray(env, types);
338 ScopedJavaLocalRef<jintArray> ProfileSyncServiceAndroid::GetPreferredDataTypes(
339 JNIEnv* env, jobject obj) {
340 syncer::ModelTypeSet types = sync_service_->GetPreferredDataTypes();
341 types.PutAll(syncer::ControlTypes());
342 return ModelTypeSetToJavaIntArray(env, types);
345 void ProfileSyncServiceAndroid::SetPreferredDataTypes(
346 JNIEnv* env, jobject obj,
347 jboolean sync_everything,
348 jintArray model_type_array) {
349 DCHECK_CURRENTLY_ON(BrowserThread::UI);
350 std::vector<int> types_vector;
351 base::android::JavaIntArrayToIntVector(env, model_type_array, &types_vector);
352 syncer::ModelTypeSet types;
353 for (size_t i = 0; i < types_vector.size(); i++) {
354 types.Put(static_cast<syncer::ModelType>(types_vector[i]));
356 types.RetainAll(syncer::UserSelectableTypes());
357 sync_service_->OnUserChoseDatatypes(sync_everything, types);
360 void ProfileSyncServiceAndroid::SetSetupInProgress(
361 JNIEnv* env, jobject obj, jboolean in_progress) {
362 DCHECK_CURRENTLY_ON(BrowserThread::UI);
363 sync_service_->SetSetupInProgress(in_progress);
366 void ProfileSyncServiceAndroid::SetSyncSetupCompleted(
367 JNIEnv* env, jobject obj) {
368 DCHECK_CURRENTLY_ON(BrowserThread::UI);
369 sync_service_->SetSyncSetupCompleted();
372 jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted(
373 JNIEnv* env, jobject obj) {
374 DCHECK_CURRENTLY_ON(BrowserThread::UI);
375 return sync_service_->HasSyncSetupCompleted();
378 jboolean ProfileSyncServiceAndroid::IsSyncRequested(
379 JNIEnv* env, jobject obj) {
380 DCHECK_CURRENTLY_ON(BrowserThread::UI);
381 return sync_service_->IsSyncRequested();
384 jboolean ProfileSyncServiceAndroid::IsSyncActive(JNIEnv* env, jobject obj) {
385 DCHECK_CURRENTLY_ON(BrowserThread::UI);
386 return sync_service_->IsSyncActive();
389 void ProfileSyncServiceAndroid::EnableEncryptEverything(
390 JNIEnv* env, jobject obj) {
391 DCHECK_CURRENTLY_ON(BrowserThread::UI);
392 sync_service_->EnableEncryptEverything();
395 jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced(
396 JNIEnv* env, jobject) {
397 DCHECK_CURRENTLY_ON(BrowserThread::UI);
398 return sync_prefs_->HasKeepEverythingSynced();
401 jboolean ProfileSyncServiceAndroid::HasUnrecoverableError(
402 JNIEnv* env, jobject) {
403 DCHECK_CURRENTLY_ON(BrowserThread::UI);
404 return sync_service_->HasUnrecoverableError();
407 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest(
408 JNIEnv* env, jobject) {
409 DCHECK_CURRENTLY_ON(BrowserThread::UI);
411 scoped_ptr<base::DictionaryValue> about_info =
412 sync_ui_util::ConstructAboutInformation(sync_service_,
413 sync_service_->signin(),
414 chrome::GetChannel());
415 std::string about_info_json;
416 base::JSONWriter::Write(*about_info, &about_info_json);
418 return ConvertUTF8ToJavaString(env, about_info_json);
421 jlong ProfileSyncServiceAndroid::GetLastSyncedTimeForTest(
422 JNIEnv* env, jobject obj) {
423 // Use profile preferences here instead of SyncPrefs to avoid an extra
424 // conversion, since SyncPrefs::GetLastSyncedTime() converts the stored value
425 // to to base::Time.
426 return static_cast<jlong>(
427 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
430 void ProfileSyncServiceAndroid::OverrideNetworkResourcesForTest(
431 JNIEnv* env,
432 jobject obj,
433 jlong network_resources) {
434 syncer::NetworkResources* resources =
435 reinterpret_cast<syncer::NetworkResources*>(network_resources);
436 sync_service_->OverrideNetworkResourcesForTest(
437 make_scoped_ptr<syncer::NetworkResources>(resources));
440 // static
441 ScopedJavaLocalRef<jintArray>
442 ProfileSyncServiceAndroid::ModelTypeSetToJavaIntArray(
443 JNIEnv* env,
444 syncer::ModelTypeSet types) {
445 std::vector<int> type_vector;
446 for (syncer::ModelTypeSet::Iterator it = types.First(); it.Good(); it.Inc()) {
447 type_vector.push_back(it.Get());
449 return base::android::ToJavaIntArray(env, type_vector);
452 // static
453 ProfileSyncServiceAndroid*
454 ProfileSyncServiceAndroid::GetProfileSyncServiceAndroid() {
455 return reinterpret_cast<ProfileSyncServiceAndroid*>(
456 Java_ProfileSyncService_getProfileSyncServiceAndroid(
457 AttachCurrentThread()));
460 static jlong Init(JNIEnv* env, const JavaParamRef<jobject>& obj) {
461 ProfileSyncServiceAndroid* profile_sync_service_android =
462 new ProfileSyncServiceAndroid(env, obj);
463 profile_sync_service_android->Init();
464 return reinterpret_cast<intptr_t>(profile_sync_service_android);
467 // static
468 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) {
469 return RegisterNativesImpl(env);