12de215a6b7a8052ea5dbd1d49926b9f55ed90f8
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / profile_sync_service_android.h
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 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_
6 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_
7
8 #include <jni.h>
9 #include <map>
10
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"
20
21 class Profile;
22 class ProfileSyncService;
23
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 {
30  public:
31
32   ProfileSyncServiceAndroid(JNIEnv* env, jobject obj);
33
34   // This method should be called once right after contructing the object.
35   void Init();
36
37   // Called from Java when we need to nudge native syncer. The |objectSource|,
38   // |objectId|, |version| and |payload| values should come from an
39   // invalidation.
40   void NudgeSyncer(JNIEnv* env,
41                    jobject obj,
42                    jint objectSource,
43                    jstring objectId,
44                    jlong version,
45                    jstring payload);
46
47   // Called from Java when we need to nudge native syncer but have lost state on
48   // which types have changed.
49   void NudgeSyncerForAllTypes(JNIEnv* env, jobject obj);
50
51   // Called from Java when the user manually enables sync
52   void EnableSync(JNIEnv* env, jobject obj);
53
54   // Called from Java when the user manually disables sync
55   void DisableSync(JNIEnv* env, jobject obj);
56
57   // Called from Java when the user signs in to Chrome. Starts up sync.
58   void SignInSync(JNIEnv* env, jobject obj);
59
60   // Called from Java when the user signs out of Chrome
61   void SignOutSync(JNIEnv* env, jobject obj);
62
63   // Returns a string version of browser_sync::SyncBackendHost::StatusSummary
64   base::android::ScopedJavaLocalRef<jstring> QuerySyncStatusSummary(
65       JNIEnv* env, jobject obj);
66
67   // Called from Java early during startup to ensure we use the correct
68   // unique machine tag in session sync. Returns true if the machine tag was
69   // succesfully set.
70   // This must be called before the |SessionModelAssociator| is initialized.
71   jboolean SetSyncSessionsId(JNIEnv* env, jobject obj, jstring tag);
72
73   // Returns true if the sync backend is initialized.
74   jboolean IsSyncInitialized(JNIEnv* env, jobject obj);
75
76   // Returns true if the sync is currently being setup for the first time.
77   jboolean IsFirstSetupInProgress(JNIEnv* env, jobject obj);
78
79   // Returns true if encrypting everything is allowed.
80   jboolean IsEncryptEverythingAllowed(JNIEnv* env, jobject obj);
81
82   // Returns true if the user is currently encrypting everything.
83   jboolean IsEncryptEverythingEnabled(JNIEnv* env, jobject obj);
84
85   // Returns true if the sync code needs a passphrase for either encryption or
86   // decryption (can need a passphrase for encryption if the user is turning on
87   // encryption and no passphrase has been set yet).
88   jboolean IsPassphraseRequired(JNIEnv* env, jobject obj);
89
90   // Returns true if the sync code needs a decryption passphrase for one of the
91   // currently enabled types.
92   jboolean IsPassphraseRequiredForDecryption(JNIEnv* env, jobject obj);
93
94   // Returns true if the sync code needs a decryption passphrase for *any* type,
95   // even types that aren't supported on this platform (like passwords).
96   jboolean IsPassphraseRequiredForExternalType(JNIEnv* env, jobject obj);
97
98   // Returns true if the sync code needs a custom decryption passphrase.
99   // Can not be called if the sync backend is not initialized.
100   jboolean IsUsingSecondaryPassphrase(JNIEnv* env, jobject obj);
101
102   // Returns true if the decryption passphrase works (was able to decrypt the
103   // stored sync data). Should only be called if
104   // IsPassphraseRequiredForDecryption() returns true.
105   jboolean SetDecryptionPassphrase(JNIEnv* env,
106                                    jobject obj,
107                                    jstring passphrase);
108
109   // Encrypts the user's data with the passed passphrase. If |is_gaia| == true
110   // then the passphrase is treated as a google (GAIA) passphrase, otherwise
111   // it's treated like an explicit/custom passphrase.
112   void SetEncryptionPassphrase(JNIEnv* env,
113                                jobject obj,
114                                jstring passphrase,
115                                jboolean is_gaia);
116
117   // Returns whether the cryptographer is ready (i.e. encrypted types can be
118   // handled).
119   jboolean IsCryptographerReady(JNIEnv* env, jobject);
120
121   // Returns the actual passphrase type being used for encryption. This is a
122   // value from the enum defined in syncer::PassphraseType and must be matched
123   // in Java.
124   jint GetPassphraseType(JNIEnv* env, jobject obj);
125
126   // Returns true if the current explicit passphrase time is defined.
127   jboolean HasExplicitPassphraseTime(JNIEnv* env, jobject);
128
129   // Returns the current explicit passphrase time.
130   jlong GetExplicitPassphraseTime(JNIEnv* env, jobject);
131
132   base::android::ScopedJavaLocalRef<jstring>
133       GetSyncEnterGooglePassphraseBodyWithDateText(
134           JNIEnv* env, jobject);
135
136   base::android::ScopedJavaLocalRef<jstring>
137       GetSyncEnterCustomPassphraseBodyWithDateText(
138           JNIEnv* env, jobject);
139
140   base::android::ScopedJavaLocalRef<jstring>
141       GetCurrentSignedInAccountText(
142           JNIEnv* env, jobject);
143
144   base::android::ScopedJavaLocalRef<jstring>
145       GetSyncEnterCustomPassphraseBodyText(
146           JNIEnv* env, jobject);
147
148   // Returns true if sync has been migrated.
149   jboolean IsSyncKeystoreMigrationDone(JNIEnv* env, jobject obj);
150
151   // Get the set of enabled data types. These are the types currently both
152   // registered and preferred. Note that control types are always included here.
153   // Returns a bit map of the values from
154   // profile_sync_service_model_type_selection_android.h.
155   jlong GetEnabledDataTypes(JNIEnv* env, jobject obj);
156
157   // Enables the passed data types.
158   // If |sync_everything| is true, then all data types are enabled and the
159   // contents of |model_type_selection| is
160   // ignored.
161   // Otherwise, the values of |model_type_selection| must contain a bit map of
162   // values from profile_sync_service_model_type_selection_android.h.
163   void SetPreferredDataTypes(JNIEnv* env,
164                              jobject obj,
165                              jboolean sync_everything,
166                              jlong model_type_selection);
167
168   // Tells sync that we're currently configuring so no data types should be
169   // downloaded yet.
170   void SetSetupInProgress(JNIEnv* env, jobject obj, jboolean in_progress);
171
172   // Tells sync that sync setup is complete so we can start syncing now.
173   void SetSyncSetupCompleted(JNIEnv* env, jobject obj);
174
175   // Returns true if sync setup has been completed.
176   jboolean HasSyncSetupCompleted(JNIEnv* env, jobject obj);
177
178   // Returns true if sync startup is currently suppressed.
179   jboolean IsStartSuppressed(JNIEnv* env, jobject obj);
180
181   // Returns true if sync is configured to "sync everything".
182   jboolean HasKeepEverythingSynced(JNIEnv* env, jobject obj);
183
184   // Turns on encryption for all data types. This is an asynchronous operation
185   // which happens after the current configuration pass is done, so a call to
186   // this routine must be followed by a call to SetEnabledDataTypes().
187   void EnableEncryptEverything(JNIEnv* env, jobject obj);
188
189   // Returns true if sync has encountered an unrecoverable error.
190   jboolean HasUnrecoverableError(JNIEnv* env, jobject obj);
191
192   // Returns sync internals in a JSON-formatted Java string.
193   base::android::ScopedJavaLocalRef<jstring> GetAboutInfoForTest(JNIEnv* env,
194                                                                  jobject obj);
195
196   // Returns the integer value corresponding to the current auth error state
197   // (GoogleServiceAuthError.State).
198   jint GetAuthError(JNIEnv* env, jobject obj);
199
200   // ProfileSyncServiceObserver:
201   virtual void OnStateChanged() OVERRIDE;
202
203   // Returns a timestamp for when a sync was last executed. The return value is
204   // the internal value of base::Time.
205   jlong GetLastSyncedTimeForTest(JNIEnv* env, jobject obj);
206
207   // Overrides ProfileSyncService's NetworkResources object. This is used to
208   // set up the Sync FakeServer for testing.
209   void OverrideNetworkResourcesForTest(JNIEnv* env,
210                                        jobject obj,
211                                        jlong network_resources);
212
213   // Public for tests.
214   static jlong ModelTypeSetToSelection(syncer::ModelTypeSet model_types);
215
216   // Converts a bitmap of model types to a set of Java ModelTypes, and returns
217   // their string descriptions separated by commas.
218   static std::string ModelTypeSelectionToStringForTest(
219       jlong model_type_selection);
220
221   static ProfileSyncServiceAndroid* GetProfileSyncServiceAndroid();
222
223   // Registers the ProfileSyncServiceAndroid's native methods through JNI.
224   static bool Register(JNIEnv* env);
225
226  private:
227   typedef std::map<invalidation::ObjectId,
228                    int64,
229                    syncer::ObjectIdLessThan> ObjectIdVersionMap;
230
231   virtual ~ProfileSyncServiceAndroid();
232   // Remove observers to profile sync service.
233   void RemoveObserver();
234   // Called from Java when we need to nudge native syncer. The |object_source|,
235   // |objectId|, |version| and |payload| values should come from an
236   // invalidation.
237   void SendNudgeNotification(int object_source,
238                              const std::string& str_object_id,
239                              int64 version,
240                              const std::string& payload);
241
242   Profile* profile_;
243   ProfileSyncService* sync_service_;
244   // The class that handles getting, setting, and persisting sync
245   // preferences.
246   scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
247
248   // Java-side ProfileSyncService object.
249   JavaObjectWeakGlobalRef weak_java_profile_sync_service_;
250
251   // The invalidation API spec allows for the possibility of redundant
252   // invalidations, so keep track of the max versions and drop
253   // invalidations with old versions.
254   ObjectIdVersionMap max_invalidation_versions_;
255
256   DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceAndroid);
257 };
258
259 #endif  // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_