Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/impl/CronetEngineBase.java

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Rebased onto Charles change + Paul's Comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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);
pauljensen 2016/09/26 14:51:22 I'm not a fan of these odd functions that try and
kapishnikov 2016/09/27 18:38:26 Acknowledged. I agree.
61
62 /**
63 * Creates a {@link BidirectionalStream} object. {@code callback} methods wi ll
64 * be invoked on {@code executor}. {@code executor} must not run
65 * tasks on the current thread to prevent blocking networking operations
66 * and causing exceptions during shutdown.
67 *
68 * @param url the URL for the stream
69 * @param callback the object whose methods get invoked upon different event s
70 * @param executor the {@link Executor} on which all callbacks will be calle d
71 * @param httpMethod the HTTP method to use for the stream
72 * @param requestHeaders the list of request headers
73 * @param priority priority of the stream which should be one of the
74 * {@link BidirectionalStream.Builder#STREAM_PRIORITY_IDLE STREAM_PR IORITY_*}
75 * values.
76 * @param delayRequestHeadersUntilFirstFlush whether to delay sending reques t
77 * headers until flush() is called, and try to combine them
78 * with the next data frame.
79 * @param requestAnnotations Objects to pass on to {@link RequestFinishedInf o.Listener}.
80 * @return a new stream.
81 */
82 public abstract BidirectionalStream createBidirectionalStream(String url,
83 BidirectionalStream.Callback callback, Executor executor, String htt pMethod,
84 List<Map.Entry<String, String>> requestHeaders,
85 @BidirectionalStreamBuilderImpl.StreamPriority int priority,
86 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> reque stAnnotations);
87
88 /**
89 * @return {@code true} if the engine is enabled.
90 */
91 public abstract boolean isEnabled();
92
93 static CronetEngine createCronetEngine(CronetEngineBuilderImpl builder) {
94 CronetEngine cronetEngine = null;
95 try {
96 Class<? extends CronetEngineBase> engineClass =
97 builder.getContext()
98 .getClassLoader()
99 .loadClass(CRONET_URL_REQUEST_CONTEXT)
100 .asSubclass(CronetEngineBase.class);
101 Constructor<? extends CronetEngineBase> constructor =
102 engineClass.getConstructor(CronetEngineBuilderImpl.class);
103 CronetEngineBase possibleEngine = constructor.newInstance(builder);
pauljensen 2016/09/26 14:51:22 eh what's the point of this? I don't think we sho
kapishnikov 2016/09/27 18:38:26 Agree. I will fix it.
kapishnikov 2016/09/30 16:01:43 Done.
104 if (possibleEngine.isEnabled()) {
105 cronetEngine = possibleEngine;
106 }
107 } catch (ClassNotFoundException e) {
108 // Leave as null.
109 } catch (Exception e) {
110 throw new IllegalStateException("Cannot instantiate: " + CRONET_URL_ REQUEST_CONTEXT, e);
111 }
112 return cronetEngine;
113 }
114
115 @Override
116 public UrlRequest.Builder newUrlRequestBuilder(
117 String url, UrlRequest.Callback callback, Executor executor) {
118 return new UrlRequestBuilderImpl(url, callback, executor, this);
119 }
120
121 @IntDef({
122 REQUEST_PRIORITY_IDLE, REQUEST_PRIORITY_LOWEST, REQUEST_PRIORITY_LOW ,
123 REQUEST_PRIORITY_MEDIUM, REQUEST_PRIORITY_HIGHEST,
124 })
125 @Retention(RetentionPolicy.SOURCE)
126 public @interface RequestPriority {}
127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698