| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.components.gcm_driver.instance_id; | 5 package org.chromium.components.gcm_driver.instance_id; |
| 6 | 6 |
| 7 import android.content.Context; | |
| 8 import android.os.AsyncTask; | 7 import android.os.AsyncTask; |
| 9 import android.os.Bundle; | 8 import android.os.Bundle; |
| 10 | 9 |
| 11 import com.google.android.gms.iid.InstanceID; | 10 import com.google.android.gms.iid.InstanceID; |
| 12 | 11 |
| 12 import org.chromium.base.ContextUtils; |
| 13 import org.chromium.base.annotations.CalledByNative; | 13 import org.chromium.base.annotations.CalledByNative; |
| 14 import org.chromium.base.annotations.JNINamespace; | 14 import org.chromium.base.annotations.JNINamespace; |
| 15 | 15 |
| 16 import java.io.IOException; | 16 import java.io.IOException; |
| 17 import java.util.concurrent.ExecutionException; | 17 import java.util.concurrent.ExecutionException; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * Wraps InstanceID and InstanceIDWithSubtype so they can be used over JNI. | 20 * Wraps InstanceID and InstanceIDWithSubtype so they can be used over JNI. |
| 21 * Performs disk/network operations on a background thread and replies asynchron
ously. | 21 * Performs disk/network operations on a background thread and replies asynchron
ously. |
| 22 */ | 22 */ |
| 23 @JNINamespace("instance_id") | 23 @JNINamespace("instance_id") |
| 24 public class InstanceIDBridge { | 24 public class InstanceIDBridge { |
| 25 private final Context mContext; | |
| 26 private final String mSubtype; | 25 private final String mSubtype; |
| 27 private long mNativeInstanceIDAndroid; | 26 private long mNativeInstanceIDAndroid; |
| 28 /** | 27 /** |
| 29 * Underlying InstanceID. May be shared by multiple InstanceIDBridges. Must
be initialized on | 28 * Underlying InstanceID. May be shared by multiple InstanceIDBridges. Must
be initialized on |
| 30 * a background thread. | 29 * a background thread. |
| 31 */ | 30 */ |
| 32 private InstanceID mInstanceID; | 31 private InstanceID mInstanceID; |
| 33 | 32 |
| 34 private static boolean sBlockOnAsyncTasksForTesting; | 33 private static boolean sBlockOnAsyncTasksForTesting; |
| 35 | 34 |
| 36 private InstanceIDBridge( | 35 private InstanceIDBridge(long nativeInstanceIDAndroid, String subtype) { |
| 37 long nativeInstanceIDAndroid, Context context, String subtype) { | |
| 38 mContext = context.getApplicationContext(); // Storing activity context
would leak activity. | |
| 39 mSubtype = subtype; | 36 mSubtype = subtype; |
| 40 mNativeInstanceIDAndroid = nativeInstanceIDAndroid; | 37 mNativeInstanceIDAndroid = nativeInstanceIDAndroid; |
| 41 } | 38 } |
| 42 | 39 |
| 43 /** | 40 /** |
| 44 * Returns a wrapped {@link InstanceIDWithSubtype}. Multiple InstanceIDBridg
e instances may | 41 * Returns a wrapped {@link InstanceIDWithSubtype}. Multiple InstanceIDBridg
e instances may |
| 45 * share an underlying InstanceIDWithSubtype. | 42 * share an underlying InstanceIDWithSubtype. |
| 46 */ | 43 */ |
| 47 @CalledByNative | 44 @CalledByNative |
| 48 public static InstanceIDBridge create( | 45 public static InstanceIDBridge create(long nativeInstanceIDAndroid, String s
ubtype) { |
| 49 long nativeInstanceIDAndroid, Context context, String subtype) { | 46 return new InstanceIDBridge(nativeInstanceIDAndroid, subtype); |
| 50 return new InstanceIDBridge(nativeInstanceIDAndroid, context, subtype); | |
| 51 } | 47 } |
| 52 | 48 |
| 53 /** | 49 /** |
| 54 * Called when our C++ counterpart is destroyed. Clears the handle to our na
tive C++ object, | 50 * Called when our C++ counterpart is destroyed. Clears the handle to our na
tive C++ object, |
| 55 * ensuring it's not called by pending async tasks. | 51 * ensuring it's not called by pending async tasks. |
| 56 */ | 52 */ |
| 57 @CalledByNative | 53 @CalledByNative |
| 58 private void destroy() { | 54 private void destroy() { |
| 59 mNativeInstanceIDAndroid = 0; | 55 mNativeInstanceIDAndroid = 0; |
| 60 } | 56 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 protected abstract Result doBackgroundWork(); | 187 protected abstract Result doBackgroundWork(); |
| 192 | 188 |
| 193 protected abstract void sendResultToNative(Result result); | 189 protected abstract void sendResultToNative(Result result); |
| 194 | 190 |
| 195 public void execute() { | 191 public void execute() { |
| 196 AsyncTask<Void, Void, Result> task = new AsyncTask<Void, Void, Resul
t>() { | 192 AsyncTask<Void, Void, Result> task = new AsyncTask<Void, Void, Resul
t>() { |
| 197 @Override | 193 @Override |
| 198 protected Result doInBackground(Void... params) { | 194 protected Result doInBackground(Void... params) { |
| 199 synchronized (InstanceIDBridge.this) { | 195 synchronized (InstanceIDBridge.this) { |
| 200 if (mInstanceID == null) { | 196 if (mInstanceID == null) { |
| 201 mInstanceID = InstanceIDWithSubtype.getInstance(mCon
text, mSubtype); | 197 mInstanceID = InstanceIDWithSubtype.getInstance( |
| 198 ContextUtils.getApplicationContext(), mSubty
pe); |
| 202 } | 199 } |
| 203 } | 200 } |
| 204 return doBackgroundWork(); | 201 return doBackgroundWork(); |
| 205 } | 202 } |
| 206 @Override | 203 @Override |
| 207 protected void onPostExecute(Result result) { | 204 protected void onPostExecute(Result result) { |
| 208 if (!sBlockOnAsyncTasksForTesting && mNativeInstanceIDAndroi
d != 0) { | 205 if (!sBlockOnAsyncTasksForTesting && mNativeInstanceIDAndroi
d != 0) { |
| 209 sendResultToNative(result); | 206 sendResultToNative(result); |
| 210 } | 207 } |
| 211 } | 208 } |
| 212 }; | 209 }; |
| 213 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | 210 task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
| 214 if (sBlockOnAsyncTasksForTesting) { | 211 if (sBlockOnAsyncTasksForTesting) { |
| 215 Result result; | 212 Result result; |
| 216 try { | 213 try { |
| 217 // Synchronously block the UI thread until doInBackground re
turns result. | 214 // Synchronously block the UI thread until doInBackground re
turns result. |
| 218 result = task.get(); | 215 result = task.get(); |
| 219 } catch (InterruptedException | ExecutionException e) { | 216 } catch (InterruptedException | ExecutionException e) { |
| 220 throw new IllegalStateException(e); // Shouldn't happen in t
ests. | 217 throw new IllegalStateException(e); // Shouldn't happen in t
ests. |
| 221 } | 218 } |
| 222 sendResultToNative(result); | 219 sendResultToNative(result); |
| 223 } | 220 } |
| 224 } | 221 } |
| 225 } | 222 } |
| 226 } | 223 } |
| OLD | NEW |