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

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: Renamed UrlRequestFactory into UrlRequestContext, addressed some comments. Created 6 years, 2 months 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; 7 import org.json.JSONArray;
8 import org.json.JSONException; 8 import org.json.JSONException;
9 import org.json.JSONObject; 9 import org.json.JSONObject;
10 10
11 /** 11 /**
12 * A config for HttpUrlRequestFactory, which allows runtime configuration of 12 * A config for HttpUrlRequestFactory, which allows runtime configuration of
13 * HttpUrlRequestFactory. 13 * HttpUrlRequestFactory.
14 */ 14 */
15 public class HttpUrlRequestFactoryConfig { 15 public class HttpUrlRequestFactoryConfig extends UrlRequestContextConfig {
16 16
17 /** 17 /**
18 * Default config enables SPDY, QUIC, in memory http cache. 18 * Default config enables SPDY, QUIC, in memory http cache.
19 */ 19 */
20 public HttpUrlRequestFactoryConfig() { 20 public HttpUrlRequestFactoryConfig() {
21 enableLegacyMode(false); 21 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 } 22 }
33 23
34 /** 24 /**
35 * Create config from json serialized using @toString. 25 * Create config from json serialized using @toString.
36 */ 26 */
37 public HttpUrlRequestFactoryConfig(String json) throws JSONException { 27 public HttpUrlRequestFactoryConfig(String json) throws JSONException {
38 mConfig = new JSONObject(json); 28 super(json);
39 } 29 }
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 try {
115 JSONArray quicHints = mConfig.optJSONArray(
116 UrlRequestContextConfig.QUIC_HINTS);
117 if (quicHints == null) {
118 quicHints = new JSONArray();
119 mConfig.put(UrlRequestContextConfig.QUIC_HINTS, quicHints);
120 }
121
122 JSONObject hint = new JSONObject();
123 hint.put(UrlRequestContextConfig.QUIC_HINT_HOST, host);
124 hint.put(UrlRequestContextConfig.QUIC_HINT_PORT, port);
125 hint.put(UrlRequestContextConfig.QUIC_HINT_ALT_PORT, alternatePort);
126 quicHints.put(hint);
127 } catch (JSONException e) {
128 // Intentionally do nothing.
129 }
130 return this;
131 }
132
133 /**
134 * Get JSON string representation of the config.
135 */
136 @Override
137 public String toString() {
138 return mConfig.toString();
139 }
140
141 /**
142 * Sets a boolean value in the config. Returns a reference to the same
143 * config object, so you can chain put calls together.
144 */
145 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) {
146 try {
147 mConfig.put(key, value);
148 } catch (JSONException e) {
149 // Intentionally do nothing.
150 }
151 return this;
152 }
153
154 /**
155 * Sets a long value in the config. Returns a reference to the same
156 * config object, so you can chain put calls together.
157 */
158 private HttpUrlRequestFactoryConfig putLong(String key, long value) {
159 try {
160 mConfig.put(key, value);
161 } catch (JSONException e) {
162 // Intentionally do nothing.
163 }
164 return this;
165 }
166
167 /**
168 * Sets a string value in the config. Returns a reference to the same
169 * config object, so you can chain put calls together.
170 */
171 private HttpUrlRequestFactoryConfig putString(String key, String value) {
172 try {
173 mConfig.put(key, value);
174 } catch (JSONException e) {
175 // Intentionally do nothing.
176 }
177 return this;
178 }
179
180 private JSONObject mConfig = new JSONObject();
181 } 30 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698