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

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

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 6 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.net; 5 package org.chromium.net;
6 6
7 import android.content.Context;
8 import android.util.Log;
9
10 import java.lang.reflect.Constructor;
7 import java.util.concurrent.Executor; 11 import java.util.concurrent.Executor;
8 12
9 /** 13 /**
10 * A factory for {@link UrlRequest}'s, which uses the best HTTP stack 14 * A factory for {@link UrlRequest}'s, which uses the best HTTP stack
11 * available on the current platform. 15 * available on the current platform.
12 */ 16 */
13 public interface UrlRequestFactory { 17 public abstract class UrlRequestFactory {
18 private static final String TAG = "UrlRequestFactory";
19 private static final String CRONET_URL_REQUEST_CONTEXT =
20 "org.chromium.net.CronetUrlRequestContext";
21
14 /** 22 /**
15 * Creates an UrlRequest object. All UrlRequest functions must 23 * Creates an UrlRequest object. All UrlRequest functions must
16 * be called on the Executor's thread, and all callbacks will be called 24 * be called on the Executor's thread, and all callbacks will be called
17 * on the Executor's thread as well. 25 * on the Executor's thread as well.
18 * createRequest itself may be called on any thread. 26 * createRequest itself may be called on any thread.
19 * @param url URL for the request. 27 * @param url URL for the request.
20 * @param listener Callback interface that gets called on different events. 28 * @param listener Callback interface that gets called on different events.
21 * @param executor Executor on which all callbacks will be called. 29 * @param executor Executor on which all callbacks will be called.
22 * @return new request. 30 * @return new request.
23 */ 31 */
24 public abstract UrlRequest createRequest(String url, 32 public abstract UrlRequest createRequest(String url,
25 UrlRequestListener listener, Executor executor); 33 UrlRequestListener listener, Executor executor);
34
35 public static UrlRequestFactory createFactory(Context context,
mmenke 2014/10/06 18:28:45 Should have a comment here.
mmenke 2014/10/06 18:28:45 Don't think we really get anything by having two f
mef 2014/10/07 00:45:18 Done.
mef 2014/10/21 21:27:47 Done.
36 String userAgent, HttpUrlRequestFactoryConfig config) {
37 UrlRequestFactory factory = null;
38 if (config.userAgent().isEmpty()) {
39 config.setUserAgent(UserAgent.from(context));
40 }
41 if (!config.legacyMode()) {
42 factory = createCronetFactory(context, config);
43 }
44 if (factory == null) {
45 // TODO(mef): Fallback to stub implementation.
46 factory = createCronetFactory(context, config);
47 }
48 Log.i(TAG, "Using network stack: " + factory.getName());
49 return factory;
50 }
51
52 /**
53 * @return true if the factory is enabled.
54 */
55 public abstract boolean isEnabled();
56
57 /**
58 * @return a human-readable name of the factory.
59 */
60 public abstract String getName();
61
62 private static UrlRequestFactory createCronetFactory(Context context,
63 HttpUrlRequestFactoryConfig config) {
64 UrlRequestFactory factory = null;
65 try {
66 Class<? extends UrlRequestFactory> factoryClass =
67 UrlRequestFactory.class.getClassLoader().
68 loadClass(CRONET_URL_REQUEST_CONTEXT).
69 asSubclass(UrlRequestFactory.class);
70 Constructor<? extends UrlRequestFactory> constructor =
71 factoryClass.getConstructor(
72 Context.class, HttpUrlRequestFactoryConfig.class);
73 UrlRequestFactory cronetFactory =
74 constructor.newInstance(context, config);
75 if (cronetFactory.isEnabled()) {
76 factory = cronetFactory;
77 }
78 } catch (ClassNotFoundException e) {
79 // Leave as null
mmenke 2014/10/06 18:28:45 nit: +.
mmenke 2014/10/06 18:28:45 Should we even have this case for now, or just thr
mef 2014/10/21 21:27:47 Done.
80 } catch (Exception e) {
81 throw new IllegalStateException(
82 "Cannot instantiate: " +
83 CRONET_URL_REQUEST_CONTEXT,
84 e);
85 }
86 return factory;
87 }
26 } 88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698