OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/sync/profile_sync_service_android.h" | |
6 | |
7 #include "base/android/jni_android.h" | |
8 #include "base/android/jni_string.h" | |
9 #include "base/bind.h" | |
10 #include "base/i18n/time_formatting.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/logging.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/time.h" | |
15 #include "chrome/browser/browser_process.h" | |
16 #include "chrome/browser/profiles/profile_manager.h" | |
17 #include "chrome/browser/signin/signin_manager.h" | |
18 #include "chrome/browser/signin/signin_manager_factory.h" | |
19 #include "chrome/browser/signin/token_service.h" | |
20 #include "chrome/browser/signin/token_service_factory.h" | |
21 #include "chrome/browser/sync/about_sync_util.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_prefs.h" | |
25 #include "chrome/browser/sync/sync_ui_util.h" | |
26 #include "chrome/common/chrome_notification_types.h" | |
27 #include "chrome/common/pref_names.h" | |
28 #include "content/public/browser/notification_service.h" | |
29 #include "google/cacheinvalidation/types.pb.h" | |
30 #include "google_apis/gaia/gaia_constants.h" | |
31 #include "google_apis/gaia/google_service_auth_error.h" | |
32 #include "grit/generated_resources.h" | |
33 #include "jni/ProfileSyncService_jni.h" | |
34 #include "sync/internal_api/public/base/model_type_invalidation_map.h" | |
35 #include "sync/internal_api/public/read_transaction.h" | |
36 #include "ui/base/l10n/l10n_util.h" | |
37 | |
38 using base::android::AttachCurrentThread; | |
39 using base::android::CheckException; | |
40 using base::android::ConvertJavaStringToUTF8; | |
41 using base::android::ConvertUTF8ToJavaString; | |
42 using base::android::ScopedJavaLocalRef; | |
43 using content::BrowserThread; | |
44 | |
45 namespace { | |
46 const char kSyncDisabledStatus[] = "OFFLINE_DISABLED"; | |
47 } // namespace | |
48 | |
49 ProfileSyncServiceAndroid::ProfileSyncServiceAndroid(JNIEnv* env, jobject obj) | |
50 : weak_java_profile_sync_service_(env, obj) { | |
51 profile_ = NULL; | |
52 sync_service_ = NULL; | |
53 if (g_browser_process == NULL || | |
54 g_browser_process->profile_manager() == NULL) { | |
55 NOTREACHED() << "Browser process or profile manager not initialized"; | |
56 return; | |
57 } | |
58 | |
59 profile_ = g_browser_process->profile_manager()->GetDefaultProfile(); | |
60 if (profile_ == NULL) { | |
61 NOTREACHED() << "Sync Init: Profile not found."; | |
62 return; | |
63 } | |
64 | |
65 sync_service_ = | |
66 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); | |
67 DCHECK(sync_service_); | |
68 AddObserver(); | |
69 } | |
70 | |
71 void ProfileSyncServiceAndroid::AddObserver() { | |
72 if (!sync_service_->HasObserver(this)) { | |
Yaron
2013/02/27 23:10:25
only called in ctor, remove guard. Also, the other
nyquist
2013/02/28 22:38:50
Done.
| |
73 sync_service_->AddObserver(this); | |
74 } | |
75 | |
76 std::string signed_in_username = | |
77 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); | |
78 if (!signed_in_username.empty()) { | |
79 // If the user is logged in, see if he has a valid token - if not, fetch | |
80 // a new one. | |
81 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
82 if (!token_service->HasTokenForService(GaiaConstants::kSyncService) || | |
83 (sync_service_->GetAuthError().state() == | |
84 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS)) { | |
85 DVLOG(2) << "Trying to update token for user " << signed_in_username; | |
86 InvalidateAuthToken(); | |
87 } | |
88 } | |
89 } | |
90 | |
91 void ProfileSyncServiceAndroid::RemoveObserver() { | |
92 if (sync_service_->HasObserver(this)) { | |
93 sync_service_->RemoveObserver(this); | |
94 } | |
95 } | |
96 | |
97 ProfileSyncServiceAndroid::~ProfileSyncServiceAndroid() { | |
98 RemoveObserver(); | |
99 } | |
100 | |
101 void ProfileSyncServiceAndroid::Destroy(JNIEnv* env, jobject obj) { | |
102 delete this; | |
103 } | |
104 | |
105 // Must be called on the UI thread. | |
Yaron
2013/02/27 23:10:25
remove comment (asserted on first line)
nyquist
2013/02/28 22:38:50
Done.
| |
106 void ProfileSyncServiceAndroid::SendNudgeNotification( | |
107 const std::string& str_object_id, | |
108 int64 version, | |
109 const std::string& state) { | |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
111 | |
112 // TODO(nileshagrawal): Merge this with ChromeInvalidationClient::Invalidate. | |
113 // Construct the ModelTypeStateMap and send it over with the notification. | |
114 syncer::ModelType model_type; | |
115 if (!syncer::NotificationTypeToRealModelType(str_object_id, &model_type)) { | |
116 DVLOG(1) << "Could not get invalidation model type; " | |
117 << "Sending notification with empty state map."; | |
118 syncer::ModelTypeInvalidationMap model_types_with_states; | |
119 content::NotificationService::current()->Notify( | |
120 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | |
121 content::Source<Profile>(profile_), | |
122 content::Details<const syncer::ModelTypeInvalidationMap>( | |
123 &model_types_with_states)); | |
124 return; | |
125 } | |
126 | |
127 if (version != ipc::invalidation::Constants::UNKNOWN) { | |
128 std::map<syncer::ModelType, int64>::const_iterator it = | |
129 max_invalidation_versions_.find(model_type); | |
130 if ((it != max_invalidation_versions_.end()) && | |
131 (version <= it->second)) { | |
132 DVLOG(1) << "Dropping redundant invalidation with version " << version; | |
133 return; | |
134 } | |
135 max_invalidation_versions_[model_type] = version; | |
136 } | |
137 | |
138 syncer::ModelTypeSet types; | |
139 types.Put(model_type); | |
140 syncer::ModelTypeInvalidationMap model_types_with_states = | |
141 syncer::ModelTypeSetToInvalidationMap(types, state); | |
142 | |
143 content::NotificationService::current()->Notify( | |
144 chrome::NOTIFICATION_SYNC_REFRESH_REMOTE, | |
145 content::Source<Profile>(profile_), | |
146 content::Details<const syncer::ModelTypeInvalidationMap>( | |
147 &model_types_with_states)); | |
148 } | |
149 | |
150 | |
151 void ProfileSyncServiceAndroid::OnStateChanged() { | |
152 // Check for auth errors. | |
153 const GoogleServiceAuthError& auth_error = sync_service_->GetAuthError(); | |
154 if (auth_error.state() == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS) { | |
155 DVLOG(2) << "Updating auth token."; | |
156 InvalidateAuthToken(); | |
157 } | |
158 | |
159 // Notify the java world that our sync state has changed. | |
160 JNIEnv* env = AttachCurrentThread(); | |
161 Java_ProfileSyncService_syncStateChanged( | |
162 env, weak_java_profile_sync_service_.get(env).obj()); | |
163 } | |
164 | |
165 void ProfileSyncServiceAndroid::TokenAvailable( | |
166 JNIEnv* env, jobject, jstring username, jstring auth_token) { | |
167 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
168 TokenServiceFactory::GetForProfile(profile_)->OnIssueAuthTokenSuccess( | |
169 GaiaConstants::kSyncService, token); | |
170 } | |
171 | |
172 void ProfileSyncServiceAndroid::EnableSync(JNIEnv* env, jobject) { | |
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
174 // Don't need to do anything if we're already enabled. | |
175 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | |
176 if (prefs.IsStartSuppressed()) | |
177 sync_service_->UnsuppressAndStart(); | |
178 else | |
179 DVLOG(2) << "Ignoring call to EnableSync() because sync is already enabled"; | |
180 } | |
181 | |
182 void ProfileSyncServiceAndroid::DisableSync(JNIEnv* env, jobject) { | |
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
184 sync_service_->StopAndSuppress(); | |
185 } | |
186 | |
187 void ProfileSyncServiceAndroid::SignInSync( | |
188 JNIEnv* env, jobject, jstring username, jstring auth_token) { | |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
190 // Just return if sync already has everything it needs to start up (sync | |
191 // should start up automatically as long as it has credentials). This can | |
192 // happen normally if (for example) the user closes and reopens the sync | |
193 // settings window quickly during initial startup. | |
194 if (sync_service_->IsSyncEnabledAndLoggedIn() && | |
195 sync_service_->IsSyncTokenAvailable() && | |
196 sync_service_->HasSyncSetupCompleted()) | |
197 return; | |
198 | |
199 if (!sync_service_->IsSyncEnabledAndLoggedIn() || | |
200 !sync_service_->IsSyncTokenAvailable()) { | |
201 // Set the currently-signed-in username, fetch an auth token if necessary, | |
202 // and enable sync. | |
203 std::string name = ConvertJavaStringToUTF8(env, username); | |
204 SigninManagerFactory::GetForProfile(profile_)-> | |
205 SetAuthenticatedUsername(name); | |
206 std::string token = ConvertJavaStringToUTF8(env, auth_token); | |
207 if (token.empty()) { | |
208 // No credentials passed in - request an auth token. | |
209 // If fetching the auth token is successful, this will cause | |
210 // ProfileSyncService to start sync when it receives | |
211 // NOTIFICATION_TOKEN_AVAILABLE. | |
212 DVLOG(2) << "Fetching auth token for " << name; | |
213 InvalidateAuthToken(); | |
214 } else { | |
215 // OnIssueAuthTokenSuccess will send out a notification to the sync | |
216 // service that will cause the sync backend to initialize. | |
217 TokenService* token_service = | |
218 TokenServiceFactory::GetForProfile(profile_); | |
219 token_service->OnIssueAuthTokenSuccess(GaiaConstants::kSyncService, | |
220 token); | |
221 } | |
222 } | |
223 | |
224 // Enable sync (if we don't have credentials yet, this will enable sync but | |
225 // will not start it up - sync will start once credentials arrive). | |
226 sync_service_->UnsuppressAndStart(); | |
227 } | |
228 | |
229 void ProfileSyncServiceAndroid::SignOutSync(JNIEnv* env, jobject) { | |
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
231 DCHECK(profile_); | |
232 sync_service_->DisableForUser(); | |
233 | |
234 // Clear the tokens. | |
235 SigninManagerFactory::GetForProfile(profile_)->SignOut(); | |
236 | |
237 // Need to clear suppress start flag manually | |
238 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | |
239 prefs.SetStartSuppressed(false); | |
240 } | |
241 | |
242 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::QuerySyncStatusSummary( | |
243 JNIEnv* env, jobject) { | |
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
245 DCHECK(profile_); | |
246 std::string status(sync_service_->QuerySyncStatusSummary()); | |
247 return ConvertUTF8ToJavaString(env, status); | |
248 } | |
249 | |
250 jboolean ProfileSyncServiceAndroid::SetSyncSessionsId( | |
251 JNIEnv* env, jobject obj, jstring tag) { | |
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
253 DCHECK(profile_); | |
254 std::string machine_tag = ConvertJavaStringToUTF8(env, tag); | |
255 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | |
256 prefs.SetSyncSessionsGUID(machine_tag); | |
257 return true; | |
258 } | |
259 | |
260 jint ProfileSyncServiceAndroid::GetAuthError(JNIEnv* env, jobject) { | |
261 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
262 return sync_service_->GetAuthError().state(); | |
263 } | |
264 | |
265 jboolean ProfileSyncServiceAndroid::IsEncryptEverythingEnabled( | |
266 JNIEnv* env, jobject) { | |
267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
268 return sync_service_->EncryptEverythingEnabled(); | |
269 } | |
270 | |
271 jboolean ProfileSyncServiceAndroid::IsSyncInitialized(JNIEnv* env, jobject) { | |
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
273 return sync_service_->sync_initialized(); | |
274 } | |
275 | |
276 jboolean ProfileSyncServiceAndroid::IsFirstSetupInProgress( | |
277 JNIEnv* env, jobject) { | |
278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
279 return sync_service_->FirstSetupInProgress(); | |
280 } | |
281 | |
282 jboolean ProfileSyncServiceAndroid::IsPassphraseRequired(JNIEnv* env, jobject) { | |
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
284 return sync_service_->IsPassphraseRequired(); | |
285 } | |
286 | |
287 jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForDecryption( | |
288 JNIEnv* env, jobject obj) { | |
289 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
290 // In case of CUSTOM_PASSPHRASE we always sync passwords. Prompt the user for | |
291 // a passphrase if cryptographer has any pending keys. | |
292 if (sync_service_->GetPassphraseType() == syncer::CUSTOM_PASSPHRASE) { | |
293 return !IsCryptographerReady(env, obj); | |
294 } | |
295 if (sync_service_->IsPassphraseRequiredForDecryption()) { | |
296 // Passwords datatype should never prompt for a passphrase, except when | |
297 // user is using a custom passphrase. Do not prompt for a passphrase if | |
298 // passwords are the only encrypted datatype. This prevents a temporary | |
299 // notification for passphrase when PSS has not completed configuring | |
300 // DataTypeManager, after configuration password datatype shall be disabled. | |
301 const syncer::ModelTypeSet encrypted_types = | |
302 sync_service_->GetEncryptedDataTypes(); | |
303 const bool are_passwords_the_only_encrypted_type = | |
304 encrypted_types.Has(syncer::PASSWORDS) && encrypted_types.Size() == 1 && | |
305 !sync_service_->ShouldEnablePasswordSyncForAndroid(); | |
306 return !are_passwords_the_only_encrypted_type; | |
307 } | |
308 return false; | |
309 } | |
310 | |
311 jboolean ProfileSyncServiceAndroid::IsPassphraseRequiredForExternalType( | |
312 JNIEnv* env, jobject) { | |
313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
314 return | |
315 sync_service_->passphrase_required_reason() == syncer::REASON_DECRYPTION; | |
316 } | |
317 | |
318 jboolean ProfileSyncServiceAndroid::IsUsingSecondaryPassphrase( | |
319 JNIEnv* env, jobject) { | |
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
321 return sync_service_->IsUsingSecondaryPassphrase(); | |
322 } | |
323 | |
324 jboolean ProfileSyncServiceAndroid::SetDecryptionPassphrase( | |
325 JNIEnv* env, jobject obj, jstring passphrase) { | |
326 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
327 std::string key = ConvertJavaStringToUTF8(env, passphrase); | |
328 return sync_service_->SetDecryptionPassphrase(key); | |
329 } | |
330 | |
331 void ProfileSyncServiceAndroid::SetEncryptionPassphrase( | |
332 JNIEnv* env, jobject obj, jstring passphrase, jboolean is_gaia) { | |
333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
334 std::string key = ConvertJavaStringToUTF8(env, passphrase); | |
335 sync_service_->SetEncryptionPassphrase( | |
336 key, | |
337 is_gaia ? ProfileSyncService::IMPLICIT : ProfileSyncService::EXPLICIT); | |
338 } | |
339 | |
340 jboolean ProfileSyncServiceAndroid::IsCryptographerReady(JNIEnv* env, jobject) { | |
341 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | |
342 return sync_service_->IsCryptographerReady(&trans); | |
343 } | |
344 | |
345 jint ProfileSyncServiceAndroid::GetPassphraseType(JNIEnv* env, jobject) { | |
346 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
347 return sync_service_->GetPassphraseType(); | |
348 } | |
349 | |
350 jboolean ProfileSyncServiceAndroid::HasExplicitPassphraseTime( | |
351 JNIEnv* env, jobject) { | |
352 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
353 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
354 return !passphrase_time.is_null(); | |
355 } | |
356 | |
357 ScopedJavaLocalRef<jstring> | |
358 ProfileSyncServiceAndroid::GetSyncEnterGooglePassphraseBodyWithDateText( | |
359 JNIEnv* env, jobject) { | |
360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
361 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
362 string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); | |
363 return base::android::ConvertUTF16ToJavaString(env, | |
364 l10n_util::GetStringFUTF16( | |
365 IDS_SYNC_ENTER_GOOGLE_PASSPHRASE_BODY_WITH_DATE, | |
366 passphrase_time_str)); | |
367 } | |
368 | |
369 ScopedJavaLocalRef<jstring> | |
370 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyWithDateText( | |
371 JNIEnv* env, jobject) { | |
372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
373 base::Time passphrase_time = sync_service_->GetExplicitPassphraseTime(); | |
374 string16 passphrase_time_str = base::TimeFormatShortDate(passphrase_time); | |
375 return base::android::ConvertUTF16ToJavaString(env, | |
376 l10n_util::GetStringFUTF16(IDS_SYNC_ENTER_PASSPHRASE_BODY_WITH_DATE, | |
377 passphrase_time_str)); | |
378 } | |
379 | |
380 static jstring GetSyncEnterCustomPassphraseBodyText(JNIEnv* env, jclass clazz) { | |
Yaron
2013/02/27 23:10:25
Nit: convert to member function (this becomes a sc
| |
381 return ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( | |
382 env, clazz).Release(); | |
383 } | |
384 | |
385 ScopedJavaLocalRef<jstring> | |
386 ProfileSyncServiceAndroid::GetSyncEnterCustomPassphraseBodyText( | |
387 JNIEnv* env, jclass) { | |
388 return ConvertUTF8ToJavaString( | |
389 env, l10n_util::GetStringUTF8(IDS_SYNC_ENTER_PASSPHRASE_BODY)); | |
390 } | |
391 | |
392 jboolean ProfileSyncServiceAndroid::IsMigrated(JNIEnv* env, jobject) { | |
393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
394 syncer::SyncStatus status; | |
395 bool is_status_valid = sync_service_->QueryDetailedSyncStatus(&status); | |
396 return is_status_valid && !status.keystore_migration_time.is_null(); | |
397 } | |
398 | |
399 void ProfileSyncServiceAndroid::SetPreferredDataTypes( | |
400 JNIEnv* env, jobject obj, | |
401 jboolean sync_everything, | |
402 jboolean sync_autofill, | |
403 jboolean sync_bookmarks, | |
404 jboolean sync_passwords, | |
405 jboolean sync_sessions, | |
406 jboolean sync_typed_urls) { | |
407 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
408 syncer::ModelTypeSet types; | |
409 if (sync_autofill) | |
410 types.Put(syncer::AUTOFILL); | |
411 if (sync_bookmarks) | |
412 types.Put(syncer::BOOKMARKS); | |
413 if (sync_passwords) | |
414 types.Put(syncer::PASSWORDS); | |
415 if (sync_sessions) | |
416 types.Put(syncer::SESSIONS); | |
417 if (sync_typed_urls) | |
418 types.Put(syncer::TYPED_URLS); | |
419 sync_service_->OnUserChoseDatatypes(sync_everything, types); | |
420 } | |
421 | |
422 void ProfileSyncServiceAndroid::SetSetupInProgress( | |
423 JNIEnv* env, jobject obj, jboolean in_progress) { | |
424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
425 sync_service_->SetSetupInProgress(in_progress); | |
426 } | |
427 | |
428 void ProfileSyncServiceAndroid::SetSyncSetupCompleted( | |
429 JNIEnv* env, jobject obj) { | |
430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
431 sync_service_->SetSyncSetupCompleted(); | |
432 } | |
433 | |
434 jboolean ProfileSyncServiceAndroid::HasSyncSetupCompleted( | |
435 JNIEnv* env, jobject obj) { | |
436 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
437 return sync_service_->HasSyncSetupCompleted(); | |
438 } | |
439 | |
440 void ProfileSyncServiceAndroid::EnableEncryptEverything( | |
441 JNIEnv* env, jobject obj) { | |
442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
443 sync_service_->EnableEncryptEverything(); | |
444 } | |
445 | |
446 jboolean ProfileSyncServiceAndroid::HasKeepEverythingSynced( | |
447 JNIEnv* env, jobject) { | |
448 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
449 browser_sync::SyncPrefs prefs(profile_->GetPrefs()); | |
450 return prefs.HasKeepEverythingSynced(); | |
451 } | |
452 | |
453 jboolean ProfileSyncServiceAndroid::IsAutofillSyncEnabled( | |
454 JNIEnv* env, jobject obj) { | |
455 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
456 return HasKeepEverythingSynced(env, obj) || | |
457 sync_service_->GetPreferredDataTypes().Has(syncer::AUTOFILL); | |
458 } | |
459 | |
460 jboolean ProfileSyncServiceAndroid::IsBookmarkSyncEnabled( | |
461 JNIEnv* env, jobject obj) { | |
462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
463 return HasKeepEverythingSynced(env, obj) || | |
464 sync_service_->GetPreferredDataTypes().Has(syncer::BOOKMARKS); | |
465 } | |
466 | |
467 jboolean ProfileSyncServiceAndroid::IsPasswordSyncEnabled( | |
468 JNIEnv* env, jobject obj) { | |
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
470 return HasKeepEverythingSynced(env, obj) || | |
471 sync_service_->GetPreferredDataTypes().Has(syncer::PASSWORDS); | |
472 } | |
473 | |
474 jboolean ProfileSyncServiceAndroid::IsTypedUrlSyncEnabled( | |
475 JNIEnv* env, jobject obj) { | |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
477 return HasKeepEverythingSynced(env, obj) || | |
478 sync_service_->GetPreferredDataTypes().Has(syncer::TYPED_URLS); | |
479 } | |
480 | |
481 jboolean ProfileSyncServiceAndroid::IsSessionSyncEnabled( | |
482 JNIEnv* env, jobject obj) { | |
483 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
484 return HasKeepEverythingSynced(env, obj) || | |
485 sync_service_->GetPreferredDataTypes().Has(syncer::SESSIONS); | |
486 } | |
487 | |
488 jboolean ProfileSyncServiceAndroid::HasUnrecoverableError( | |
489 JNIEnv* env, jobject) { | |
490 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
491 return sync_service_->HasUnrecoverableError(); | |
492 } | |
493 | |
494 ScopedJavaLocalRef<jstring> ProfileSyncServiceAndroid::GetAboutInfoForTest( | |
495 JNIEnv* env, jobject) { | |
496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
497 | |
498 scoped_ptr<DictionaryValue> about_info = | |
499 sync_ui_util::ConstructAboutInformation(sync_service_); | |
500 std::string about_info_json; | |
501 base::JSONWriter::Write(about_info.get(), &about_info_json); | |
502 | |
503 return ConvertUTF8ToJavaString(env, about_info_json); | |
504 } | |
505 | |
506 void ProfileSyncServiceAndroid::InvalidateAuthToken() { | |
Yaron
2013/02/27 23:10:25
In theory this could be moved to TokenServiceAndro
nyquist
2013/02/28 22:38:50
Yes, we should move auth token and possibly part o
| |
507 // Get the token from token-db. If there's no token yet, this must be the | |
508 // the first time the user is signing in so we don't need to invalidate | |
509 // anything. | |
510 TokenService* token_service = TokenServiceFactory::GetForProfile(profile_); | |
511 std::string invalid_token; | |
512 if (token_service->HasTokenForService(GaiaConstants::kSyncService)) { | |
513 invalid_token = token_service->GetTokenForService( | |
514 GaiaConstants::kSyncService); | |
515 } | |
516 const std::string& sync_username = | |
517 SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername(); | |
518 // Call into java to invalidate the current token and get a new one. | |
519 JNIEnv* env = AttachCurrentThread(); | |
520 ScopedJavaLocalRef<jstring> j_sync_username = | |
521 ConvertUTF8ToJavaString(env, sync_username); | |
522 ScopedJavaLocalRef<jstring> j_invalid_token = | |
523 ConvertUTF8ToJavaString(env, invalid_token); | |
524 Java_ProfileSyncService_getNewAuthToken( | |
525 env, weak_java_profile_sync_service_.get(env).obj(), | |
526 j_sync_username.obj(), j_invalid_token.obj()); | |
527 } | |
528 | |
529 void ProfileSyncServiceAndroid::NudgeSyncer(JNIEnv* env, | |
530 jobject obj, | |
Yaron
2013/02/27 23:10:25
nit: indentation off
nyquist
2013/02/28 22:38:50
Done.
| |
531 jstring objectId, | |
532 jlong version, | |
533 jstring state) { | |
534 if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
535 SendNudgeNotification(ConvertJavaStringToUTF8(env, objectId), version, | |
536 ConvertJavaStringToUTF8(env, state)); | |
537 } else { | |
538 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
539 base::Bind(&ProfileSyncServiceAndroid::SendNudgeNotification, | |
540 base::Unretained(this), /* this will not go away. */ | |
541 ConvertJavaStringToUTF8(env, objectId), version, | |
542 ConvertJavaStringToUTF8(env, state))); | |
543 } | |
544 } | |
545 | |
546 static int Init(JNIEnv* env, jobject obj) { | |
547 ProfileSyncServiceAndroid* profile_sync_service_android = | |
548 new ProfileSyncServiceAndroid(env, obj); | |
549 return reinterpret_cast<jint>(profile_sync_service_android); | |
550 } | |
551 | |
552 // static | |
553 bool ProfileSyncServiceAndroid::Register(JNIEnv* env) { | |
554 return RegisterNativesImpl(env); | |
555 } | |
OLD | NEW |