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.
4
5package org.chromium.net;
6
7import org.json.JSONException;
8import org.json.JSONObject;
9
10/**
11 * A config for HttpUrlRequestFactory, which allows runtime configuration of
12 * HttpUrlRequestFactory.
13 */
14public class HttpUrlRequestFactoryConfig {
15    /**
16     * Default config enables SPDY, QUIC, in memory http cache.
17     */
18    public HttpUrlRequestFactoryConfig() {
19        enableLegacyMode(false);
20        enableQUIC(false);
21        enableSPDY(true);
22        enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
23    }
24
25    /**
26     * Create config from json serialized using @toString.
27     */
28    public HttpUrlRequestFactoryConfig(String json) throws JSONException {
29        mConfig = new JSONObject(json);
30    }
31
32    /**
33     * Boolean, use HttpUrlRequest-based implementation if true. All other
34     * keys are not applicable.
35     */
36    public HttpUrlRequestFactoryConfig enableLegacyMode(boolean value) {
37        return putBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE, value);
38    }
39
40    public boolean legacyMode() {
41        return mConfig.optBoolean(UrlRequestContextConfig.ENABLE_LEGACY_MODE);
42    }
43
44    /**
45     * Boolean, enable QUIC if true.
46     */
47    public HttpUrlRequestFactoryConfig enableQUIC(boolean value) {
48        return putBoolean(UrlRequestContextConfig.ENABLE_QUIC, value);
49    }
50
51    /**
52     * Boolean, enable SPDY if true.
53     */
54    public HttpUrlRequestFactoryConfig enableSPDY(boolean value) {
55        return putBoolean(UrlRequestContextConfig.ENABLE_SPDY, value);
56    }
57
58    /**
59     * Enumeration, Disable or Enable Disk or Memory Cache and specify its
60     * maximum size in bytes.
61     */
62    public enum HttpCache { DISABLED, IN_MEMORY, DISK };
63    public HttpUrlRequestFactoryConfig enableHttpCache(HttpCache value,
64                                                       long maxSize) {
65        switch(value) {
66            case DISABLED:
67                return putString(UrlRequestContextConfig.HTTP_CACHE,
68                                 UrlRequestContextConfig.HTTP_CACHE_DISABLED);
69            case DISK:
70                putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
71                return putString(UrlRequestContextConfig.HTTP_CACHE,
72                                 UrlRequestContextConfig.HTTP_CACHE_DISK);
73            case IN_MEMORY:
74                putLong(UrlRequestContextConfig.HTTP_CACHE_MAX_SIZE, maxSize);
75                return putString(UrlRequestContextConfig.HTTP_CACHE,
76                                 UrlRequestContextConfig.HTTP_CACHE_MEMORY);
77        }
78        return this;
79    }
80
81    /**
82     * String, path to directory for HTTP Cache and Cookie Storage.
83     */
84    public HttpUrlRequestFactoryConfig setStoragePath(String value) {
85        return putString(UrlRequestContextConfig.STORAGE_PATH, value);
86    }
87
88    /**
89     * Get JSON string representation of the config.
90     */
91    public String toString() {
92        return mConfig.toString();
93    }
94
95    /**
96     * Sets a boolean value in the config. Returns a reference to the same
97     * config object, so you can chain put calls together.
98     */
99    private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) {
100        try {
101            mConfig.put(key, value);
102        } catch (JSONException e) {
103            ;
104        }
105        return this;
106    }
107
108    /**
109     * Sets a long value in the config. Returns a reference to the same
110     * config object, so you can chain put calls together.
111     */
112    private HttpUrlRequestFactoryConfig putLong(String key, long value)  {
113        try {
114            mConfig.put(key, value);
115        } catch (JSONException e) {
116            ;
117        }
118        return this;
119    }
120
121    /**
122     * Sets a string value in the config. Returns a reference to the same
123     * config object, so you can chain put calls together.
124     */
125    private HttpUrlRequestFactoryConfig putString(String key, String value) {
126        try {
127            mConfig.put(key, value);
128        } catch (JSONException e) {
129            ;
130        }
131        return this;
132    }
133
134    private JSONObject mConfig = new JSONObject();
135}
136