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

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

Issue 1363723002: [Cronet] Create Builders, rename UrlRequestContext to CronetEngine (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 2014 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
5 package org.chromium.net;
6
7 import android.content.Context;
8 import android.util.Log;
9
10 import java.lang.reflect.Constructor;
11 import java.util.concurrent.Executor;
12
13 /**
14 * A context for {@link UrlRequest}'s, which uses the best HTTP stack
15 * available on the current platform.
16 */
17 public abstract class UrlRequestContext {
18 private static final String TAG = "UrlRequestFactory";
19 private static final String CRONET_URL_REQUEST_CONTEXT =
20 "org.chromium.net.CronetUrlRequestContext";
21
22 /**
23 * Creates a {@link UrlRequest} object. All callbacks will
24 * be called on {@code executor}'s thread. {@code executor} must not run
25 * tasks on the current thread to prevent blocking networking operations
26 * and causing exceptions during shutdown. Request is given medium priority,
27 * see {@link UrlRequest#REQUEST_PRIORITY_MEDIUM}. To specify other
28 * priorities see {@link #createRequest(String, UrlRequestListener,
29 * Executor, int priority)}.
30 *
31 * @param url {@link java.net.URL} for the request.
32 * @param listener callback class that gets called on different events.
33 * @param executor {@link Executor} on which all callbacks will be called.
34 * @return new request.
35 */
36 public abstract UrlRequest createRequest(String url,
37 UrlRequestListener listener, Executor executor);
38
39 /**
40 * Creates a {@link UrlRequest} object. All callbacks will
41 * be called on {@code executor}'s thread. {@code executor} must not run
42 * tasks on the current thread to prevent blocking networking operations
43 * and causing exceptions during shutdown.
44 *
45 * @param url {@link java.net.URL} for the request.
46 * @param listener callback class that gets called on different events.
47 * @param executor {@link Executor} on which all callbacks will be called.
48 * @param priority priority of the request which should be one of the
49 * {@link UrlRequest#REQUEST_PRIORITY_IDLE REQUEST_PRIORITY_*}
50 * values.
51 * @return new request.
52 */
53 public abstract UrlRequest createRequest(String url,
54 UrlRequestListener listener, Executor executor, int priority);
55
56 /**
57 * @return {@code true} if the context is enabled.
58 */
59 abstract boolean isEnabled();
60
61 /**
62 * @return a human-readable version string of the context.
63 */
64 public abstract String getVersionString();
65
66 /**
67 * Shuts down the {@link UrlRequestContext} if there are no active requests,
68 * otherwise throws an exception.
69 *
70 * Cannot be called on network thread - the thread Cronet calls into
71 * Executor on (which is different from the thread the Executor invokes
72 * callbacks on). May block until all the {@code UrlRequestContext}'s
73 * resources have been cleaned up.
74 */
75 public abstract void shutdown();
76
77 /**
78 * Starts NetLog logging to a file. The NetLog is useful for debugging.
79 * The file can be viewed using a Chrome browser navigated to
80 * chrome://net-internals/#import
81 * @param fileName the complete file path. It must not be empty. If the file
82 * exists, it is truncated before starting. If actively logging,
83 * this method is ignored.
84 * @param logAll {@code true} to include basic events, user cookies,
85 * credentials and all transferred bytes in the log.
86 * {@code false} to just include basic events.
87 */
88 public abstract void startNetLogToFile(String fileName, boolean logAll);
89
90 /**
91 * Stops NetLog logging and flushes file to disk. If a logging session is
92 * not in progress, this call is ignored.
93 */
94 public abstract void stopNetLog();
95
96 /**
97 * Creates a {@link UrlRequestContext} with the given
98 * {@link UrlRequestContextConfig}.
99 * @param context Android {@link Context}.
100 * @param config context configuration.
101 */
102 public static UrlRequestContext createContext(Context context,
103 UrlRequestContextConfig config) {
104 UrlRequestContext urlRequestContext = null;
105 if (config.userAgent().isEmpty()) {
106 config.setUserAgent(UserAgent.from(context));
107 }
108 if (!config.legacyMode()) {
109 urlRequestContext = createCronetContext(context, config);
110 }
111 if (urlRequestContext == null) {
112 // TODO(mef): Fallback to stub implementation. Once stub
113 // implementation is available merge with createCronetFactory.
114 urlRequestContext = createCronetContext(context, config);
115 }
116 Log.i(TAG, "Using network stack: "
117 + urlRequestContext.getVersionString());
118 return urlRequestContext;
119 }
120
121 private static UrlRequestContext createCronetContext(Context context,
122 UrlRequestContextConfig config) {
123 UrlRequestContext urlRequestContext = null;
124 try {
125 Class<? extends UrlRequestContext> contextClass =
126 UrlRequestContext.class.getClassLoader()
127 .loadClass(CRONET_URL_REQUEST_CONTEXT)
128 .asSubclass(UrlRequestContext.class);
129 Constructor<? extends UrlRequestContext> constructor =
130 contextClass.getConstructor(
131 Context.class, UrlRequestContextConfig.class);
132 UrlRequestContext cronetContext =
133 constructor.newInstance(context, config);
134 if (cronetContext.isEnabled()) {
135 urlRequestContext = cronetContext;
136 }
137 } catch (ClassNotFoundException e) {
138 // Leave as null.
139 } catch (Exception e) {
140 throw new IllegalStateException(
141 "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT,
142 e);
143 }
144 return urlRequestContext;
145 }
146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698