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 16 matching lines...) Expand all Loading... |
27 } | 27 } |
28 } | 28 } |
29 return ""; | 29 return ""; |
30 } | 30 } |
31 | 31 |
32 } // namespace | 32 } // namespace |
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 TestURLFetcher { |
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 content::URLFetcher::RequestType request_type, |
43 content::URLFetcherDelegate* 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& GetUrl() const OVERRIDE { | |
49 return url_; | |
50 } | |
51 | |
52 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE { | |
53 return status_; | |
54 } | |
55 | |
56 virtual int GetResponseCode() const OVERRIDE { | 48 virtual int GetResponseCode() const OVERRIDE { |
57 return response_.response_code; | 49 return response_.response_code; |
58 } | 50 } |
59 | 51 |
60 virtual bool GetResponseAsString( | 52 virtual bool GetResponseAsString( |
61 std::string* out_response_string) const OVERRIDE { | 53 std::string* out_response_string) const OVERRIDE { |
62 *out_response_string = response_.response_data; | 54 *out_response_string = response_.response_data; |
63 return true; | 55 return true; |
64 } | 56 } |
65 | 57 |
66 private: | 58 private: |
67 GURL url_; | |
68 net::URLRequestStatus status_; | |
69 TestURLResponse response_; | 59 TestURLResponse response_; |
70 base::WeakPtr<TestingPolicyURLFetcherFactory> parent_; | 60 base::WeakPtr<TestingPolicyURLFetcherFactory> parent_; |
71 | 61 |
72 DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher); | 62 DISALLOW_COPY_AND_ASSIGN(TestingPolicyURLFetcher); |
73 }; | 63 }; |
74 | 64 |
75 TestingPolicyURLFetcher::TestingPolicyURLFetcher( | 65 TestingPolicyURLFetcher::TestingPolicyURLFetcher( |
76 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, | 66 const base::WeakPtr<TestingPolicyURLFetcherFactory>& parent, |
77 const GURL& url, | 67 const GURL& url, |
78 URLFetcher::RequestType request_type, | 68 content::URLFetcher::RequestType request_type, |
79 content::URLFetcherDelegate* delegate) | 69 content::URLFetcherDelegate* delegate) |
80 : URLFetcher(url, request_type, delegate), | 70 : TestURLFetcher(0, url, request_type, delegate), |
81 url_(url), | 71 parent_(parent) { |
82 status_(net::URLRequestStatus::SUCCESS, 0), | 72 set_url(url); |
83 parent_(parent) { | 73 set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 0)); |
84 } | 74 } |
85 | 75 |
86 void TestingPolicyURLFetcher::Start() { | 76 void TestingPolicyURLFetcher::Start() { |
87 if (!parent_.get()) return; | 77 if (!parent_.get()) return; |
88 | 78 |
89 std::string auth_header; | 79 std::string auth_header; |
90 net::HttpRequestHeaders headers; | 80 net::HttpRequestHeaders headers; |
91 std::string request = GetRequestType(url_); | 81 std::string request = GetRequestType(GetUrl()); |
92 GetExtraRequestHeaders(&headers); | 82 GetExtraRequestHeaders(&headers); |
93 headers.GetHeader("Authorization", &auth_header); | 83 headers.GetHeader("Authorization", &auth_header); |
94 // The following method is mocked by the currently running test. | 84 // The following method is mocked by the currently running test. |
95 parent_->GetResponse(auth_header, request, &response_); | 85 parent_->GetResponse(auth_header, request, &response_); |
96 | 86 |
97 // We need to channel this through the central event logger, so that ordering | 87 // We need to channel this through the central event logger, so that ordering |
98 // with other logged tasks that have a delay is preserved. | 88 // with other logged tasks that have a delay is preserved. |
99 parent_->scheduler()->PostDelayedWork( | 89 parent_->scheduler()->PostDelayedWork( |
100 base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)), | 90 base::Bind(&TestingPolicyURLFetcher::Respond, base::Unretained(this)), |
101 0); | 91 0); |
(...skipping 19 matching lines...) Expand all Loading... |
121 } | 111 } |
122 | 112 |
123 void TestingPolicyURLFetcherFactory::GetResponse( | 113 void TestingPolicyURLFetcherFactory::GetResponse( |
124 const std::string& auth_header, | 114 const std::string& auth_header, |
125 const std::string& request, | 115 const std::string& request, |
126 TestURLResponse* response) { | 116 TestURLResponse* response) { |
127 logger_->RegisterEvent(); | 117 logger_->RegisterEvent(); |
128 Intercept(auth_header, request, response); | 118 Intercept(auth_header, request, response); |
129 } | 119 } |
130 | 120 |
131 URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher( | 121 content::URLFetcher* TestingPolicyURLFetcherFactory::CreateURLFetcher( |
132 int id, | 122 int id, |
133 const GURL& url, | 123 const GURL& url, |
134 URLFetcher::RequestType request_type, | 124 content::URLFetcher::RequestType request_type, |
135 content::URLFetcherDelegate* delegate) { | 125 content::URLFetcherDelegate* delegate) { |
136 return new TestingPolicyURLFetcher( | 126 return new TestingPolicyURLFetcher( |
137 weak_ptr_factory_.GetWeakPtr(), url, request_type, delegate); | 127 weak_ptr_factory_.GetWeakPtr(), url, request_type, delegate); |
138 } | 128 } |
139 | 129 |
140 } // namespace policy | 130 } // namespace policy |
OLD | NEW |