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