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 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_
6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_
11 #include "base/android/jni_weak_ref.h"
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/time/time.h"
15 #include "components/invalidation/invalidation_util.h"
16 #include "components/sync_driver/sync_prefs.h"
17 #include "components/sync_driver/sync_service_observer.h"
18 #include "google/cacheinvalidation/include/types.h"
19 #include "google_apis/gaia/google_service_auth_error.h"
22 class ProfileSyncService
;
24 // Android wrapper of the ProfileSyncService which provides access from the Java
25 // layer. Note that on Android, there's only a single profile, and therefore
26 // a single instance of this wrapper. The name of the Java class is
27 // ProfileSyncService.
28 // This class should only be accessed from the UI thread.
29 class ProfileSyncServiceAndroid
: public sync_driver::SyncServiceObserver
{
32 ProfileSyncServiceAndroid(JNIEnv
* env
, jobject obj
);
34 // This method should be called once right after contructing the object.
37 // Called from Java when the user manually enables sync
38 void EnableSync(JNIEnv
* env
, jobject obj
);
40 // Called from Java when the user manually disables sync
41 void DisableSync(JNIEnv
* env
, jobject obj
);
43 // Called from Java when the user signs in to Chrome. Starts up sync.
44 void SignInSync(JNIEnv
* env
, jobject obj
);
46 // Called from Java when the user signs out of Chrome
47 void SignOutSync(JNIEnv
* env
, jobject obj
);
49 // Called from Java when we get a signal that the Directory should be saved.
50 void FlushDirectory(JNIEnv
* env
, jobject obj
);
52 // Returns a string version of browser_sync::SyncBackendHost::StatusSummary
53 base::android::ScopedJavaLocalRef
<jstring
> QuerySyncStatusSummary(
54 JNIEnv
* env
, jobject obj
);
56 // Called from Java early during startup to ensure we use the correct
57 // unique machine tag in session sync. Returns true if the machine tag was
59 // This must be called before the |SessionModelAssociator| is initialized.
60 jboolean
SetSyncSessionsId(JNIEnv
* env
, jobject obj
, jstring tag
);
62 // Returns true if the sync backend is initialized.
63 jboolean
IsSyncInitialized(JNIEnv
* env
, jobject obj
);
65 // Returns true if the sync is currently being setup for the first time.
66 jboolean
IsFirstSetupInProgress(JNIEnv
* env
, jobject obj
);
68 // Returns true if encrypting everything is allowed.
69 jboolean
IsEncryptEverythingAllowed(JNIEnv
* env
, jobject obj
);
71 // Returns true if the user is currently encrypting everything.
72 jboolean
IsEncryptEverythingEnabled(JNIEnv
* env
, jobject obj
);
74 // Returns true if the sync code needs a passphrase for either encryption or
75 // decryption (can need a passphrase for encryption if the user is turning on
76 // encryption and no passphrase has been set yet).
77 jboolean
IsPassphraseRequired(JNIEnv
* env
, jobject obj
);
79 // Returns true if the sync code needs a decryption passphrase for one of the
80 // currently enabled types.
81 jboolean
IsPassphraseRequiredForDecryption(JNIEnv
* env
, jobject obj
);
83 // Returns true if the sync code needs a decryption passphrase for *any* type,
84 // even types that aren't supported on this platform (like passwords).
85 jboolean
IsPassphraseRequiredForExternalType(JNIEnv
* env
, jobject obj
);
87 // Returns true if the sync code needs a custom decryption passphrase.
88 // Can not be called if the sync backend is not initialized.
89 jboolean
IsUsingSecondaryPassphrase(JNIEnv
* env
, jobject obj
);
91 // Returns true if the decryption passphrase works (was able to decrypt the
92 // stored sync data). Should only be called if
93 // IsPassphraseRequiredForDecryption() returns true.
94 jboolean
SetDecryptionPassphrase(JNIEnv
* env
,
98 // Encrypts the user's data with the passed passphrase. If |is_gaia| == true
99 // then the passphrase is treated as a google (GAIA) passphrase, otherwise
100 // it's treated like an explicit/custom passphrase.
101 void SetEncryptionPassphrase(JNIEnv
* env
,
106 // Returns whether the cryptographer is ready (i.e. encrypted types can be
108 jboolean
IsCryptographerReady(JNIEnv
* env
, jobject
);
110 // Returns the actual passphrase type being used for encryption. This is a
111 // value from the enum defined in syncer::PassphraseType and must be matched
113 jint
GetPassphraseType(JNIEnv
* env
, jobject obj
);
115 // Returns true if the current explicit passphrase time is defined.
116 jboolean
HasExplicitPassphraseTime(JNIEnv
* env
, jobject
);
118 // Returns the current explicit passphrase time.
119 jlong
GetExplicitPassphraseTime(JNIEnv
* env
, jobject
);
121 base::android::ScopedJavaLocalRef
<jstring
>
122 GetSyncEnterGooglePassphraseBodyWithDateText(
123 JNIEnv
* env
, jobject
);
125 base::android::ScopedJavaLocalRef
<jstring
>
126 GetSyncEnterCustomPassphraseBodyWithDateText(
127 JNIEnv
* env
, jobject
);
129 base::android::ScopedJavaLocalRef
<jstring
>
130 GetCurrentSignedInAccountText(
131 JNIEnv
* env
, jobject
);
133 base::android::ScopedJavaLocalRef
<jstring
>
134 GetSyncEnterCustomPassphraseBodyText(
135 JNIEnv
* env
, jobject
);
137 // Returns true if sync has been migrated.
138 jboolean
IsSyncKeystoreMigrationDone(JNIEnv
* env
, jobject obj
);
140 // Get the set of active data types. These are the types currently being
141 // synced. Note that control types are always included here.
142 // Returns a bit map of the values from
143 // profile_sync_service_model_type_selection_android.h.
144 jlong
GetActiveDataTypes(JNIEnv
* env
, jobject obj
);
146 // Get the set of preferred data types. These are the types that the user
147 // has requested be synced.
148 // Returns a bit map of the values from
149 // profile_sync_service_model_type_selection_android.h.
150 jlong
GetPreferredDataTypes(JNIEnv
* env
, jobject obj
);
152 // Enables the passed data types.
153 // If |sync_everything| is true, then all data types are enabled and the
154 // contents of |model_type_selection| is
156 // Otherwise, the values of |model_type_selection| must contain a bit map of
157 // values from profile_sync_service_model_type_selection_android.h.
158 void SetPreferredDataTypes(JNIEnv
* env
,
160 jboolean sync_everything
,
161 jlong model_type_selection
);
163 // Tells sync that we're currently configuring so no data types should be
165 void SetSetupInProgress(JNIEnv
* env
, jobject obj
, jboolean in_progress
);
167 // Tells sync that sync setup is complete so we can start syncing now.
168 void SetSyncSetupCompleted(JNIEnv
* env
, jobject obj
);
170 // Returns true if sync setup has been completed.
171 jboolean
HasSyncSetupCompleted(JNIEnv
* env
, jobject obj
);
173 // Returns true if sync startup is currently suppressed.
174 jboolean
IsStartSuppressed(JNIEnv
* env
, jobject obj
);
176 // Returns true if sync is configured to "sync everything".
177 jboolean
HasKeepEverythingSynced(JNIEnv
* env
, jobject obj
);
179 // Turns on encryption for all data types. This is an asynchronous operation
180 // which happens after the current configuration pass is done, so a call to
181 // this routine must be followed by a call to SetEnabledDataTypes().
182 void EnableEncryptEverything(JNIEnv
* env
, jobject obj
);
184 // Returns true if sync has encountered an unrecoverable error.
185 jboolean
HasUnrecoverableError(JNIEnv
* env
, jobject obj
);
187 // Returns sync internals in a JSON-formatted Java string.
188 base::android::ScopedJavaLocalRef
<jstring
> GetAboutInfoForTest(JNIEnv
* env
,
191 // Returns the integer value corresponding to the current auth error state
192 // (GoogleServiceAuthError.State).
193 jint
GetAuthError(JNIEnv
* env
, jobject obj
);
195 // sync_driver::SyncServiceObserver:
196 void OnStateChanged() override
;
198 // Returns a timestamp for when a sync was last executed. The return value is
199 // the internal value of base::Time.
200 jlong
GetLastSyncedTimeForTest(JNIEnv
* env
, jobject obj
);
202 // Overrides ProfileSyncService's NetworkResources object. This is used to
203 // set up the Sync FakeServer for testing.
204 void OverrideNetworkResourcesForTest(JNIEnv
* env
,
206 jlong network_resources
);
209 static jlong
ModelTypeSetToSelection(syncer::ModelTypeSet model_types
);
211 // Converts a bitmap of model types to a set of Java ModelTypes, and returns
212 // their string descriptions separated by commas.
213 static std::string
ModelTypeSelectionToStringForTest(
214 jlong model_type_selection
);
216 static ProfileSyncServiceAndroid
* GetProfileSyncServiceAndroid();
218 // Registers the ProfileSyncServiceAndroid's native methods through JNI.
219 static bool Register(JNIEnv
* env
);
222 typedef std::map
<invalidation::ObjectId
,
224 syncer::ObjectIdLessThan
> ObjectIdVersionMap
;
226 ~ProfileSyncServiceAndroid() override
;
227 // Remove observers to profile sync service.
228 void RemoveObserver();
231 ProfileSyncService
* sync_service_
;
232 // The class that handles getting, setting, and persisting sync
234 scoped_ptr
<sync_driver::SyncPrefs
> sync_prefs_
;
236 // Java-side ProfileSyncService object.
237 JavaObjectWeakGlobalRef weak_java_profile_sync_service_
;
239 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceAndroid
);
242 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_