OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "net/url_request/test_url_request_interceptor.h" | 5 #include "net/url_request/test_url_request_interceptor.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
9 #include "base/threading/thread_restrictions.h" | 9 #include "base/threading/thread_restrictions.h" |
10 #include "net/url_request/url_request.h" | 10 #include "net/url_request/url_request.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 public: | 23 public: |
24 TestURLRequestJob(net::URLRequest* request, | 24 TestURLRequestJob(net::URLRequest* request, |
25 net::NetworkDelegate* network_delegate, | 25 net::NetworkDelegate* network_delegate, |
26 const base::FilePath& file_path, | 26 const base::FilePath& file_path, |
27 const scoped_refptr<base::TaskRunner>& worker_task_runner) | 27 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
28 : net::URLRequestFileJob(request, | 28 : net::URLRequestFileJob(request, |
29 network_delegate, | 29 network_delegate, |
30 file_path, | 30 file_path, |
31 worker_task_runner) {} | 31 worker_task_runner) {} |
32 | 32 |
33 virtual int GetResponseCode() const override { return 200; } | 33 int GetResponseCode() const override { return 200; } |
34 | 34 |
35 private: | 35 private: |
36 virtual ~TestURLRequestJob() {} | 36 ~TestURLRequestJob() override {} |
37 | 37 |
38 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob); | 38 DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob); |
39 }; | 39 }; |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 // This class handles the actual URL request interception. It may be constructed | 43 // This class handles the actual URL request interception. It may be constructed |
44 // on any thread, but all other methods are called on the |network_task_runner| | 44 // on any thread, but all other methods are called on the |network_task_runner| |
45 // thread. It is destroyed by the net::URLRequestFilter singleton. | 45 // thread. It is destroyed by the net::URLRequestFilter singleton. |
46 class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor { | 46 class TestURLRequestInterceptor::Delegate : public net::URLRequestInterceptor { |
47 public: | 47 public: |
48 Delegate(const std::string& scheme, | 48 Delegate(const std::string& scheme, |
49 const std::string& hostname, | 49 const std::string& hostname, |
50 const scoped_refptr<base::TaskRunner>& network_task_runner, | 50 const scoped_refptr<base::TaskRunner>& network_task_runner, |
51 const scoped_refptr<base::TaskRunner>& worker_task_runner) | 51 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
52 : scheme_(scheme), | 52 : scheme_(scheme), |
53 hostname_(hostname), | 53 hostname_(hostname), |
54 network_task_runner_(network_task_runner), | 54 network_task_runner_(network_task_runner), |
55 worker_task_runner_(worker_task_runner), | 55 worker_task_runner_(worker_task_runner), |
56 hit_count_(0) {} | 56 hit_count_(0) {} |
57 virtual ~Delegate() {} | 57 ~Delegate() override {} |
58 | 58 |
59 void Register() { | 59 void Register() { |
60 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( | 60 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
61 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); | 61 scheme_, hostname_, scoped_ptr<net::URLRequestInterceptor>(this)); |
62 } | 62 } |
63 | 63 |
64 static void Unregister(const std::string& scheme, | 64 static void Unregister(const std::string& scheme, |
65 const std::string& hostname) { | 65 const std::string& hostname) { |
66 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, | 66 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, |
67 hostname); | 67 hostname); |
(...skipping 16 matching lines...) Expand all Loading... |
84 // Returns how many requests have been issued that have a stored reply. | 84 // Returns how many requests have been issued that have a stored reply. |
85 int GetHitCount() const { | 85 int GetHitCount() const { |
86 base::AutoLock auto_lock(hit_count_lock_); | 86 base::AutoLock auto_lock(hit_count_lock_); |
87 return hit_count_; | 87 return hit_count_; |
88 } | 88 } |
89 | 89 |
90 private: | 90 private: |
91 typedef std::map<GURL, base::FilePath> ResponseMap; | 91 typedef std::map<GURL, base::FilePath> ResponseMap; |
92 | 92 |
93 // When computing matches, this ignores the query parameters of the url. | 93 // When computing matches, this ignores the query parameters of the url. |
94 virtual net::URLRequestJob* MaybeInterceptRequest( | 94 net::URLRequestJob* MaybeInterceptRequest( |
95 net::URLRequest* request, | 95 net::URLRequest* request, |
96 net::NetworkDelegate* network_delegate) const override { | 96 net::NetworkDelegate* network_delegate) const override { |
97 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 97 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
98 if (request->url().scheme() != scheme_ || | 98 if (request->url().scheme() != scheme_ || |
99 request->url().host() != hostname_) { | 99 request->url().host() != hostname_) { |
100 return NULL; | 100 return NULL; |
101 } | 101 } |
102 | 102 |
103 ResponseMap::const_iterator it = responses_.find(request->url()); | 103 ResponseMap::const_iterator it = responses_.find(request->url()); |
104 if (it == responses_.end()) { | 104 if (it == responses_.end()) { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor( | 190 LocalHostTestURLRequestInterceptor::LocalHostTestURLRequestInterceptor( |
191 const scoped_refptr<base::TaskRunner>& network_task_runner, | 191 const scoped_refptr<base::TaskRunner>& network_task_runner, |
192 const scoped_refptr<base::TaskRunner>& worker_task_runner) | 192 const scoped_refptr<base::TaskRunner>& worker_task_runner) |
193 : TestURLRequestInterceptor("http", | 193 : TestURLRequestInterceptor("http", |
194 "localhost", | 194 "localhost", |
195 network_task_runner, | 195 network_task_runner, |
196 worker_task_runner) { | 196 worker_task_runner) { |
197 } | 197 } |
198 | 198 |
199 } // namespace net | 199 } // namespace net |
OLD | NEW |