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

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: Misha's and Matt'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
21 /**
22 * An implementation of HttpURLConnection that uses Cronet to send requests and
23 * receive response. This class inherits a {@code connected} field from the
24 * superclass. That field indicates whether a connection has ever been
25 * attempted.
26 */
27 public class CronetHttpURLConnection extends HttpURLConnection {
28 private final UrlRequestContext mUrlRequestContext;
29 private final MessageLoop mMessageLoop;
30 private final UrlRequest mRequest;
31
32 private ResponseInfo mResponseInfo;
33 private CronetInputStream mInputStream;
34 private ByteBuffer mResponseByteBuffer;
35
36 protected CronetHttpURLConnection(URL url,
37 UrlRequestContext urlRequestContext) {
38 super(url);
39 mUrlRequestContext = urlRequestContext;
40 mMessageLoop = new MessageLoop();
41 mRequest = mUrlRequestContext.createRequest(url.toString(),
42 new CronetUrlRequestListener(), new MessageLoopExecutor());
43 }
44
45 /**
46 * Wrapper executor class which posts tasks to message loop.
47 */
48 private class MessageLoopExecutor implements Executor {
49 public MessageLoopExecutor() {
50 }
51
52 @Override
53 public void execute(Runnable command) {
54 mMessageLoop.postTask(command);
55 }
56 }
57
58 /**
59 * Opens a connection to the resource. If the connect method is called when
60 * the connection has already been opened (indicated by the connected field
61 * having the value true), the call is ignored.
62 */
63 @Override
64 public void connect() throws IOException {
65 maybeStartRequest();
66 }
67
68 /**
69 * Releases this connection so that its resources may be either reused or
70 * closed.
71 */
72 @Override
73 public void disconnect() {
mmenke 2014/11/26 17:09:12 We should also cancel the URLRequest if started.
mmenke 2014/11/26 17:09:12 Should we set connected to false here? I assume w
xunjieli 2014/11/26 18:37:14 We should not set connected to false, since it ind
xunjieli 2014/11/26 18:37:14 Done. If not started (connected), the disconnect()
74 if (mInputStream != null) {
75 try {
76 mInputStream.close();
77 } catch (IOException e) {
78 e.printStackTrace();
79 }
80 }
81 }
82
83 /**
84 * Returns the response message returned by the remote HTTP server.
85 */
86 @Override
87 public String getResponseMessage() {
88 maybeStartRequest();
89 return mResponseInfo.getHttpStatusText();
90 }
91
92 /**
93 * Returns the response code returned by the remote HTTP server.
94 */
95 @Override
96 public int getResponseCode() {
97 maybeStartRequest();
98 return mResponseInfo.getHttpStatusCode();
mmenke 2014/11/26 17:09:12 Note that these currently throw null pointer excep
xunjieli 2014/11/26 18:37:14 Done.
99 }
100
101 /**
102 * Returns an InputStream for reading data from the resource pointed by this
103 * URLConnection.
104 */
105 @Override
106 public InputStream getInputStream() {
107 if (mInputStream == null) {
108 maybeStartRequest();
109 mInputStream = new CronetInputStream(this);
110 }
111 return mInputStream;
112 }
113
114 /**
115 * Adds the given property to the request header.
116 */
117 @Override
118 public final void addRequestProperty(String key, String value) {
119 setRequestProperty(key, value);
120 }
121
122 /**
123 * Sets the value of the specified request header field.
124 */
125 @Override
126 public final void setRequestProperty(String key, String value) {
127 if (connected) {
128 throw new IllegalStateException(
129 "Cannot set request property after connection is made");
130 }
131 mRequest.addHeader(key, value);
132 }
133
134 /**
135 * Returns whether this connection uses a proxy server or not.
136 */
137 @Override
138 public boolean usingProxy() {
139 // TODO(xunjieli): implement this.
140 return false;
141 }
142
143 /**
144 * Used by {@link CronetInputStream} to get more data from the network
145 * stack. This should only be called after the request has started. Note
146 * that this call might block if there isn't any more data to be read.
147 */
148 ByteBuffer getMoreData() {
149 mMessageLoop.loop();
150 return mResponseByteBuffer;
mmenke 2014/11/26 17:09:12 Suggest setting mResponseByteBuffer to NULL before
xunjieli 2014/11/26 18:37:14 Done.
151 }
152
153 private class CronetUrlRequestListener implements UrlRequestListener {
154 public CronetUrlRequestListener() {
155 }
156
157 @Override
158 public void onResponseStarted(UrlRequest request, ResponseInfo info) {
159 mResponseInfo = info;
160 // Quits the message loop since we have the headers now.
161 mMessageLoop.postQuitTask();
162 }
163
164 @Override
165 public void onDataReceived(UrlRequest request, ResponseInfo info,
166 ByteBuffer byteBuffer) {
167 mResponseInfo = info;
168 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity());
169 mResponseByteBuffer.put(byteBuffer);
170 mResponseByteBuffer.rewind();
mmenke 2014/11/26 17:09:12 Suggest using mResponseByteBuffer.flip() instead -
xunjieli 2014/11/26 18:37:14 Done.
171 mMessageLoop.postQuitTask();
172 }
173
174 @Override
175 public void onRedirect(UrlRequest request, ResponseInfo info,
176 String newLocationUrl) {
177 // TODO(xunjieli): Handle redirect.
178 mResponseInfo = info;
mmenke 2014/11/26 17:09:12 If we follow a couple redirects, and then get an e
xunjieli 2014/11/26 18:37:14 Done.
179 }
180
181 @Override
182 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) {
mmenke 2014/11/26 17:09:12 Set mResponseInfo?
xunjieli 2014/11/26 18:37:14 Done.
183 setResponseDataCompleted();
184 }
185
186 @Override
187 public void onFailed(UrlRequest request, ResponseInfo info,
188 UrlRequestException exception) {
189 // TODO(xunjieli): Handle failure.
190 setResponseDataCompleted();
191 }
192
193 /**
194 * Notifies {@link #mInputStream} that transferring of response data has
195 * completed.
196 */
197 private void setResponseDataCompleted() {
198 if (mInputStream != null) {
199 mInputStream.setResponseDataCompleted();
200 }
201 mMessageLoop.postQuitTask();
202 }
203 }
204
205 /**
206 * On first call, creates and starts UrlRequest, and waits until response
207 * headers are received. Does nothing on subsequent calls.
208 */
209 private void maybeStartRequest() {
210 if (connected) {
211 return;
212 }
213 connected = true;
214 mRequest.start();
215 // Blocks until onResponseStarted or onFailed is called.
216 mMessageLoop.loop();
217 }
218 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698