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