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 | 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" | |
|
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.
| |
| 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. |
| 22 class AutoUpdateTestRequestJob : public net::URLRequestTestJob { | 23 class AutoUpdateTestRequestJob : public net::URLRequestTestJob { |
| 23 public: | 24 public: |
| 24 AutoUpdateTestRequestJob(net::URLRequest* request, | 25 AutoUpdateTestRequestJob(net::URLRequest* request, |
| 25 net::NetworkDelegate* network_delegate, | 26 net::NetworkDelegate* network_delegate, |
| 26 const std::string& response_data) | 27 const std::string& response_data) |
| 27 : net::URLRequestTestJob(request, | 28 : net::URLRequestTestJob(request, |
| 28 network_delegate, | 29 network_delegate, |
| 29 net::URLRequestTestJob::test_headers(), | 30 net::URLRequestTestJob::test_headers(), |
| 30 response_data, | 31 response_data, |
| 31 true) { | 32 true) { |
| 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 |
| 41 class AutoUpdateInterceptor::Delegate | |
| 42 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 43 public: | |
| 44 Delegate() {} | |
| 45 virtual ~Delegate() { | |
| 46 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 47 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http", | |
| 48 "localhost"); | |
| 49 } | |
| 50 void Register() { | |
| 51 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 52 net::URLRequestFilter::GetInstance()->AddHostnameProtocolHandler( | |
| 53 "http", "localhost", this); | |
| 54 } | |
| 40 | 55 |
| 41 AutoUpdateInterceptor::AutoUpdateInterceptor() { | 56 // When computing matches, this ignores query parameters (since the autoupdate |
| 42 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | 57 // fetch code appends a bunch of them to manifest fetches). |
| 58 virtual net::URLRequestJob* MaybeCreateJob( | |
| 59 net::URLRequest* request, | |
| 60 net::NetworkDelegate* network_delegate) const OVERRIDE { | |
| 61 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 62 if (request->url().scheme() != "http" || | |
| 63 request->url().host() != "localhost") { | |
| 64 return NULL; | |
| 65 } | |
| 66 | |
| 67 // It's ok to do a blocking disk access on this thread; this class | |
| 68 // is just used for tests. | |
| 69 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 70 | |
| 71 // Search for this request's url, ignoring any query parameters. | |
| 72 GURL url = request->url(); | |
| 73 if (url.has_query()) { | |
| 74 GURL::Replacements replacements; | |
| 75 replacements.ClearQuery(); | |
| 76 url = url.ReplaceComponents(replacements); | |
| 77 } | |
| 78 std::map<GURL, FilePath>::const_iterator i = responses_.find(url); | |
| 79 if (i == responses_.end()) { | |
| 80 return NULL; | |
| 81 } | |
| 82 std::string contents; | |
| 83 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); | |
| 84 | |
| 85 return new AutoUpdateTestRequestJob(request, network_delegate, contents); | |
| 86 } | |
| 87 | |
| 88 // When requests for |url| arrive, respond with the contents of |path|. The | |
| 89 // hostname of |url| must be "localhost" to avoid DNS lookups, and the scheme | |
| 90 // must be "http" so MaybeIntercept can ignore "chrome" and other schemes. | |
| 91 // Also, the match for |url| will ignore any query parameters. | |
| 92 void SetResponse(const std::string url, const FilePath& path) { | |
| 93 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 94 // It's ok to do a blocking disk access on this thread; this class | |
| 95 // is just used for tests. | |
| 96 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 97 GURL gurl(url); | |
| 98 EXPECT_EQ("http", gurl.scheme()); | |
| 99 EXPECT_EQ("localhost", gurl.host()); | |
| 100 EXPECT_TRUE(file_util::PathExists(path)); | |
| 101 responses_[gurl] = path; | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 std::map<GURL, FilePath> responses_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(Delegate); | |
| 108 }; | |
| 109 | |
| 110 AutoUpdateInterceptor::AutoUpdateInterceptor() : delegate_(new Delegate) { | |
| 111 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 112 base::Bind(&Delegate::Register, | |
| 113 base::Unretained(delegate_))); | |
| 43 } | 114 } |
| 44 | 115 |
| 45 AutoUpdateInterceptor::~AutoUpdateInterceptor() { | 116 AutoUpdateInterceptor::~AutoUpdateInterceptor() { |
| 46 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 117 BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, delegate_); |
| 47 } | 118 } |
| 48 | 119 |
| 49 net::URLRequestJob* AutoUpdateInterceptor::MaybeIntercept( | |
| 50 net::URLRequest* request, net::NetworkDelegate* network_delegate) { | |
| 51 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 52 if (request->url().scheme() != "http" || | |
| 53 request->url().host() != "localhost") { | |
| 54 return NULL; | |
| 55 } | |
| 56 | |
| 57 // It's ok to do a blocking disk access on this thread; this class | |
| 58 // is just used for tests. | |
| 59 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 60 | |
| 61 // Search for this request's url, ignoring any query parameters. | |
| 62 GURL url = request->url(); | |
| 63 if (url.has_query()) { | |
| 64 GURL::Replacements replacements; | |
| 65 replacements.ClearQuery(); | |
| 66 url = url.ReplaceComponents(replacements); | |
| 67 } | |
| 68 std::map<GURL, FilePath>::iterator i = responses_.find(url); | |
| 69 if (i == responses_.end()) { | |
| 70 return NULL; | |
| 71 } | |
| 72 std::string contents; | |
| 73 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); | |
| 74 | |
| 75 return new AutoUpdateTestRequestJob(request, network_delegate, contents); | |
| 76 } | |
| 77 | |
| 78 | |
| 79 void AutoUpdateInterceptor::SetResponse(const std::string url, | 120 void AutoUpdateInterceptor::SetResponse(const std::string url, |
| 80 const FilePath& path) { | 121 const FilePath& path) { |
| 81 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 122 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 82 // It's ok to do a blocking disk access on this thread; this class | 123 base::Bind(&Delegate::SetResponse, |
| 83 // is just used for tests. | 124 base::Unretained(delegate_), url, path)); |
| 84 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 85 GURL gurl(url); | |
| 86 EXPECT_EQ("http", gurl.scheme()); | |
| 87 EXPECT_EQ("localhost", gurl.host()); | |
| 88 EXPECT_TRUE(file_util::PathExists(path)); | |
| 89 responses_[gurl] = path; | |
| 90 } | |
| 91 | |
| 92 | |
| 93 void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, | |
| 94 const FilePath& path) { | |
| 95 BrowserThread::PostTask( | |
| 96 BrowserThread::IO, FROM_HERE, | |
| 97 base::Bind(&AutoUpdateInterceptor::SetResponse, this, url, path)); | |
| 98 } | 125 } |
| 99 | 126 |
| 100 } // namespace extensions | 127 } // namespace extensions |
| OLD | NEW |