Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Unified Diff: components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridge.java

Issue 1830983002: Implement InstanceIDAndroid using InstanceIDWithSubtype.java (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@iid1subtype
Patch Set: Improve comments Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridge.java
diff --git a/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridge.java b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridge.java
new file mode 100644
index 0000000000000000000000000000000000000000..fabf769870b774f23030401a7629de9081d8a7e5
--- /dev/null
+++ b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridge.java
@@ -0,0 +1,146 @@
+// 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.os.AsyncTask;
+import android.os.Bundle;
+import android.text.TextUtils;
+
+import com.google.android.gms.iid.InstanceID;
+
+import org.chromium.base.annotations.CalledByNative;
+import org.chromium.base.annotations.JNINamespace;
+
+import java.io.IOException;
+
+/**
+ * Wraps InstanceID and InstanceIDWithSubtype so they can be used over JNI.
+ * Performs disk/network operations on a background thread and replies asynchronously.
+ */
+@JNINamespace("instance_id")
+public class InstanceIDBridge {
+ /** Underlying InstanceID. May be shared by multiple InstanceIDBridges. */
+ private final InstanceID mInstanceID;
+ private final long mNativeInstanceIDAndroid;
+
+ private InstanceIDBridge(InstanceID instanceID, long nativeInstanceIDAndroid) {
+ mInstanceID = instanceID;
+ mNativeInstanceIDAndroid = nativeInstanceIDAndroid;
+ }
+
+ /**
+ * Returns a wrapped {@link InstanceIDWithSubtype} or {@link InstanceID}, depending on whether
+ * subtype is empty. Multiple InstanceIDBridge instances may share an underlying InstanceID.
+ */
+ @CalledByNative
+ public static InstanceIDBridge getInstance(
+ long nativeInstanceIDAndroid, Context context, String subtype) {
+ InstanceID instanceID = TextUtils.isEmpty(subtype)
Peter Beverloo 2016/04/14 14:30:47 There's a DCHECK in the C++ code to make sure the
johnme 2016/04/14 18:00:15 It would be valid for Chrome components to use an
+ ? InstanceID.getInstance(context)
+ : InstanceIDWithSubtype.getInstance(context, subtype);
+ return new InstanceIDBridge(instanceID, nativeInstanceIDAndroid);
+ }
+
+ /**
+ * Called when our C++ counterpart is destroyed. Clears the handle to our native C++ object,
+ * ensuring it's not called by pending async tasks.
+ */
+ @CalledByNative
+ private void destroy() {
+ mNativeInstanceIDAndroid = 0;
+ }
+
+ /** Wrapper for {@link InstanceID#getId}. */
+ @CalledByNative
+ public String getId() {
+ return mInstanceID.getId();
Peter Beverloo 2016/04/14 14:30:47 Any reason for not making this asynchronous immedi
johnme 2016/04/14 18:00:15 Convenience (note that create will also need to be
+ }
+
+ /** Wrapper for {@link InstanceID#getCreationTime}. */
+ @CalledByNative
+ public long getCreationTime() {
+ return mInstanceID.getCreationTime();
+ }
+
+ /** Async wrapper for {@link InstanceID#getToken(String, String, Bundle)}. */
+ @CalledByNative
+ private void getToken(final int requestId, final String authorizedEntity, final String scope,
+ String[] extrasStrings) {
+ final Bundle extras = new Bundle();
+ assert extrasStrings.length % 2 == 0;
+ for (int i = 0; i < extrasStrings.length; i += 2) {
+ extras.putString(extrasStrings[i], extrasStrings[i + 1]);
+ }
+ new AsyncTask<Void, Void, String>() {
+ @Override
+ protected String doInBackground(Void... params) {
+ try {
+ return mInstanceID.getToken(authorizedEntity, scope, extras);
+ } catch (IOException ex) {
+ return "";
+ }
+ }
+ @Override
+ protected void onPostExecute(String token) {
+ if (mNativeInstanceIDAndroid != 0) {
+ nativeDidGetToken(mNativeInstanceIDAndroid, requestId, token);
+ }
+ }
+ }.execute();
+ }
+
+ /** Async wrapper for {@link InstanceID#deleteToken(String, String)}. */
+ @CalledByNative
+ private void deleteToken(
+ final int requestId, final String authorizedEntity, final String scope) {
+ new AsyncTask<Void, Void, Boolean>() {
+ @Override
+ protected Boolean doInBackground(Void... params) {
+ try {
+ mInstanceID.deleteToken(authorizedEntity, scope);
+ return true;
+ } catch (IOException ex) {
+ return false;
+ }
+ }
+ @Override
+ protected void onPostExecute(Boolean success) {
+ if (mNativeInstanceIDAndroid != 0) {
+ nativeDidDeleteToken(mNativeInstanceIDAndroid, requestId, success);
+ }
+ }
+ }.execute();
+ }
+
+ /** Async wrapper for {@link InstanceID#deleteInstanceID}. */
+ @CalledByNative
+ private void deleteInstanceID(final int requestId) {
+ new AsyncTask<Void, Void, Boolean>() {
+ @Override
+ protected Boolean doInBackground(Void... params) {
+ try {
+ mInstanceID.deleteInstanceID();
+ return true;
+ } catch (IOException ex) {
+ return false;
+ }
+ }
+ @Override
+ protected void onPostExecute(Boolean success) {
+ if (mNativeInstanceIDAndroid != 0) {
+ nativeDidDeleteID(mNativeInstanceIDAndroid, requestId, success);
+ }
+ }
+ }.execute();
+ }
+
+ private native void nativeDidGetToken(
+ long nativeInstanceIDAndroid, int requestId, String token);
+ private native void nativeDidDeleteToken(
+ long nativeInstanceIDAndroid, int requestId, boolean success);
+ private native void nativeDidDeleteID(
+ long nativeInstanceIDAndroid, int requestId, boolean success);
+}

Powered by Google App Engine
This is Rietveld 408576698