| Index: components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java
|
| diff --git a/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java
|
| index f3d376c0278596dad3c9f4f81239fc7e8564f5a0..91ca2639d9fa296f507e77cb1d4be1b835ebacd8 100644
|
| --- a/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java
|
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java
|
| @@ -5,10 +5,13 @@
|
| package org.chromium.net.urlconnection;
|
|
|
| import java.io.IOException;
|
| +import java.io.InterruptedIOException;
|
| +import java.net.SocketTimeoutException;
|
| import java.util.concurrent.BlockingQueue;
|
| import java.util.concurrent.Executor;
|
| import java.util.concurrent.LinkedBlockingQueue;
|
| import java.util.concurrent.RejectedExecutionException;
|
| +import java.util.concurrent.TimeUnit;
|
|
|
| /**
|
| * A MessageLoop class for use in {@link CronetHttpURLConnection}.
|
| @@ -42,12 +45,51 @@ class MessageLoop implements Executor {
|
| }
|
|
|
| /**
|
| + * Retrieves a task from the queue with the given timeout.
|
| + *
|
| + * @param timeout Time to wait, in milliseconds, or 0 for no timeout.
|
| + * @return A non-{@code null} Runnable from the queue.
|
| + * @throws InterruptedIOException
|
| + */
|
| + private Runnable take(int timeout) throws InterruptedIOException {
|
| + Runnable task = null;
|
| + try {
|
| + if (timeout <= 0) {
|
| + task = mQueue.take(); // Blocks if the queue is empty.
|
| + } else {
|
| + // poll returns null upon timeout.
|
| + task = mQueue.poll(timeout, TimeUnit.MILLISECONDS);
|
| + }
|
| + } catch (InterruptedException e) {
|
| + InterruptedIOException exception = new InterruptedIOException();
|
| + exception.initCause(e);
|
| + throw exception;
|
| + }
|
| + if (task == null) {
|
| + // This will terminate the loop.
|
| + throw new SocketTimeoutException();
|
| + }
|
| + return task;
|
| + }
|
| +
|
| + /**
|
| * Runs the message loop. Be sure to call {@link MessageLoop#quit()}
|
| * to end the loop. If an interruptedException occurs, the loop cannot be
|
| * started again (see {@link #mLoopFailed}).
|
| * @throws IOException
|
| */
|
| public void loop() throws IOException {
|
| + loop(0);
|
| + }
|
| +
|
| + /**
|
| + * Runs the message loop. Be sure to call {@link MessageLoop#quit()}
|
| + * to end the loop. If an interruptedException occurs, the loop cannot be
|
| + * started again (see {@link #mLoopFailed}).
|
| + * @param timeout Timeout, in milliseconds, or 0 for no timeout.
|
| + * @throws IOException
|
| + */
|
| + public void loop(int timeout) throws IOException {
|
| assert calledOnValidThread();
|
| if (mLoopFailed) {
|
| throw new IllegalStateException(
|
| @@ -60,16 +102,11 @@ class MessageLoop implements Executor {
|
| mLoopRunning = true;
|
| while (mLoopRunning) {
|
| try {
|
| - Runnable task = mQueue.take(); // Blocks if the queue is empty.
|
| - task.run();
|
| - } catch (InterruptedException | RuntimeException e) {
|
| + take(timeout).run();
|
| + } catch (InterruptedIOException | RuntimeException e) {
|
| mLoopRunning = false;
|
| mLoopFailed = true;
|
| - if (e instanceof InterruptedException) {
|
| - throw new IOException(e);
|
| - } else if (e instanceof RuntimeException) {
|
| - throw (RuntimeException) e;
|
| - }
|
| + throw e;
|
| }
|
| }
|
| }
|
|
|