Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / components / cronet / android / java / src / org / chromium / net / UrlRequestContextConfig.java
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
5 package org.chromium.net;
6
7 import org.json.JSONArray;
8 import org.json.JSONException;
9 import org.json.JSONObject;
10
11 /**
12  * A config for UrlRequestContext, which allows runtime configuration of
13  * UrlRequestContext.
14  */
15 public class UrlRequestContextConfig {
16
17     /**
18      * Default config enables SPDY, QUIC, in memory http cache.
19      */
20     public UrlRequestContextConfig() {
21         enableLegacyMode(false);
22         enableQUIC(false);
23         enableSPDY(true);
24         enableHttpCache(HttpCache.IN_MEMORY, 100 * 1024);
25     }
26
27     /**
28      * Create config from json serialized using @toString.
29      */
30     public UrlRequestContextConfig(String json) throws JSONException {
31         mConfig = new JSONObject(json);
32     }
33
34     /**
35      * Override the user-agent header for all requests.
36      */
37     public UrlRequestContextConfig setUserAgent(String userAgent) {
38         return putString(UrlRequestContextConfigList.USER_AGENT, userAgent);
39     }
40
41     String userAgent() {
42         return mConfig.optString(UrlRequestContextConfigList.USER_AGENT);
43     }
44
45     /**
46      * String, path to directory for HTTP Cache and Cookie Storage.
47      */
48     public UrlRequestContextConfig setStoragePath(String value) {
49         return putString(UrlRequestContextConfigList.STORAGE_PATH, value);
50     }
51
52     /**
53      * Boolean, use HttpUrlConnection-based implementation if true. All other
54      * keys are not applicable.
55      */
56     public UrlRequestContextConfig enableLegacyMode(boolean value) {
57         return putBoolean(UrlRequestContextConfigList.ENABLE_LEGACY_MODE,
58                           value);
59     }
60
61     boolean legacyMode() {
62         return mConfig.optBoolean(
63                 UrlRequestContextConfigList.ENABLE_LEGACY_MODE);
64     }
65
66     /**
67      * Override the name of the native library backing cronet.
68      */
69     public UrlRequestContextConfig setLibraryName(String libName) {
70         return putString(UrlRequestContextConfigList.NATIVE_LIBRARY_NAME,
71                          libName);
72     }
73
74     String libraryName() {
75         return mConfig.optString(
76                 UrlRequestContextConfigList.NATIVE_LIBRARY_NAME, "cronet");
77     }
78
79     /**
80      * Boolean, enable QUIC if true.
81      */
82     public UrlRequestContextConfig enableQUIC(boolean value) {
83         return putBoolean(UrlRequestContextConfigList.ENABLE_QUIC, value);
84     }
85
86     /**
87      * Boolean, enable SPDY if true.
88      */
89     public UrlRequestContextConfig enableSPDY(boolean value) {
90         return putBoolean(UrlRequestContextConfigList.ENABLE_SPDY, value);
91     }
92
93     /**
94      * Enumeration, Disable or Enable Disk or Memory Cache and specify its
95      * maximum size in bytes.
96      */
97     public enum HttpCache { DISABLED, IN_MEMORY, DISK };
98     public UrlRequestContextConfig enableHttpCache(HttpCache value,
99             long maxSize) {
100         switch(value) {
101             case DISABLED:
102                 return putString(UrlRequestContextConfigList.HTTP_CACHE,
103                         UrlRequestContextConfigList.HTTP_CACHE_DISABLED);
104             case DISK:
105                 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
106                         maxSize);
107                 return putString(UrlRequestContextConfigList.HTTP_CACHE,
108                         UrlRequestContextConfigList.HTTP_CACHE_DISK);
109             case IN_MEMORY:
110                 putLong(UrlRequestContextConfigList.HTTP_CACHE_MAX_SIZE,
111                         maxSize);
112                 return putString(UrlRequestContextConfigList.HTTP_CACHE,
113                         UrlRequestContextConfigList.HTTP_CACHE_MEMORY);
114         }
115         return this;
116     }
117
118     /**
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.
122      *
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.
126      */
127     public UrlRequestContextConfig addQuicHint(String host,
128             int port,
129             int alternatePort) {
130         if (host.contains("/")) {
131             throw new IllegalArgumentException("Illegal QUIC Hint Host: "
132                                                + host);
133         }
134         try {
135             JSONArray quicHints = mConfig.optJSONArray(
136                     UrlRequestContextConfigList.QUIC_HINTS);
137             if (quicHints == null) {
138                 quicHints = new JSONArray();
139                 mConfig.put(UrlRequestContextConfigList.QUIC_HINTS, quicHints);
140             }
141
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,
146                      alternatePort);
147             quicHints.put(hint);
148         } catch (JSONException e) {
149             // Intentionally do nothing.
150         }
151         return this;
152     }
153
154     /**
155      * Get JSON string representation of the config.
156      */
157     @Override
158     public String toString() {
159         return mConfig.toString();
160     }
161
162     /**
163      * Sets a boolean value in the config. Returns a reference to the same
164      * config object, so you can chain put calls together.
165      */
166     private UrlRequestContextConfig putBoolean(String key, boolean value) {
167         try {
168             mConfig.put(key, value);
169         } catch (JSONException e) {
170             // Intentionally do nothing.
171         }
172         return this;
173     }
174
175     /**
176      * Sets a long value in the config. Returns a reference to the same
177      * config object, so you can chain put calls together.
178      */
179     private UrlRequestContextConfig putLong(String key, long value)  {
180         try {
181             mConfig.put(key, value);
182         } catch (JSONException e) {
183             // Intentionally do nothing.
184         }
185         return this;
186     }
187
188     /**
189      * Sets a string value in the config. Returns a reference to the same
190      * config object, so you can chain put calls together.
191      */
192     private UrlRequestContextConfig putString(String key, String value) {
193         try {
194             mConfig.put(key, value);
195         } catch (JSONException e) {
196             // Intentionally do nothing.
197         }
198         return this;
199     }
200
201     private JSONObject mConfig = new JSONObject();
202 }