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

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: Make UrlRequestFactory into a class that can create factories. 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,
36 String userAgent, HttpUrlRequestFactoryConfig config) {
mef 2014/09/26 19:23:52 I wonder whether |userAgent| should be a separate
Charles 2014/09/26 22:52:26 I vote for config.
mef 2014/09/29 17:57:37 SGTM, will do.
mef 2014/09/30 20:23:15 Done.
37 UrlRequestFactory factory = null;
38 if (!config.legacyMode()) {
39 factory = createCronetFactory(context, userAgent, config);
40 }
41 if (factory == null) {
42 // TODO(mef): Fallback to stub implementation.
43 // throw new ClassNotFoundException();
44 }
45 Log.i(TAG, "Using network stack: " + factory.getName());
46 return factory;
47 }
48
49 /**
50 * Returns true if the factory is enabled.
51 */
52 public abstract boolean isEnabled();
53
54 /**
55 * Returns a human-readable name of the factory.
56 */
57 public abstract String getName();
58
59 private static UrlRequestFactory createCronetFactory(Context context,
60 String userAgent, HttpUrlRequestFactoryConfig config) {
61 UrlRequestFactory factory = null;
62 try {
63 Class<? extends UrlRequestFactory> factoryClass =
64 UrlRequestFactory.class.getClassLoader().
65 loadClass(CRONET_URL_REQUEST_CONTEXT).
66 asSubclass(UrlRequestFactory.class);
67 Constructor<? extends UrlRequestFactory> constructor =
68 factoryClass.getConstructor(
69 Context.class, String.class,
70 HttpUrlRequestFactoryConfig.class);
71 UrlRequestFactory cronetFactory =
72 constructor.newInstance(context, userAgent, config);
73 if (cronetFactory.isEnabled()) {
74 factory = cronetFactory;
75 }
76 } catch (ClassNotFoundException e) {
77 // Leave as null
78 } catch (Exception e) {
79 throw new IllegalStateException(
80 "Cannot instantiate: " +
81 CRONET_URL_REQUEST_CONTEXT,
82 e);
83 }
84 return factory;
85 }
26 } 86 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698