Home | History | Annotate | Download | only in sync
      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/sync_driver/sync_prefs.h"
     17 #include "google/cacheinvalidation/include/types.h"
     18 #include "google_apis/gaia/google_service_auth_error.h"
     19 
     20 class Profile;
     21 class 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.
     28 class 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 |objectSource|,
     37   // |objectId|, |version| and |payload| values should come from an
     38   // invalidation.
     39   void NudgeSyncer(JNIEnv* env,
     40                    jobject obj,
     41                    jint objectSource,
     42                    jstring objectId,
     43                    jlong version,
     44                    jstring payload);
     45 
     46   // Called from Java when we need to nudge native syncer but have lost state on
     47   // which types have changed.
     48   void NudgeSyncerForAllTypes(JNIEnv* env, jobject obj);
     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   // Returns a timestamp for when a sync was last executed. The return value is
    197   // the internal value of base::Time.
    198   jlong GetLastSyncedTimeForTest(JNIEnv* env, jobject obj);
    199 
    200   static ProfileSyncServiceAndroid* GetProfileSyncServiceAndroid();
    201 
    202   // Registers the ProfileSyncServiceAndroid's native methods through JNI.
    203   static bool Register(JNIEnv* env);
    204 
    205  private:
    206   typedef std::map<invalidation::ObjectId,
    207                    int64,
    208                    syncer::ObjectIdLessThan> ObjectIdVersionMap;
    209 
    210   virtual ~ProfileSyncServiceAndroid();
    211   // Remove observers to profile sync service.
    212   void RemoveObserver();
    213   // Called from Java when we need to nudge native syncer. The |object_source|,
    214   // |objectId|, |version| and |payload| values should come from an
    215   // invalidation.
    216   void SendNudgeNotification(int object_source,
    217                              const std::string& str_object_id,
    218                              int64 version,
    219                              const std::string& payload);
    220 
    221   Profile* profile_;
    222   ProfileSyncService* sync_service_;
    223   // The class that handles getting, setting, and persisting sync
    224   // preferences.
    225   scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
    226 
    227   // Java-side ProfileSyncService object.
    228   JavaObjectWeakGlobalRef weak_java_profile_sync_service_;
    229 
    230   // The invalidation API spec allows for the possibility of redundant
    231   // invalidations, so keep track of the max versions and drop
    232   // invalidations with old versions.
    233   ObjectIdVersionMap max_invalidation_versions_;
    234 
    235   DISALLOW_COPY_AND_ASSIGN(ProfileSyncServiceAndroid);
    236 };
    237 
    238 #endif  // CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_ANDROID_H_
    239