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