| Index: components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java
|
| diff --git a/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..8af5f6e3a3ab82669d5b8bb60d3fb302b45da8d7
|
| --- /dev/null
|
| +++ b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java
|
| @@ -0,0 +1,72 @@
|
| +// Copyright 2016 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.components.gcm_driver.instance_id;
|
| +
|
| +import android.content.Context;
|
| +import android.text.TextUtils;
|
| +
|
| +import com.google.android.gms.iid.InstanceID;
|
| +
|
| +import java.io.IOException;
|
| +import java.util.HashMap;
|
| +import java.util.Map;
|
| +
|
| +/**
|
| + * InstanceID wrapper that allows multiple InstanceIDs to be created, depending
|
| + * on the provided subtype. Only for platforms-within-platforms like browsers.
|
| + */
|
| +public class InstanceIDWithSubtype extends InstanceID {
|
| + private final String mSubtype;
|
| +
|
| + /** Cached instances. May be accessed from multiple threads; synchronize on InstanceID.class. */
|
| + private static Map<String, InstanceIDWithSubtype> sSubtypeInstances = new HashMap<>();
|
| +
|
| + private InstanceIDWithSubtype(Context context, String subtype) {
|
| + super(context, subtype, null /* options */);
|
| + mSubtype = subtype;
|
| + }
|
| +
|
| + /**
|
| + * Returns an instance of this class. Unlike {@link InstanceID#getInstance(Context)}, it is not
|
| + * a singleton, but instead a different instance will be returned for each {@code subtype}.
|
| + */
|
| + public static InstanceIDWithSubtype getInstance(Context context, String subtype) {
|
| + if (TextUtils.isEmpty(subtype)) {
|
| + throw new IllegalArgumentException("subtype must not be empty");
|
| + }
|
| + context = context.getApplicationContext();
|
| +
|
| + // Synchronize on the base class, to match the synchronized statements in
|
| + // InstanceID.getInstance.
|
| + synchronized (InstanceID.class) {
|
| + if (sSubtypeInstances.isEmpty()) {
|
| + // The static InstanceID.getInstance method performs some one-time initialization
|
| + // logic that is also necessary for users of this sub-class. To work around this,
|
| + // first get (but don't use) the default InstanceID.
|
| + InstanceID.getInstance(context);
|
| + }
|
| +
|
| + InstanceIDWithSubtype existing = sSubtypeInstances.get(subtype);
|
| + if (existing == null) {
|
| + existing = new InstanceIDWithSubtype(context, subtype);
|
| + sSubtypeInstances.put(subtype, existing);
|
| + }
|
| + return existing;
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void deleteInstanceID() throws IOException {
|
| + // Synchronize on the base class, to match getInstance.
|
| + synchronized (InstanceID.class) {
|
| + sSubtypeInstances.remove(mSubtype);
|
| + super.deleteInstanceID();
|
| + }
|
| + }
|
| +
|
| + public String getSubtype() {
|
| + return mSubtype;
|
| + }
|
| +}
|
|
|