summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/testing_policy_url_fetcher_factory.cc
blob: 76cf2079b06b121f35a8d135e58f03bb86cec8c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/policy/testing_policy_url_fetcher_factory.h"

#include "base/bind.h"
#include "chrome/browser/policy/logging_work_scheduler.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"
#include "googleurl/src/gurl.h"
#include "googleurl/src/url_parse.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_status.h"

namespace {

// Extracts the GET parameter by the name "request" from an URL.
std::string GetRequestType(const GURL& url) {
  url_parse::Component query = url.parsed_for_possibly_invalid_spec().query;
  url_parse::Component key, value;
  const char* url_spec = url.spec().c_str();
  while (url_parse::ExtractQueryKeyValue(url_spec, &query, &key, &value)) {
    std::string key_str(url_spec, key.begin, key.len);
    std::string value_str(url_spec, value.begin, value.len);
    if (key_str == "request") {
      return value_str;
    }
  }
  return "";
}

}  // namespace

namespace policy {

// An URLFetcher that calls back to its factory to figure out what to respond.
class TestingPolicyURLFetcher : public TestURLFetcher {
 public:
  TestingPolicyURLFetcher(
      const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent,
      const GURL& url,
      content::URLFetcherDelegate* delegate);

  virtual void Start() OVERRIDE;
  void Respond();

  virtual int GetResponseCode() const OVERRIDE {
    return response_.response_code;
  }

  virtual bool GetResponseAsString(
      std::string* out_response_string) const OVERRIDE {
    *out_response_string = response_.response_data;
    return true;
  }

 private:
  TestURLResponse response_;
  base::WeakPtr<TestingPolicyURLFetcherFactory> parent_;

  DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher);
};

TestingPolicyURLFetcher::TestingPolicyURLFetcher(
    const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent,
    const GURL& url,
    content::URLFetcherDelegate* delegate)
    : TestURLFetcher(0, url, delegate),
      parent_(parent) {
  set_url(url);
  set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0));
}

void TestingPolicyURLFetcher::Start() {
  if (!parent_.get()) return;

  std::string auth_header;
  net::HttpRequestHeaders headers;
  std::string request_type = GetRequestType(GetURL());
  GetExtraRequestHeaders(&headers);
  headers.GetHeader("Authorization", &auth_header);

  enterprise_management::DeviceManagementRequest request;
  request.ParseFromString(upload_data());

  // The following method is mocked by the currently running test.
  parent_->GetResponse(auth_header, request_type, request, &response_);

  // We need to channel this through the central event logger, so that ordering
  // with other logged tasks that have a delay is preserved.
  parent_->scheduler()->PostDelayedWork(
      base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)),
      0);
}

void TestingPolicyURLFetcher::Respond() {
  delegate()->OnURLFetchComplete(this);
}

TestingPolicyURLFetcherFactory::TestingPolicyURLFetcherFactory(
    EventLogger* logger)
    : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
      logger_(logger),
      scheduler_(new LoggingWorkScheduler(logger)),
      ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {
}

TestingPolicyURLFetcherFactory::~TestingPolicyURLFetcherFactory() {
}

LoggingWorkScheduler* TestingPolicyURLFetcherFactory::scheduler() {
  return scheduler_.get();
}

void TestingPolicyURLFetcherFactory::GetResponse(
    const std::string& auth_header,
    const std::string& request_type,
    const enterprise_management::DeviceManagementRequest& request,
    TestURLResponse* response) {
  logger_->RegisterEvent();
  Intercept(auth_header, request_type, request, response);
}

content::URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher(
    int id,
    const GURL& url,
    content::URLFetcher::RequestType request_type,
    content::URLFetcherDelegate* delegate) {
  return new TestingPolicyURLFetcher(
      weak_ptr_factory_.GetWeakPtr(), url, delegate);
}

}  // namespace policy