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

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/BidirectionalStreamBuilderImpl.java

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebase Created 4 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
(Empty)
1 // Copyright 2016 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 package org.chromium.net.impl;
5
6 import android.annotation.SuppressLint;
7 import android.support.annotation.IntDef;
8
9 import org.chromium.net.BidirectionalStream;
10 import org.chromium.net.CronetEngine;
11 import org.chromium.net.ExperimentalBidirectionalStream;
12
13 import java.lang.annotation.Retention;
14 import java.lang.annotation.RetentionPolicy;
15 import java.util.AbstractMap;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Map;
19 import java.util.concurrent.Executor;
20
21 /**
22 * Implementation of {@link ExperimentalBidirectionalStream.Builder}.
23 */
24 public class BidirectionalStreamBuilderImpl extends ExperimentalBidirectionalStr eam.Builder {
25 // All fields are temporary storage of BidirectionalStream configuration to be
26 // copied to BidirectionalStream.
27
28 // CronetEngine to create the stream.
29 private final CronetEngineBase mCronetEngine;
30 // URL to request.
31 private final String mUrl;
32 // Callback to receive progress callbacks.
33 private final BidirectionalStream.Callback mCallback;
34 // Executor on which callbacks will be invoked.
35 private final Executor mExecutor;
36 // List of request headers, stored as header field name and value pairs.
37 private final ArrayList<Map.Entry<String, String>> mRequestHeaders = new Arr ayList<>();
38
39 // HTTP method for the request. Default to POST.
40 private String mHttpMethod = "POST";
41 // Priority of the stream. Default is medium.
42 @StreamPriority
43 private int mPriority = STREAM_PRIORITY_MEDIUM;
44
45 private boolean mDelayRequestHeadersUntilFirstFlush;
46
47 // Request reporting annotations.
48 private Collection<Object> mRequestAnnotations;
49
50 /**
51 * Creates a builder for {@link BidirectionalStream} objects. All callbacks for
52 * generated {@code BidirectionalStream} objects will be invoked on
53 * {@code executor}. {@code executor} must not run tasks on the
54 * current thread, otherwise the networking operations may block and excepti ons
55 * may be thrown at shutdown time.
56 *
57 * @param url the URL for the generated stream
58 * @param callback the {@link BidirectionalStream.Callback} object that gets invoked upon
59 * different events
60 * occuring
61 * @param executor the {@link Executor} on which {@code callback} methods wi ll be invoked
62 * @param cronetEngine the {@link CronetEngine} used to create the stream
63 */
64 BidirectionalStreamBuilderImpl(String url, BidirectionalStream.Callback call back,
65 Executor executor, CronetEngineBase cronetEngine) {
66 super();
67 if (url == null) {
68 throw new NullPointerException("URL is required.");
69 }
70 if (callback == null) {
71 throw new NullPointerException("Callback is required.");
72 }
73 if (executor == null) {
74 throw new NullPointerException("Executor is required.");
75 }
76 if (cronetEngine == null) {
77 throw new NullPointerException("CronetEngine is required.");
78 }
79 mUrl = url;
80 mCallback = callback;
81 mExecutor = executor;
82 mCronetEngine = cronetEngine;
83 }
84
85 /**
86 * Sets the HTTP method for the request. Returns builder to facilitate chain ing.
87 *
88 * @param method the method to use for request. Default is 'POST'
89 * @return the builder to facilitate chaining
90 */
91 public BidirectionalStreamBuilderImpl setHttpMethod(String method) {
92 if (method == null) {
93 throw new NullPointerException("Method is required.");
94 }
95 mHttpMethod = method;
96 return this;
97 }
98
99 /**
100 * Adds a request header. Returns builder to facilitate chaining.
101 *
102 * @param header the header name
103 * @param value the header value
104 * @return the builder to facilitate chaining
105 */
106 public BidirectionalStreamBuilderImpl addHeader(String header, String value) {
107 if (header == null) {
108 throw new NullPointerException("Invalid header name.");
109 }
110 if (value == null) {
111 throw new NullPointerException("Invalid header value.");
112 }
113 mRequestHeaders.add(new AbstractMap.SimpleImmutableEntry<>(header, value ));
114 return this;
115 }
116
117 /**
118 * Sets priority of the stream which should be one of the
119 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values.
120 * The stream is given {@link #STREAM_PRIORITY_MEDIUM} priority
121 * if this method is not called.
122 *
123 * @param priority priority of the stream which should be one of the
124 * {@link #STREAM_PRIORITY_IDLE STREAM_PRIORITY_*} values.
125 * @return the builder to facilitate chaining.
126 */
127 public BidirectionalStreamBuilderImpl setPriority(@StreamPriority int priori ty) {
128 mPriority = priority;
129 return this;
130 }
131
132 /**
133 * Delays sending request headers until {@link BidirectionalStream#flush()}
134 * is called. This flag is currently only respected when QUIC is negotiated.
135 * When true, QUIC will send request header frame along with data frame(s)
136 * as a single packet when possible.
137 *
138 * @param delayRequestHeadersUntilFirstFlush if true, sending request header s will
139 * be delayed until flush() is called.
140 * @return the builder to facilitate chaining.
141 */
142 public BidirectionalStreamBuilderImpl delayRequestHeadersUntilFirstFlush(
143 boolean delayRequestHeadersUntilFirstFlush) {
144 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilFirstFlush ;
145 return this;
146 }
147
148 @Override
149 public ExperimentalBidirectionalStream.Builder addRequestAnnotation(Object a nnotation) {
150 if (annotation == null) {
151 throw new NullPointerException("Invalid metrics annotation.");
152 }
153 if (mRequestAnnotations == null) {
154 mRequestAnnotations = new ArrayList<Object>();
155 }
156 mRequestAnnotations.add(annotation);
157 return this;
158 }
159
160 /**
161 * Creates a {@link BidirectionalStream} using configuration from this
162 * instance. The returned {@code BidirectionalStream} can then be started
163 * by calling {@link BidirectionalStream#start}.
164 *
165 * @return constructed {@link BidirectionalStream}.
166 */
167 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rolling to the N SDK.
168 public BidirectionalStream build() {
169 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExecuto r, mHttpMethod,
170 mRequestHeaders, mPriority, mDelayRequestHeadersUntilFirstFlush,
171 mRequestAnnotations);
172 }
173
174 @IntDef({
175 STREAM_PRIORITY_IDLE, STREAM_PRIORITY_LOWEST, STREAM_PRIORITY_LOW,
176 STREAM_PRIORITY_MEDIUM, STREAM_PRIORITY_HIGHEST,
177 })
178 @Retention(RetentionPolicy.SOURCE)
179 public @interface StreamPriority {}
180 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698