OLD | NEW |
| (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.Collection; | |
17 import java.util.List; | |
18 import java.util.Map; | |
19 import java.util.concurrent.Executor; | |
20 import java.util.concurrent.ExecutorService; | |
21 import java.util.concurrent.Executors; | |
22 import java.util.concurrent.ThreadFactory; | |
23 | |
24 /** | |
25 * {@link java.net.HttpURLConnection} backed CronetEngine. | |
26 * | |
27 * <p>Does not support netlogs, transferred data measurement, bidistream, cache,
or priority. | |
28 */ | |
29 final class JavaCronetEngine extends CronetEngine { | |
30 private final String mUserAgent; | |
31 | |
32 private final ExecutorService mExecutorService = | |
33 Executors.newCachedThreadPool(new ThreadFactory() { | |
34 @Override | |
35 public Thread newThread(final Runnable r) { | |
36 return Executors.defaultThreadFactory().newThread(new Runnab
le() { | |
37 @Override | |
38 public void run() { | |
39 Thread.currentThread().setName("JavaCronetEngine"); | |
40 // On android, all background threads (and all threa
ds that are part | |
41 // of background processes) are put in a cgroup that
is allowed to | |
42 // consume up to 5% of CPU - these worker threads sp
end the vast | |
43 // majority of their time waiting on I/O, so making
them contend with | |
44 // background applications for a slice of CPU doesn'
t make much sense. | |
45 // We want to hurry up and get idle. | |
46 android.os.Process.setThreadPriority( | |
47 THREAD_PRIORITY_BACKGROUND + THREAD_PRIORITY
_MORE_FAVORABLE); | |
48 r.run(); | |
49 } | |
50 }); | |
51 } | |
52 }); | |
53 | |
54 JavaCronetEngine(String userAgent) { | |
55 this.mUserAgent = userAgent; | |
56 } | |
57 | |
58 @Override | |
59 public UrlRequest createRequest(String url, UrlRequest.Callback callback, Ex
ecutor executor, | |
60 int priority, Collection<Object> connectionAnnotations, boolean disa
bleCache, | |
61 boolean disableConnectionMigration, boolean allowDirectExecutor) { | |
62 return new JavaUrlRequest( | |
63 callback, mExecutorService, executor, url, mUserAgent, allowDire
ctExecutor); | |
64 } | |
65 | |
66 @Override | |
67 public BidirectionalStream createBidirectionalStream(String url, | |
68 BidirectionalStream.Callback callback, Executor executor, String htt
pMethod, | |
69 List<Map.Entry<String, String>> requestHeaders, | |
70 @BidirectionalStream.Builder.StreamPriority int priority, | |
71 boolean delayRequestHeadersUntilFirstFlush, Collection<Object> conne
ctionAnnotations) { | |
72 throw new UnsupportedOperationException( | |
73 "Can't create a bidi stream - httpurlconnection doesn't have tho
se APIs"); | |
74 } | |
75 | |
76 @Override | |
77 public boolean isEnabled() { | |
78 return true; | |
79 } | |
80 | |
81 @Override | |
82 public String getVersionString() { | |
83 return "CronetHttpURLConnection/" + ApiVersion.getVersion(); | |
84 } | |
85 | |
86 @Override | |
87 public void shutdown() { | |
88 mExecutorService.shutdown(); | |
89 } | |
90 | |
91 @Override | |
92 public void startNetLogToFile(String fileName, boolean logAll) {} | |
93 | |
94 @Override | |
95 public void startNetLogToDisk(String dirPath, boolean logAll, int maxSize) {
} | |
96 | |
97 @Override | |
98 public void stopNetLog() {} | |
99 | |
100 @Override | |
101 public String getCertVerifierData(long timeout) { | |
102 return ""; | |
103 } | |
104 | |
105 @Override | |
106 public byte[] getGlobalMetricsDeltas() { | |
107 return new byte[0]; | |
108 } | |
109 | |
110 @Override | |
111 public int getEffectiveConnectionType() { | |
112 return EffectiveConnectionType.TYPE_UNKNOWN; | |
113 } | |
114 | |
115 @Override | |
116 public int getHttpRttMs() { | |
117 return RttThroughputValues.INVALID_RTT_THROUGHPUT; | |
118 } | |
119 | |
120 @Override | |
121 public int getTransportRttMs() { | |
122 return RttThroughputValues.INVALID_RTT_THROUGHPUT; | |
123 } | |
124 | |
125 @Override | |
126 public int getDownstreamThroughputKbps() { | |
127 return RttThroughputValues.INVALID_RTT_THROUGHPUT; | |
128 } | |
129 | |
130 @Override | |
131 public void configureNetworkQualityEstimatorForTesting( | |
132 boolean useLocalHostRequests, boolean useSmallerResponses) {} | |
133 | |
134 @Override | |
135 public void addRttListener(NetworkQualityRttListener listener) {} | |
136 | |
137 @Override | |
138 public void removeRttListener(NetworkQualityRttListener listener) {} | |
139 | |
140 @Override | |
141 public void addThroughputListener(NetworkQualityThroughputListener listener)
{} | |
142 | |
143 @Override | |
144 public void removeThroughputListener(NetworkQualityThroughputListener listen
er) {} | |
145 | |
146 @Override | |
147 public void addRequestFinishedListener(RequestFinishedInfo.Listener listener
) {} | |
148 | |
149 @Override | |
150 public void removeRequestFinishedListener(RequestFinishedInfo.Listener liste
ner) {} | |
151 | |
152 @Override | |
153 public URLConnection openConnection(URL url) throws IOException { | |
154 return url.openConnection(); | |
155 } | |
156 | |
157 @Override | |
158 public URLConnection openConnection(URL url, Proxy proxy) throws IOException
{ | |
159 return url.openConnection(proxy); | |
160 } | |
161 | |
162 @Override | |
163 public URLStreamHandlerFactory createURLStreamHandlerFactory() { | |
164 // Returning null causes this factory to pass though, which ends up usin
g the platform's | |
165 // implementation. | |
166 return new URLStreamHandlerFactory() { | |
167 @Override | |
168 public URLStreamHandler createURLStreamHandler(String protocol) { | |
169 return null; | |
170 } | |
171 }; | |
172 } | |
173 } | |
OLD | NEW |