Home | History | Annotate | Download | only in net
      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 HttpUrlRequestFactory, which allows runtime configuration of
     13  * HttpUrlRequestFactory.
     14  */
     15 public class HttpUrlRequestFactoryConfig {
     16 
     17     /**
     18      * Default config enables SPDY, QUIC, in memory http cache.
     19      */
     20     public HttpUrlRequestFactoryConfig() {
     21         enableLegacyMode(false);
     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     }
     33 
     34     /**
     35      * Create config from json serialized using @toString.
     36      */
     37     public HttpUrlRequestFactoryConfig(String json) throws JSONException {
     38         mConfig = new JSONObject(json);
     39     }
     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             ;
    129         }
    130         return this;
    131     }
    132 
    133     /**
    134      * Get JSON string representation of the config.
    135      */
    136     public String toString() {
    137         return mConfig.toString();
    138     }
    139 
    140     /**
    141      * Sets a boolean value in the config. Returns a reference to the same
    142      * config object, so you can chain put calls together.
    143      */
    144     private HttpUrlRequestFactoryConfig putBoolean(String key, boolean value) {
    145         try {
    146             mConfig.put(key, value);
    147         } catch (JSONException e) {
    148             ;
    149         }
    150         return this;
    151     }
    152 
    153     /**
    154      * Sets a long value in the config. Returns a reference to the same
    155      * config object, so you can chain put calls together.
    156      */
    157     private HttpUrlRequestFactoryConfig putLong(String key, long value)  {
    158         try {
    159             mConfig.put(key, value);
    160         } catch (JSONException e) {
    161             ;
    162         }
    163         return this;
    164     }
    165 
    166     /**
    167      * Sets a string value in the config. Returns a reference to the same
    168      * config object, so you can chain put calls together.
    169      */
    170     private HttpUrlRequestFactoryConfig putString(String key, String value) {
    171         try {
    172             mConfig.put(key, value);
    173         } catch (JSONException e) {
    174             ;
    175         }
    176         return this;
    177     }
    178 
    179     private JSONObject mConfig = new JSONObject();
    180 }
    181