Commit 00c9185e authored by Ben Kirchman's avatar Ben Kirchman Committed by Commit Bot

Reland "Move Java classes needed by all Java Cronet implementations to utils."

This is a reland of 5a08e77e

Revert of this CL was due to a build failure. Fix the build failure by
adding the dependency for the new utils target to the Java Cronet
implementation's package target.

Original change's description:
> Move Java classes needed by all Java Cronet implementations to utils.
>
> To minimize code duplication, code that can be shared between the
> JavaUrlRequest and the FakeUrlRequest should be moved to a new shared
> file: JavaUrlRequestUtils.
>
> Context:https://chromium-review.googlesource.com/c/chromium/src/+/1703325
>
> Bug: 669707
> Change-Id: Id0dda127694090504f26b083bf4c561ee902cef4
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1721771
> Reviewed-by: Paul Jensen <pauljensen@chromium.org>
> Commit-Queue: Benjamin Kirchman <kirchman@google.com>
> Cr-Commit-Position: refs/heads/master@{#681835}

Bug: 669707
Change-Id: Ifd4f6118e1bc3f41a151f942199e69451d5ac5c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1724502Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Commit-Queue: Benjamin Kirchman <kirchman@google.com>
Cr-Commit-Position: refs/heads/master@{#682872}
parent 33901df9
......@@ -318,6 +318,16 @@ android_library("cronet_impl_common_base_java") {
srcjar_deps = cronet_impl_common_java_srcjar_deps
}
# cronet_impl_java_util_java.jar - Classes shared between Java Cronet implementations.
android_library("cronet_impl_java_util_java") {
java_files = [ "java/src/org/chromium/net/impl/JavaUrlRequestUtils.java" ]
deps = [
":cronet_api_java",
"//third_party/android_deps:com_android_support_support_annotations_java",
]
}
# cronet_impl_platform_base_java.jar - Java platform based implementation of the Cronet engine.
android_library("cronet_impl_platform_base_java") {
java_files = [
......@@ -331,6 +341,7 @@ android_library("cronet_impl_platform_base_java") {
deps = [
":cronet_api_java",
":cronet_impl_common_base_java",
":cronet_impl_java_util_java",
"//net/android:net_thread_stats_uid_java",
"//third_party/android_deps:com_android_support_support_annotations_java",
"//third_party/jsr-305:jsr_305_javalib",
......@@ -340,10 +351,10 @@ android_library("cronet_impl_platform_base_java") {
# cronet_impl_fake_base_java.jar - Fake implementation of Cronet.
android_library("cronet_impl_fake_base_java") {
java_files = [
"fake/java/org/chromium/net/test/FakeUrlResponse.java",
"fake/java/org/chromium/net/test/FakeCronetController.java",
"fake/java/org/chromium/net/test/FakeCronetEngine.java",
"fake/java/org/chromium/net/test/FakeCronetProvider.java",
"fake/java/org/chromium/net/test/FakeUrlResponse.java",
"fake/java/org/chromium/net/test/ResponseMatcher.java",
"fake/java/org/chromium/net/test/UrlResponseMatcher.java",
]
......@@ -351,6 +362,7 @@ android_library("cronet_impl_fake_base_java") {
deps = [
":cronet_api_java",
":cronet_impl_common_base_java",
":cronet_impl_java_util_java",
]
}
......@@ -551,11 +563,20 @@ java_prebuilt("package_impl_native_java") {
]
}
android_java_prebuilt("package_impl_util_java") {
jar_path = "$_package_dir/cronet_impl_util_java.jar"
deps = [
":package_api_java",
":repackage_util",
]
}
android_java_prebuilt("package_impl_platform_java") {
jar_path = "$_package_dir/cronet_impl_platform_java.jar"
deps = [
":package_api_java",
":package_impl_common_java",
":package_impl_util_java",
":repackage_platform",
]
}
......@@ -565,6 +586,7 @@ android_java_prebuilt("package_impl_fake_java") {
deps = [
":package_api_java",
":package_impl_common_java",
":package_impl_util_java",
":repackage_fake",
]
}
......@@ -672,6 +694,12 @@ jar_src("jar_cronet_impl_fake_java_source") {
jar_path = "$_package_dir/cronet_impl_fake_java-src.jar"
}
jar_src("jar_cronet_impl_util_java_source") {
src_search_dirs = [ "java/src" ]
source_deps = [ ":cronet_impl_java_util_java" ]
jar_path = "$_package_dir/cronet_impl_util_java-src.jar"
}
# List of patterns of .class files to exclude from the jar.
_jar_excluded_patterns = [
# Excludes Android support libraries crbug.com/832770.
......@@ -715,6 +743,13 @@ repackage_jars("repackage_fake") {
]
}
repackage_jars("repackage_util") {
output = "$_package_dir/cronet_impl_util_java.jar"
deps = [
":cronet_impl_java_util_java",
]
}
repackage_jars("repackage_native") {
output = "$_package_dir/cronet_impl_native_java.jar"
deps = cronet_impl_native_java_deps_to_package + [
......@@ -876,6 +911,7 @@ if (!is_component_build) {
]
cronet_javatests_deps_to_package = [
":cronet_fake_javatests",
":cronet_test_apk_java",
"//base:base_java",
"//base:base_java_test_support",
......@@ -884,7 +920,6 @@ if (!is_component_build) {
"//net/android:net_java_test_support",
"//url:url_java",
"//third_party/junit",
":cronet_fake_javatests",
]
android_library("cronet_javatests") {
......@@ -1541,4 +1576,4 @@ if (!is_component_build) {
]
}
}
}
}
\ No newline at end of file
......@@ -18,6 +18,9 @@ import org.chromium.net.ThreadStatsUid;
import org.chromium.net.UploadDataProvider;
import org.chromium.net.UploadDataSink;
import org.chromium.net.UrlResponseInfo;
import org.chromium.net.impl.JavaUrlRequestUtils.CheckedRunnable;
import org.chromium.net.impl.JavaUrlRequestUtils.DirectPreventingExecutor;
import org.chromium.net.impl.JavaUrlRequestUtils.State;
import java.io.IOException;
import java.io.InputStream;
......@@ -104,29 +107,6 @@ final class JavaUrlRequest extends UrlRequestBase {
private HttpURLConnection mCurrentUrlConnection; // Only accessed on mExecutor.
private OutputStreamDataSink mOutputStreamDataSink; // Only accessed on mExecutor.
/**
* /- AWAITING_FOLLOW_REDIRECT <- REDIRECT_RECEIVED <-\ /- READING <--\
* | | | |
* \ / \ /
* NOT_STARTED ---> STARTED ----> AWAITING_READ --->
* COMPLETE
*/
@IntDef({State.NOT_STARTED, State.STARTED, State.REDIRECT_RECEIVED,
State.AWAITING_FOLLOW_REDIRECT, State.AWAITING_READ, State.READING, State.ERROR,
State.COMPLETE, State.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
private @interface State {
int NOT_STARTED = 0;
int STARTED = 1;
int REDIRECT_RECEIVED = 2;
int AWAITING_FOLLOW_REDIRECT = 3;
int AWAITING_READ = 4;
int READING = 5;
int ERROR = 6;
int COMPLETE = 7;
int CANCELLED = 8;
}
// Executor that runs one task at a time on an underlying Executor.
// NOTE: Do not use to wrap user supplied Executor as lock is held while underlying execute()
// is called.
......@@ -780,8 +760,6 @@ final class JavaUrlRequest extends UrlRequestBase {
};
}
private interface CheckedRunnable { void run() throws Exception; }
@Override
public void read(final ByteBuffer buffer) {
Preconditions.checkDirect(buffer);
......@@ -1034,54 +1012,4 @@ final class JavaUrlRequest extends UrlRequestBase {
}
});
}
/**
* Executor that detects and throws if its mDelegate runs a submitted runnable inline.
*/
static final class DirectPreventingExecutor implements Executor {
private final Executor mDelegate;
DirectPreventingExecutor(Executor delegate) {
this.mDelegate = delegate;
}
@Override
public void execute(Runnable command) {
Thread currentThread = Thread.currentThread();
InlineCheckingRunnable runnable = new InlineCheckingRunnable(command, currentThread);
mDelegate.execute(runnable);
if (runnable.mExecutedInline != null) {
throw runnable.mExecutedInline;
} else {
// It's possible that this method is being called on an executor, and the runnable
// that
// was just queued will run on this thread after the current runnable returns. By
// nulling out the mCallingThread field, the InlineCheckingRunnable's current thread
// comparison will not fire.
runnable.mCallingThread = null;
}
}
private static final class InlineCheckingRunnable implements Runnable {
private final Runnable mCommand;
private Thread mCallingThread;
private InlineExecutionProhibitedException mExecutedInline;
private InlineCheckingRunnable(Runnable command, Thread callingThread) {
this.mCommand = command;
this.mCallingThread = callingThread;
}
@Override
public void run() {
if (Thread.currentThread() == mCallingThread) {
// Can't throw directly from here, since the delegate executor could catch this
// exception.
mExecutedInline = new InlineExecutionProhibitedException();
return;
}
mCommand.run();
}
}
}
}
// Copyright 2019 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.net.impl;
import android.support.annotation.IntDef;
import org.chromium.net.InlineExecutionProhibitedException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.Executor;
/**
* Utilities for Java-based UrlRequest implementations.
* {@hide}
*/
public final class JavaUrlRequestUtils {
/**
* State interface for keeping track of the internal state of a {@link UrlRequest}.
*
* /- AWAITING_FOLLOW_REDIRECT <- REDIRECT_RECEIVED <-\ /- READING <--\
* | | | |
* V / V /
* NOT_STARTED -> STARTED -----------------------------------------------> AWAITING_READ -------
* --> COMPLETE
*
*
*/
@IntDef({State.NOT_STARTED, State.STARTED, State.REDIRECT_RECEIVED,
State.AWAITING_FOLLOW_REDIRECT, State.AWAITING_READ, State.READING, State.ERROR,
State.COMPLETE, State.CANCELLED})
@Retention(RetentionPolicy.SOURCE)
public @interface State {
int NOT_STARTED = 0;
int STARTED = 1;
int REDIRECT_RECEIVED = 2;
int AWAITING_FOLLOW_REDIRECT = 3;
int AWAITING_READ = 4;
int READING = 5;
int ERROR = 6;
int COMPLETE = 7;
int CANCELLED = 8;
}
/**
* Interface used to run commands that could throw an exception. Specifically useful for
* calling {@link UrlRequest.Callback}s on a user-supplied executor.
*/
public interface CheckedRunnable { void run() throws Exception; }
/**
* Executor that detects and throws if its mDelegate runs a submitted runnable inline.
*/
public static final class DirectPreventingExecutor implements Executor {
private final Executor mDelegate;
/**
* Constructs an {@link DirectPreventingExecutor} that executes {@link runnable}s on the
* provided {@link Executor}.
*
* @param delegate the {@link Executor} used to run {@link Runnable}s
*/
public DirectPreventingExecutor(Executor delegate) {
this.mDelegate = delegate;
}
/**
* Executes a {@link Runnable} on this {@link Executor} and throws an exception if it is
* being run on the same thread as the calling thread.
*
* @param command the {@link Runnable} to attempt to run
*/
@Override
public void execute(Runnable command) {
Thread currentThread = Thread.currentThread();
InlineCheckingRunnable runnable = new InlineCheckingRunnable(command, currentThread);
mDelegate.execute(runnable);
if (runnable.mExecutedInline != null) {
throw runnable.mExecutedInline;
} else {
// It's possible that this method is being called on an executor, and the runnable
// that
// was just queued will run on this thread after the current runnable returns. By
// nulling out the mCallingThread field, the InlineCheckingRunnable's current thread
// comparison will not fire.
runnable.mCallingThread = null;
}
}
private static final class InlineCheckingRunnable implements Runnable {
private final Runnable mCommand;
private Thread mCallingThread;
private InlineExecutionProhibitedException mExecutedInline;
private InlineCheckingRunnable(Runnable command, Thread callingThread) {
this.mCommand = command;
this.mCallingThread = callingThread;
}
@Override
public void run() {
if (Thread.currentThread() == mCallingThread) {
// Can't throw directly from here, since the delegate executor could catch this
// exception.
mExecutedInline = new InlineExecutionProhibitedException();
return;
}
mCommand.run();
}
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment