Chromium Code Reviews| Index: chrome/browser/component_updater/component_updater_interceptor.cc |
| diff --git a/chrome/browser/component_updater/component_updater_interceptor.cc b/chrome/browser/component_updater/component_updater_interceptor.cc |
| index 41d7eaef893ce082bb93cc5007faa4c7e61ef10d..52327e5e5afe3713ca502c344144947660e25436 100644 |
| --- a/chrome/browser/component_updater/component_updater_interceptor.cc |
| +++ b/chrome/browser/component_updater/component_updater_interceptor.cc |
| @@ -7,59 +7,118 @@ |
| #include "base/threading/thread_restrictions.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_filter.h" |
| #include "net/url_request/url_request_test_job.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| using content::BrowserThread; |
| -ComponentUpdateInterceptor::ComponentUpdateInterceptor() |
| - : hit_count_(0) { |
| - net::URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| -} |
| +class ComponentUpdateInterceptor::Delegate |
| + : public net::URLRequestJobFactory::ProtocolHandler { |
| + public: |
| + Delegate() : hit_count_(0) {} |
|
erikwright (departed)
2012/12/06 15:26:28
It might be appropriate to DCHECK that there is on
pauljensen
2012/12/07 18:47:42
Done. I did this by having URLRequestFilter DCHEC
|
| + virtual ~Delegate() { |
| + net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| + "localhost"); |
| + } |
| + void Register() { |
| + net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
| + "http", "localhost", this); |
| + } |
| -ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { |
| - net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| -} |
| + // When requests for |url| arrive, respond with the contents of |path|. The |
| + // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme |
| + // must be "http". |
| + void SetResponse(const std::string& url, |
| + const std::string& headers, |
| + const FilePath& path) { |
| + // It's ok to do a blocking disk access on this thread; this class |
| + // is just used for tests. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + GURL gurl(url); |
| + EXPECT_EQ("http", gurl.scheme()); |
| + EXPECT_EQ("localhost", gurl.host()); |
| + EXPECT_TRUE(file_util::PathExists(path)); |
| + Response response = { path, headers }; |
| + responses_[gurl] = response; |
| + } |
| -net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( |
| - net::URLRequest* request, net::NetworkDelegate* network_delegate) { |
| - EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - if (request->url().scheme() != "http" || |
| - request->url().host() != "localhost") { |
| - return NULL; |
| + // Returns how many requests have been issued that have a stored reply. |
| + int GetHitCount() const { |
| + base::AutoLock auto_lock(hit_count_lock_); |
| + return hit_count_; |
| } |
| - // It's ok to do a blocking disk access on this thread; this class |
| - // is just used for tests. |
| - base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + private: |
| + // When computing matches, this ignores the query parameters of the url. |
| + virtual net::URLRequestJob* MaybeCreateJob( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const OVERRIDE { |
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + if (request->url().scheme() != "http" || |
| + request->url().host() != "localhost") { |
| + return NULL; |
| + } |
| + |
| + // It's ok to do a blocking disk access on this thread; this class |
| + // is just used for tests. |
| + base::ThreadRestrictions::ScopedAllowIO allow_io; |
| + |
| + ResponseMap::const_iterator it = responses_.find(request->url()); |
| + if (it == responses_.end()) { |
| + return NULL; |
| + } |
| + const Response& response = it->second; |
| + { |
| + base::AutoLock auto_lock(hit_count_lock_); |
| + ++hit_count_; |
| + } |
| + |
| + std::string contents; |
| + EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); |
| - ResponseMap::iterator it = responses_.find(request->url()); |
| - if (it == responses_.end()) { |
| - return NULL; |
| + return new net::URLRequestTestJob(request, |
| + network_delegate, |
| + response.headers, |
| + contents, |
| + true); |
| } |
| - const Response& response = it->second; |
| - ++hit_count_; |
| - std::string contents; |
| - EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); |
| + struct Response { |
| + FilePath data_path; |
| + std::string headers; |
| + }; |
| + |
| + typedef std::map<GURL, Response> ResponseMap; |
| + ResponseMap responses_; |
| + |
| + mutable base::Lock hit_count_lock_; |
| + mutable int hit_count_; |
| - return new net::URLRequestTestJob(request, |
| - network_delegate, |
| - response.headers, |
| - contents, |
| - true); |
| + DISALLOW_COPY_AND_ASSIGN(Delegate); |
| +}; |
| + |
| + |
| +ComponentUpdateInterceptor::ComponentUpdateInterceptor() |
| + : delegate_(new Delegate) { |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::Register, |
| + base::Unretained(delegate_))); |
| +} |
| + |
| +ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { |
| + BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| } |
| void ComponentUpdateInterceptor::SetResponse(const std::string& url, |
| const std::string& headers, |
| const FilePath& path) { |
| - // It's ok to do a blocking disk access on this thread; this class |
| - // is just used for tests. |
| - base::ThreadRestrictions::ScopedAllowIO allow_io; |
| - GURL gurl(url); |
| - EXPECT_EQ("http", gurl.scheme()); |
| - EXPECT_EQ("localhost", gurl.host()); |
| - EXPECT_TRUE(file_util::PathExists(path)); |
| - Response response = { path, headers }; |
| - responses_[gurl] = response; |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::SetResponse, |
| + base::Unretained(delegate_), url, headers, |
| + path)); |
| +} |
| + |
| +int ComponentUpdateInterceptor::GetHitCount() { |
| + return delegate_->GetHitCount(); |
| } |