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

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

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync Created 6 years, 1 month 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 org.json.JSONArray;
8 import org.json.JSONException; 7 import org.json.JSONException;
9 import org.json.JSONObject;
10 8
11 /** 9 /**
12 * A config for HttpUrlRequestFactory, which allows runtime configuration of 10 * A config for HttpUrlRequestFactory, which allows runtime configuration of
13 * HttpUrlRequestFactory. 11 * HttpUrlRequestFactory.
14 */ 12 */
15 public class HttpUrlRequestFactoryConfig { 13 public class HttpUrlRequestFactoryConfig extends UrlRequestContextConfig {
16 14
17 /** 15 /**
18 * Default config enables SPDY, QUIC, in memory http cache. 16 * Default config enables SPDY, QUIC, in memory http cache.
19 */ 17 */
20 public HttpUrlRequestFactoryConfig() { 18 public HttpUrlRequestFactoryConfig() {
21 enableLegacyMode(false); 19 super();
22 enableQUIC(false);
23 enableSPDY(true);
24 enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
25 }
26
27 /**
28 * Override the name of the native library backing cronet.
29 */
30 public HttpUrlRequestFactoryConfig setLibraryName(String libName) {
31 return putString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME, libName);
32 } 20 }
33 21
34 /** 22 /**
35 * Create config from json serialized using @toString. 23 * Create config from json serialized using @toString.
36 */ 24 */
37 public HttpUrlRequestFactoryConfig(String json) throws JSONException { 25 public HttpUrlRequestFactoryConfig(String json) throws JSONException {
38 mConfig = new JSONObject(json); 26 super(json);
39 } 27 }
40
41 /**
42 * Boolean, use HttpUrlRequest-based implementation if true. All other
43 * keys are not applicable.
44 */
45 public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) {
46 return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value);
47 }
48
49 boolean legacyMode() {
50 return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE);
51 }
52
53 /**
54 * Boolean, enable QUIC if true.
55 */
56 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) {
57 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value);
58 }
59
60 /**
61 * Boolean, enable SPDY if true.
62 */
63 public HttpUrlRequestFactoryConfig enableSPDY(boolean value) {
64 return putBoolean(UrlRequestContextConfig.ENABLE_SPDY, value);
65 }
66
67 String libraryName() {
68 return mConfig.optString(UrlRequestContextConfig.NATIVE_LIBRARY_NAME,
69 "cronet");
70 }
71
72 /**
73 * Enumeration, Disable or Enable Disk or Memory Cache and specify its
74 * maximum size in bytes.
75 */
76 public enum HttpCache { DISABLED, IN_MEMORY, DISK };
77 public HttpUrlRequestFactoryConfig enableHttpCache(HttpCache value,
78 long maxSize) {
79 switch(value) {
80 case DISABLED:
81 return putString(UrlRequestContextConfig.HTTP_CACHE,
82 UrlRequestContextConfig.HTTP_CACHE_DISABLED);
83 case DISK:
84 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
85 return putString(UrlRequestContextConfig.HTTP_CACHE,
86 UrlRequestContextConfig.HTTP_CACHE_DISK);
87 case IN_MEMORY:
88 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
89 return putString(UrlRequestContextConfig.HTTP_CACHE,
90 UrlRequestContextConfig.HTTP_CACHE_MEMORY);
91 }
92 return this;
93 }
94
95 /**
96 * String, path to directory for HTTP Cache and Cookie Storage.
97 */
98 public HttpUrlRequestFactoryConfig setStoragePath(String value) {
99 return putString(UrlRequestContextConfig.STORAGE_PATH, value);
100 }
101
102 /**
103 * Explicitly mark |host| as supporting QUIC.
104 * Note that enableHttpCache(DISK) is needed to take advantage of 0-RTT
105 * connection establishment between sessions.
106 *
107 * @param host of the server that supports QUIC.
108 * @param port of the server that supports QUIC.
109 * @param alternatePort to use for QUIC.
110 */
111 public HttpUrlRequestFactoryConfig addQuicHint(String host,
112 int port,
113 int alternatePort) {
114 if (host.contains("/")) {
115 throw new IllegalArgumentException("Illegal QUIC Hint Host: " +
116 host);
117 }
118 try {
119 JSONArray quicHints = mConfig.optJSONArray(
120 UrlRequestContextConfig.QUIC_HINTS);
121 if (quicHints == null) {
122 quicHints = new JSONArray();
123 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints);
124 }
125
126 JSONObject hint = new JSONObject();
127 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host);
128 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port);
129 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort);
130 quicHints.put(hint);
131 } catch (JSONException e) {
132 // Intentionally do nothing.
133 }
134 return this;
135 }
136
137 /**
138 * Get JSON string representation of the config.
139 */
140 @Override
141 public String toString() {
142 return mConfig.toString();
143 }
144
145 /**
146 * Sets a boolean value in the config. Returns a reference to the same
147 * config object, so you can chain put calls together.
148 */
149 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) {
150 try {
151 mConfig.put(key, value);
152 } catch (JSONException e) {
153 // Intentionally do nothing.
154 }
155 return this;
156 }
157
158 /**
159 * Sets a long value in the config. Returns a reference to the same
160 * config object, so you can chain put calls together.
161 */
162 private HttpUrlRequestFactoryConfig putLong(String key, long value) {
163 try {
164 mConfig.put(key, value);
165 } catch (JSONException e) {
166 // Intentionally do nothing.
167 }
168 return this;
169 }
170
171 /**
172 * Sets a string value in the config. Returns a reference to the same
173 * config object, so you can chain put calls together.
174 */
175 private HttpUrlRequestFactoryConfig putString(String key, String value) {
176 try {
177 mConfig.put(key, value);
178 } catch (JSONException e) {
179 // Intentionally do nothing.
180 }
181 return this;
182 }
183
184 private JSONObject mConfig = new JSONObject();
185 } 28 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698