| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "chrome/browser/policy/testing_policy_url_fetcher_factory.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "chrome/browser/policy/logging_work_scheduler.h" | |
| 9 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 10 #include "googleurl/src/gurl.h" | |
| 11 #include "googleurl/src/url_parse.h" | |
| 12 #include "net/http/http_request_headers.h" | |
| 13 #include "net/url_request/url_request.h" | |
| 14 #include "net/url_request/url_request_status.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // Extracts the GET parameter by the name "request" from an URL. | |
| 19 std::string GetRequestType(const GURL& url) { | |
| 20 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | |
| 21 url_parse::Component key, value; | |
| 22 const char* url_spec = url.spec().c_str(); | |
| 23 while (url_parse::ExtractQueryKeyValue(url_spec, &query, &key, &value)) { | |
| 24 std::string key_str(url_spec, key.begin, key.len); | |
| 25 std::string value_str(url_spec, value.begin, value.len); | |
| 26 if (key_str == "request") { | |
| 27 return value_str; | |
| 28 } | |
| 29 } | |
| 30 return ""; | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 namespace policy { | |
| 36 | |
| 37 // An URLFetcher that calls back to its factory to figure out what to respond. | |
| 38 class TestingPolicyURLFetcher : public net::TestURLFetcher { | |
| 39 public: | |
| 40 TestingPolicyURLFetcher( | |
| 41 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, | |
| 42 const GURL& url, | |
| 43 net::URLFetcherDelegate* delegate); | |
| 44 | |
| 45 virtual void Start() OVERRIDE; | |
| 46 void Respond(); | |
| 47 | |
| 48 virtual int GetResponseCode() const OVERRIDE { | |
| 49 return response_.response_code; | |
| 50 } | |
| 51 | |
| 52 virtual bool GetResponseAsString( | |
| 53 std::string* out_response_string) const OVERRIDE { | |
| 54 *out_response_string = response_.response_data; | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 TestURLResponse response_; | |
| 60 base::WeakPtr<TestingPolicyURLFetcherFactory> parent_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher); | |
| 63 }; | |
| 64 | |
| 65 TestingPolicyURLFetcher::TestingPolicyURLFetcher( | |
| 66 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, | |
| 67 const GURL& url, | |
| 68 net::URLFetcherDelegate* delegate) | |
| 69 : TestURLFetcher(0, url, delegate), | |
| 70 parent_(parent) { | |
| 71 set_url(url); | |
| 72 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); | |
| 73 } | |
| 74 | |
| 75 void TestingPolicyURLFetcher::Start() { | |
| 76 if (!parent_.get()) return; | |
| 77 | |
| 78 std::string auth_header; | |
| 79 net::HttpRequestHeaders headers; | |
| 80 std::string request_type = GetRequestType(GetURL()); | |
| 81 GetExtraRequestHeaders(&headers); | |
| 82 headers.GetHeader("Authorization", &auth_header); | |
| 83 | |
| 84 enterprise_management::DeviceManagementRequest request; | |
| 85 request.ParseFromString(upload_data()); | |
| 86 | |
| 87 // The following method is mocked by the currently running test. | |
| 88 parent_->GetResponse(auth_header, request_type, request, &response_); | |
| 89 | |
| 90 // We need to channel this through the central event logger, so that ordering | |
| 91 // with other logged tasks that have a delay is preserved. | |
| 92 parent_->scheduler()->PostDelayedWork( | |
| 93 base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)), | |
| 94 0); | |
| 95 } | |
| 96 | |
| 97 void TestingPolicyURLFetcher::Respond() { | |
| 98 delegate()->OnURLFetchComplete(this); | |
| 99 } | |
| 100 | |
| 101 TestingPolicyURLFetcherFactory::TestingPolicyURLFetcherFactory( | |
| 102 EventLogger* logger) | |
| 103 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | |
| 104 logger_(logger), | |
| 105 scheduler_(new LoggingWorkScheduler(logger)), | |
| 106 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 107 } | |
| 108 | |
| 109 TestingPolicyURLFetcherFactory::~TestingPolicyURLFetcherFactory() { | |
| 110 } | |
| 111 | |
| 112 LoggingWorkScheduler* TestingPolicyURLFetcherFactory::scheduler() { | |
| 113 return scheduler_.get(); | |
| 114 } | |
| 115 | |
| 116 void TestingPolicyURLFetcherFactory::GetResponse( | |
| 117 const std::string& auth_header, | |
| 118 const std::string& request_type, | |
| 119 const enterprise_management::DeviceManagementRequest& request, | |
| 120 TestURLResponse* response) { | |
| 121 logger_->RegisterEvent(); | |
| 122 Intercept(auth_header, request_type, request, response); | |
| 123 } | |
| 124 | |
| 125 net::URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher( | |
| 126 int id, | |
| 127 const GURL& url, | |
| 128 net::URLFetcher::RequestType request_type, | |
| 129 net::URLFetcherDelegate* delegate) { | |
| 130 return new TestingPolicyURLFetcher( | |
| 131 weak_ptr_factory_.GetWeakPtr(), url, delegate); | |
| 132 } | |
| 133 | |
| 134 } // namespace policy | |
| OLD | NEW |