1 // Copyright 2013 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 "components/policy/core/common/cloud/system_policy_request_context.h" 6 7 #include "base/logging.h" 8 #include "base/single_thread_task_runner.h" 9 #include "net/cookies/cookie_monster.h" 10 #include "net/http/http_network_layer.h" 11 #include "net/url_request/url_request_context.h" 12 13 namespace policy { 14 15 SystemPolicyRequestContext::SystemPolicyRequestContext( 16 scoped_refptr<net::URLRequestContextGetter> system_context_getter, 17 const std::string& user_agent) 18 : system_context_getter_(system_context_getter), 19 http_user_agent_settings_("*", user_agent) { 20 DCHECK(system_context_getter.get()); 21 } 22 23 SystemPolicyRequestContext::~SystemPolicyRequestContext() { 24 } 25 26 net::URLRequestContext* 27 SystemPolicyRequestContext::GetURLRequestContext() { 28 DCHECK(GetNetworkTaskRunner()->RunsTasksOnCurrentThread()); 29 if (!context_.get()) { 30 // Create our URLRequestContext(). 31 context_.reset(new net::URLRequestContext()); 32 33 net::URLRequestContext* system_context = 34 system_context_getter_->GetURLRequestContext(); 35 // Share resolver, proxy service and ssl bits with the system context. 36 // This is important so we don't make redundant requests (e.g. when 37 // resolving proxy auto configuration). 38 // TODO(atwilson): Consider using CopyFrom() here to copy all services - 39 // http://crbug.com/322422. 40 context_->set_net_log(system_context->net_log()); 41 context_->set_host_resolver(system_context->host_resolver()); 42 context_->set_proxy_service(system_context->proxy_service()); 43 context_->set_ssl_config_service( 44 system_context->ssl_config_service()); 45 46 // Share the job factory (if there is one). This allows tests to intercept 47 // requests made via this request context if they install protocol handlers 48 // at the system request context. 49 context_->set_job_factory(system_context->job_factory()); 50 51 // Set our custom UserAgent. 52 context_->set_http_user_agent_settings(&http_user_agent_settings_); 53 54 // Share the http session. 55 http_transaction_factory_.reset(new net::HttpNetworkLayer( 56 system_context->http_transaction_factory()->GetSession())); 57 context_->set_http_transaction_factory(http_transaction_factory_.get()); 58 59 // No cookies, please. We also don't track channel IDs (no 60 // ChannelIDService). 61 context_->set_cookie_store(new net::CookieMonster(NULL, NULL)); 62 } 63 64 return context_.get(); 65 } 66 67 scoped_refptr<base::SingleThreadTaskRunner> 68 SystemPolicyRequestContext::GetNetworkTaskRunner() const { 69 return system_context_getter_->GetNetworkTaskRunner(); 70 } 71 72 } // namespace policy 73