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

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: Added test fixture to compare with the default implementation 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() {
mmenke 2014/11/26 14:59:46 Maybe throw an exception if already running on ano
xunjieli 2014/11/26 16:11:55 Done.
+ 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();
mmenke 2014/11/26 14:59:46 Should we really just be silently eating these?
+ }
+ }
+ }
+
+ /**
+ * Quits the loop. This causes the {@link MessageLoop#loop()} to terminate
+ * after processing all currently enqueued messages.
+ */
+ public void quit() {
mmenke 2014/11/26 14:59:46 postQuitMessage? That makes it clearer it doesn't
xunjieli 2014/11/26 16:11:56 Done.
+ try {
+ mQueue.put(new Message(null));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Message class that represents a task to run.
+ */
+ static class Message {
mmenke 2014/11/26 14:59:46 Alternatively... Charles was concerned about maki
mmenke 2014/11/26 14:59:46 per other comment, suggest making this private.
xunjieli 2014/11/26 16:11:56 Done. That's neat! thanks!
+ 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