OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h" | 5 #include "chrome/browser/policy/testing_policy_url_fetcher_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/policy/logging_work_scheduler.h" | 8 #include "chrome/browser/policy/logging_work_scheduler.h" |
9 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
10 #include "googleurl/src/url_parse.h" | 10 #include "googleurl/src/url_parse.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 namespace policy { | 34 namespace policy { |
35 | 35 |
36 // An URLFetcher that calls back to its factory to figure out what to respond. | 36 // An URLFetcher that calls back to its factory to figure out what to respond. |
37 class TestingPolicyURLFetcher : public URLFetcher { | 37 class TestingPolicyURLFetcher : public URLFetcher { |
38 public: | 38 public: |
39 TestingPolicyURLFetcher( | 39 TestingPolicyURLFetcher( |
40 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, | 40 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, |
41 const GURL& url, | 41 const GURL& url, |
42 URLFetcher::RequestType request_type, | 42 URLFetcher::RequestType request_type, |
43 URLFetcher::Delegate* delegate); | 43 content::URLFetcherDelegate* delegate); |
44 | 44 |
45 virtual void Start() OVERRIDE; | 45 virtual void Start() OVERRIDE; |
46 void Respond(); | 46 void Respond(); |
47 | 47 |
| 48 virtual const GURL& url() const { |
| 49 return url_; |
| 50 } |
| 51 |
| 52 virtual const net::URLRequestStatus& status() const { |
| 53 return status_; |
| 54 } |
| 55 |
| 56 virtual int response_code() const { |
| 57 return response_.response_code; |
| 58 } |
| 59 |
| 60 virtual bool GetResponseAsString(std::string* out_response_string) const { |
| 61 *out_response_string = response_.response_data; |
| 62 return true; |
| 63 } |
| 64 |
48 private: | 65 private: |
49 GURL url_; | 66 GURL url_; |
| 67 net::URLRequestStatus status_; |
50 TestURLResponse response_; | 68 TestURLResponse response_; |
51 base::WeakPtr<TestingPolicyURLFetcherFactory> parent_; | 69 base::WeakPtr<TestingPolicyURLFetcherFactory> parent_; |
52 | 70 |
53 DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher); | 71 DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher); |
54 }; | 72 }; |
55 | 73 |
56 TestingPolicyURLFetcher::TestingPolicyURLFetcher( | 74 TestingPolicyURLFetcher::TestingPolicyURLFetcher( |
57 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, | 75 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, |
58 const GURL& url, | 76 const GURL& url, |
59 URLFetcher::RequestType request_type, | 77 URLFetcher::RequestType request_type, |
60 URLFetcher::Delegate* delegate) | 78 content::URLFetcherDelegate* delegate) |
61 : URLFetcher(url, request_type, delegate), | 79 : URLFetcher(url, request_type, delegate), |
62 url_(url), | 80 url_(url), |
| 81 status_(net::URLRequestStatus::SUCCESS, 0), |
63 parent_(parent) { | 82 parent_(parent) { |
64 } | 83 } |
65 | 84 |
66 void TestingPolicyURLFetcher::Start() { | 85 void TestingPolicyURLFetcher::Start() { |
67 if (!parent_.get()) return; | 86 if (!parent_.get()) return; |
68 | 87 |
69 std::string auth_header; | 88 std::string auth_header; |
70 net::HttpRequestHeaders headers; | 89 net::HttpRequestHeaders headers; |
71 std::string request = GetRequestType(url_); | 90 std::string request = GetRequestType(url_); |
72 GetExtraRequestHeaders(&headers); | 91 GetExtraRequestHeaders(&headers); |
73 headers.GetHeader("Authorization", &auth_header); | 92 headers.GetHeader("Authorization", &auth_header); |
74 // The following method is mocked by the currently running test. | 93 // The following method is mocked by the currently running test. |
75 parent_->GetResponse(auth_header, request, &response_); | 94 parent_->GetResponse(auth_header, request, &response_); |
76 | 95 |
77 // We need to channel this through the central event logger, so that ordering | 96 // We need to channel this through the central event logger, so that ordering |
78 // with other logged tasks that have a delay is preserved. | 97 // with other logged tasks that have a delay is preserved. |
79 parent_->scheduler()->PostDelayedWork( | 98 parent_->scheduler()->PostDelayedWork( |
80 base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)), | 99 base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)), |
81 0); | 100 0); |
82 } | 101 } |
83 | 102 |
84 void TestingPolicyURLFetcher::Respond() { | 103 void TestingPolicyURLFetcher::Respond() { |
85 delegate()->OnURLFetchComplete( | 104 delegate()->OnURLFetchComplete(this); |
86 this, | |
87 url_, | |
88 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0), | |
89 response_.response_code, | |
90 net::ResponseCookies(), | |
91 response_.response_data); | |
92 } | 105 } |
93 | 106 |
94 TestingPolicyURLFetcherFactory::TestingPolicyURLFetcherFactory( | 107 TestingPolicyURLFetcherFactory::TestingPolicyURLFetcherFactory( |
95 EventLogger* logger) | 108 EventLogger* logger) |
96 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 109 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
97 logger_(logger), | 110 logger_(logger), |
98 scheduler_(new LoggingWorkScheduler(logger)), | 111 scheduler_(new LoggingWorkScheduler(logger)), |
99 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 112 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
100 } | 113 } |
101 | 114 |
102 TestingPolicyURLFetcherFactory::~TestingPolicyURLFetcherFactory() { | 115 TestingPolicyURLFetcherFactory::~TestingPolicyURLFetcherFactory() { |
103 } | 116 } |
104 | 117 |
105 LoggingWorkScheduler* TestingPolicyURLFetcherFactory::scheduler() { | 118 LoggingWorkScheduler* TestingPolicyURLFetcherFactory::scheduler() { |
106 return scheduler_.get(); | 119 return scheduler_.get(); |
107 } | 120 } |
108 | 121 |
109 void TestingPolicyURLFetcherFactory::GetResponse( | 122 void TestingPolicyURLFetcherFactory::GetResponse( |
110 const std::string& auth_header, | 123 const std::string& auth_header, |
111 const std::string& request, | 124 const std::string& request, |
112 TestURLResponse* response) { | 125 TestURLResponse* response) { |
113 logger_->RegisterEvent(); | 126 logger_->RegisterEvent(); |
114 Intercept(auth_header, request, response); | 127 Intercept(auth_header, request, response); |
115 } | 128 } |
116 | 129 |
117 URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher( | 130 URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher( |
118 int id, | 131 int id, |
119 const GURL& url, | 132 const GURL& url, |
120 URLFetcher::RequestType request_type, | 133 URLFetcher::RequestType request_type, |
121 URLFetcher::Delegate* delegate) { | 134 content::URLFetcherDelegate* delegate) { |
122 return new TestingPolicyURLFetcher( | 135 return new TestingPolicyURLFetcher( |
123 weak_ptr_factory_.GetWeakPtr(), url, request_type, delegate); | 136 weak_ptr_factory_.GetWeakPtr(), url, request_type, delegate); |
124 } | 137 } |
125 | 138 |
126 } // namespace policy | 139 } // namespace policy |
OLD | NEW |