Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / chrome / android / java / src / org / chromium / chrome / browser / sync / ProfileSyncService.java
index 4ebfac9..91e2417 100644 (file)
@@ -7,17 +7,19 @@ package org.chromium.chrome.browser.sync;
 import android.content.Context;
 import android.util.Log;
 
-import com.google.common.annotations.VisibleForTesting;
-
 import org.chromium.base.CalledByNative;
 import org.chromium.base.ThreadUtils;
+import org.chromium.base.VisibleForTesting;
 import org.chromium.chrome.browser.identity.UniqueIdentificationGenerator;
 import org.chromium.sync.internal_api.pub.SyncDecryptionPassphraseType;
 import org.chromium.sync.internal_api.pub.base.ModelType;
 
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import java.util.SortedSet;
+import java.util.TreeSet;
 import java.util.concurrent.CopyOnWriteArrayList;
 
 /**
@@ -53,7 +55,7 @@ public class ProfileSyncService {
             new CopyOnWriteArrayList<SyncStateChangedListener>();
 
     // Native ProfileSyncServiceAndroid object. Can not be final since we set it to 0 in destroy().
-    private final int mNativeProfileSyncServiceAndroid;
+    private final long mNativeProfileSyncServiceAndroid;
 
     /**
      * A helper method for retrieving the application-wide SyncSetupManager.
@@ -86,7 +88,7 @@ public class ProfileSyncService {
     }
 
     @CalledByNative
-    private static int getProfileSyncServiceAndroid(Context context) {
+    private static long getProfileSyncServiceAndroid(Context context) {
         return get(context).mNativeProfileSyncServiceAndroid;
     }
 
@@ -227,6 +229,14 @@ public class ProfileSyncService {
         return nativeHasExplicitPassphraseTime(mNativeProfileSyncServiceAndroid);
     }
 
+    /**
+     * Returns the current explicit passphrase time in milliseconds since epoch.
+     */
+    public long getExplicitPassphraseTime() {
+        assert isSyncInitialized();
+        return nativeGetExplicitPassphraseTime(mNativeProfileSyncServiceAndroid);
+    }
+
     public String getSyncEnterGooglePassphraseBodyWithDateText() {
         assert isSyncInitialized();
         return nativeGetSyncEnterGooglePassphraseBodyWithDateText(mNativeProfileSyncServiceAndroid);
@@ -299,6 +309,17 @@ public class ProfileSyncService {
     }
 
     /**
+     * Checks if encrypting all the data types is allowed.
+     *
+     * @return true if encrypting all data types is allowed, false if only passwords are allowed to
+     * be encrypted.
+     */
+    public boolean isEncryptEverythingAllowed() {
+        assert isSyncInitialized();
+        return nativeIsEncryptEverythingAllowed(mNativeProfileSyncServiceAndroid);
+    }
+
+    /**
      * Checks if the all the data types are encrypted.
      *
      * @return true if all data types are encrypted, false if only passwords are encrypted.
@@ -344,46 +365,54 @@ public class ProfileSyncService {
      */
     public Set<ModelType> getPreferredDataTypes() {
         long modelTypeSelection =
-            nativeGetEnabledDataTypes(mNativeProfileSyncServiceAndroid);
+                nativeGetEnabledDataTypes(mNativeProfileSyncServiceAndroid);
+        return modelTypeSelectionToSet(modelTypeSelection);
+    }
+
+    @VisibleForTesting
+    public static Set<ModelType> modelTypeSelectionToSet(long modelTypeSelection) {
         Set<ModelType> syncTypes = new HashSet<ModelType>();
         if ((modelTypeSelection & ModelTypeSelection.AUTOFILL) != 0) {
-          syncTypes.add(ModelType.AUTOFILL);
+            syncTypes.add(ModelType.AUTOFILL);
         }
         if ((modelTypeSelection & ModelTypeSelection.AUTOFILL_PROFILE) != 0) {
-          syncTypes.add(ModelType.AUTOFILL_PROFILE);
+            syncTypes.add(ModelType.AUTOFILL_PROFILE);
         }
         if ((modelTypeSelection & ModelTypeSelection.BOOKMARK) != 0) {
-          syncTypes.add(ModelType.BOOKMARK);
+            syncTypes.add(ModelType.BOOKMARK);
         }
         if ((modelTypeSelection & ModelTypeSelection.EXPERIMENTS) != 0) {
-          syncTypes.add(ModelType.EXPERIMENTS);
+            syncTypes.add(ModelType.EXPERIMENTS);
         }
         if ((modelTypeSelection & ModelTypeSelection.NIGORI) != 0) {
-          syncTypes.add(ModelType.NIGORI);
+            syncTypes.add(ModelType.NIGORI);
         }
         if ((modelTypeSelection & ModelTypeSelection.PASSWORD) != 0) {
-          syncTypes.add(ModelType.PASSWORD);
+            syncTypes.add(ModelType.PASSWORD);
         }
         if ((modelTypeSelection & ModelTypeSelection.SESSION) != 0) {
-          syncTypes.add(ModelType.SESSION);
+            syncTypes.add(ModelType.SESSION);
         }
         if ((modelTypeSelection & ModelTypeSelection.TYPED_URL) != 0) {
-          syncTypes.add(ModelType.TYPED_URL);
+            syncTypes.add(ModelType.TYPED_URL);
         }
         if ((modelTypeSelection & ModelTypeSelection.HISTORY_DELETE_DIRECTIVE) != 0) {
-          syncTypes.add(ModelType.HISTORY_DELETE_DIRECTIVE);
+            syncTypes.add(ModelType.HISTORY_DELETE_DIRECTIVE);
         }
         if ((modelTypeSelection & ModelTypeSelection.DEVICE_INFO) != 0) {
-          syncTypes.add(ModelType.DEVICE_INFO);
+            syncTypes.add(ModelType.DEVICE_INFO);
         }
         if ((modelTypeSelection & ModelTypeSelection.PROXY_TABS) != 0) {
-          syncTypes.add(ModelType.PROXY_TABS);
+            syncTypes.add(ModelType.PROXY_TABS);
         }
         if ((modelTypeSelection & ModelTypeSelection.FAVICON_IMAGE) != 0) {
-          syncTypes.add(ModelType.FAVICON_IMAGE);
+            syncTypes.add(ModelType.FAVICON_IMAGE);
         }
         if ((modelTypeSelection & ModelTypeSelection.FAVICON_TRACKING) != 0) {
-          syncTypes.add(ModelType.FAVICON_TRACKING);
+            syncTypes.add(ModelType.FAVICON_TRACKING);
+        }
+        if ((modelTypeSelection & ModelTypeSelection.SUPERVISED_USER_SETTING) != 0) {
+            syncTypes.add(ModelType.MANAGED_USER_SETTING);
         }
         return syncTypes;
     }
@@ -497,58 +526,96 @@ public class ProfileSyncService {
      * @return The difference measured in microseconds, between last sync cycle completion time
      * and 1 January 1970 00:00:00 UTC.
      */
+    @VisibleForTesting
     public long getLastSyncedTimeForTest() {
         return nativeGetLastSyncedTimeForTest(mNativeProfileSyncServiceAndroid);
     }
 
+    /**
+     * Overrides the Sync engine's NetworkResources. This is used to set up the Sync FakeServer for
+     * testing.
+     *
+     * @param networkResources the pointer to the NetworkResources created by the native code. It
+     *                         is assumed that the Java caller has ownership of this pointer;
+     *                         ownership is transferred as part of this call.
+     */
+    public void overrideNetworkResourcesForTest(long networkResources) {
+        nativeOverrideNetworkResourcesForTest(mNativeProfileSyncServiceAndroid, networkResources);
+    }
+
+    @CalledByNative
+    private static String modelTypeSelectionToStringForTest(long modelTypeSelection) {
+        SortedSet<String> set = new TreeSet<String>();
+        Set<ModelType> filteredTypes = ModelType.filterOutNonInvalidationTypes(
+                modelTypeSelectionToSet(modelTypeSelection));
+        for (ModelType type : filteredTypes) {
+            set.add(type.toString());
+        }
+        StringBuilder sb = new StringBuilder();
+        Iterator<String> it = set.iterator();
+        if (it.hasNext()) {
+            sb.append(it.next());
+            while (it.hasNext()) {
+                sb.append(", ");
+                sb.append(it.next());
+            }
+        }
+        return sb.toString();
+    }
+
     // Native methods
     private native void nativeNudgeSyncer(
-            int nativeProfileSyncServiceAndroid, int objectSource, String objectId, long version,
+            long nativeProfileSyncServiceAndroid, int objectSource, String objectId, long version,
             String payload);
-    private native void nativeNudgeSyncerForAllTypes(int nativeProfileSyncServiceAndroid);
-    private native int nativeInit();
-    private native void nativeEnableSync(int nativeProfileSyncServiceAndroid);
-    private native void nativeDisableSync(int nativeProfileSyncServiceAndroid);
-    private native void nativeSignInSync(int nativeProfileSyncServiceAndroid);
-    private native void nativeSignOutSync(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeSetSyncSessionsId(int nativeProfileSyncServiceAndroid, String tag);
-    private native String nativeQuerySyncStatusSummary(int nativeProfileSyncServiceAndroid);
-    private native int nativeGetAuthError(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsSyncInitialized(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsFirstSetupInProgress(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsEncryptEverythingEnabled(int nativeProfileSyncServiceAndroid);
-    private native void nativeEnableEncryptEverything(int nativeProfileSyncServiceAndroid);
+    private native void nativeNudgeSyncerForAllTypes(long nativeProfileSyncServiceAndroid);
+    private native long nativeInit();
+    private native void nativeEnableSync(long nativeProfileSyncServiceAndroid);
+    private native void nativeDisableSync(long nativeProfileSyncServiceAndroid);
+    private native void nativeSignInSync(long nativeProfileSyncServiceAndroid);
+    private native void nativeSignOutSync(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeSetSyncSessionsId(
+            long nativeProfileSyncServiceAndroid, String tag);
+    private native String nativeQuerySyncStatusSummary(long nativeProfileSyncServiceAndroid);
+    private native int nativeGetAuthError(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsSyncInitialized(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsFirstSetupInProgress(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsEncryptEverythingAllowed(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsEncryptEverythingEnabled(long nativeProfileSyncServiceAndroid);
+    private native void nativeEnableEncryptEverything(long nativeProfileSyncServiceAndroid);
     private native boolean nativeIsPassphraseRequiredForDecryption(
-            int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid);
     private native boolean nativeIsPassphraseRequiredForExternalType(
-            int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsUsingSecondaryPassphrase(int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsUsingSecondaryPassphrase(long nativeProfileSyncServiceAndroid);
     private native boolean nativeSetDecryptionPassphrase(
-            int nativeProfileSyncServiceAndroid, String passphrase);
+            long nativeProfileSyncServiceAndroid, String passphrase);
     private native void nativeSetEncryptionPassphrase(
-            int nativeProfileSyncServiceAndroid, String passphrase, boolean isGaia);
-    private native boolean nativeIsCryptographerReady(int nativeProfileSyncServiceAndroid);
-    private native int nativeGetPassphraseType(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeHasExplicitPassphraseTime(int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid, String passphrase, boolean isGaia);
+    private native boolean nativeIsCryptographerReady(long nativeProfileSyncServiceAndroid);
+    private native int nativeGetPassphraseType(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeHasExplicitPassphraseTime(long nativeProfileSyncServiceAndroid);
+    private native long nativeGetExplicitPassphraseTime(long nativeProfileSyncServiceAndroid);
     private native String nativeGetSyncEnterGooglePassphraseBodyWithDateText(
-            int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid);
     private native String nativeGetSyncEnterCustomPassphraseBodyWithDateText(
-            int nativeProfileSyncServiceAndroid);
-    private native String nativeGetCurrentSignedInAccountText(int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid);
+    private native String nativeGetCurrentSignedInAccountText(long nativeProfileSyncServiceAndroid);
     private native String nativeGetSyncEnterCustomPassphraseBodyText(
-            int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsSyncKeystoreMigrationDone(int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsSyncKeystoreMigrationDone(long nativeProfileSyncServiceAndroid);
     private native long nativeGetEnabledDataTypes(
-        int nativeProfileSyncServiceAndroid);
+        long nativeProfileSyncServiceAndroid);
     private native void nativeSetPreferredDataTypes(
-            int nativeProfileSyncServiceAndroid, boolean syncEverything, long modelTypeSelection);
+            long nativeProfileSyncServiceAndroid, boolean syncEverything, long modelTypeSelection);
     private native void nativeSetSetupInProgress(
-            int nativeProfileSyncServiceAndroid, boolean inProgress);
-    private native void nativeSetSyncSetupCompleted(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeHasSyncSetupCompleted(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeIsStartSuppressed(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeHasKeepEverythingSynced(int nativeProfileSyncServiceAndroid);
-    private native boolean nativeHasUnrecoverableError(int nativeProfileSyncServiceAndroid);
-    private native String nativeGetAboutInfoForTest(int nativeProfileSyncServiceAndroid);
-    private native long nativeGetLastSyncedTimeForTest(int nativeProfileSyncServiceAndroid);
+            long nativeProfileSyncServiceAndroid, boolean inProgress);
+    private native void nativeSetSyncSetupCompleted(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeHasSyncSetupCompleted(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeIsStartSuppressed(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeHasKeepEverythingSynced(long nativeProfileSyncServiceAndroid);
+    private native boolean nativeHasUnrecoverableError(long nativeProfileSyncServiceAndroid);
+    private native String nativeGetAboutInfoForTest(long nativeProfileSyncServiceAndroid);
+    private native long nativeGetLastSyncedTimeForTest(long nativeProfileSyncServiceAndroid);
+    private native void nativeOverrideNetworkResourcesForTest(
+            long nativeProfileSyncServiceAndroid, long networkResources);
 }