Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(841)

Unified Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java

Issue 725683002: [Cronet] Initial implementation of HttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Matt's comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a919d9875a04fbe940ceba1ae12adaa72a5722f9
--- /dev/null
+++ b/components/cronet/android/java/src/org/chromium/net/urlconnection/MessageLoop.java
@@ -0,0 +1,66 @@
+// 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.util.concurrent.BlockingQueue;
+
+/**
+ * A package protected MessageLoop class for use in
+ * {@link CronetHttpURLConnection}
+ */
+final class MessageLoop {
+ private final BlockingQueue<Message> mQueue;
+
+ public MessageLoop(BlockingQueue<Message> queue) {
+ mQueue = queue;
+ }
+
+ /**
+ * Runs the message loop. Be sure to call {@link MessageLoop#quit()} to end
+ * the loop.
+ */
+ public void loop() {
+ while (true) {
+ try {
+ Message msg = mQueue.take(); // Blocks if the queue is empty.
+ if (msg.mTask != null) {
+ msg.mTask.run();
+ } else {
+ // Quits the message loop.
+ break;
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ /**
+ * Quits the loop. This causes the {@link MessageLoop#loop()} to terminate
+ * after processing all currently enqueued messages.
+ */
+ public void quit() {
+ try {
+ mQueue.put(new Message(null));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Message class that represents a task to run.
+ */
+ static class Message {
+ private final Runnable mTask;
+
+ /**
+ * Constructs a Message with a given task.
+ * @param task If null, it is a quit signal to the message loop.
+ */
+ public Message(Runnable task) {
+ mTask = task;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698