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