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.JSONException;
8 import org.json.JSONObject;
11 * A config for HttpUrlRequestFactory, which allows runtime configuration of
12 * HttpUrlRequestFactory.
14 public class HttpUrlRequestFactoryConfig {
16 * Default config enables SPDY, QUIC, in memory http cache.
18 public HttpUrlRequestFactoryConfig() {
19 enableLegacyMode(false);
22 enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
26 * Create config from json serialized using @toString.
28 public HttpUrlRequestFactoryConfig(String json) throws JSONException {
29 mConfig = new JSONObject(json);
33 * Boolean, use HttpUrlRequest-based implementation if true. All other
34 * keys are not applicable.
36 public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) {
37 return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value);
40 public boolean legacyMode() {
41 return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE);
45 * Boolean, enable QUIC if true.
47 public HttpUrlRequestFactoryConfig enableQUIC(boolean value) {
48 return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value);
52 * Boolean, enable SPDY if true.
54 public HttpUrlRequestFactoryConfig enableSPDY(boolean value) {
55 return putBoolean(UrlRequestContextConfig.ENABLE_SPDY, value);
59 * Enumeration, Disable or Enable Disk or Memory Cache and specify its
60 * maximum size in bytes.
62 public enum HttpCache { DISABLED, IN_MEMORY, DISK };
63 public HttpUrlRequestFactoryConfig enableHttpCache(HttpCache value,
67 return putString(UrlRequestContextConfig.HTTP_CACHE,
68 UrlRequestContextConfig.HTTP_CACHE_DISABLED);
70 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
71 return putString(UrlRequestContextConfig.HTTP_CACHE,
72 UrlRequestContextConfig.HTTP_CACHE_DISK);
74 putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
75 return putString(UrlRequestContextConfig.HTTP_CACHE,
76 UrlRequestContextConfig.HTTP_CACHE_MEMORY);
82 * String, path to directory for HTTP Cache and Cookie Storage.
84 public HttpUrlRequestFactoryConfig setStoragePath(String value) {
85 return putString(UrlRequestContextConfig.STORAGE_PATH, value);
89 * Get JSON string representation of the config.
91 public String toString() {
92 return mConfig.toString();
96 * Sets a boolean value in the config. Returns a reference to the same
97 * config object, so you can chain put calls together.
99 private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) {
101 mConfig.put(key, value);
102 } catch (JSONException e) {
109 * Sets a long value in the config. Returns a reference to the same
110 * config object, so you can chain put calls together.
112 private HttpUrlRequestFactoryConfig putLong(String key, long value) {
114 mConfig.put(key, value);
115 } catch (JSONException e) {
122 * Sets a string value in the config. Returns a reference to the same
123 * config object, so you can chain put calls together.
125 private HttpUrlRequestFactoryConfig putString(String key, String value) {
127 mConfig.put(key, value);
128 } catch (JSONException e) {
134 private JSONObject mConfig = new JSONObject();