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.
5 #include "components/policy/core/common/cloud/user_policy_request_context.h"
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"
15 UserPolicyRequestContext::UserPolicyRequestContext(
16 scoped_refptr<net::URLRequestContextGetter> user_context_getter,
17 scoped_refptr<net::URLRequestContextGetter> system_context_getter,
18 const std::string& user_agent)
19 : user_context_getter_(user_context_getter),
20 system_context_getter_(system_context_getter),
21 http_user_agent_settings_("*", user_agent) {
22 DCHECK(user_context_getter_.get());
25 UserPolicyRequestContext::~UserPolicyRequestContext() {
28 net::URLRequestContext*
29 UserPolicyRequestContext::GetURLRequestContext() {
30 DCHECK(GetNetworkTaskRunner()->RunsTasksOnCurrentThread());
31 if (!context_.get()) {
32 // Create our URLRequestContext().
33 context_.reset(new net::URLRequestContext());
34 net::URLRequestContext* user_context =
35 user_context_getter_->GetURLRequestContext();
37 // Reuse pretty much everything from the user context, except we
38 // use the system context's proxy and resolver (see below).
39 context_->CopyFrom(user_context);
41 // Use the system context's proxy and resolver to ensure that we can still
42 // fetch policy updates even if a bad proxy config is pushed via user
44 // TODO(atwilson): Re-enable the following lines in a followup CL per
46 // net::URLRequestContext* system_context =
47 // system_context_getter_->GetURLRequestContext();
48 // context_->set_host_resolver(system_context->host_resolver());
49 // context_->set_proxy_service(system_context->proxy_service());
51 // Set our custom UserAgent.
52 context_->set_http_user_agent_settings(&http_user_agent_settings_);
54 return context_.get();
57 scoped_refptr<base::SingleThreadTaskRunner>
58 UserPolicyRequestContext::GetNetworkTaskRunner() const {
59 return user_context_getter_->GetNetworkTaskRunner();