summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Luke Huang <huangluke@google.com> 2019-03-22 03:26:21 +0000
committer Gerrit Code Review <noreply-gerritcodereview@google.com> 2019-03-22 03:26:21 +0000
commitaed6ea52dea9a1df5b385a585ec6d2f3f50b0411 (patch)
treebbd6db9e4b207406721ff69cfece8833c462fa28
parent64bb980f457d843026fd301e05b666a84441d96d (diff)
parentc09f2d69a129daffaeccc56504acd8b3efe4805e (diff)
Merge "Support query cancellation for async DNS API"
-rwxr-xr-xapi/current.txt4
-rw-r--r--core/java/android/net/DnsResolver.java38
-rw-r--r--core/java/android/net/NetworkUtils.java6
-rw-r--r--core/jni/android_net_NetUtils.cpp6
4 files changed, 48 insertions, 6 deletions
diff --git a/api/current.txt b/api/current.txt
index 7580365c1774..e740b85a3e46 100755
--- a/api/current.txt
+++ b/api/current.txt
@@ -27228,8 +27228,8 @@ package android.net {
public final class DnsResolver {
method @NonNull public static android.net.DnsResolver getInstance();
- method public <T> void query(@Nullable android.net.Network, @NonNull byte[], int, @NonNull java.util.concurrent.Executor, @NonNull android.net.DnsResolver.AnswerCallback<T>);
- method public <T> void query(@Nullable android.net.Network, @NonNull String, int, int, int, @NonNull java.util.concurrent.Executor, @NonNull android.net.DnsResolver.AnswerCallback<T>);
+ method public <T> void query(@Nullable android.net.Network, @NonNull byte[], int, @NonNull java.util.concurrent.Executor, @Nullable android.os.CancellationSignal, @NonNull android.net.DnsResolver.AnswerCallback<T>);
+ method public <T> void query(@Nullable android.net.Network, @NonNull String, int, int, int, @NonNull java.util.concurrent.Executor, @Nullable android.os.CancellationSignal, @NonNull android.net.DnsResolver.AnswerCallback<T>);
field public static final int CLASS_IN = 1; // 0x1
field public static final int FLAG_EMPTY = 0; // 0x0
field public static final int FLAG_NO_CACHE_LOOKUP = 4; // 0x4
diff --git a/core/java/android/net/DnsResolver.java b/core/java/android/net/DnsResolver.java
index 93b8cf801d45..59802514c7a3 100644
--- a/core/java/android/net/DnsResolver.java
+++ b/core/java/android/net/DnsResolver.java
@@ -16,6 +16,7 @@
package android.net;
+import static android.net.NetworkUtils.resNetworkCancel;
import static android.net.NetworkUtils.resNetworkQuery;
import static android.net.NetworkUtils.resNetworkResult;
import static android.net.NetworkUtils.resNetworkSend;
@@ -26,6 +27,7 @@ import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
+import android.os.CancellationSignal;
import android.os.Looper;
import android.system.ErrnoException;
import android.util.Log;
@@ -191,11 +193,18 @@ public final class DnsResolver {
* @param query blob message
* @param flags flags as a combination of the FLAGS_* constants
* @param executor The {@link Executor} that the callback should be executed on.
+ * @param cancellationSignal used by the caller to signal if the query should be
+ * cancelled. May be {@code null}.
* @param callback an {@link AnswerCallback} which will be called to notify the caller
- * of the result of dns query.
+ * of the result of dns query.
*/
public <T> void query(@Nullable Network network, @NonNull byte[] query, @QueryFlag int flags,
- @NonNull @CallbackExecutor Executor executor, @NonNull AnswerCallback<T> callback) {
+ @NonNull @CallbackExecutor Executor executor,
+ @Nullable CancellationSignal cancellationSignal,
+ @NonNull AnswerCallback<T> callback) {
+ if (cancellationSignal != null && cancellationSignal.isCanceled()) {
+ return;
+ }
final FileDescriptor queryfd;
try {
queryfd = resNetworkSend((network != null
@@ -205,6 +214,7 @@ public final class DnsResolver {
return;
}
+ maybeAddCancellationSignal(cancellationSignal, queryfd);
registerFDListener(executor, queryfd, callback);
}
@@ -219,12 +229,19 @@ public final class DnsResolver {
* @param nsType dns resource record (RR) type as one of the TYPE_* constants
* @param flags flags as a combination of the FLAGS_* constants
* @param executor The {@link Executor} that the callback should be executed on.
+ * @param cancellationSignal used by the caller to signal if the query should be
+ * cancelled. May be {@code null}.
* @param callback an {@link AnswerCallback} which will be called to notify the caller
- * of the result of dns query.
+ * of the result of dns query.
*/
public <T> void query(@Nullable Network network, @NonNull String domain,
@QueryClass int nsClass, @QueryType int nsType, @QueryFlag int flags,
- @NonNull @CallbackExecutor Executor executor, @NonNull AnswerCallback<T> callback) {
+ @NonNull @CallbackExecutor Executor executor,
+ @Nullable CancellationSignal cancellationSignal,
+ @NonNull AnswerCallback<T> callback) {
+ if (cancellationSignal != null && cancellationSignal.isCanceled()) {
+ return;
+ }
final FileDescriptor queryfd;
try {
queryfd = resNetworkQuery((network != null
@@ -233,6 +250,8 @@ public final class DnsResolver {
callback.onQueryException(e);
return;
}
+
+ maybeAddCancellationSignal(cancellationSignal, queryfd);
registerFDListener(executor, queryfd, callback);
}
@@ -264,6 +283,17 @@ public final class DnsResolver {
});
}
+ private void maybeAddCancellationSignal(@Nullable CancellationSignal cancellationSignal,
+ @NonNull FileDescriptor queryfd) {
+ if (cancellationSignal == null) return;
+ cancellationSignal.setOnCancelListener(
+ () -> {
+ Looper.getMainLooper().getQueue()
+ .removeOnFileDescriptorEventListener(queryfd);
+ resNetworkCancel(queryfd);
+ });
+ }
+
private static class DnsAddressAnswer extends DnsPacket {
private static final String TAG = "DnsResolver.DnsAddressAnswer";
private static final boolean DBG = false;
diff --git a/core/java/android/net/NetworkUtils.java b/core/java/android/net/NetworkUtils.java
index 0ae29b125149..d2d886ba9a7f 100644
--- a/core/java/android/net/NetworkUtils.java
+++ b/core/java/android/net/NetworkUtils.java
@@ -172,6 +172,12 @@ public class NetworkUtils {
public static native byte[] resNetworkResult(FileDescriptor fd) throws ErrnoException;
/**
+ * DNS resolver series jni method.
+ * Attempts to cancel the in-progress query associated with the {@code fd}.
+ */
+ public static native void resNetworkCancel(FileDescriptor fd);
+
+ /**
* Add an entry into the ARP cache.
*/
public static void addArpEntry(Inet4Address ipv4Addr, MacAddress ethAddr, String ifname,
diff --git a/core/jni/android_net_NetUtils.cpp b/core/jni/android_net_NetUtils.cpp
index cfb2dd199f39..d7a981ed3e9d 100644
--- a/core/jni/android_net_NetUtils.cpp
+++ b/core/jni/android_net_NetUtils.cpp
@@ -487,6 +487,11 @@ static jbyteArray android_net_utils_resNetworkResult(JNIEnv *env, jobject thiz,
return answer;
}
+static void android_net_utils_resNetworkCancel(JNIEnv *env, jobject thiz, jobject javaFd) {
+ int fd = jniGetFDFromFileDescriptor(env, javaFd);
+ resNetworkCancel(fd);
+}
+
static jobject android_net_utils_getTcpRepairWindow(JNIEnv *env, jobject thiz, jobject javaFd) {
if (javaFd == NULL) {
jniThrowNullPointerException(env, NULL);
@@ -546,6 +551,7 @@ static const JNINativeMethod gNetworkUtilMethods[] = {
{ "resNetworkSend", "(I[BII)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkSend },
{ "resNetworkQuery", "(ILjava/lang/String;III)Ljava/io/FileDescriptor;", (void*) android_net_utils_resNetworkQuery },
{ "resNetworkResult", "(Ljava/io/FileDescriptor;)[B", (void*) android_net_utils_resNetworkResult },
+ { "resNetworkCancel", "(Ljava/io/FileDescriptor;)V", (void*) android_net_utils_resNetworkCancel },
};
int register_android_net_NetworkUtils(JNIEnv* env)