Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: content/network/network_context.cc

Issue 2874163004: Add support in the network service for different contexts. (Closed)
Patch Set: review comment and clang fixes Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « content/network/network_context.h ('k') | content/network/network_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/network/network_context.h" 5 #include "content/network/network_context.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "content/network/cache_url_loader.h"
11 #include "content/network/network_service_url_loader_factory_impl.h"
10 #include "content/network/url_loader_impl.h" 12 #include "content/network/url_loader_impl.h"
11 #include "content/public/common/content_client.h" 13 #include "content/public/common/content_client.h"
12 #include "content/public/common/content_switches.h" 14 #include "content/public/common/content_switches.h"
13 #include "net/dns/host_resolver.h" 15 #include "net/dns/host_resolver.h"
14 #include "net/dns/mapped_host_resolver.h" 16 #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"
17 #include "net/proxy/proxy_config.h" 17 #include "net/proxy/proxy_config.h"
18 #include "net/proxy/proxy_config_service_fixed.h" 18 #include "net/proxy/proxy_config_service_fixed.h"
19 #include "net/url_request/url_request_context.h" 19 #include "net/url_request/url_request_context.h"
20 #include "net/url_request/url_request_context_builder.h" 20 #include "net/url_request/url_request_context_builder.h"
21 21
22 namespace content { 22 namespace content {
23 23
24 namespace { 24 namespace {
25 25
26 std::unique_ptr<net::URLRequestContext> MakeURLRequestContext() { 26 std::unique_ptr<net::URLRequestContext> MakeURLRequestContext() {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 builder.set_proxy_config_service(std::move(fixed_config_service)); 77 builder.set_proxy_config_service(std::move(fixed_config_service));
78 } else { 78 } else {
79 builder.set_proxy_service(net::ProxyService::CreateDirect()); 79 builder.set_proxy_service(net::ProxyService::CreateDirect());
80 } 80 }
81 81
82 return builder.Build(); 82 return builder.Build();
83 } 83 }
84 84
85 } // namespace 85 } // namespace
86 86
87 class NetworkContext::MojoNetLog : public net::NetLog { 87 NetworkContext::NetworkContext(mojom::NetworkContextRequest request,
88 public: 88 mojom::NetworkContextParamsPtr params)
89 MojoNetLog() { 89 : url_request_context_(MakeURLRequestContext()),
90 const base::CommandLine* command_line = 90 in_shutdown_(false),
91 base::CommandLine::ForCurrentProcess(); 91 params_(std::move(params)),
92 if (!command_line->HasSwitch(switches::kLogNetLog)) 92 binding_(this, std::move(request)) {}
93 return;
94 base::FilePath log_path =
95 command_line->GetSwitchValuePath(switches::kLogNetLog);
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 93
128 NetworkContext::~NetworkContext() { 94 NetworkContext::~NetworkContext() {
129 in_shutdown_ = true; 95 in_shutdown_ = true;
130 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the 96 // Call each URLLoaderImpl and ask it to release its net::URLRequest, as the
131 // corresponding net::URLRequestContext is going away with this 97 // corresponding net::URLRequestContext is going away with this
132 // NetworkContext. The loaders can be deregistering themselves in Cleanup(), 98 // NetworkContext. The loaders can be deregistering themselves in Cleanup(),
133 // so iterate over a copy. 99 // so iterate over a copy.
134 for (auto* url_loader : url_loaders_) 100 for (auto* url_loader : url_loaders_)
135 url_loader->Cleanup(); 101 url_loader->Cleanup();
136 } 102 }
137 103
104 std::unique_ptr<NetworkContext> NetworkContext::CreateForTesting() {
105 return base::WrapUnique(new NetworkContext);
106 }
107
138 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) { 108 void NetworkContext::RegisterURLLoader(URLLoaderImpl* url_loader) {
139 DCHECK(url_loaders_.count(url_loader) == 0); 109 DCHECK(url_loaders_.count(url_loader) == 0);
140 url_loaders_.insert(url_loader); 110 url_loaders_.insert(url_loader);
141 } 111 }
142 112
143 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) { 113 void NetworkContext::DeregisterURLLoader(URLLoaderImpl* url_loader) {
144 if (!in_shutdown_) { 114 if (!in_shutdown_) {
145 size_t removed_count = url_loaders_.erase(url_loader); 115 size_t removed_count = url_loaders_.erase(url_loader);
146 DCHECK(removed_count); 116 DCHECK(removed_count);
147 } 117 }
148 } 118 }
149 119
120 void NetworkContext::CreateURLLoaderFactory(
121 mojom::URLLoaderFactoryRequest request,
122 uint32_t process_id) {
123 loader_factory_bindings_.AddBinding(
124 base::MakeUnique<NetworkServiceURLLoaderFactoryImpl>(this, process_id),
125 std::move(request));
126 }
127
128 void NetworkContext::HandleViewCacheRequest(const GURL& url,
129 mojom::URLLoaderClientPtr client) {
130 StartCacheURLLoader(url, url_request_context_.get(), std::move(client));
131 }
132
133 NetworkContext::NetworkContext()
134 : url_request_context_(MakeURLRequestContext()),
135 in_shutdown_(false),
136 binding_(this) {}
137
150 } // namespace content 138 } // namespace content
OLDNEW
« no previous file with comments | « content/network/network_context.h ('k') | content/network/network_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698