[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | // This class is useful for building a simple URLRequestContext. Most creators | ||||
6 | // of new URLRequestContexts should use this helper class to construct it. Call | ||||
7 | // any configuration params, and when done, invoke Build() to construct the | ||||
8 | // URLRequestContext. This URLRequestContext will own all its own storage. | ||||
9 | // | ||||
10 | // URLRequestContextBuilder and its associated params classes are initially | ||||
11 | // populated with "sane" default values. Read through the comments to figure out | ||||
12 | // what these are. | ||||
13 | |||||
14 | #ifndef NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ | ||||
15 | #define NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 16 | |
17 | #include <string> | ||||
18 | |||||
19 | #include "base/basictypes.h" | ||||
20 | #include "base/file_path.h" | ||||
21 | #include "base/memory/ref_counted.h" | ||||
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 22 | #include "base/memory/scoped_ptr.h" |
23 | #include "build/build_config.h" | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 24 | #include "net/base/net_export.h" |
25 | |||||
26 | namespace net { | ||||
27 | |||||
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 28 | class HostMappingRules; |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 29 | class ProxyConfigService; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 30 | class URLRequestContext; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 31 | class NetworkDelegate; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 32 | |
33 | class NET_EXPORT URLRequestContextBuilder { | ||||
34 | public: | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 35 | struct NET_EXPORT HttpCacheParams { |
36 | enum Type { | ||||
37 | IN_MEMORY, | ||||
38 | DISK, | ||||
39 | }; | ||||
40 | |||||
41 | HttpCacheParams(); | ||||
42 | ~HttpCacheParams(); | ||||
43 | |||||
[email protected] | 393e5ec6 | 2012-07-09 19:08:28 | [diff] [blame] | 44 | // The type of HTTP cache. Default is IN_MEMORY. |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 45 | Type type; |
46 | |||||
47 | // The max size of the cache in bytes. Default is algorithmically determined | ||||
48 | // based off available disk space. | ||||
49 | int max_size; | ||||
50 | |||||
51 | // The cache path (when type is DISK). | ||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame^] | 52 | base::FilePath path; |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 53 | }; |
54 | |||||
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 55 | struct NET_EXPORT HttpNetworkSessionParams { |
56 | HttpNetworkSessionParams(); | ||||
57 | ~HttpNetworkSessionParams(); | ||||
58 | |||||
59 | // These fields mirror those in net::HttpNetworkSession::Params; | ||||
60 | bool ignore_certificate_errors; | ||||
61 | HostMappingRules* host_mapping_rules; | ||||
62 | bool http_pipelining_enabled; | ||||
63 | uint16 testing_fixed_http_port; | ||||
64 | uint16 testing_fixed_https_port; | ||||
65 | std::string trusted_spdy_proxy; | ||||
66 | }; | ||||
67 | |||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 68 | URLRequestContextBuilder(); |
69 | ~URLRequestContextBuilder(); | ||||
70 | |||||
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 71 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 72 | void set_proxy_config_service(ProxyConfigService* proxy_config_service); |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 73 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 74 | |
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 75 | // Call these functions to specify hard-coded Accept-Language, |
76 | // Accept-Charset, or User-Agent header values for all requests that don't | ||||
77 | // have the headers already set. | ||||
78 | void set_accept_language(const std::string& accept_language) { | ||||
79 | accept_language_ = accept_language; | ||||
80 | } | ||||
81 | void set_accept_charset(const std::string& accept_charset) { | ||||
82 | accept_charset_ = accept_charset; | ||||
83 | } | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 84 | void set_user_agent(const std::string& user_agent) { |
85 | user_agent_ = user_agent; | ||||
86 | } | ||||
87 | |||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 88 | // By default it's disabled. |
89 | void set_ftp_enabled(bool enable) { | ||||
90 | ftp_enabled_ = enable; | ||||
91 | } | ||||
92 | |||||
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 93 | // Uses BasicNetworkDelegate by default. Note that calling Build will unset |
94 | // any custom delegate in builder, so this must be called each time before | ||||
95 | // Build is called. | ||||
96 | void set_network_delegate(NetworkDelegate* delegate) { | ||||
97 | network_delegate_.reset(delegate); | ||||
98 | } | ||||
99 | |||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 100 | // By default HttpCache is enabled with a default constructed HttpCacheParams. |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 101 | void EnableHttpCache(const HttpCacheParams& params) { |
102 | http_cache_params_ = params; | ||||
103 | } | ||||
104 | |||||
105 | void DisableHttpCache() { | ||||
106 | http_cache_params_ = HttpCacheParams(); | ||||
107 | } | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 108 | |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 109 | // Override default net::HttpNetworkSession::Params settings. |
110 | void set_http_network_session_params( | ||||
111 | const HttpNetworkSessionParams& http_network_session_params) { | ||||
112 | http_network_session_params_ = http_network_session_params; | ||||
113 | } | ||||
114 | |||||
[email protected] | ef2bf42 | 2012-05-11 03:27:09 | [diff] [blame] | 115 | URLRequestContext* Build(); |
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 116 | |
117 | private: | ||||
[email protected] | ee4c30d | 2012-11-07 15:08:43 | [diff] [blame] | 118 | std::string accept_language_; |
119 | std::string accept_charset_; | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 120 | std::string user_agent_; |
121 | bool ftp_enabled_; | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 122 | bool http_cache_enabled_; |
123 | HttpCacheParams http_cache_params_; | ||||
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 124 | HttpNetworkSessionParams http_network_session_params_; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 125 | #if defined(OS_LINUX) || defined(OS_ANDROID) |
[email protected] | e52acc8 | 2012-04-21 01:21:06 | [diff] [blame] | 126 | scoped_ptr<ProxyConfigService> proxy_config_service_; |
[email protected] | 03a2e98 | 2012-10-27 03:20:29 | [diff] [blame] | 127 | #endif // defined(OS_LINUX) || defined(OS_ANDROID) |
128 | scoped_ptr<NetworkDelegate> network_delegate_; | ||||
[email protected] | e26fe07 | 2012-04-17 21:38:17 | [diff] [blame] | 129 | |
130 | DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder); | ||||
131 | }; | ||||
132 | |||||
133 | } // namespace net | ||||
134 | |||||
135 | #endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_ |