jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 1 | // Copyright 2017 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 | #include "content/network/network_context.h" |
| 6 | |
| 7 | #include "base/command_line.h" |
| 8 | #include "base/logging.h" |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 10 | #include "content/network/url_loader_impl.h" |
| 11 | #include "content/public/common/content_client.h" |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 12 | #include "content/public/common/content_switches.h" |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 13 | #include "net/dns/host_resolver.h" |
| 14 | #include "net/dns/mapped_host_resolver.h" |
| 15 | #include "net/log/net_log_util.h" |
| 16 | #include "net/log/write_to_file_net_log_observer.h" |
yzshen | a90291c | 2017-04-26 16:22:52 | [diff] [blame] | 17 | #include "net/proxy/proxy_config.h" |
| 18 | #include "net/proxy/proxy_config_service_fixed.h" |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 19 | #include "net/url_request/url_request_context.h" |
| 20 | #include "net/url_request/url_request_context_builder.h" |
| 21 | |
| 22 | namespace content { |
| 23 | |
| 24 | namespace { |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 25 | |
| 26 | std::unique_ptr<net::URLRequestContext> MakeURLRequestContext() { |
| 27 | net::URLRequestContextBuilder builder; |
| 28 | net::URLRequestContextBuilder::HttpNetworkSessionParams params; |
| 29 | const base::CommandLine* command_line = |
| 30 | base::CommandLine::ForCurrentProcess(); |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 31 | if (command_line->HasSwitch(switches::kIgnoreCertificateErrors)) |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 32 | params.ignore_certificate_errors = true; |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 33 | |
| 34 | if (command_line->HasSwitch(switches::kTestingFixedHttpPort)) { |
| 35 | int value; |
| 36 | base::StringToInt( |
| 37 | command_line->GetSwitchValueASCII(switches::kTestingFixedHttpPort), |
| 38 | &value); |
| 39 | params.testing_fixed_http_port = value; |
| 40 | } |
| 41 | if (command_line->HasSwitch(switches::kTestingFixedHttpsPort)) { |
| 42 | int value; |
| 43 | base::StringToInt( |
| 44 | command_line->GetSwitchValueASCII(switches::kTestingFixedHttpsPort), |
| 45 | &value); |
| 46 | params.testing_fixed_https_port = value; |
| 47 | } |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 48 | builder.set_http_network_session_params(params); |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 49 | if (command_line->HasSwitch(switches::kHostResolverRules)) { |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 50 | std::unique_ptr<net::HostResolver> host_resolver( |
| 51 | net::HostResolver::CreateDefaultResolver(nullptr)); |
| 52 | std::unique_ptr<net::MappedHostResolver> remapped_host_resolver( |
| 53 | new net::MappedHostResolver(std::move(host_resolver))); |
| 54 | remapped_host_resolver->SetRulesFromString( |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 55 | command_line->GetSwitchValueASCII(switches::kHostResolverRules)); |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 56 | builder.set_host_resolver(std::move(remapped_host_resolver)); |
| 57 | } |
| 58 | builder.set_accept_language("en-us,en"); |
| 59 | builder.set_user_agent(GetContentClient()->GetUserAgent()); |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 60 | net::URLRequestContextBuilder::HttpCacheParams cache_params; |
| 61 | |
| 62 | // We store the cache in memory so we can run many shells in parallel when |
| 63 | // running tests, otherwise the network services in each shell will corrupt |
| 64 | // the disk cache. |
| 65 | cache_params.type = net::URLRequestContextBuilder::HttpCacheParams::IN_MEMORY; |
| 66 | |
| 67 | builder.EnableHttpCache(cache_params); |
| 68 | builder.set_file_enabled(true); |
jam | 73438654 | 2017-05-08 21:21:35 | [diff] [blame^] | 69 | builder.set_data_enabled(true); |
scottmg | fd5066f | 2017-04-14 00:49:17 | [diff] [blame] | 70 | |
yzshen | a90291c | 2017-04-26 16:22:52 | [diff] [blame] | 71 | if (command_line->HasSwitch(switches::kProxyServer)) { |
| 72 | net::ProxyConfig config; |
| 73 | config.proxy_rules().ParseFromString( |
| 74 | command_line->GetSwitchValueASCII(switches::kProxyServer)); |
| 75 | std::unique_ptr<net::ProxyConfigService> fixed_config_service = |
| 76 | base::MakeUnique<net::ProxyConfigServiceFixed>(config); |
| 77 | builder.set_proxy_config_service(std::move(fixed_config_service)); |
| 78 | } else { |
| 79 | builder.set_proxy_service(net::ProxyService::CreateDirect()); |
| 80 | } |
| 81 | |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 82 | return builder.Build(); |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | class NetworkContext::MojoNetLog : public net::NetLog { |
| 88 | public: |
| 89 | MojoNetLog() { |
| 90 | const base::CommandLine* command_line = |
| 91 | base::CommandLine::ForCurrentProcess(); |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 92 | if (!command_line->HasSwitch(switches::kLogNetLog)) |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 93 | return; |
yzshen | 6da6865f | 2017-04-18 17:49:13 | [diff] [blame] | 94 | base::FilePath log_path = |
| 95 | command_line->GetSwitchValuePath(switches::kLogNetLog); |
jam | 6f02ddc | 2017-04-12 01:43:50 | [diff] [blame] | 96 | base::ScopedFILE file; |
| 97 | #if defined(OS_WIN) |
| 98 | file.reset(_wfopen(log_path.value().c_str(), L"w")); |
| 99 | #elif defined(OS_POSIX) |
| 100 | file.reset(fopen(log_path.value().c_str(), "w")); |
| 101 | #endif |
| 102 | if (!file) { |
| 103 | LOG(ERROR) << "Could not open file " << log_path.value() |
| 104 | << " for net logging"; |
| 105 | } else { |
| 106 | write_to_file_observer_.reset(new net::WriteToFileNetLogObserver()); |
| 107 | write_to_file_observer_->set_capture_mode( |
| 108 | net::NetLogCaptureMode::IncludeCookiesAndCredentials()); |
| 109 | write_to_file_observer_->StartObserving(this, std::move(file), nullptr, |
| 110 | nullptr); |
| 111 | } |
| 112 | } |
| 113 | ~MojoNetLog() override { |
| 114 | if (write_to_file_observer_) |
| 115 | write_to_file_observer_->StopObserving(nullptr); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | std::unique_ptr<net::WriteToFileNetLogObserver> write_to_file_observer_; |
| 120 | DISALLOW_COPY_AND_ASSIGN(MojoNetLog); |
| 121 | }; |
| 122 | |
| 123 | NetworkContext::NetworkContext() |
| 124 | : net_log_(new MojoNetLog), |
| 125 | url_request_context_(MakeURLRequestContext()), |
| 126 | in_shutdown_(false) {} |
| 127 | |
| 128 | NetworkContext::~NetworkContext() { |
| 129 | in_shutdown_ = true; |
| 130 | // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the |
| 131 | // corresponding net::URLRequestContext is going away with this |
| 132 | // NetworkContext. The loaders can be deregistering themselves in Cleanup(), |
| 133 | // so iterate over a copy. |
| 134 | for (auto* url_loader : url_loaders_) |
| 135 | url_loader->Cleanup(); |
| 136 | } |
| 137 | |
| 138 | void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) { |
| 139 | DCHECK(url_loaders_.count(url_loader) == 0); |
| 140 | url_loaders_.insert(url_loader); |
| 141 | } |
| 142 | |
| 143 | void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) { |
| 144 | if (!in_shutdown_) { |
| 145 | size_t removed_count = url_loaders_.erase(url_loader); |
| 146 | DCHECK(removed_count); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | } // namespace content |