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

Side by Side Diff: components/cronet/android/api/src/org/chromium/net/BidirectionalStream.java

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase & Conflict Resolution Created 4 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 unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import android.annotation.SuppressLint; 7 import android.annotation.SuppressLint;
8 import android.support.annotation.IntDef;
9 8
10 import java.lang.annotation.Retention;
11 import java.lang.annotation.RetentionPolicy;
12 import java.nio.ByteBuffer; 9 import java.nio.ByteBuffer;
13 import java.util.AbstractMap;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.concurrent.Executor; 10 import java.util.concurrent.Executor;
18 11
19 /** 12 /**
20 * Class for bidirectional sending and receiving of data over HTTP/2 or QUIC con nections. 13 * Class for bidirectional sending and receiving of data over HTTP/2 or QUIC con nections.
21 * Created using {@link BidirectionalStream.Builder}. 14 * Created by {@link Builder}.
22 * 15 *
23 * Note: There are ordering restrictions on methods of {@link BidirectionalStrea m}; 16 * Note: There are ordering restrictions on methods of {@link BidirectionalStrea m};
24 * please see individual methods for description of restrictions. 17 * please see individual methods for description of restrictions.
18 *
19 * {@hide experimental}
25 */ 20 */
26 public abstract class BidirectionalStream { 21 public abstract class BidirectionalStream {
27 /** 22 /**
28 * Builder for {@link BidirectionalStream}s. Allows configuring stream befor e constructing 23 * Builder for {@link BidirectionalStream}s. Allows configuring stream befor e constructing
29 * it via {@link Builder#build}. 24 * it via {@link Builder#build}. Created by
25 * {@link ExperimentalCronetEngine#newBidirectionalStreamBuilder}.
30 */ 26 */
31 public static class Builder { 27 public abstract static class Builder {
32 // All fields are temporary storage of BidirectionalStream configuration to be
33 // copied to BidirectionalStream.
34
35 // CronetEngine to create the stream.
36 private final CronetEngine mCronetEngine;
37 // URL to request.
38 private final String mUrl;
39 // Callback to receive progress callbacks.
40 private final Callback mCallback;
41 // Executor on which callbacks will be invoked.
42 private final Executor mExecutor;
43 // List of request headers, stored as header field name and value pairs.
44 private final ArrayList<Map.Entry<String, String>> mRequestHeaders =
45 new ArrayList<Map.Entry<String, String>>();
46
47 // HTTP method for the request. Default to POST.
48 private String mHttpMethod = "POST";
49 // Priority of the stream. Default is medium.
50 @StreamPriority private int mPriority = STREAM_PRIORITY_MEDIUM;
51
52 private boolean mDelayRequestHeadersUntilFirstFlush;
53
54 // Request reporting annotations.
55 private Collection<Object> mRequestAnnotations;
56
57 /**
58 * Creates a builder for {@link BidirectionalStream} objects. All callba cks for
59 * generated {@code BidirectionalStream} objects will be invoked on
60 * {@code executor}. {@code executor} must not run tasks on the
61 * current thread, otherwise the networking operations may block and exc eptions
62 * may be thrown at shutdown time.
63 *
64 * @param url the URL for the generated stream
65 * @param callback the {@link Callback} object that gets invoked upon di fferent events
66 * occuring
67 * @param executor the {@link Executor} on which {@code callback} method s will be invoked
68 * @param cronetEngine the {@link CronetEngine} used to create the strea m
69 */
70 public Builder(
71 String url, Callback callback, Executor executor, CronetEngine c ronetEngine) {
72 if (url == null) {
73 throw new NullPointerException("URL is required.");
74 }
75 if (callback == null) {
76 throw new NullPointerException("Callback is required.");
77 }
78 if (executor == null) {
79 throw new NullPointerException("Executor is required.");
80 }
81 if (cronetEngine == null) {
82 throw new NullPointerException("CronetEngine is required.");
83 }
84 mUrl = url;
85 mCallback = callback;
86 mExecutor = executor;
87 mCronetEngine = cronetEngine;
88 }
89
90 /** 28 /**
91 * Sets the HTTP method for the request. Returns builder to facilitate c haining. 29 * Sets the HTTP method for the request. Returns builder to facilitate c haining.
92 * 30 *
93 * @param method the method to use for request. Default is 'POST' 31 * @param method the method to use for request. Default is 'POST'
94 * @return the builder to facilitate chaining 32 * @return the builder to facilitate chaining
95 */ 33 */
96 public Builder setHttpMethod(String method) { 34 public abstract Builder setHttpMethod(String method);
97 if (method == null) {
98 throw new NullPointerException("Method is required.");
99 }
100 mHttpMethod = method;
101 return this;
102 }
103 35
104 /** 36 /**
105 * Adds a request header. Returns builder to facilitate chaining. 37 * Adds a request header. Returns builder to facilitate chaining.
106 * 38 *
107 * @param header the header name 39 * @param header the header name
108 * @param value the header value 40 * @param value the header value
109 * @return the builder to facilitate chaining 41 * @return the builder to facilitate chaining
110 */ 42 */
111 public Builder addHeader(String header, String value) { 43 public abstract Builder addHeader(String header, String value);
112 if (header == null) {
113 throw new NullPointerException("Invalid header name.");
114 }
115 if (value == null) {
116 throw new NullPointerException("Invalid header value.");
117 }
118 mRequestHeaders.add(
119 new AbstractMap.SimpleImmutableEntry<String, String>(header, value));
120 return this;
121 }
122
123 /** @hide */
124 @IntDef({
125 STREAM_PRIORITY_IDLE, STREAM_PRIORITY_LOWEST, STREAM_PRIORITY_LO W,
126 STREAM_PRIORITY_MEDIUM, STREAM_PRIORITY_HIGHEST,
127 })
128 @Retention(RetentionPolicy.SOURCE)
129 public @interface StreamPriority {}
130 44
131 /** 45 /**
132 * Lowest stream priority. Passed to {@link #setPriority}. 46 * Lowest stream priority. Passed to {@link #setPriority}.
133 */ 47 */
134 public static final int STREAM_PRIORITY_IDLE = 0; 48 public static final int STREAM_PRIORITY_IDLE = 0;
135 /** 49 /**
136 * Very low stream priority. Passed to {@link #setPriority}. 50 * Very low stream priority. Passed to {@link #setPriority}.
137 */ 51 */
138 public static final int STREAM_PRIORITY_LOWEST = 1; 52 public static final int STREAM_PRIORITY_LOWEST = 1;
139 /** 53 /**
140 * Low stream priority. Passed to {@link #setPriority}. 54 * Low stream priority. Passed to {@link #setPriority}.
141 */ 55 */
142 public static final int STREAM_PRIORITY_LOW = 2; 56 public static final int STREAM_PRIORITY_LOW = 2;
143 /** 57 /**
144 * Medium stream priority. Passed to {@link #setPriority}. This is the 58 * Medium stream priority. Passed to {@link #setPriority}. This is the
145 * default priority given to the stream. 59 * default priority given to the stream.
146 */ 60 */
147 public static final int STREAM_PRIORITY_MEDIUM = 3; 61 public static final int STREAM_PRIORITY_MEDIUM = 3;
148 /** 62 /**
149 * Highest stream priority. Passed to {@link #setPriority}. 63 * Highest stream priority. Passed to {@link #setPriority}.
150 */ 64 */
151 public static final int STREAM_PRIORITY_HIGHEST = 4; 65 public static final int STREAM_PRIORITY_HIGHEST = 4;
152 66
153 /** 67 /**
154 * Sets priority of the stream which should be one of the 68 * Sets priority of the stream which should be one of the
155 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. 69 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values.
156 * The stream is given {@link #STREAM_PRIORITY_MEDIUM} priority if {@lin k 70 * The stream is given {@link #STREAM_PRIORITY_MEDIUM} priority if
157 * #setPriority} is not called. 71 * this method is not called.
158 * 72 *
159 * @param priority priority of the stream which should be one of the 73 * @param priority priority of the stream which should be one of the
160 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values. 74 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values.
161 * @return the builder to facilitate chaining. 75 * @return the builder to facilitate chaining.
162 */ 76 */
163 public Builder setPriority(@StreamPriority int priority) { 77 public abstract Builder setPriority(int priority);
164 mPriority = priority;
165 return this;
166 }
167 78
168 /** 79 /**
169 * Delays sending request headers until {@link BidirectionalStream#flush ()} 80 * Delays sending request headers until {@link BidirectionalStream#flush ()}
170 * is called. This flag is currently only respected when QUIC is negotia ted. 81 * is called. This flag is currently only respected when QUIC is negotia ted.
171 * When true, QUIC will send request header frame along with data frame( s) 82 * When true, QUIC will send request header frame along with data frame( s)
172 * as a single packet when possible. 83 * as a single packet when possible.
173 * 84 *
174 * @param delayRequestHeadersUntilFirstFlush if true, sending request he aders will 85 * @param delayRequestHeadersUntilFirstFlush if true, sending request he aders will
175 * be delayed until flush() is called. 86 * be delayed until flush() is called.
176 * @return the builder to facilitate chaining. 87 * @return the builder to facilitate chaining.
177 */ 88 */
178 public Builder delayRequestHeadersUntilFirstFlush( 89 public abstract Builder delayRequestHeadersUntilFirstFlush(
179 boolean delayRequestHeadersUntilFirstFlush) { 90 boolean delayRequestHeadersUntilFirstFlush);
180 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilFirstF lush;
181 return this;
182 }
183
184 /**
185 * Associates the annotation object with this request. May add more than one.
186 * Passed through to a {@link RequestFinishedInfo.Listener},
187 * see {@link RequestFinishedInfo#getAnnotations}.
188 *
189 * @param annotation an object to pass on to the {@link RequestFinishedI nfo.Listener} with a
190 * {@link RequestFinishedInfo}.
191 * @return the builder to facilitate chaining.
192 *
193 * @hide as it's a prototype.
194 */
195 public Builder addRequestAnnotation(Object annotation) {
196 if (annotation == null) {
197 throw new NullPointerException("Invalid metrics annotation.");
198 }
199 if (mRequestAnnotations == null) {
200 mRequestAnnotations = new ArrayList<Object>();
201 }
202 mRequestAnnotations.add(annotation);
203 return this;
204 }
205 91
206 /** 92 /**
207 * Creates a {@link BidirectionalStream} using configuration from this 93 * Creates a {@link BidirectionalStream} using configuration from this
208 * {@link Builder}. The returned {@code BidirectionalStream} can then be started 94 * {@link Builder}. The returned {@code BidirectionalStream} can then be started
209 * by calling {@link BidirectionalStream#start}. 95 * by calling {@link BidirectionalStream#start}.
210 * 96 *
211 * @return constructed {@link BidirectionalStream} using configuration f rom 97 * @return constructed {@link BidirectionalStream} using configuration f rom
212 * this {@link Builder} 98 * this {@link Builder}
213 */ 99 */
214 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rol ling to the N SDK. 100 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rol ling to the N SDK.
215 public BidirectionalStream build() { 101 public abstract BidirectionalStream build();
216 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExe cutor, mHttpMethod,
217 mRequestHeaders, mPriority, mDelayRequestHeadersUntilFirstFl ush,
218 mRequestAnnotations);
219 }
220 } 102 }
221 103
222 /** 104 /**
223 * Callback class used to receive callbacks from a {@link BidirectionalStrea m}. 105 * Callback class used to receive callbacks from a {@link BidirectionalStrea m}.
224 */ 106 */
225 public abstract static class Callback { 107 public abstract static class Callback {
226 /** 108 /**
227 * Invoked when the stream is ready for reading and writing. 109 * Invoked when the stream is ready for reading and writing.
228 * Consumer may call {@link BidirectionalStream#read read()} to start re ading data. 110 * Consumer may call {@link BidirectionalStream#read read()} to start re ading data.
229 * Consumer may call {@link BidirectionalStream#write write()} to start writing data. 111 * Consumer may call {@link BidirectionalStream#write write()} to start writing data.
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 /** 294 /**
413 * Returns {@code true} if the stream was successfully started and is now 295 * Returns {@code true} if the stream was successfully started and is now
414 * done (succeeded, canceled, or failed). 296 * done (succeeded, canceled, or failed).
415 * 297 *
416 * @return {@code true} if the stream was successfully started and is now 298 * @return {@code true} if the stream was successfully started and is now
417 * done (completed, canceled, or failed), otherwise returns {@code f alse} 299 * done (completed, canceled, or failed), otherwise returns {@code f alse}
418 * to indicate stream is not yet started or is in progress. 300 * to indicate stream is not yet started or is in progress.
419 */ 301 */
420 public abstract boolean isDone(); 302 public abstract boolean isDone();
421 } 303 }
OLDNEW
« no previous file with comments | « components/cronet/android/BUILD.gn ('k') | components/cronet/android/api/src/org/chromium/net/CronetEngine.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698