Chromium Code Reviews| Index: chrome/browser/extensions/autoupdate_interceptor.cc |
| diff --git a/chrome/browser/extensions/autoupdate_interceptor.cc b/chrome/browser/extensions/autoupdate_interceptor.cc |
| index febc9cf5f19b458cc14ef626fbdf11a75472bc91..5d4a9ed73f810fb735e4ba8b9958b5eae32dea58 100644 |
| --- a/chrome/browser/extensions/autoupdate_interceptor.cc |
| +++ b/chrome/browser/extensions/autoupdate_interceptor.cc |
| @@ -9,6 +9,7 @@ |
| #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" |
|
erikwright (departed)
2012/12/06 15:26:28
It might be worth it to take this opportunity to d
pauljensen
2012/12/07 18:47:42
Done.
|
| #include "net/url_request/url_request_test_job.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -37,64 +38,90 @@ class AutoUpdateTestRequestJob : public net::URLRequestTestJob { |
| ~AutoUpdateTestRequestJob() {} |
| }; |
| +class AutoUpdateInterceptor::Delegate |
| + : public net::URLRequestJobFactory::ProtocolHandler { |
| + public: |
| + Delegate() {} |
| + virtual ~Delegate() { |
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| + "localhost"); |
| + } |
| + void Register() { |
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( |
| + "http", "localhost", this); |
| + } |
| -AutoUpdateInterceptor::AutoUpdateInterceptor() { |
| - net::URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| -} |
| + // When computing matches, this ignores query parameters (since the autoupdate |
| + // fetch code appends a bunch of them to manifest fetches). |
| + 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; |
| + |
| + // Search for this request's url, ignoring any query parameters. |
| + GURL url = request->url(); |
| + if (url.has_query()) { |
| + GURL::Replacements replacements; |
| + replacements.ClearQuery(); |
| + url = url.ReplaceComponents(replacements); |
| + } |
| + std::map<GURL, FilePath>::const_iterator i = responses_.find(url); |
| + if (i == responses_.end()) { |
| + return NULL; |
| + } |
| + std::string contents; |
| + EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); |
| -AutoUpdateInterceptor::~AutoUpdateInterceptor() { |
| - net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| -} |
| + return new AutoUpdateTestRequestJob(request, network_delegate, contents); |
| + } |
| -net::URLRequestJob* AutoUpdateInterceptor::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; |
| + // 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" so MaybeIntercept can ignore "chrome" and other schemes. |
| + // Also, the match for |url| will ignore any query parameters. |
| + void SetResponse(const std::string url, const FilePath& path) { |
| + EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + // 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)); |
| + responses_[gurl] = 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; |
| + private: |
| + std::map<GURL, FilePath> responses_; |
| - // Search for this request's url, ignoring any query parameters. |
| - GURL url = request->url(); |
| - if (url.has_query()) { |
| - GURL::Replacements replacements; |
| - replacements.ClearQuery(); |
| - url = url.ReplaceComponents(replacements); |
| - } |
| - std::map<GURL, FilePath>::iterator i = responses_.find(url); |
| - if (i == responses_.end()) { |
| - return NULL; |
| - } |
| - std::string contents; |
| - EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); |
| + DISALLOW_COPY_AND_ASSIGN(Delegate); |
| +}; |
| - return new AutoUpdateTestRequestJob(request, network_delegate, contents); |
| +AutoUpdateInterceptor::AutoUpdateInterceptor() : delegate_(new Delegate) { |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::Register, |
| + base::Unretained(delegate_))); |
| } |
| +AutoUpdateInterceptor::~AutoUpdateInterceptor() { |
| + BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| +} |
| void AutoUpdateInterceptor::SetResponse(const std::string url, |
| const FilePath& path) { |
| - EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| - // 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)); |
| - responses_[gurl] = path; |
| -} |
| - |
| - |
| -void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, |
| - const FilePath& path) { |
| - BrowserThread::PostTask( |
| - BrowserThread::IO, FROM_HERE, |
| - base::Bind(&AutoUpdateInterceptor::SetResponse, this, url, path)); |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&Delegate::SetResponse, |
| + base::Unretained(delegate_), url, path)); |
| } |
| } // namespace extensions |