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

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

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 6 years, 1 month 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 an UrlRequest object. All UrlRequest functions must
24 * be called on the Executor's thread, and all callbacks will be called
25 * on the Executor's thread as well. Executor must not run tasks on the
26 * current thread to prevent network jank and exception during shutdown.
27 *
28 * createRequest itself may be called on any thread.
29 * @param url URL for the request.
30 * @param listener Callback interface that gets called on different events.
31 * @param executor Executor on which all callbacks will be called.
32 * @return new request.
33 */
34 public abstract UrlRequest createRequest(String url,
35 UrlRequestListener listener, Executor executor);
36
37 /**
38 * @return true if the context is enabled.
39 */
40 public abstract boolean isEnabled();
41
42 /**
43 * @return a human-readable version string of the context.
44 */
45 public abstract String getVersionString();
46
47 /**
48 * Shutdown the UrlRequestContext if there are no active requests, otherwise
49 * throw an exception. Cannot be called on network thread.
mmenke 2014/11/06 17:31:29 nit: Maybe "Cannot be called on network thread -
mef 2014/11/06 22:51:47 Done.
50 */
51 public abstract void shutdown();
52
53 /**
54 * Create context with given config. If config.legacyMode is true, or
55 * native library is not available, then creates HttpUrlConnection-based
56 * context.
57 * @param context application context.
58 * @param config context configuration.
59 */
60 public static UrlRequestContext createContext(Context context,
61 UrlRequestContextConfig config) {
62 UrlRequestContext urlRequestContext = null;
63 if (config.userAgent().isEmpty()) {
64 config.setUserAgent(UserAgent.from(context));
65 }
66 if (!config.legacyMode()) {
67 urlRequestContext = createCronetContext(context, config);
68 }
69 if (urlRequestContext == null) {
70 // TODO(mef): Fallback to stub implementation. Once stub
71 // implementation is available merge with createCronetFactory.
72 urlRequestContext = createCronetContext(context, config);
73 }
74 Log.i(TAG, "Using network stack: "
75 + urlRequestContext.getVersionString());
76 return urlRequestContext;
77 }
78
79 private static UrlRequestContext createCronetContext(Context context,
80 UrlRequestContextConfig config) {
81 UrlRequestContext urlRequestContext = null;
82 try {
83 Class<? extends UrlRequestContext> contextClass =
84 UrlRequestContext.class.getClassLoader()
85 .loadClass(CRONET_URL_REQUEST_CONTEXT)
86 .asSubclass(UrlRequestContext.class);
87 Constructor<? extends UrlRequestContext> constructor =
88 contextClass.getConstructor(
89 Context.class, UrlRequestContextConfig.class);
90 UrlRequestContext cronetContext =
91 constructor.newInstance(context, config);
92 if (cronetContext.isEnabled()) {
93 urlRequestContext = cronetContext;
94 }
95 } catch (ClassNotFoundException e) {
96 // Leave as null.
97 } catch (Exception e) {
98 throw new IllegalStateException(
99 "Cannot instantiate: " + CRONET_URL_REQUEST_CONTEXT,
100 e);
101 }
102 return urlRequestContext;
103 }
104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698