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
3 // found in the LICENSE file.
5 package org.chromium.net;
7 import org.json.JSONArray;
8 import org.json.JSONException;
9 import org.json.JSONObject;
12 * A config for UrlRequestContext, which allows runtime configuration of
15 public class UrlRequestContextConfig {
18 * Default config enables SPDY, QUIC, in memory http cache.
20 public UrlRequestContextConfig() {
21 enableLegacyMode(false);
24 enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
28 * Create config from json serialized using @toString.
30 public UrlRequestContextConfig(String json) throws JSONException {
31 mConfig = new JSONObject(json);
35 * Override the user-agent header for all requests.
37 public UrlRequestContextConfig setUserAgent(String userAgent) {
38 return putString(UrlRequestContextConfigList.USER_AGENT, userAgent);
42 return mConfig.optString(UrlRequestContextConfigList.USER_AGENT);
46 * String, path to directory for HTTP Cache and Cookie Storage.
48 public UrlRequestContextConfig setStoragePath(String value) {
49 return putString(UrlRequestContextConfigList.STORAGE_PATH, value);
53 * Boolean, use HttpUrlConnection-based implementation if true. All other
54 * keys are not applicable.
56 public UrlRequestContextConfig enableLegacyMode(boolean value) {
57 return putBoolean(UrlRequestContextConfigList.ENABLE_LEGACY_MODE,
61 boolean legacyMode() {
62 return mConfig.optBoolean(
63 UrlRequestContextConfigList.ENABLE_LEGACY_MODE);
67 * Override the name of the native library backing cronet.
69 public UrlRequestContextConfig setLibraryName(String libName) {
70 return putString(UrlRequestContextConfigList.NATIVE_LIBRARY_NAME,
74 String libraryName() {
75 return mConfig.optString(
76 UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, "cronet");
80 * Boolean, enable QUIC if true.
82 public UrlRequestContextConfig enableQUIC(boolean value) {
83 return putBoolean(UrlRequestContextConfigList.ENABLE_QUIC, value);
87 * Boolean, enable SPDY if true.
89 public UrlRequestContextConfig enableSPDY(boolean value) {
90 return putBoolean(UrlRequestContextConfigList.ENABLE_SPDY, value);
94 * Enumeration, Disable or Enable Disk or Memory Cache and specify its
95 * maximum size in bytes.
97 public enum HttpCache { DISABLED, IN_MEMORY, DISK };
98 public UrlRequestContextConfig enableHttpCache(HttpCache value,
102 return putString(UrlRequestContextConfigList.HTTP_CACHE,
103 UrlRequestContextConfigList.HTTP_CACHE_DISABLED);
105 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
107 return putString(UrlRequestContextConfigList.HTTP_CACHE,
108 UrlRequestContextConfigList.HTTP_CACHE_DISK);
110 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
112 return putString(UrlRequestContextConfigList.HTTP_CACHE,
113 UrlRequestContextConfigList.HTTP_CACHE_MEMORY);
119 * Explicitly mark |host| as supporting QUIC.
120 * Note that enableHttpCache(DISK) is needed to take advantage of 0-RTT
121 * connection establishment between sessions.
123 * @param host of the server that supports QUIC.
124 * @param port of the server that supports QUIC.
125 * @param alternatePort to use for QUIC.
127 public UrlRequestContextConfig addQuicHint(String host,
130 if (host.contains("/")) {
131 throw new IllegalArgumentException("Illegal QUIC Hint Host: "
135 JSONArray quicHints = mConfig.optJSONArray(
136 UrlRequestContextConfigList.QUIC_HINTS);
137 if (quicHints == null) {
138 quicHints = new JSONArray();
139 mConfig.put(UrlRequestContextConfigList.QUIC_HINTS, quicHints);
142 JSONObject hint = new JSONObject();
143 hint.put(UrlRequestContextConfigList.QUIC_HINT_HOST, host);
144 hint.put(UrlRequestContextConfigList.QUIC_HINT_PORT, port);
145 hint.put(UrlRequestContextConfigList.QUIC_HINT_ALT_PORT,
148 } catch (JSONException e) {
149 // Intentionally do nothing.
155 * Get JSON string representation of the config.
158 public String toString() {
159 return mConfig.toString();
163 * Sets a boolean value in the config. Returns a reference to the same
164 * config object, so you can chain put calls together.
166 private UrlRequestContextConfig putBoolean(String key, boolean value) {
168 mConfig.put(key, value);
169 } catch (JSONException e) {
170 // Intentionally do nothing.
176 * Sets a long value in the config. Returns a reference to the same
177 * config object, so you can chain put calls together.
179 private UrlRequestContextConfig putLong(String key, long value) {
181 mConfig.put(key, value);
182 } catch (JSONException e) {
183 // Intentionally do nothing.
189 * Sets a string value in the config. Returns a reference to the same
190 * config object, so you can chain put calls together.
192 private UrlRequestContextConfig putString(String key, String value) {
194 mConfig.put(key, value);
195 } catch (JSONException e) {
196 // Intentionally do nothing.
201 private JSONObject mConfig = new JSONObject();