Chromium Code Reviews| 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 #include "chrome/browser/component_updater/component_updater_interceptor.h" | 4 #include "chrome/browser/component_updater/component_updater_interceptor.h" |
| 5 | 5 |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/threading/thread_restrictions.h" | 7 #include "base/threading/thread_restrictions.h" |
| 8 #include "content/public/browser/browser_thread.h" | 8 #include "content/public/browser/browser_thread.h" |
| 9 #include "net/url_request/url_request.h" | 9 #include "net/url_request/url_request.h" |
| 10 #include "net/url_request/url_request_filter.h" | |
| 10 #include "net/url_request/url_request_test_job.h" | 11 #include "net/url_request/url_request_test_job.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 using content::BrowserThread; | 14 using content::BrowserThread; |
| 14 | 15 |
| 16 class ComponentUpdateInterceptor::Delegate | |
| 17 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 18 public: | |
| 19 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
| |
| 20 virtual ~Delegate() { | |
| 21 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | |
| 22 "localhost"); | |
| 23 } | |
| 24 void Register() { | |
| 25 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | |
| 26 "http", "localhost", this); | |
| 27 } | |
| 28 | |
| 29 // When requests for |url| arrive, respond with the contents of |path|. The | |
| 30 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme | |
| 31 // must be "http". | |
| 32 void SetResponse(const std::string& url, | |
| 33 const std::string& headers, | |
| 34 const FilePath& path) { | |
| 35 // It's ok to do a blocking disk access on this thread; this class | |
| 36 // is just used for tests. | |
| 37 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 38 GURL gurl(url); | |
| 39 EXPECT_EQ("http", gurl.scheme()); | |
| 40 EXPECT_EQ("localhost", gurl.host()); | |
| 41 EXPECT_TRUE(file_util::PathExists(path)); | |
| 42 Response response = { path, headers }; | |
| 43 responses_[gurl] = response; | |
| 44 } | |
| 45 | |
| 46 // Returns how many requests have been issued that have a stored reply. | |
| 47 int GetHitCount() const { | |
| 48 base::AutoLock auto_lock(hit_count_lock_); | |
| 49 return hit_count_; | |
| 50 } | |
| 51 | |
| 52 private: | |
| 53 // When computing matches, this ignores the query parameters of the url. | |
| 54 virtual net::URLRequestJob* MaybeCreateJob( | |
| 55 net::URLRequest* request, | |
| 56 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 57 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 58 if (request->url().scheme() != "http" || | |
| 59 request->url().host() != "localhost") { | |
| 60 return NULL; | |
| 61 } | |
| 62 | |
| 63 // It's ok to do a blocking disk access on this thread; this class | |
| 64 // is just used for tests. | |
| 65 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 66 | |
| 67 ResponseMap::const_iterator it = responses_.find(request->url()); | |
| 68 if (it == responses_.end()) { | |
| 69 return NULL; | |
| 70 } | |
| 71 const Response& response = it->second; | |
| 72 { | |
| 73 base::AutoLock auto_lock(hit_count_lock_); | |
| 74 ++hit_count_; | |
| 75 } | |
| 76 | |
| 77 std::string contents; | |
| 78 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); | |
| 79 | |
| 80 return new net::URLRequestTestJob(request, | |
| 81 network_delegate, | |
| 82 response.headers, | |
| 83 contents, | |
| 84 true); | |
| 85 } | |
| 86 | |
| 87 struct Response { | |
| 88 FilePath data_path; | |
| 89 std::string headers; | |
| 90 }; | |
| 91 | |
| 92 typedef std::map<GURL, Response> ResponseMap; | |
| 93 ResponseMap responses_; | |
| 94 | |
| 95 mutable base::Lock hit_count_lock_; | |
| 96 mutable int hit_count_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 99 }; | |
| 100 | |
| 101 | |
| 15 ComponentUpdateInterceptor::ComponentUpdateInterceptor() | 102 ComponentUpdateInterceptor::ComponentUpdateInterceptor() |
| 16 : hit_count_(0) { | 103 : delegate_(new Delegate) { |
| 17 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 104 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 105 base::Bind(&Delegate::Register, | |
| 106 base::Unretained(delegate_))); | |
| 18 } | 107 } |
| 19 | 108 |
| 20 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { | 109 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { |
| 21 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 110 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| 22 } | |
| 23 | |
| 24 net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( | |
| 25 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | |
| 26 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 27 if (request->url().scheme() != "http" || | |
| 28 request->url().host() != "localhost") { | |
| 29 return NULL; | |
| 30 } | |
| 31 | |
| 32 // It's ok to do a blocking disk access on this thread; this class | |
| 33 // is just used for tests. | |
| 34 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 35 | |
| 36 ResponseMap::iterator it = responses_.find(request->url()); | |
| 37 if (it == responses_.end()) { | |
| 38 return NULL; | |
| 39 } | |
| 40 const Response& response = it->second; | |
| 41 ++hit_count_; | |
| 42 | |
| 43 std::string contents; | |
| 44 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); | |
| 45 | |
| 46 return new net::URLRequestTestJob(request, | |
| 47 network_delegate, | |
| 48 response.headers, | |
| 49 contents, | |
| 50 true); | |
| 51 } | 111 } |
| 52 | 112 |
| 53 void ComponentUpdateInterceptor::SetResponse(const std::string& url, | 113 void ComponentUpdateInterceptor::SetResponse(const std::string& url, |
| 54 const std::string& headers, | 114 const std::string& headers, |
| 55 const FilePath& path) { | 115 const FilePath& path) { |
| 56 // It's ok to do a blocking disk access on this thread; this class | 116 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 57 // is just used for tests. | 117 base::Bind(&Delegate::SetResponse, |
| 58 base::ThreadRestrictions::ScopedAllowIO allow_io; | 118 base::Unretained(delegate_), url, headers, |
| 59 GURL gurl(url); | 119 path)); |
| 60 EXPECT_EQ("http", gurl.scheme()); | |
| 61 EXPECT_EQ("localhost", gurl.host()); | |
| 62 EXPECT_TRUE(file_util::PathExists(path)); | |
| 63 Response response = { path, headers }; | |
| 64 responses_[gurl] = response; | |
| 65 } | 120 } |
| 121 | |
| 122 int ComponentUpdateInterceptor::GetHitCount() { | |
| 123 return delegate_->GetHitCount(); | |
| 124 } | |
| OLD | NEW |