Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_service_android.h
blob70d7950a8090a16917d9caf1fe3b7e8b0332217e
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_
8 #include <jni.h>
9 #include <map>
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/public/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"
21 class Profile;
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 {
30 public:
32 ProfileSyncServiceAndroid(JNIEnv* env, jobject obj);
34 // This method should be called once right after contructing the object.
35 void Init();
37 // Called from Java when the user manually enables sync
38 void RequestStart(JNIEnv* env, jobject obj);
40 // Called from Java when the user manually disables sync
41 void RequestStop(JNIEnv* env, jobject obj);
43 // Called from Java when the user signs out of Chrome
44 void SignOutSync(JNIEnv* env, jobject obj);
46 // Called from Java when we get a signal that the Directory should be saved.
47 void FlushDirectory(JNIEnv* env, jobject obj);
49 // Returns a string version of browser_sync::SyncBackendHost::StatusSummary
50 base::android::ScopedJavaLocalRef<jstring> QuerySyncStatusSummary(
51 JNIEnv* env, jobject obj);
53 // Retrieves all Sync data as JSON. This method is asynchronous; all data is
54 // passed to |callback| upon completion.
55 void GetAllNodes(JNIEnv* env, jobject obj, jobject callback);
57 // Called from Java early during startup to ensure we use the correct
58 // unique machine tag in session sync.
59 // This must be called before the |SessionModelAssociator| is initialized.
60 void SetSyncSessionsId(JNIEnv* env, jobject obj, jstring tag);
62 // Returns true if the sync backend is initialized.
63 jboolean IsBackendInitialized(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 custom decryption passphrase.
84 // Can not be called if the sync backend is not initialized.
85 jboolean IsUsingSecondaryPassphrase(JNIEnv* env, jobject obj);
87 // Returns true if the decryption passphrase works (was able to decrypt the
88 // stored sync data). Should only be called if
89 // IsPassphraseRequiredForDecryption() returns true.
90 jboolean SetDecryptionPassphrase(JNIEnv* env,
91 jobject obj,
92 jstring passphrase);
94 // Encrypts the user's data with the passed custom passphrase.
95 void SetEncryptionPassphrase(JNIEnv* env,
96 jobject obj,
97 jstring passphrase);
99 // Returns whether the cryptographer is ready (i.e. encrypted types can be
100 // handled).
101 jboolean IsCryptographerReady(JNIEnv* env, jobject);
103 // Returns the actual passphrase type being used for encryption. This is a
104 // value from the enum defined in syncer::PassphraseType and must be matched
105 // in Java.
106 jint GetPassphraseType(JNIEnv* env, jobject obj);
108 // Returns true if the current explicit passphrase time is defined.
109 jboolean HasExplicitPassphraseTime(JNIEnv* env, jobject);
111 // Returns the current explicit passphrase time.
112 jlong GetExplicitPassphraseTime(JNIEnv* env, jobject);
114 base::android::ScopedJavaLocalRef<jstring>
115 GetSyncEnterGooglePassphraseBodyWithDateText(
116 JNIEnv* env, jobject);
118 base::android::ScopedJavaLocalRef<jstring>
119 GetSyncEnterCustomPassphraseBodyWithDateText(
120 JNIEnv* env, jobject);
122 base::android::ScopedJavaLocalRef<jstring>
123 GetCurrentSignedInAccountText(
124 JNIEnv* env, jobject);
126 base::android::ScopedJavaLocalRef<jstring>
127 GetSyncEnterCustomPassphraseBodyText(
128 JNIEnv* env, jobject);
130 // Returns true if sync has been migrated.
131 jboolean IsSyncKeystoreMigrationDone(JNIEnv* env, jobject obj);
133 // Get the set of active data types. These are the types currently being
134 // synced. Note that control types are always included here.
135 // Returns a bit map of the values from
136 // profile_sync_service_android.cc.
137 base::android::ScopedJavaLocalRef<jintArray> GetActiveDataTypes(JNIEnv* env,
138 jobject obj);
140 // Get the set of preferred data types. These are the types that the user
141 // has requested be synced.
142 // Returns a bit map of the values from
143 // profile_sync_service_android.cc.
144 base::android::ScopedJavaLocalRef<jintArray> GetPreferredDataTypes(
145 JNIEnv* env,
146 jobject obj);
148 // Enables the passed data types.
149 // If |sync_everything| is true, then all data types are enabled and the
150 // contents of |model_type_selection| is
151 // ignored.
152 // Otherwise, the values of |model_type_selection| must contain a bit map of
153 // values from profile_sync_service_android.cc.
154 void SetPreferredDataTypes(JNIEnv* env,
155 jobject obj,
156 jboolean sync_everything,
157 jintArray model_type_selection);
159 // Tells sync that we're currently configuring so no data types should be
160 // downloaded yet.
161 void SetSetupInProgress(JNIEnv* env, jobject obj, jboolean in_progress);
163 // Tells sync that sync setup is complete so we can start syncing now.
164 void SetSyncSetupCompleted(JNIEnv* env, jobject obj);
166 // Returns true if sync setup has been completed.
167 jboolean HasSyncSetupCompleted(JNIEnv* env, jobject obj);
169 // See ProfileSyncService::IsSyncRequested().
170 jboolean IsSyncRequested(JNIEnv* env, jobject obj);
172 // See ProfileSyncService::IsSyncActive().
173 jboolean IsSyncActive(JNIEnv* env, jobject obj);
175 // Returns true if sync is configured to "sync everything".
176 jboolean HasKeepEverythingSynced(JNIEnv* env, jobject obj);
178 // Turns on encryption for all data types. This is an asynchronous operation
179 // which happens after the current configuration pass is done, so a call to
180 // this routine must be followed by a call to SetEnabledDataTypes().
181 void EnableEncryptEverything(JNIEnv* env, jobject obj);
183 // Returns true if sync has encountered an unrecoverable error.
184 jboolean HasUnrecoverableError(JNIEnv* env, jobject obj);
186 // Returns sync internals in a JSON-formatted Java string.
187 base::android::ScopedJavaLocalRef<jstring> GetAboutInfoForTest(JNIEnv* env,
188 jobject obj);
190 // Returns the integer value corresponding to the current auth error state
191 // (GoogleServiceAuthError.State).
192 jint GetAuthError(JNIEnv* env, jobject obj);
194 // sync_driver::SyncServiceObserver:
195 void OnStateChanged() override;
197 // Getter/setter for out of band (system notification) passphrase prompt.
198 jboolean IsPassphrasePrompted(JNIEnv* env, jobject obj);
199 void SetPassphrasePrompted(JNIEnv* env, jobject obj, jboolean prompted);
201 // Returns a timestamp for when a sync was last executed. The return value is
202 // the internal value of base::Time.
203 jlong GetLastSyncedTimeForTest(JNIEnv* env, jobject obj);
205 // Overrides ProfileSyncService's NetworkResources object. This is used to
206 // set up the Sync FakeServer for testing.
207 void OverrideNetworkResourcesForTest(JNIEnv* env,
208 jobject obj,
209 jlong network_resources);
211 // Public for tests.
212 static base::android::ScopedJavaLocalRef<jintArray>
213 ModelTypeSetToJavaIntArray(JNIEnv* env, syncer::ModelTypeSet model_types);
215 static ProfileSyncServiceAndroid* GetProfileSyncServiceAndroid();
217 // Registers the ProfileSyncServiceAndroid's native methods through JNI.
218 static bool Register(JNIEnv* env);
220 private:
221 typedef std::map<invalidation::ObjectId,
222 int64,
223 syncer::ObjectIdLessThan> ObjectIdVersionMap;
225 ~ProfileSyncServiceAndroid() override;
226 // Remove observers to profile sync service.
227 void RemoveObserver();
229 Profile* profile_;
230 ProfileSyncService* sync_service_;
231 // The class that handles getting, setting, and persisting sync
232 // preferences.
233 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
235 // Java-side ProfileSyncService object.
236 JavaObjectWeakGlobalRef weak_java_profile_sync_service_;
238 DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceAndroid);
241 #endif // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_