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