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

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: Addressed comments 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(
59 String url, UrlRequest.Callback callback, Executor executor, int pri ority) {
60 return new JavaUrlRequest(callback, mExecutorService, executor, url, mUs erAgent);
61 }
62
63 @Override
64 BidirectionalStream createBidirectionalStream(String url, BidirectionalStrea m.Callback callback,
65 Executor executor, String httpMethod, List<Map.Entry<String, String> > requestHeaders,
66 @BidirectionalStream.Builder.StreamPriority int priority) {
67 throw new UnsupportedOperationException(
68 "Can't create a bidi stream - httpurlconnection doesn't have tho se APIs");
69 }
70
71 @Override
72 boolean isEnabled() {
73 return true;
74 }
75
76 @Override
77 public String getVersionString() {
78 return "CronetHttpURLConnection/" + Version.getVersion();
79 }
80
81 @Override
82 public void shutdown() {
83 mExecutorService.shutdown();
84 }
85
86 @Override
87 public void startNetLogToFile(String fileName, boolean logAll) {}
88
89 @Override
90 public void stopNetLog() {}
91
92 @Override
93 public byte[] getGlobalMetricsDeltas() {
94 return new byte[0];
95 }
96
97 @Override
98 public void enableNetworkQualityEstimator(Executor executor) {}
99
100 @Override
101 void enableNetworkQualityEstimatorForTesting(
102 boolean useLocalHostRequests, boolean useSmallerResponses, Executor executor) {}
103
104 @Override
105 public void addRttListener(NetworkQualityRttListener listener) {}
106
107 @Override
108 public void removeRttListener(NetworkQualityRttListener listener) {}
109
110 @Override
111 public void addThroughputListener(NetworkQualityThroughputListener listener) {}
112
113 @Override
114 public void removeThroughputListener(NetworkQualityThroughputListener listen er) {}
115
116 @Override
117 public URLConnection openConnection(URL url) throws IOException {
118 return url.openConnection();
119 }
120
121 @Override
122 public URLConnection openConnection(URL url, Proxy proxy) throws IOException {
123 return url.openConnection(proxy);
124 }
125
126 @Override
127 public URLStreamHandlerFactory createURLStreamHandlerFactory() {
128 // Returning null causes this factory to pass though, which ends up usin g the platform's
129 // implementation.
130 return new URLStreamHandlerFactory() {
131 @Override
132 public URLStreamHandler createURLStreamHandler(String protocol) {
133 return null;
134 }
135 };
136 }
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698