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