Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 | |
| 20 /** | |
| 21 * An implementation of HttpURLConnection that uses Cronet to send requests and | |
| 22 * receive response. This class inherits a {@code connected} field from the | |
| 23 * superclass. That field indicates whether a connection has ever been | |
| 24 * attempted. | |
| 25 */ | |
| 26 public class CronetHttpURLConnection extends HttpURLConnection { | |
|
mef
2014/12/04 20:01:15
So, what happens if they call a method that we don
xunjieli
2014/12/04 20:06:50
They will get default implementations, a lot of th
mef
2014/12/04 20:37:56
Sounds good, thanks!
| |
| 27 private final UrlRequestContext mUrlRequestContext; | |
| 28 private final MessageLoop mMessageLoop; | |
| 29 private final UrlRequest mRequest; | |
| 30 | |
| 31 private CronetInputStream mInputStream; | |
| 32 private ResponseInfo mResponseInfo; | |
| 33 private ByteBuffer mResponseByteBuffer; | |
| 34 | |
| 35 protected CronetHttpURLConnection(URL url, | |
| 36 UrlRequestContext urlRequestContext) { | |
| 37 super(url); | |
| 38 mUrlRequestContext = urlRequestContext; | |
| 39 mMessageLoop = new MessageLoop(); | |
| 40 mRequest = mUrlRequestContext.createRequest(url.toString(), | |
| 41 new CronetUrlRequestListener(), mMessageLoop); | |
| 42 mInputStream = new CronetInputStream(this); | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * Opens a connection to the resource. If the connect method is called when | |
| 47 * the connection has already been opened (indicated by the connected field | |
| 48 * having the value true), the call is ignored. | |
| 49 */ | |
| 50 @Override | |
| 51 public void connect() throws IOException { | |
| 52 if (connected) { | |
| 53 return; | |
| 54 } | |
| 55 connected = true; | |
| 56 mRequest.start(); | |
| 57 // Blocks until onResponseStarted or onFailed is called. | |
| 58 mMessageLoop.loop(); | |
| 59 } | |
| 60 | |
| 61 /** | |
| 62 * Releases this connection so that its resources may be either reused or | |
| 63 * closed. | |
| 64 */ | |
| 65 @Override | |
| 66 public void disconnect() { | |
| 67 // Disconnect before connection is made should have no effect. | |
| 68 if (connected) { | |
| 69 try { | |
| 70 mInputStream.close(); | |
| 71 } catch (IOException e) { | |
| 72 e.printStackTrace(); | |
| 73 } | |
| 74 mInputStream = null; | |
| 75 mRequest.cancel(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 /** | |
| 80 * Returns the response message returned by the remote HTTP server. | |
| 81 */ | |
| 82 @Override | |
| 83 public String getResponseMessage() throws IOException { | |
| 84 connect(); | |
| 85 if (mResponseInfo == null) { | |
| 86 return ""; | |
| 87 } | |
| 88 return mResponseInfo.getHttpStatusText(); | |
| 89 } | |
| 90 | |
| 91 /** | |
| 92 * Returns the response code returned by the remote HTTP server. | |
| 93 */ | |
| 94 @Override | |
| 95 public int getResponseCode() throws IOException { | |
| 96 connect(); | |
| 97 if (mResponseInfo == null) { | |
| 98 return 0; | |
| 99 } | |
| 100 return mResponseInfo.getHttpStatusCode(); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Returns an InputStream for reading data from the resource pointed by this | |
| 105 * URLConnection. | |
| 106 */ | |
| 107 @Override | |
| 108 public InputStream getInputStream() throws IOException { | |
|
mef
2014/12/04 20:37:56
Does default implementation of getContent() use th
xunjieli
2014/12/04 20:55:53
Yes, default implementation uses getInputStream()
| |
| 109 connect(); | |
| 110 return mInputStream; | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * Adds the given property to the request header. | |
| 115 */ | |
| 116 @Override | |
| 117 public final void addRequestProperty(String key, String value) { | |
| 118 // Note that Cronet right now does not allow setting multiple headers | |
| 119 // of the same key, see crbug.com/432719 for more details. | |
| 120 setRequestProperty(key, value); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * Sets the value of the specified request header field. | |
| 125 */ | |
| 126 @Override | |
| 127 public final void setRequestProperty(String key, String value) { | |
| 128 if (connected) { | |
| 129 throw new IllegalStateException( | |
| 130 "Cannot set request property after connection is made"); | |
| 131 } | |
| 132 mRequest.addHeader(key, value); | |
| 133 } | |
| 134 | |
| 135 /** | |
| 136 * Returns whether this connection uses a proxy server. | |
| 137 */ | |
| 138 @Override | |
| 139 public boolean usingProxy() { | |
| 140 // TODO(xunjieli): implement this. | |
| 141 return false; | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * Used by {@link CronetInputStream} to get more data from the network | |
| 146 * stack. This should only be called after the request has started. Note | |
| 147 * that this call might block if there isn't any more data to be read. | |
| 148 */ | |
| 149 ByteBuffer getMoreData() throws IOException { | |
| 150 mResponseByteBuffer = null; | |
| 151 mMessageLoop.loop(); | |
| 152 return mResponseByteBuffer; | |
| 153 } | |
| 154 | |
| 155 private class CronetUrlRequestListener implements UrlRequestListener { | |
| 156 public CronetUrlRequestListener() { | |
| 157 } | |
| 158 | |
| 159 @Override | |
| 160 public void onResponseStarted(UrlRequest request, ResponseInfo info) { | |
| 161 mResponseInfo = info; | |
| 162 // Quits the message loop since we have the headers now. | |
| 163 mMessageLoop.postQuitTask(); | |
| 164 } | |
| 165 | |
| 166 @Override | |
| 167 public void onDataReceived(UrlRequest request, ResponseInfo info, | |
| 168 ByteBuffer byteBuffer) { | |
| 169 mResponseInfo = info; | |
| 170 mResponseByteBuffer = ByteBuffer.allocate(byteBuffer.capacity()); | |
| 171 mResponseByteBuffer.put(byteBuffer); | |
| 172 mResponseByteBuffer.flip(); | |
| 173 mMessageLoop.postQuitTask(); | |
| 174 } | |
| 175 | |
| 176 @Override | |
| 177 public void onRedirect(UrlRequest request, ResponseInfo info, | |
| 178 String newLocationUrl) { | |
| 179 // TODO(xunjieli): Handle redirect and test it. | |
| 180 } | |
| 181 | |
| 182 @Override | |
| 183 public void onSucceeded(UrlRequest request, ExtendedResponseInfo info) { | |
| 184 mResponseInfo = info.getResponseInfo(); | |
| 185 setResponseDataCompleted(); | |
| 186 } | |
| 187 | |
| 188 @Override | |
| 189 public void onFailed(UrlRequest request, ResponseInfo info, | |
| 190 UrlRequestException exception) { | |
| 191 // TODO(xunjieli): Handle failure. | |
| 192 setResponseDataCompleted(); | |
| 193 } | |
| 194 | |
| 195 /** | |
| 196 * Notifies {@link #mInputStream} that transferring of response data has | |
| 197 * completed. | |
| 198 */ | |
| 199 private void setResponseDataCompleted() { | |
| 200 if (mInputStream != null) { | |
| 201 mInputStream.setResponseDataCompleted(); | |
| 202 } | |
| 203 mMessageLoop.postQuitTask(); | |
| 204 } | |
| 205 } | |
| 206 } | |
| OLD | NEW |