summaryrefslogtreecommitdiffstats
path: root/sync/android/java/src/org/chromium/sync/ModelTypeHelper.java
blob: 3e053c296a10a3e9ceafdd979f1fa2fb8494dfcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.sync;

import com.google.ipc.invalidation.external.client.types.ObjectId;
import com.google.protos.ipc.invalidation.Types;

import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.JNINamespace;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Helper methods for dealing with ModelTypes.
 *
 * This class deals primarily with converting ModelTypes into notification types (string
 * representations that are used to register for invalidations) and converting notification
 * types into the actual ObjectIds used for invalidations.
 *
 */
@JNINamespace("syncer")
public class ModelTypeHelper {
    private static final String TAG = "ModelTypeHelper";

    private static final Object sLock = new Object();

    private static final int[] NON_INVALIDATION_TYPES_ARRAY = new int[] {
        ModelType.PROXY_TABS
    };

    // Convenience sets for checking whether a type can have invalidations. Some ModelTypes
    // such as PROXY_TABS are not real types and can't be registered. Initializing these
    // once reduces toNotificationType() calls in the isInvalidationType() method.
    private static Set<String> sNonInvalidationTypes = null;

    /**
     * Initializes the non-invalidation sets. Called lazily the first time they're needed.
     */
    private static void initNonInvalidationTypes() {
        synchronized (sLock) {
            if (sNonInvalidationTypes != null) return;

            sNonInvalidationTypes = new HashSet<String>();
            for (int i = 0; i < NON_INVALIDATION_TYPES_ARRAY.length; i++) {
                sNonInvalidationTypes.add(toNotificationType(NON_INVALIDATION_TYPES_ARRAY[i]));
            }
        }
    }

    /**
     * Checks whether a type is allowed to register for invalidations.
     */
    private static boolean isInvalidationType(String notificationType) {
        initNonInvalidationTypes();
        return !sNonInvalidationTypes.contains(notificationType);
    }

    /**
     * Converts a notification type into an ObjectId.
     *
     * If the model type is not an invalidation type, this function uses the string "NULL".
     */
    private static ObjectId toObjectId(String notificationType) {
        String objectIdString = isInvalidationType(notificationType) ? notificationType : "NULL";
        return ObjectId.newInstance(Types.ObjectSource.CHROME_SYNC, objectIdString.getBytes());
    }

    @VisibleForTesting
    public static ObjectId toObjectId(int modelType) {
        return toObjectId(toNotificationType(modelType));
    }

    /**
     * Converts a model type to its notification type representation using JNI.
     *
     * This is the value that is stored in the invalidation preferences and used to
     * register for invalidations.
     *
     * @param modelType the model type to convert to a string.
     * @return the string representation of the model type constant.
     */
    public static String toNotificationType(int modelType) {
        // Because PROXY_TABS isn't an invalidation type, it doesn't have a string from native,
        // but for backwards compatibility we need to keep its pref value the same as the old
        // ModelType enum name value.
        if (modelType == ModelType.PROXY_TABS) {
            return "PROXY_TABS";
        }
        return nativeModelTypeToNotificationType(modelType);
    }

    /**
     * Converts a set of {@link String} notification types to a set of {@link ObjectId}.
     *
     * This function assumes that all the strings passed in were generated with
     * ModelTypeHelper.toNotificationType. Any notification types that are nonInvalidationTypes
     * are filtered out.
     */
    public static Set<ObjectId> notificationTypesToObjectIds(Collection<String> notificationTypes) {
        Set<ObjectId> objectIds = new HashSet<ObjectId>();
        for (String notificationType : notificationTypes) {
            if (isInvalidationType(notificationType)) {
                objectIds.add(toObjectId(notificationType));
            }
        }
        return objectIds;
    }

    private static native String nativeModelTypeToNotificationType(int modelType);
}