| 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 "content/test/net/url_request_prepackaged_interceptor.h" | 5 #include "content/test/net/url_request_prepackaged_interceptor.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/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 "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "net/url_request/url_request.h" | 11 #include "net/url_request/url_request.h" |
| 12 #include "net/url_request/url_request_file_job.h" | 12 #include "net/url_request/url_request_file_job.h" |
| 13 #include "net/url_request/url_request_filter.h" | 13 #include "net/url_request/url_request_filter.h" |
| 14 #include "net/url_request/url_request_interceptor.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 class URLRequestPrepackagedJob : public net::URLRequestFileJob { | 23 class URLRequestPrepackagedJob : public net::URLRequestFileJob { |
| 23 public: | 24 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 34 | 35 |
| 35 private: | 36 private: |
| 36 virtual ~URLRequestPrepackagedJob() {} | 37 virtual ~URLRequestPrepackagedJob() {} |
| 37 | 38 |
| 38 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); | 39 DISALLOW_COPY_AND_ASSIGN(URLRequestPrepackagedJob); |
| 39 }; | 40 }; |
| 40 | 41 |
| 41 } // namespace | 42 } // namespace |
| 42 | 43 |
| 43 class URLRequestPrepackagedInterceptor::Delegate | 44 class URLRequestPrepackagedInterceptor::Delegate |
| 44 : public net::URLRequestJobFactory::ProtocolHandler { | 45 : public net::URLRequestInterceptor { |
| 45 public: | 46 public: |
| 46 Delegate(const std::string& scheme, const std::string& hostname) | 47 Delegate(const std::string& scheme, const std::string& hostname) |
| 47 : scheme_(scheme), hostname_(hostname), hit_count_(0) {} | 48 : scheme_(scheme), hostname_(hostname), hit_count_(0) {} |
| 48 virtual ~Delegate() {} | 49 virtual ~Delegate() {} |
| 49 | 50 |
| 50 void Register() { | 51 void Register() { |
| 51 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | 52 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor( |
| 52 scheme_, hostname_, | 53 scheme_, hostname_, |
| 53 scoped_ptr<net::URLRequestJobFactory::ProtocolHandler>(this)); | 54 scoped_ptr<net::URLRequestInterceptor>(this)); |
| 54 } | 55 } |
| 55 | 56 |
| 56 static void Unregister( | 57 static void Unregister( |
| 57 const std::string& scheme, | 58 const std::string& scheme, |
| 58 const std::string& hostname) { | 59 const std::string& hostname) { |
| 59 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, | 60 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler(scheme, |
| 60 hostname); | 61 hostname); |
| 61 } | 62 } |
| 62 | 63 |
| 63 // When requests for |url| arrive, respond with the contents of |path|. The | 64 // When requests for |url| arrive, respond with the contents of |path|. The |
| (...skipping 17 matching lines...) Expand all Loading... |
| 81 // Returns how many requests have been issued that have a stored reply. | 82 // Returns how many requests have been issued that have a stored reply. |
| 82 int GetHitCount() const { | 83 int GetHitCount() const { |
| 83 base::AutoLock auto_lock(hit_count_lock_); | 84 base::AutoLock auto_lock(hit_count_lock_); |
| 84 return hit_count_; | 85 return hit_count_; |
| 85 } | 86 } |
| 86 | 87 |
| 87 private: | 88 private: |
| 88 typedef std::map<GURL, base::FilePath> ResponseMap; | 89 typedef std::map<GURL, base::FilePath> ResponseMap; |
| 89 | 90 |
| 90 // When computing matches, this ignores the query parameters of the url. | 91 // When computing matches, this ignores the query parameters of the url. |
| 91 virtual net::URLRequestJob* MaybeCreateJob( | 92 virtual net::URLRequestJob* MaybeInterceptRequest( |
| 92 net::URLRequest* request, | 93 net::URLRequest* request, |
| 93 net::NetworkDelegate* network_delegate) const OVERRIDE { | 94 net::NetworkDelegate* network_delegate) const OVERRIDE { |
| 94 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 95 if (request->url().scheme() != scheme_ || | 96 if (request->url().scheme() != scheme_ || |
| 96 request->url().host() != hostname_) { | 97 request->url().host() != hostname_) { |
| 97 return NULL; | 98 return NULL; |
| 98 } | 99 } |
| 99 | 100 |
| 100 ResponseMap::const_iterator it = responses_.find(request->url()); | 101 ResponseMap::const_iterator it = responses_.find(request->url()); |
| 101 if (it == responses_.end()) { | 102 if (it == responses_.end()) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 return delegate_->GetHitCount(); | 178 return delegate_->GetHitCount(); |
| 178 } | 179 } |
| 179 | 180 |
| 180 | 181 |
| 181 URLLocalHostRequestPrepackagedInterceptor | 182 URLLocalHostRequestPrepackagedInterceptor |
| 182 ::URLLocalHostRequestPrepackagedInterceptor() | 183 ::URLLocalHostRequestPrepackagedInterceptor() |
| 183 : URLRequestPrepackagedInterceptor("http", "localhost") { | 184 : URLRequestPrepackagedInterceptor("http", "localhost") { |
| 184 } | 185 } |
| 185 | 186 |
| 186 } // namespace content | 187 } // namespace content |
| OLD | NEW |