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 |
| 8 import org.chromium.net.BidirectionalStream; |
| 9 import org.chromium.net.CronetEngine; |
| 10 import org.chromium.net.ExperimentalBidirectionalStream; |
| 11 |
| 12 import java.util.AbstractMap; |
| 13 import java.util.ArrayList; |
| 14 import java.util.Collection; |
| 15 import java.util.Map; |
| 16 import java.util.concurrent.Executor; |
| 17 |
| 18 /** |
| 19 * Implementation of {@link ExperimentalBidirectionalStream.Builder}. |
| 20 */ |
| 21 public class BidirectionalStreamBuilderImpl extends ExperimentalBidirectionalStr
eam.Builder { |
| 22 // All fields are temporary storage of ExperimentalBidirectionalStream confi
guration to be |
| 23 // copied to CronetBidirectionalStream. |
| 24 |
| 25 // CronetEngine to create the stream. |
| 26 private final CronetEngineBase mCronetEngine; |
| 27 // URL to request. |
| 28 private final String mUrl; |
| 29 // Callback to receive progress callbacks. |
| 30 private final BidirectionalStream.Callback mCallback; |
| 31 // Executor on which callbacks will be invoked. |
| 32 private final Executor mExecutor; |
| 33 // List of request headers, stored as header field name and value pairs. |
| 34 private final ArrayList<Map.Entry<String, String>> mRequestHeaders = new Arr
ayList<>(); |
| 35 |
| 36 // HTTP method for the request. Default to POST. |
| 37 private String mHttpMethod = "POST"; |
| 38 // Priority of the stream. Default is medium. |
| 39 @CronetEngineBase.StreamPriority |
| 40 private int mPriority = STREAM_PRIORITY_MEDIUM; |
| 41 |
| 42 private boolean mDelayRequestHeadersUntilFirstFlush; |
| 43 |
| 44 // Request reporting annotations. |
| 45 private Collection<Object> mRequestAnnotations; |
| 46 |
| 47 /** |
| 48 * Creates a builder for {@link BidirectionalStream} objects. All callbacks
for |
| 49 * generated {@code BidirectionalStream} objects will be invoked on |
| 50 * {@code executor}. {@code executor} must not run tasks on the |
| 51 * current thread, otherwise the networking operations may block and excepti
ons |
| 52 * may be thrown at shutdown time. |
| 53 * |
| 54 * @param url the URL for the generated stream |
| 55 * @param callback the {@link BidirectionalStream.Callback} object that gets
invoked upon |
| 56 * different events |
| 57 * occuring |
| 58 * @param executor the {@link Executor} on which {@code callback} methods wi
ll be invoked |
| 59 * @param cronetEngine the {@link CronetEngine} used to create the stream |
| 60 */ |
| 61 BidirectionalStreamBuilderImpl(String url, BidirectionalStream.Callback call
back, |
| 62 Executor executor, CronetEngineBase cronetEngine) { |
| 63 super(); |
| 64 if (url == null) { |
| 65 throw new NullPointerException("URL is required."); |
| 66 } |
| 67 if (callback == null) { |
| 68 throw new NullPointerException("Callback is required."); |
| 69 } |
| 70 if (executor == null) { |
| 71 throw new NullPointerException("Executor is required."); |
| 72 } |
| 73 if (cronetEngine == null) { |
| 74 throw new NullPointerException("CronetEngine is required."); |
| 75 } |
| 76 mUrl = url; |
| 77 mCallback = callback; |
| 78 mExecutor = executor; |
| 79 mCronetEngine = cronetEngine; |
| 80 } |
| 81 |
| 82 @Override |
| 83 public BidirectionalStreamBuilderImpl setHttpMethod(String method) { |
| 84 if (method == null) { |
| 85 throw new NullPointerException("Method is required."); |
| 86 } |
| 87 mHttpMethod = method; |
| 88 return this; |
| 89 } |
| 90 |
| 91 @Override |
| 92 public BidirectionalStreamBuilderImpl addHeader(String header, String value)
{ |
| 93 if (header == null) { |
| 94 throw new NullPointerException("Invalid header name."); |
| 95 } |
| 96 if (value == null) { |
| 97 throw new NullPointerException("Invalid header value."); |
| 98 } |
| 99 mRequestHeaders.add(new AbstractMap.SimpleImmutableEntry<>(header, value
)); |
| 100 return this; |
| 101 } |
| 102 |
| 103 @Override |
| 104 public BidirectionalStreamBuilderImpl setPriority( |
| 105 @CronetEngineBase.StreamPriority int priority) { |
| 106 mPriority = priority; |
| 107 return this; |
| 108 } |
| 109 |
| 110 @Override |
| 111 public BidirectionalStreamBuilderImpl delayRequestHeadersUntilFirstFlush( |
| 112 boolean delayRequestHeadersUntilFirstFlush) { |
| 113 mDelayRequestHeadersUntilFirstFlush = delayRequestHeadersUntilFirstFlush
; |
| 114 return this; |
| 115 } |
| 116 |
| 117 @Override |
| 118 public ExperimentalBidirectionalStream.Builder addRequestAnnotation(Object a
nnotation) { |
| 119 if (annotation == null) { |
| 120 throw new NullPointerException("Invalid metrics annotation."); |
| 121 } |
| 122 if (mRequestAnnotations == null) { |
| 123 mRequestAnnotations = new ArrayList<Object>(); |
| 124 } |
| 125 mRequestAnnotations.add(annotation); |
| 126 return this; |
| 127 } |
| 128 |
| 129 @SuppressLint("WrongConstant") // TODO(jbudorick): Remove this after rolling
to the N SDK. |
| 130 public ExperimentalBidirectionalStream build() { |
| 131 return mCronetEngine.createBidirectionalStream(mUrl, mCallback, mExecuto
r, mHttpMethod, |
| 132 mRequestHeaders, mPriority, mDelayRequestHeadersUntilFirstFlush, |
| 133 mRequestAnnotations); |
| 134 } |
| 135 } |
OLD | NEW |