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 "chrome/browser/sync/profile_sync_service_observer.h"
16 #include "components/invalidation/invalidation_util.h"
17 #include "components/sync_driver/sync_prefs.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 ProfileSyncServiceObserver
{
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 the user is currently encrypting everything.
69 jboolean
IsEncryptEverythingEnabled(JNIEnv
* env
, jobject obj
);
71 // Returns true if the sync code needs a passphrase for either encryption or
72 // decryption (can need a passphrase for encryption if the user is turning on
73 // encryption and no passphrase has been set yet).
74 jboolean
IsPassphraseRequired(JNIEnv
* env
, jobject obj
);
76 // Returns true if the sync code needs a decryption passphrase for one of the
77 // currently enabled types.
78 jboolean
IsPassphraseRequiredForDecryption(JNIEnv
* env
, jobject obj
);
80 // Returns true if the sync code needs a decryption passphrase for *any* type,
81 // even types that aren't supported on this platform (like passwords).
82 jboolean
IsPassphraseRequiredForExternalType(JNIEnv
* env
, jobject obj
);
84 // Returns true if the sync code needs a custom decryption passphrase.
85 // Can not be called if the sync backend is not initialized.
86 jboolean
IsUsingSecondaryPassphrase(JNIEnv
* env
, jobject obj
);
88 // Returns true if the decryption passphrase works (was able to decrypt the
89 // stored sync data). Should only be called if
90 // IsPassphraseRequiredForDecryption() returns true.
91 jboolean
SetDecryptionPassphrase(JNIEnv
* env
,
95 // Encrypts the user's data with the passed passphrase. If |is_gaia| == true
96 // then the passphrase is treated as a google (GAIA) passphrase, otherwise
97 // it's treated like an explicit/custom passphrase.
98 void SetEncryptionPassphrase(JNIEnv
* env
,
103 // Returns whether the cryptographer is ready (i.e. encrypted types can be
105 jboolean
IsCryptographerReady(JNIEnv
* env
, jobject
);
107 // Returns the actual passphrase type being used for encryption. This is a
108 // value from the enum defined in syncer::PassphraseType and must be matched
110 jint
GetPassphraseType(JNIEnv
* env
, jobject obj
);
112 // Returns true if the current explicit passphrase time is defined.
113 jboolean
HasExplicitPassphraseTime(JNIEnv
* env
, jobject
);
115 // Returns the current explicit passphrase time.
116 jlong
GetExplicitPassphraseTime(JNIEnv
* env
, jobject
);
118 base::android::ScopedJavaLocalRef
<jstring
>
119 GetSyncEnterGooglePassphraseBodyWithDateText(
120 JNIEnv
* env
, jobject
);
122 base::android::ScopedJavaLocalRef
<jstring
>
123 GetSyncEnterCustomPassphraseBodyWithDateText(
124 JNIEnv
* env
, jobject
);
126 base::android::ScopedJavaLocalRef
<jstring
>
127 GetCurrentSignedInAccountText(
128 JNIEnv
* env
, jobject
);
130 base::android::ScopedJavaLocalRef
<jstring
>
131 GetSyncEnterCustomPassphraseBodyText(
132 JNIEnv
* env
, jobject
);
134 // Returns true if sync has been migrated.
135 jboolean
IsSyncKeystoreMigrationDone(JNIEnv
* env
, jobject obj
);
137 // Get the set of enabled data types. These are the types currently both
138 // registered and preferred. Note that control types are always included here.
139 // Returns a bit map of the values from
140 // profile_sync_service_model_type_selection_android.h.
141 jlong
GetEnabledDataTypes(JNIEnv
* env
, jobject obj
);
143 // Enables the passed data types.
144 // If |sync_everything| is true, then all data types are enabled and the
145 // contents of |model_type_selection| is
147 // Otherwise, the values of |model_type_selection| must contain a bit map of
148 // values from profile_sync_service_model_type_selection_android.h.
149 void SetPreferredDataTypes(JNIEnv
* env
,
151 jboolean sync_everything
,
152 jlong model_type_selection
);
154 // Tells sync that we're currently configuring so no data types should be
156 void SetSetupInProgress(JNIEnv
* env
, jobject obj
, jboolean in_progress
);
158 // Tells sync that sync setup is complete so we can start syncing now.
159 void SetSyncSetupCompleted(JNIEnv
* env
, jobject obj
);
161 // Returns true if sync setup has been completed.
162 jboolean
HasSyncSetupCompleted(JNIEnv
* env
, jobject obj
);
164 // Returns true if sync startup is currently suppressed.
165 jboolean
IsStartSuppressed(JNIEnv
* env
, jobject obj
);
167 // Returns true if sync is configured to "sync everything".
168 jboolean
HasKeepEverythingSynced(JNIEnv
* env
, jobject obj
);
170 // Turns on encryption for all data types. This is an asynchronous operation
171 // which happens after the current configuration pass is done, so a call to
172 // this routine must be followed by a call to SetEnabledDataTypes().
173 void EnableEncryptEverything(JNIEnv
* env
, jobject obj
);
175 // Returns true if sync has encountered an unrecoverable error.
176 jboolean
HasUnrecoverableError(JNIEnv
* env
, jobject obj
);
178 // Returns sync internals in a JSON-formatted Java string.
179 base::android::ScopedJavaLocalRef
<jstring
> GetAboutInfoForTest(JNIEnv
* env
,
182 // Returns the integer value corresponding to the current auth error state
183 // (GoogleServiceAuthError.State).
184 jint
GetAuthError(JNIEnv
* env
, jobject obj
);
186 // ProfileSyncServiceObserver:
187 virtual void OnStateChanged() override
;
189 // Returns a timestamp for when a sync was last executed. The return value is
190 // the internal value of base::Time.
191 jlong
GetLastSyncedTimeForTest(JNIEnv
* env
, jobject obj
);
193 // Overrides ProfileSyncService's NetworkResources object. This is used to
194 // set up the Sync FakeServer for testing.
195 void OverrideNetworkResourcesForTest(JNIEnv
* env
,
197 jlong network_resources
);
200 static jlong
ModelTypeSetToSelection(syncer::ModelTypeSet model_types
);
202 // Converts a bitmap of model types to a set of Java ModelTypes, and returns
203 // their string descriptions separated by commas.
204 static std::string
ModelTypeSelectionToStringForTest(
205 jlong model_type_selection
);
207 static ProfileSyncServiceAndroid
* GetProfileSyncServiceAndroid();
209 // Registers the ProfileSyncServiceAndroid's native methods through JNI.
210 static bool Register(JNIEnv
* env
);
213 typedef std::map
<invalidation::ObjectId
,
215 syncer::ObjectIdLessThan
> ObjectIdVersionMap
;
217 virtual ~ProfileSyncServiceAndroid();
218 // Remove observers to profile sync service.
219 void RemoveObserver();
222 ProfileSyncService
* sync_service_
;
223 // The class that handles getting, setting, and persisting sync
225 scoped_ptr
<sync_driver::SyncPrefs
> sync_prefs_
;
227 // Java-side ProfileSyncService object.
228 JavaObjectWeakGlobalRef weak_java_profile_sync_service_
;
230 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceAndroid
);
233 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_