| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/autoupdate_interceptor.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/threading/thread_restrictions.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "net/url_request/url_request.h" | |
| 12 #include "net/url_request/url_request_test_job.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 // 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 // code relies on. | |
| 22 class AutoUpdateTestRequestJob : public net::URLRequestTestJob { | |
| 23 public: | |
| 24 AutoUpdateTestRequestJob(net::URLRequest* request, | |
| 25 net::NetworkDelegate* network_delegate, | |
| 26 const std::string& response_data) | |
| 27 : net::URLRequestTestJob(request, | |
| 28 network_delegate, | |
| 29 net::URLRequestTestJob::test_headers(), | |
| 30 response_data, | |
| 31 true) { | |
| 32 } | |
| 33 | |
| 34 virtual int GetResponseCode() const { return 200; } | |
| 35 | |
| 36 private: | |
| 37 ~AutoUpdateTestRequestJob() {} | |
| 38 }; | |
| 39 | |
| 40 | |
| 41 AutoUpdateInterceptor::AutoUpdateInterceptor() { | |
| 42 net::URLRequest::Deprecated::RegisterRequestInterceptor(this); | |
| 43 } | |
| 44 | |
| 45 AutoUpdateInterceptor::~AutoUpdateInterceptor() { | |
| 46 net::URLRequest::Deprecated::UnregisterRequestInterceptor(this); | |
| 47 } | |
| 48 | |
| 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, | |
| 80 const FilePath& path) { | |
| 81 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 82 // It's ok to do a blocking disk access on this thread; this class | |
| 83 // is just used for tests. | |
| 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 } | |
| 99 | |
| 100 } // namespace extensions | |
| OLD | NEW |