| 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 |
| 15 ComponentUpdateInterceptor::ComponentUpdateInterceptor() | 16 ComponentUpdateInterceptor::ComponentUpdateInterceptor() |
| 16 : hit_count_(0) { | 17 : hit_count_(0) { |
| 17 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 18 BrowserThread::PostTask( |
| 19 BrowserThread::IO, FROM_HERE, |
| 20 base::Bind(&ComponentUpdateInterceptor::Register, this)); |
| 18 } | 21 } |
| 19 | 22 |
| 20 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { | 23 ComponentUpdateInterceptor::~ComponentUpdateInterceptor() { |
| 21 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 24 BrowserThread::PostTask(BrowserThread::IO, |
| 25 FROM_HERE, |
| 26 base::Bind(Unregister)); |
| 22 } | 27 } |
| 23 | 28 |
| 24 net::URLRequestJob* ComponentUpdateInterceptor::MaybeIntercept( | 29 void ComponentUpdateInterceptor::Register() { |
| 25 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | 30 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler("http", |
| 31 "localhost", |
| 32 this); |
| 33 } |
| 34 |
| 35 // static |
| 36 void ComponentUpdateInterceptor::Unregister() { |
| 37 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| 38 "localhost"); |
| 39 } |
| 40 |
| 41 net::URLRequestJob* ComponentUpdateInterceptor::MaybeCreateJob( |
| 42 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 26 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 43 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 27 if (request->url().scheme() != "http" || | 44 if (request->url().scheme() != "http" || |
| 28 request->url().host() != "localhost") { | 45 request->url().host() != "localhost") { |
| 29 return NULL; | 46 return NULL; |
| 30 } | 47 } |
| 31 | 48 |
| 32 // It's ok to do a blocking disk access on this thread; this class | 49 // It's ok to do a blocking disk access on this thread; this class |
| 33 // is just used for tests. | 50 // is just used for tests. |
| 34 base::ThreadRestrictions::ScopedAllowIO allow_io; | 51 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 35 | 52 |
| 36 ResponseMap::iterator it = responses_.find(request->url()); | 53 ResponseMap::const_iterator it = responses_.find(request->url()); |
| 37 if (it == responses_.end()) { | 54 if (it == responses_.end()) { |
| 38 return NULL; | 55 return NULL; |
| 39 } | 56 } |
| 40 const Response& response = it->second; | 57 const Response& response = it->second; |
| 41 ++hit_count_; | 58 ++hit_count_; |
| 42 | 59 |
| 43 std::string contents; | 60 std::string contents; |
| 44 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); | 61 EXPECT_TRUE(file_util::ReadFileToString(response.data_path, &contents)); |
| 45 | 62 |
| 46 return new net::URLRequestTestJob(request, | 63 return new net::URLRequestTestJob(request, |
| 47 network_delegate, | 64 network_delegate, |
| 48 response.headers, | 65 response.headers, |
| 49 contents, | 66 contents, |
| 50 true); | 67 true); |
| 51 } | 68 } |
| 52 | 69 |
| 53 void ComponentUpdateInterceptor::SetResponse(const std::string& url, | 70 void ComponentUpdateInterceptor::SetResponse(const std::string& url, |
| 54 const std::string& headers, | 71 const std::string& headers, |
| 55 const FilePath& path) { | 72 const FilePath& path) { |
| 56 // It's ok to do a blocking disk access on this thread; this class | 73 // It's ok to do a blocking disk access on this thread; this class |
| 57 // is just used for tests. | 74 // is just used for tests. |
| 58 base::ThreadRestrictions::ScopedAllowIO allow_io; | 75 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 59 GURL gurl(url); | 76 GURL gurl(url); |
| 60 EXPECT_EQ("http", gurl.scheme()); | 77 EXPECT_EQ("http", gurl.scheme()); |
| 61 EXPECT_EQ("localhost", gurl.host()); | 78 EXPECT_EQ("localhost", gurl.host()); |
| 62 EXPECT_TRUE(file_util::PathExists(path)); | 79 EXPECT_TRUE(file_util::PathExists(path)); |
| 63 Response response = { path, headers }; | 80 Response response = { path, headers }; |
| 64 responses_[gurl] = response; | 81 responses_[gurl] = response; |
| 65 } | 82 } |
| OLD | NEW |