Chromium Code Reviews| 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 |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..97e7e4e4d9c7888b93667502530fb4bff89b6b42 |
| --- /dev/null |
| +++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java |
| @@ -0,0 +1,81 @@ |
| +// Copyright 2014 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.urlconnection; |
| + |
| +import java.io.IOException; |
| +import java.util.concurrent.BlockingQueue; |
| +import java.util.concurrent.LinkedBlockingQueue; |
| + |
| +/** |
| + * A package protected MessageLoop class for use in |
| + * {@link CronetHttpURLConnection}. |
| + */ |
| +final class MessageLoop { |
| + private final BlockingQueue<Runnable> mQueue; |
| + // A reusable runnable to quit the message loop. |
| + private final Runnable mQuitTask; |
| + // Indicates whether this message loop is currently running. |
| + private boolean mLoopRunning; |
| + |
| + public MessageLoop() { |
| + mQueue = new LinkedBlockingQueue<Runnable>(); |
| + mQuitTask = new Runnable() { |
| + @Override |
| + public void run() { |
| + mLoopRunning = false; |
| + } |
| + }; |
| + } |
| + |
| + /** |
| + * Runs the message loop. Be sure to call {@link MessageLoop#postQuitTask()} |
| + * to end the loop. |
| + * @throws IOException |
| + */ |
| + public void loop() throws IOException { |
| + if (mLoopRunning) { |
| + throw new IllegalStateException( |
| + "Cannot run loop when it is already running."); |
| + } |
| + mLoopRunning = true; |
| + while (mLoopRunning) { |
| + try { |
| + Runnable task = mQueue.take(); // Blocks if the queue is empty. |
| + task.run(); |
| + } catch (InterruptedException e) { |
| + throw new IOException(e); |
|
mmenke
2014/12/02 19:01:49
This doesn't support resumption after an Interrupt
xunjieli
2014/12/02 20:35:34
OkHttp's implementation retries until the request
mmenke
2014/12/02 20:51:07
We should throw an exception of some sort, then, o
|
| + } |
| + } |
| + } |
| + |
| + /** |
| + * Posts a reusable runnable, {@link #mQuitTask} to quit the loop. This |
| + * causes the {@link #loop()} to terminate after processing all currently |
| + * enqueued messages. |
| + * @throws IOException |
| + */ |
| + public void postQuitTask() throws IOException { |
| + try { |
| + mQueue.put(mQuitTask); |
| + } catch (InterruptedException e) { |
| + throw new IOException(e); |
| + } |
| + } |
| + |
| + /** |
| + * Posts a task to the message loop. |
| + * @throws IOException |
| + */ |
| + public void postTask(Runnable task) throws IOException { |
| + if (task == null) { |
| + throw new IllegalArgumentException(); |
| + } |
| + try { |
| + mQueue.put(task); |
| + } catch (InterruptedException e) { |
| + throw new IOException(e); |
| + } |
| + } |
| +} |