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/user_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
13namespace policy {
14
15UserPolicyRequestContext::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_);
23}
24
25UserPolicyRequestContext::~UserPolicyRequestContext() {
26}
27
28net::URLRequestContext*
29UserPolicyRequestContext::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();
36
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);
40
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
43    // policy.
44    // TODO(atwilson): Re-enable the following lines in a followup CL per
45    // reviewer request.
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());
50
51    // Set our custom UserAgent.
52    context_->set_http_user_agent_settings(&http_user_agent_settings_);
53  }
54  return context_.get();
55}
56
57scoped_refptr<base::SingleThreadTaskRunner>
58UserPolicyRequestContext::GetNetworkTaskRunner() const {
59  return user_context_getter_->GetNetworkTaskRunner();
60}
61
62}  // namespace policy
63