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

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

Issue 1492583002: Add HttpUrlConnection backed implementation of CronetEngine. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update tests and header ordering. Created 5 years 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 2015 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 static android.os.Process.THREAD_PRIORITY_BACKGROUND;
8 import static android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE;
9
10 import java.io.IOException;
11 import java.net.Proxy;
12 import java.net.URL;
13 import java.net.URLConnection;
14 import java.net.URLStreamHandler;
15 import java.net.URLStreamHandlerFactory;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.concurrent.Executor;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.ThreadFactory;
22
23 /**
24 * {@link java.net.HttpURLConnection} backed CronetEngine.
25 *
26 * <p>Does not support netlogs, transferred data measurement, bidistream, cache, or priority.
27 */
28 final class JavaCronetEngine extends CronetEngine {
29 private final String mUserAgent;
30
31 private final ExecutorService mExecutorService =
32 Executors.newCachedThreadPool(new ThreadFactory() {
33 @Override
34 public Thread newThread(final Runnable r) {
35 return Executors.defaultThreadFactory().newThread(new Runnab le() {
36 @Override
37 public void run() {
38 Thread.currentThread().setName("JavaCronetEngine");
39 // On android, all background threads (and all threa ds that are part
40 // of background processes) are put in a cgroup that is allowed to
41 // consume up to 5% of CPU - these worker threads sp end the vast
42 // majority of their time waiting on I/O, so making them contend with
43 // background applications for a slice of CPU doesn' t make much sense.
44 // We want to hurry up and get idle.
45 android.os.Process.setThreadPriority(
46 THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY _MORE_FAVORABLE);
47 r.run();
48 }
49 });
50 }
51 });
52
53 JavaCronetEngine(String userAgent) {
54 this.mUserAgent = userAgent;
55 }
56
57 @Override
58 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex ecutor executor) {
59 return new JavaUrlRequest(callback, mExecutorService, executor, url, mUs erAgent);
pauljensen 2015/12/14 20:02:05 can we make this call createRequest(blah, blah, bl
Charles 2015/12/15 22:32:36 Done.
60 }
61
62 @Override
63 public UrlRequest createRequest(
64 String url, UrlRequest.Callback callback, Executor executor, int pri ority) {
65 return new JavaUrlRequest(callback, mExecutorService, executor, url, mUs erAgent);
66 }
67
68 @Override
69 BidirectionalStream createBidirectionalStream(String url, BidirectionalStrea m.Callback callback,
70 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders) {
71 throw new UnsupportedOperationException(
72 "Can't create a bidi stream - httpurlconnection doesn't have tho se APIs");
73 }
74
75 @Override
76 boolean isEnabled() {
77 return true;
78 }
79
80 @Override
81 public String getVersionString() {
82 return "CronetHttpURLConnection/" + Version.getVersion();
83 }
84
85 @Override
86 public void shutdown() {
87 mExecutorService.shutdown();
88 }
89
90 @Override
91 public void startNetLogToFile(String fileName, boolean logAll) {}
92
93 @Override
94 public void stopNetLog() {}
95
96 @Override
97 public byte[] getGlobalMetricsDeltas() {
98 return new byte[0];
99 }
100
101 @Override
102 public void enableNetworkQualityEstimator(Executor executor) {}
103
104 @Override
105 void enableNetworkQualityEstimatorForTesting(
106 boolean useLocalHostRequests, boolean useSmallerResponses, Executor executor) {}
107
108 @Override
109 public void addRttListener(NetworkQualityRttListener listener) {}
110
111 @Override
112 public void removeRttListener(NetworkQualityRttListener listener) {}
113
114 @Override
115 public void addThroughputListener(NetworkQualityThroughputListener listener) {}
116
117 @Override
118 public void removeThroughputListener(NetworkQualityThroughputListener listen er) {}
119
120 @Override
121 public URLConnection openConnection(URL url) throws IOException {
122 return url.openConnection();
123 }
124
125 @Override
126 public URLConnection openConnection(URL url, Proxy proxy) throws IOException {
127 return url.openConnection(proxy);
128 }
129
130 @Override
131 public URLStreamHandlerFactory createURLStreamHandlerFactory() {
132 // Returning null causes this factory to pass though, which ends up usin g the platform's
133 // implementation.
pauljensen 2015/12/14 20:02:05 Hmm this isn't documented anywhere that I can find
Charles 2015/12/15 22:32:36 Acknowledged.
134 return new URLStreamHandlerFactory() {
135 @Override
136 public URLStreamHandler createURLStreamHandler(String protocol) {
137 return null;
138 }
139 };
140 }
141 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698