| 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 | 4 |
| 5 #include "chrome/browser/extensions/autoupdate_interceptor.h" | 5 #include "chrome/browser/extensions/autoupdate_interceptor.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.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_filter.h" |
| 12 #include "net/url_request/url_request_test_job.h" | 13 #include "net/url_request/url_request_test_job.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using content::BrowserThread; | 16 using content::BrowserThread; |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 // This is a specialized version of net::URLRequestTestJob that lets us specify | 20 // This is a specialized version of net::URLRequestTestJob that lets us specify |
| 20 // response data and make sure the response code is 200, which the autoupdate | 21 // response data and make sure the response code is 200, which the autoupdate |
| 21 // code relies on. | 22 // code relies on. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 32 } | 33 } |
| 33 | 34 |
| 34 virtual int GetResponseCode() const { return 200; } | 35 virtual int GetResponseCode() const { return 200; } |
| 35 | 36 |
| 36 private: | 37 private: |
| 37 ~AutoUpdateTestRequestJob() {} | 38 ~AutoUpdateTestRequestJob() {} |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 | 41 |
| 41 AutoUpdateInterceptor::AutoUpdateInterceptor() { | 42 AutoUpdateInterceptor::AutoUpdateInterceptor() { |
| 42 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 43 BrowserThread::PostTask( |
| 44 BrowserThread::IO, FROM_HERE, |
| 45 base::Bind(&AutoUpdateInterceptor::Register, this)); |
| 43 } | 46 } |
| 44 | 47 |
| 45 AutoUpdateInterceptor::~AutoUpdateInterceptor() { | 48 AutoUpdateInterceptor::~AutoUpdateInterceptor() { |
| 46 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 49 BrowserThread::PostTask(BrowserThread::IO, |
| 50 FROM_HERE, |
| 51 base::Bind(Unregister)); |
| 47 } | 52 } |
| 48 | 53 |
| 49 net::URLRequestJob* AutoUpdateInterceptor::MaybeIntercept( | 54 void AutoUpdateInterceptor::Register() { |
| 50 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | 55 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler("http", |
| 56 "localhost", |
| 57 this); |
| 58 } |
| 59 |
| 60 // static |
| 61 void AutoUpdateInterceptor::Unregister() { |
| 62 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", |
| 63 "localhost"); |
| 64 } |
| 65 |
| 66 net::URLRequestJob* AutoUpdateInterceptor::MaybeCreateJob( |
| 67 net::URLRequest* request, net::NetworkDelegate* network_delegate) const { |
| 51 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 68 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 52 if (request->url().scheme() != "http" || | 69 if (request->url().scheme() != "http" || |
| 53 request->url().host() != "localhost") { | 70 request->url().host() != "localhost") { |
| 54 return NULL; | 71 return NULL; |
| 55 } | 72 } |
| 56 | 73 |
| 57 // It's ok to do a blocking disk access on this thread; this class | 74 // It's ok to do a blocking disk access on this thread; this class |
| 58 // is just used for tests. | 75 // is just used for tests. |
| 59 base::ThreadRestrictions::ScopedAllowIO allow_io; | 76 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 60 | 77 |
| 61 // Search for this request's url, ignoring any query parameters. | 78 // Search for this request's url, ignoring any query parameters. |
| 62 GURL url = request->url(); | 79 GURL url = request->url(); |
| 63 if (url.has_query()) { | 80 if (url.has_query()) { |
| 64 GURL::Replacements replacements; | 81 GURL::Replacements replacements; |
| 65 replacements.ClearQuery(); | 82 replacements.ClearQuery(); |
| 66 url = url.ReplaceComponents(replacements); | 83 url = url.ReplaceComponents(replacements); |
| 67 } | 84 } |
| 68 std::map<GURL, FilePath>::iterator i = responses_.find(url); | 85 std::map<GURL, FilePath>::const_iterator i = responses_.find(url); |
| 69 if (i == responses_.end()) { | 86 if (i == responses_.end()) { |
| 70 return NULL; | 87 return NULL; |
| 71 } | 88 } |
| 72 std::string contents; | 89 std::string contents; |
| 73 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); | 90 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); |
| 74 | 91 |
| 75 return new AutoUpdateTestRequestJob(request, network_delegate, contents); | 92 return new AutoUpdateTestRequestJob(request, network_delegate, contents); |
| 76 } | 93 } |
| 77 | 94 |
| 78 | 95 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 91 | 108 |
| 92 | 109 |
| 93 void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, | 110 void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, |
| 94 const FilePath& path) { | 111 const FilePath& path) { |
| 95 BrowserThread::PostTask( | 112 BrowserThread::PostTask( |
| 96 BrowserThread::IO, FROM_HERE, | 113 BrowserThread::IO, FROM_HERE, |
| 97 base::Bind(&AutoUpdateInterceptor::SetResponse, this, url, path)); | 114 base::Bind(&AutoUpdateInterceptor::SetResponse, this, url, path)); |
| 98 } | 115 } |
| 99 | 116 |
| 100 } // namespace extensions | 117 } // namespace extensions |
| OLD | NEW |