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

Side by Side Diff: components/cronet/README.md

Issue 1363723002: [Cronet] Create Builders, rename UrlRequestContext to CronetEngine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address mef's comments Created 5 years, 2 months 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
1 # Quick Start Guide to Using Cronet 1 # Quick Start Guide to Using Cronet
2 Cronet is the networking stack of Chromium put into a library for use on 2 Cronet is the networking stack of Chromium put into a library for use on
3 mobile. This is the same networking stack that is used in the Chrome browser 3 mobile. This is the same networking stack that is used in the Chrome browser
4 by over a billion people. It offers an easy-to-use, high performance, 4 by over a billion people. It offers an easy-to-use, high performance,
5 standards-compliant, and secure way to perform HTTP requests. Cronet has support 5 standards-compliant, and secure way to perform HTTP requests. Cronet has support
6 for both Android and iOS. On Android, Cronet offers its own Java asynchronous 6 for both Android and iOS. On Android, Cronet offers its own Java asynchronous
7 API as well as support for the [java.net.HttpURLConnection] API. 7 API as well as support for the [java.net.HttpURLConnection] API.
8 This document gives a brief introduction to using these two Java APIs. 8 This document gives a brief introduction to using these two Java APIs.
9 9
10 ### Basics 10 ### Basics
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 ResponseInfo responseInfo, UrlRequestException error) { 61 ResponseInfo responseInfo, UrlRequestException error) {
62 // Request has failed. responseInfo might be null. 62 // Request has failed. responseInfo might be null.
63 Log.e("MyListener", "Request failed. " + error.getMessage()); 63 Log.e("MyListener", "Request failed. " + error.getMessage());
64 // Maybe handle error here. Typical errors include hostname 64 // Maybe handle error here. Typical errors include hostname
65 // not resolved, connection to server refused, etc. 65 // not resolved, connection to server refused, etc.
66 } 66 }
67 } 67 }
68 68
69 Make a request like this: 69 Make a request like this:
70 70
71 UrlRequestContextConfig myConfig = new UrlRequestContextConfig(); 71 CronetEngine.Builder engineBuilder = new CronetEngine.Builder();
72 CronetUrlRequestContext myRequestContext = 72 CronetEngine engine = engineBuilder.build(getContext());
xunjieli 2015/09/28 14:57:05 I usually see build method with 0 argument. Should
mef 2015/09/28 16:31:38 My concern with having setter is that it is not ap
73 new CronetUrlRequestContext(getContext(), myConfig);
74 Executor executor = Executors.newSingleThreadExecutor(); 73 Executor executor = Executors.newSingleThreadExecutor();
75 MyListener listener = new MyListener(); 74 MyListener listener = new MyListener();
76 UrlRequest request = myRequestContext.createRequest( 75 UrlRequest.Builder requestBuilder = new UrlRequest.Builder(
77 "https://www.example.com", listener, executor); 76 "https://www.example.com", listener, executor);
xunjieli 2015/09/28 14:57:05 Why doesn't the UrlRequest.Builder have a build me
mef 2015/09/28 16:31:38 I see your point, but what would somebody do with
mef 2015/09/28 17:13:37 I take it back. Looking at http://cr.openjdk.java.
78 request.start(); 77 UrlRequest request = engine.executeRequest(requestBuilder);
79 78
80 In the above example, `MyListener` extends `UrlRequestListener`. The request 79 In the above example, `MyListener` extends `UrlRequestListener`. The request
81 is started asynchronously. When the response is ready (fully or partially), and 80 is started asynchronously. When the response is ready (fully or partially), and
82 in the event of failures or redirects, `listener`'s methods will be invoked on 81 in the event of failures or redirects, `listener`'s methods will be invoked on
83 `executor`'s thread to inform the client of the request state and/or response 82 `executor`'s thread to inform the client of the request state and/or response
84 information. 83 information.
85 84
86 ### Downloading Data 85 ### Downloading Data
87 When Cronet fetches response headers from the server or gets them from the 86 When Cronet fetches response headers from the server or gets them from the
88 cache, `UrlRequestListener.onResponseStarted` will be invoked. To read the 87 cache, `UrlRequestListener.onResponseStarted` will be invoked. To read the
89 response body, the client should call `UrlRequest.read` and supply a 88 response body, the client should call `UrlRequest.read` and supply a
90 [ByteBuffer] for Cronet to fill. Once a portion or all of 89 [ByteBuffer] for Cronet to fill. Once a portion or all of
91 the response body is read, `UrlRequestListener.onReadCompleted` will be invoked. 90 the response body is read, `UrlRequestListener.onReadCompleted` will be invoked.
92 The client may then read and consume the data within `byteBuffer`. 91 The client may then read and consume the data within `byteBuffer`.
93 Once the client is ready to consume more data, the client should call 92 Once the client is ready to consume more data, the client should call
94 `UrlRequest.read` again. The process continues until 93 `UrlRequest.read` again. The process continues until
95 `UrlRequestListener.onSucceeded` or `UrlRequestListener.onFailed` is invoked, 94 `UrlRequestListener.onSucceeded` or `UrlRequestListener.onFailed` is invoked,
96 which signals the completion of the request. 95 which signals the completion of the request.
97 96
98 ### Uploading Data 97 ### Uploading Data
99 MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider(); 98 MyUploadDataProvider myUploadDataProvider = new MyUploadDataProvider();
100 request.setHttpMethod("POST"); 99 requestBuilder.setHttpMethod("POST");
101 request.setUploadDataProvider(myUploadDataProvider, executor); 100 requestBuilder.setUploadDataProvider(myUploadDataProvider, executor);
102 request.start();
103 101
104 In the above example, `MyUploadDataProvider` extends `UploadDataProvider`. 102 In the above example, `MyUploadDataProvider` extends `UploadDataProvider`.
105 When Cronet is ready to send the request body, 103 When Cronet is ready to send the request body,
106 `myUploadDataProvider.read(UploadDataSink uploadDataSink, 104 `myUploadDataProvider.read(UploadDataSink uploadDataSink,
107 ByteBuffer byteBuffer)` will be invoked. The client will need to write the 105 ByteBuffer byteBuffer)` will be invoked. The client will need to write the
108 request body into `byteBuffer`. Once the client is done writing into 106 request body into `byteBuffer`. Once the client is done writing into
109 `byteBuffer`, the client can let Cronet know by calling 107 `byteBuffer`, the client can let Cronet know by calling
110 `uploadDataSink.onReadSucceeded`. If the request body doesn't fit into 108 `uploadDataSink.onReadSucceeded`. If the request body doesn't fit into
111 `byteBuffer`, the client can continue writing when `UploadDataProvider.read` is 109 `byteBuffer`, the client can continue writing when `UploadDataProvider.read` is
112 invoked again. For more details, please see the API reference. 110 invoked again. For more details, please see the API reference.
113 111
114 ### <a id=configuring-cronet></a> Configuring Cronet 112 ### <a id=configuring-cronet></a> Configuring Cronet
115 Various configuration options are available via the `UrlRequestContextConfig` 113 Various configuration options are available via the `CronetEngine.Builder`
116 object. 114 object.
117 115
118 Enabling HTTP/2, QUIC, or SDCH: 116 Enabling HTTP/2, QUIC, or SDCH:
119 117
120 - For Example: 118 - For Example:
121 119
122 myConfig.enableSPDY(true).enableQUIC(true).enableSDCH(true); 120 engineBuilder.enableSPDY(true).enableQUIC(true).enableSDCH(true);
123 121
124 Controlling the cache: 122 Controlling the cache:
125 123
126 - Use a 100KiB in-memory cache: 124 - Use a 100KiB in-memory cache:
127 125
128 myConfig.enableHttpCache( 126 engineBuilder.enableHttpCache(
129 UrlRequestContextConfig.HttpCache.IN_MEMORY, 100 * 1024); 127 CronetEngine.Builder.HttpCache.IN_MEMORY, 100 * 1024);
130 128
131 - or use a 1MiB disk cache: 129 - or use a 1MiB disk cache:
132 130
133 myConfig.setStoragePath(storagePathString); 131 engineBuilder.setStoragePath(storagePathString);
134 myConfig.enableHttpCache(UrlRequestContextConfig.HttpCache.DISK, 132 engineBuilder.enableHttpCache(CronetEngine.Builder.HttpCache.DISK,
135 1024 * 1024); 133 1024 * 1024);
136 134
137 ### Debugging 135 ### Debugging
138 To get more information about how Cronet is processing network 136 To get more information about how Cronet is processing network
139 requests, you can start and stop **NetLog** logging by calling 137 requests, you can start and stop **NetLog** logging by calling
140 `UrlRequestContext.startNetLogToFile` and `UrlRequestContext.stopNetLog`. 138 `CronetEngine.startNetLogToFile` and `CronetEngine.stopNetLog`.
141 Bear in mind that logs may contain sensitive data. You may analyze the 139 Bear in mind that logs may contain sensitive data. You may analyze the
142 generated log by navigating to [chrome://net-internals#import] using a 140 generated log by navigating to [chrome://net-internals#import] using a
143 Chrome browser. 141 Chrome browser.
144 142
145 # Using the java.net.HttpURLConnection API 143 # Using the java.net.HttpURLConnection API
146 Cronet offers an implementation of the [java.net.HttpURLConnection] API to make 144 Cronet offers an implementation of the [java.net.HttpURLConnection] API to make
147 it easier for apps which rely on this API to use Cronet. 145 it easier for apps which rely on this API to use Cronet.
148 To use Cronet's implementation instead of the system's default implementation, 146 To use Cronet's implementation instead of the system's default implementation,
149 simply do the following: 147 simply do the following:
150 148
151 CronetURLStreamHandlerFactory streamHandlerFactory = 149 CronetURLStreamHandlerFactory streamHandlerFactory =
152 new CronetURLStreamHandlerFactory(getContext(), myConfig); 150 new CronetURLStreamHandlerFactory(getContext(), engineBuilder);
153 URL.setURLStreamHandlerFactory(streamHandlerFactory); 151 URL.setURLStreamHandlerFactory(streamHandlerFactory);
154 152
155 Cronet's 153 Cronet's
156 HttpURLConnection implementation has some limitations as compared to the system 154 HttpURLConnection implementation has some limitations as compared to the system
157 implementation, including not utilizing the default system HTTP cache (Please 155 implementation, including not utilizing the default system HTTP cache (Please
158 see {@link org.chromium.net.urlconnection.CronetURLStreamHandlerFactory} for 156 see {@link org.chromium.net.urlconnection.CronetURLStreamHandlerFactory} for
159 more information). 157 more information).
160 You can configure Cronet and control caching through the 158 You can configure Cronet and control caching through the
161 `UrlRequestContextConfig` instance, `myConfig` 159 `CronetEngine.Builder` instance, `engineBuilder`
162 (See [Configuring Cronet](#configuring-cronet) section), before you pass it 160 (See [Configuring Cronet](#configuring-cronet) section), before you pass it
163 into the `CronetURLStreamHandlerFactory` constructor. 161 into the `CronetURLStreamHandlerFactory` constructor.
164 162
165 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html 163 [ByteBuffer]: https://developer.android.com/reference/java/nio/ByteBuffer.html
166 [chrome://net-internals#import]: chrome://net-internals#import 164 [chrome://net-internals#import]: chrome://net-internals#import
167 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H ttpURLConnection.html 165 [java.net.HttpURLConnection]: https://developer.android.com/reference/java/net/H ttpURLConnection.html
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698