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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java

Issue 725683002: [Cronet] Initial implementation of HttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Misha's comments Created 6 years 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.net.urlconnection;
6
7 import org.chromium.net.ExtendedResponseInfo;
8 import org.chromium.net.ResponseInfo;
9 import org.chromium.net.UrlRequest;
10 import org.chromium.net.UrlRequestContext;
11 import org.chromium.net.UrlRequestException;
12 import org.chromium.net.UrlRequestListener;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.net.HttpURLConnection;
17 import java.net.URL;
18 import java.nio.ByteBuffer;
19 import java.util.concurrent.Executor;
20 import java.util.concurrent.RejectedExecutionException;
21
22 /**
23 * An implementation of HttpURLConnection that uses Cronet to send requests and
24 * receive response. This class inherits a {@code connected} field from the
25 * superclass. That field indicates whether a connection has ever been
26 * attempted.
27 */
28 public class CronetHttpURLConnection extends HttpURLConnection {
29 private final UrlRequestContext mUrlRequestContext;
30 private final MessageLoop mMessageLoop;
31 private final UrlRequest mRequest;
32
33 private CronetInputStream mInputStream;
34 private ResponseInfo mResponseInfo;
35 private ByteBuffer mResponseByteBuffer;
36
37 protected CronetHttpURLConnection(URL url,
38 UrlRequestContext urlRequestContext) {
39 super(url);
40 mUrlRequestContext = urlRequestContext;
41 mMessageLoop = new MessageLoop();
42 mRequest = mUrlRequestContext.createRequest(url.toString(),
43 new CronetUrlRequestListener(),
44 new MessageLoopExecutor(mMessageLoop));
45 mInputStream = new CronetInputStream(this);
46 }
47
48 /**
49 * Wrapper executor class which posts tasks to a message loop.
50 */
51 private static class MessageLoopExecutor implements Executor {
52 private final MessageLoop mLoop;
mmenke 2014/12/02 19:01:49 nit: Blank line between member variables and meth
xunjieli 2014/12/02 20:35:33 Done.
53 public MessageLoopExecutor(MessageLoop messageLoop) {
54 mLoop = messageLoop;
55 }
56
57 @Override
58 public void execute(Runnable command)
59 throws RejectedExecutionException {
60 try {
61 mLoop.postTask(command);
62 } catch (IOException e) {
63 throw new RejectedExecutionException(e);
64 }
65 }
66 }
67
68 /**
69 * Opens a connection to the resource. If the connect method is called when
70 * the connection has already been opened (indicated by the connected field
71 * having the value true), the call is ignored.
72 */
73 @Override
74 public void connect() throws IOException {
75 if (connected) {
76 return;
77 }
78 connected = true;
79 mRequest.start();
80 // Blocks until onResponseStarted or onFailed is called.
81 mMessageLoop.loop();
82 }
83
84 /**
85 * Releases this connection so that its resources may be either reused or
86 * closed.
87 */
88 @Override
89 public void disconnect() {
mmenke 2014/12/02 19:01:49 Disconnect isn't supposed to be threadsafe, right?
xunjieli 2014/12/02 20:35:33 It's not mentioned anywhere in the documentations,
90 // Disconnect before connection is made should have no effect.
91 if (connected) {
92 try {
93 mInputStream.close();
94 } catch (IOException e) {
95 e.printStackTrace();
96 }
97 mInputStream = null;
98 mRequest.cancel();
99 }
100 }
101
102 /**
103 * Returns the response message returned by the remote HTTP server.
104 */
105 @Override
106 public String getResponseMessage() throws IOException {
107 connect();
108 if (mResponseInfo == null) {
109 return "";
110 }
111 return mResponseInfo.getHttpStatusText();
112 }
113
114 /**
115 * Returns the response code returned by the remote HTTP server.
116 */
117 @Override
118 public int getResponseCode() throws IOException {
119 connect();
120 if (mResponseInfo == null) {
121 return 0;
122 }
123 return mResponseInfo.getHttpStatusCode();
124 }
125
126 /**
127 * Returns an InputStream for reading data from the resource pointed by this
128 * URLConnection.
129 */
130 @Override
131 public InputStream getInputStream() throws IOException {
132 connect();
133 return mInputStream;
134 }
135
136 /**
137 * Adds the given property to the request header.
138 */
139 @Override
140 public final void addRequestProperty(String key, String value) {
141 // Note that Cronet right now does not allow setting multiple headers
142 // of the same key, see crbug.com/432719 for more details.
143 setRequestProperty(key, value);
144 }
145
146 /**
147 * Sets the value of the specified request header field.
148 */
149 @Override
150 public final void setRequestProperty(String key, String value) {
151 if (connected) {
152 throw new IllegalStateException(
153 "Cannot set request property after connection is made");
154 }
155 mRequest.addHeader(key, value);
156 }
157
158 /**
159 * Returns whether this connection uses a proxy server.
160 */
161 @Override
162 public boolean usingProxy() {
163 // TODO(xunjieli): implement this.
164 return false;
165 }
166
167 /**
168 * Used by {@link CronetInputStream} to get more data from the network
169 * stack. This should only be called after the request has started. Note
170 * that this call might block if there isn't any more data to be read.
171 */
172 ByteBuffer getMoreData() throws IOException {
173 mResponseByteBuffer = null;
174 mMessageLoop.loop();
175 return mResponseByteBuffer;
176 }
177
178 private class CronetUrlRequestListener implements UrlRequestListener {
179 public CronetUrlRequestListener() {
180 }
181
182 @Override
183 public void onResponseStarted(UrlRequest request, ResponseInfo info) {
184 mResponseInfo = info;
185 // Quits the message loop since we have the headers now.
186 quitMessageLoop();
187 }
188
189 @Override
190 public void onDataReceived(UrlRequest request, ResponseInfo info,
191 ByteBuffer byteBuffer) {
192 mResponseInfo = info;
193 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity());
194 mResponseByteBuffer.put(byteBuffer);
195 mResponseByteBuffer.flip();
196 quitMessageLoop();
197 }
198
199 @Override
200 public void onRedirect(UrlRequest request, ResponseInfo info,
201 String newLocationUrl) {
202 // TODO(xunjieli): Handle redirect and test it.
203 }
204
205 @Override
206 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) {
207 mResponseInfo = info.getResponseInfo();
208 setResponseDataCompleted();
209 }
210
211 @Override
212 public void onFailed(UrlRequest request, ResponseInfo info,
213 UrlRequestException exception) {
214 // TODO(xunjieli): Handle failure.
215 setResponseDataCompleted();
216 }
217
218 /**
219 * Notifies {@link #mInputStream} that transferring of response data has
220 * completed.
221 */
222 private void setResponseDataCompleted() {
223 if (mInputStream != null) {
224 mInputStream.setResponseDataCompleted();
225 }
226 quitMessageLoop();
227 }
228 }
229
230 /**
231 * Posts a quit task to the message loop.
232 */
233 private void quitMessageLoop() {
234 try {
235 mMessageLoop.postQuitTask();
236 } catch (IOException e) {
237 // Ignored.
238 }
239 }
240 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698