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.support.annotation.IntDef; |
| 7 |
| 8 import static org.chromium.net.UrlRequest.Builder.REQUEST_PRIORITY_HIGHEST; |
| 9 import static org.chromium.net.UrlRequest.Builder.REQUEST_PRIORITY_IDLE; |
| 10 import static org.chromium.net.UrlRequest.Builder.REQUEST_PRIORITY_LOW; |
| 11 import static org.chromium.net.UrlRequest.Builder.REQUEST_PRIORITY_LOWEST; |
| 12 import static org.chromium.net.UrlRequest.Builder.REQUEST_PRIORITY_MEDIUM; |
| 13 |
| 14 import org.chromium.net.BidirectionalStream; |
| 15 import org.chromium.net.CronetEngine; |
| 16 import org.chromium.net.ExperimentalBidirectionalStream; |
| 17 import org.chromium.net.ExperimentalCronetEngine; |
| 18 import org.chromium.net.UrlRequest; |
| 19 |
| 20 import java.lang.annotation.Retention; |
| 21 import java.lang.annotation.RetentionPolicy; |
| 22 import java.lang.reflect.Constructor; |
| 23 import java.net.URL; |
| 24 import java.util.Collection; |
| 25 import java.util.List; |
| 26 import java.util.Map; |
| 27 import java.util.concurrent.Executor; |
| 28 |
| 29 /** |
| 30 * Base class of {@link CronetUrlRequestContext} and {@link JavaCronetEngine} th
at contains |
| 31 * shared logic. |
| 32 */ |
| 33 public abstract class CronetEngineBase extends ExperimentalCronetEngine { |
| 34 private static final String CRONET_URL_REQUEST_CONTEXT = |
| 35 "org.chromium.net.impl.CronetUrlRequestContext"; |
| 36 |
| 37 /** |
| 38 * Creates a {@link UrlRequest} object. All callbacks will |
| 39 * be called on {@code executor}'s thread. {@code executor} must not run |
| 40 * tasks on the current thread to prevent blocking networking operations |
| 41 * and causing exceptions during shutdown. |
| 42 * |
| 43 * @param url {@link URL} for the request. |
| 44 * @param callback callback object that gets invoked on different events. |
| 45 * @param executor {@link Executor} on which all callbacks will be invoked. |
| 46 * @param priority priority of the request which should be one of the |
| 47 * {@link UrlRequest.Builder#REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_
*} |
| 48 * values. |
| 49 * @param requestAnnotations Objects to pass on to |
| 50 * {@link org.chromium.net.RequestFinishedInfo.Listener}. |
| 51 * @param disableCache disables cache for the request. |
| 52 * If context is not set up to use cache this param has no effect. |
| 53 * @param disableConnectionMigration disables connection migration for this |
| 54 * request if it is enabled for the session. |
| 55 * @param allowDirectExecutor whether executors used by this request are per
mitted |
| 56 * to execute submitted tasks inline. |
| 57 * @return new request. |
| 58 */ |
| 59 protected abstract UrlRequestBase createRequest(String url, UrlRequest.Callb
ack callback, |
| 60 Executor executor, int priority, Collection<Object> requestAnnotatio
ns, |
| 61 boolean disableCache, boolean disableConnectionMigration, boolean al
lowDirectExecutor); |
| 62 |
| 63 /** |
| 64 * Creates a {@link BidirectionalStream} object. {@code callback} methods wi
ll |
| 65 * be invoked on {@code executor}. {@code executor} must not run |
| 66 * tasks on the current thread to prevent blocking networking operations |
| 67 * and causing exceptions during shutdown. |
| 68 * |
| 69 * @param url the URL for the stream |
| 70 * @param callback the object whose methods get invoked upon different event
s |
| 71 * @param executor the {@link Executor} on which all callbacks will be calle
d |
| 72 * @param httpMethod the HTTP method to use for the stream |
| 73 * @param requestHeaders the list of request headers |
| 74 * @param priority priority of the stream which should be one of the |
| 75 * {@link BidirectionalStream.Builder#STREAM_PRIORITY_IDLE STREAM_PR
IORITY_*} |
| 76 * values. |
| 77 * @param delayRequestHeadersUntilFirstFlush whether to delay sending reques
t |
| 78 * headers until flush() is called, and try to combine them |
| 79 * with the next data frame. |
| 80 * @param requestAnnotations Objects to pass on to |
| 81 * {@link org.chromium.net.RequestFinishedInfo.Listener}. |
| 82 * @return a new stream. |
| 83 */ |
| 84 public abstract ExperimentalBidirectionalStream createBidirectionalStream(St
ring url, |
| 85 BidirectionalStream.Callback callback, Executor executor, String htt
pMethod, |
| 86 List<Map.Entry<String, String>> requestHeaders, |
| 87 @BidirectionalStreamBuilderImpl.StreamPriority int priority, |
| 88 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque
stAnnotations); |
| 89 |
| 90 /** |
| 91 * @return {@code true} if the engine is enabled. |
| 92 */ |
| 93 public abstract boolean isEnabled(); |
| 94 |
| 95 static CronetEngine createCronetEngine(CronetEngineBuilderImpl builder) { |
| 96 CronetEngine cronetEngine = null; |
| 97 try { |
| 98 Class<? extends CronetEngineBase> engineClass = |
| 99 builder.getContext() |
| 100 .getClassLoader() |
| 101 .loadClass(CRONET_URL_REQUEST_CONTEXT) |
| 102 .asSubclass(CronetEngineBase.class); |
| 103 Constructor<? extends CronetEngineBase> constructor = |
| 104 engineClass.getConstructor(CronetEngineBuilderImpl.class); |
| 105 CronetEngineBase possibleEngine = constructor.newInstance(builder); |
| 106 if (possibleEngine.isEnabled()) { |
| 107 cronetEngine = possibleEngine; |
| 108 } |
| 109 } catch (ClassNotFoundException e) { |
| 110 // Leave as null. |
| 111 } catch (Exception e) { |
| 112 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_
REQUEST_CONTEXT, e); |
| 113 } |
| 114 return cronetEngine; |
| 115 } |
| 116 |
| 117 @Override |
| 118 public UrlRequest.Builder newUrlRequestBuilder( |
| 119 String url, UrlRequest.Callback callback, Executor executor) { |
| 120 return new UrlRequestBuilderImpl(url, callback, executor, this); |
| 121 } |
| 122 |
| 123 @IntDef({ |
| 124 REQUEST_PRIORITY_IDLE, REQUEST_PRIORITY_LOWEST, REQUEST_PRIORITY_LOW
, |
| 125 REQUEST_PRIORITY_MEDIUM, REQUEST_PRIORITY_HIGHEST, |
| 126 }) |
| 127 @Retention(RetentionPolicy.SOURCE) |
| 128 public @interface RequestPriority {} |
| 129 } |
OLD | NEW |