blob: f1ce5707a66866e11114c837d5ab771371e56eec [file] [log] [blame]
[email protected]e26fe072012-04-17 21:38:171// 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]e26fe072012-04-17 21:38:1716
17#include <string>
18
19#include "base/basictypes.h"
20#include "base/file_path.h"
21#include "base/memory/ref_counted.h"
[email protected]e52acc82012-04-21 01:21:0622#include "base/memory/scoped_ptr.h"
23#include "build/build_config.h"
[email protected]e26fe072012-04-17 21:38:1724#include "net/base/net_export.h"
25
26namespace net {
27
[email protected]c2dad292012-09-07 21:27:3528class HostMappingRules;
[email protected]e52acc82012-04-21 01:21:0629class ProxyConfigService;
[email protected]e26fe072012-04-17 21:38:1730class URLRequestContext;
[email protected]03a2e982012-10-27 03:20:2931class NetworkDelegate;
[email protected]e26fe072012-04-17 21:38:1732
33class NET_EXPORT URLRequestContextBuilder {
34 public:
[email protected]e26fe072012-04-17 21:38:1735 struct NET_EXPORT HttpCacheParams {
36 enum Type {
37 IN_MEMORY,
38 DISK,
39 };
40
41 HttpCacheParams();
42 ~HttpCacheParams();
43
[email protected]393e5ec62012-07-09 19:08:2844 // The type of HTTP cache. Default is IN_MEMORY.
[email protected]e26fe072012-04-17 21:38:1745 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]a3ef4832013-02-02 05:12:3352 base::FilePath path;
[email protected]e26fe072012-04-17 21:38:1753 };
54
[email protected]c2dad292012-09-07 21:27:3555 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]e26fe072012-04-17 21:38:1768 URLRequestContextBuilder();
69 ~URLRequestContextBuilder();
70
[email protected]03a2e982012-10-27 03:20:2971#if defined(OS_LINUX) || defined(OS_ANDROID)
[email protected]e52acc82012-04-21 01:21:0672 void set_proxy_config_service(ProxyConfigService* proxy_config_service);
[email protected]03a2e982012-10-27 03:20:2973#endif // defined(OS_LINUX) || defined(OS_ANDROID)
[email protected]e52acc82012-04-21 01:21:0674
[email protected]ee4c30d2012-11-07 15:08:4375 // 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]e26fe072012-04-17 21:38:1784 void set_user_agent(const std::string& user_agent) {
85 user_agent_ = user_agent;
86 }
87
[email protected]e26fe072012-04-17 21:38:1788 // By default it's disabled.
89 void set_ftp_enabled(bool enable) {
90 ftp_enabled_ = enable;
91 }
92
[email protected]03a2e982012-10-27 03:20:2993 // 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]e26fe072012-04-17 21:38:17100 // By default HttpCache is enabled with a default constructed HttpCacheParams.
[email protected]03a2e982012-10-27 03:20:29101 void EnableHttpCache(const HttpCacheParams& params) {
102 http_cache_params_ = params;
103 }
104
105 void DisableHttpCache() {
106 http_cache_params_ = HttpCacheParams();
107 }
[email protected]e26fe072012-04-17 21:38:17108
[email protected]c2dad292012-09-07 21:27:35109 // 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]ef2bf422012-05-11 03:27:09115 URLRequestContext* Build();
[email protected]e26fe072012-04-17 21:38:17116
117 private:
[email protected]ee4c30d2012-11-07 15:08:43118 std::string accept_language_;
119 std::string accept_charset_;
[email protected]e26fe072012-04-17 21:38:17120 std::string user_agent_;
121 bool ftp_enabled_;
[email protected]e26fe072012-04-17 21:38:17122 bool http_cache_enabled_;
123 HttpCacheParams http_cache_params_;
[email protected]c2dad292012-09-07 21:27:35124 HttpNetworkSessionParams http_network_session_params_;
[email protected]03a2e982012-10-27 03:20:29125#if defined(OS_LINUX) || defined(OS_ANDROID)
[email protected]e52acc82012-04-21 01:21:06126 scoped_ptr<ProxyConfigService> proxy_config_service_;
[email protected]03a2e982012-10-27 03:20:29127#endif // defined(OS_LINUX) || defined(OS_ANDROID)
128 scoped_ptr<NetworkDelegate> network_delegate_;
[email protected]e26fe072012-04-17 21:38:17129
130 DISALLOW_COPY_AND_ASSIGN(URLRequestContextBuilder);
131};
132
133} // namespace net
134
135#endif // NET_URL_REQUEST_URL_REQUEST_CONTEXT_BUILDER_H_