OLD | NEW |
---|---|
(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 | |
pauljensen
2016/10/03 15:22:37
BidirectionalStream->ExperimentalBidirectionalStre
kapishnikov
2016/10/03 23:49:28
Done.
| |
26 // copied to BidirectionalStream. | |
pauljensen
2016/10/03 15:22:37
BidirectionalStream->CronetBidirectionalStream
kapishnikov
2016/10/03 23:49:28
Done.
| |
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 @Override | |
86 public BidirectionalStreamBuilderImpl setHttpMethod(String method) { | |
87 if (method == null) { | |
88 throw new NullPointerException("Method is required."); | |
89 } | |
90 mHttpMethod = method; | |
91 return this; | |
92 } | |
93 | |
94 @Override | |
95 public BidirectionalStreamBuilderImpl addHeader(String header, String value) { | |
96 if (header == null) { | |
97 throw new NullPointerException("Invalid header name."); | |
98 } | |
99 if (value == null) { | |
100 throw new NullPointerException("Invalid header value."); | |
101 } | |
102 mRequestHeaders.add(new AbstractMap.SimpleImmutableEntry<>(header, value )); | |
103 return this; | |
104 } | |
105 | |
106 @Override | |
107 public BidirectionalStreamBuilderImpl setPriority(@StreamPriority int priori ty) { | |
108 mPriority = priority; | |
109 return this; | |
110 } | |
111 | |
112 @Override | |
113 public BidirectionalStreamBuilderImpl delayRequestHeadersUntilFirstFlush( | |
114 boolean delayRequestHeadersUntilFirstFlush) { | |
115 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilFirstFlush ; | |
116 return this; | |
117 } | |
118 | |
119 @Override | |
120 public ExperimentalBidirectionalStream.Builder addRequestAnnotation(Object a nnotation) { | |
121 if (annotation == null) { | |
122 throw new NullPointerException("Invalid metrics annotation."); | |
123 } | |
124 if (mRequestAnnotations == null) { | |
125 mRequestAnnotations = new ArrayList<Object>(); | |
126 } | |
127 mRequestAnnotations.add(annotation); | |
128 return this; | |
129 } | |
130 | |
131 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rolling to the N SDK. | |
132 public ExperimentalBidirectionalStream build() { | |
133 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExecuto r, mHttpMethod, | |
134 mRequestHeaders, mPriority, mDelayRequestHeadersUntilFirstFlush, | |
135 mRequestAnnotations); | |
136 } | |
137 | |
138 @IntDef({ | |
139 STREAM_PRIORITY_IDLE, STREAM_PRIORITY_LOWEST, STREAM_PRIORITY_LOW, | |
140 STREAM_PRIORITY_MEDIUM, STREAM_PRIORITY_HIGHEST, | |
141 }) | |
142 @Retention(RetentionPolicy.SOURCE) | |
143 public @interface StreamPriority {} | |
144 } | |
OLD | NEW |