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
5package org.chromium.sync.internal_api.pub;
6
7import android.os.Parcel;
8import android.os.Parcelable;
9
10import java.util.HashSet;
11import java.util.Set;
12
13/**
14 * This enum describes the type of passphrase required, if any, to decrypt synced data.
15 *
16 * It implements the Android {@link Parcelable} interface so it is easy to pass around in intents.
17 *
18 * It maps the native enum syncer::PassphraseType, but has the additional values INVALID and NONE.
19 */
20public enum SyncDecryptionPassphraseType implements Parcelable {
21    INVALID(-2),                   // Used as default value and is not a valid decryption type.
22    NONE(-1),                      // No encryption (deprecated).
23    IMPLICIT_PASSPHRASE(0),        // GAIA-based passphrase (deprecated).
24    KEYSTORE_PASSPHRASE(1),        // Keystore passphrase.
25    FROZEN_IMPLICIT_PASSPHRASE(2), // Frozen GAIA passphrase.
26    CUSTOM_PASSPHRASE(3);          // User-provided passphrase.
27
28    public static Parcelable.Creator CREATOR =
29            new Parcelable.Creator<SyncDecryptionPassphraseType>() {
30                @Override
31                public SyncDecryptionPassphraseType createFromParcel(Parcel parcel) {
32                    return fromInternalValue(parcel.readInt());
33                }
34
35                @Override
36                public SyncDecryptionPassphraseType[] newArray(int size) {
37                    return new SyncDecryptionPassphraseType[size];
38                }
39            };
40
41    public static SyncDecryptionPassphraseType fromInternalValue(int value) {
42        for (SyncDecryptionPassphraseType type : values()) {
43            if (type.internalValue() == value) {
44                return type;
45            }
46        }
47        // Falling back to INVALID. Should not happen if |value| was retrieved from native.
48        return INVALID;
49    }
50
51    private final int mNativeValue;
52
53    private SyncDecryptionPassphraseType(int nativeValue) {
54        mNativeValue = nativeValue;
55    }
56
57
58    public Set<SyncDecryptionPassphraseType> getVisibleTypes() {
59        Set<SyncDecryptionPassphraseType> visibleTypes = new HashSet<>();
60        switch (this) {
61            case NONE:  // Intentional fall through.
62            case IMPLICIT_PASSPHRASE:  // Intentional fall through.
63            case KEYSTORE_PASSPHRASE:
64                visibleTypes.add(this);
65                visibleTypes.add(CUSTOM_PASSPHRASE);
66                break;
67            case FROZEN_IMPLICIT_PASSPHRASE:
68                visibleTypes.add(KEYSTORE_PASSPHRASE);
69                visibleTypes.add(FROZEN_IMPLICIT_PASSPHRASE);
70                break;
71            case CUSTOM_PASSPHRASE:
72                visibleTypes.add(KEYSTORE_PASSPHRASE);
73                visibleTypes.add(CUSTOM_PASSPHRASE);
74                break;
75            case INVALID:  // Intentional fall through.
76            default:
77                visibleTypes.add(this);
78                break;
79        }
80        return visibleTypes;
81    }
82
83    /**
84     * Get the types that are allowed to be enabled from the current type.
85     *
86     * @param encryptEverythingAllowed Whether encrypting all data is allowed.
87     */
88    public Set<SyncDecryptionPassphraseType> getAllowedTypes(boolean encryptEverythingAllowed) {
89        Set<SyncDecryptionPassphraseType> allowedTypes = new HashSet<>();
90        switch (this) {
91            case NONE:  // Intentional fall through.
92            case IMPLICIT_PASSPHRASE:  // Intentional fall through.
93            case KEYSTORE_PASSPHRASE:
94                allowedTypes.add(this);
95                if (encryptEverythingAllowed) {
96                    allowedTypes.add(CUSTOM_PASSPHRASE);
97                }
98                break;
99            case FROZEN_IMPLICIT_PASSPHRASE:  // Intentional fall through.
100            case CUSTOM_PASSPHRASE:  // Intentional fall through.
101            case INVALID:  // Intentional fall through.
102            default:
103                break;
104        }
105        return allowedTypes;
106    }
107
108    /**
109     * TODO(maxbogue): Remove when no longer used in Clank; see http://crbug.com/424187.
110     */
111    @Deprecated
112    public Set<SyncDecryptionPassphraseType> getAllowedTypes() {
113        return getAllowedTypes(true);
114    }
115
116    public int internalValue() {
117        // Since the values in this enums are constant and very small, this cast is safe.
118        return mNativeValue;
119    }
120
121    @Override
122    public int describeContents() {
123        return 0;
124    }
125
126    @Override
127    public void writeToParcel(Parcel dest, int flags) {
128        dest.writeInt(mNativeValue);
129    }
130}
131