blob: 5cc92b1be0c7a12dc04580457b63ab38cffa39e4 [file] [log] [blame]
jam6f02ddc2017-04-12 01:43:501// 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"
yzshen6da6865f2017-04-18 17:49:139#include "base/strings/string_number_conversions.h"
jam6f02ddc2017-04-12 01:43:5010#include "content/network/url_loader_impl.h"
11#include "content/public/common/content_client.h"
yzshen6da6865f2017-04-18 17:49:1312#include "content/public/common/content_switches.h"
jam6f02ddc2017-04-12 01:43:5013#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"
yzshena90291c2017-04-26 16:22:5217#include "net/proxy/proxy_config.h"
18#include "net/proxy/proxy_config_service_fixed.h"
jam6f02ddc2017-04-12 01:43:5019#include "net/url_request/url_request_context.h"
20#include "net/url_request/url_request_context_builder.h"
21
22namespace content {
23
24namespace {
jam6f02ddc2017-04-12 01:43:5025
26std::unique_ptr<net::URLRequestContext> MakeURLRequestContext() {
27 net::URLRequestContextBuilder builder;
28 net::URLRequestContextBuilder::HttpNetworkSessionParams params;
29 const base::CommandLine* command_line =
30 base::CommandLine::ForCurrentProcess();
yzshen6da6865f2017-04-18 17:49:1331 if (command_line->HasSwitch(switches::kIgnoreCertificateErrors))
jam6f02ddc2017-04-12 01:43:5032 params.ignore_certificate_errors = true;
yzshen6da6865f2017-04-18 17:49:1333
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 }
jam6f02ddc2017-04-12 01:43:5048 builder.set_http_network_session_params(params);
yzshen6da6865f2017-04-18 17:49:1349 if (command_line->HasSwitch(switches::kHostResolverRules)) {
jam6f02ddc2017-04-12 01:43:5050 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(
yzshen6da6865f2017-04-18 17:49:1355 command_line->GetSwitchValueASCII(switches::kHostResolverRules));
jam6f02ddc2017-04-12 01:43:5056 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());
jam6f02ddc2017-04-12 01:43:5060 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);
jam734386542017-05-08 21:21:3569 builder.set_data_enabled(true);
scottmgfd5066f2017-04-14 00:49:1770
yzshena90291c2017-04-26 16:22:5271 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
jam6f02ddc2017-04-12 01:43:5082 return builder.Build();
83}
84
85} // namespace
86
87class NetworkContext::MojoNetLog : public net::NetLog {
88 public:
89 MojoNetLog() {
90 const base::CommandLine* command_line =
91 base::CommandLine::ForCurrentProcess();
yzshen6da6865f2017-04-18 17:49:1392 if (!command_line->HasSwitch(switches::kLogNetLog))
jam6f02ddc2017-04-12 01:43:5093 return;
yzshen6da6865f2017-04-18 17:49:1394 base::FilePath log_path =
95 command_line->GetSwitchValuePath(switches::kLogNetLog);
jam6f02ddc2017-04-12 01:43:5096 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
123NetworkContext::NetworkContext()
124 : net_log_(new MojoNetLog),
125 url_request_context_(MakeURLRequestContext()),
126 in_shutdown_(false) {}
127
128NetworkContext::~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
138void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) {
139 DCHECK(url_loaders_.count(url_loader) == 0);
140 url_loaders_.insert(url_loader);
141}
142
143void 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