OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/thread_restrictions.h" | |
9 #include "chrome/browser/browser_thread.h" | 8 #include "chrome/browser/browser_thread.h" |
10 #include "net/url_request/url_request_test_job.h" | 9 #include "net/url_request/url_request_test_job.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
12 | 11 |
13 // This is a specialized version of URLRequestTestJob that lets us specify | 12 // This is a specialized version of URLRequestTestJob that lets us specify |
14 // response data and make sure the response code is 200, which the autoupdate | 13 // response data and make sure the response code is 200, which the autoupdate |
15 // code relies on. | 14 // code relies on. |
16 class AutoUpdateTestRequestJob : public URLRequestTestJob { | 15 class AutoUpdateTestRequestJob : public URLRequestTestJob { |
17 public: | 16 public: |
18 AutoUpdateTestRequestJob(URLRequest* request, | 17 AutoUpdateTestRequestJob(URLRequest* request, |
(...skipping 15 matching lines...) Expand all Loading... |
34 } | 33 } |
35 | 34 |
36 | 35 |
37 URLRequestJob* AutoUpdateInterceptor::MaybeIntercept(URLRequest* request) { | 36 URLRequestJob* AutoUpdateInterceptor::MaybeIntercept(URLRequest* request) { |
38 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 37 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
39 if (request->url().scheme() != "http" || | 38 if (request->url().scheme() != "http" || |
40 request->url().host() != "localhost") { | 39 request->url().host() != "localhost") { |
41 return NULL; | 40 return NULL; |
42 } | 41 } |
43 | 42 |
44 // It's ok to do a blocking disk access on this thread; this class | |
45 // is just used for tests. | |
46 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
47 | |
48 // Search for this request's url, ignoring any query parameters. | 43 // Search for this request's url, ignoring any query parameters. |
49 GURL url = request->url(); | 44 GURL url = request->url(); |
50 if (url.has_query()) { | 45 if (url.has_query()) { |
51 GURL::Replacements replacements; | 46 GURL::Replacements replacements; |
52 replacements.ClearQuery(); | 47 replacements.ClearQuery(); |
53 url = url.ReplaceComponents(replacements); | 48 url = url.ReplaceComponents(replacements); |
54 } | 49 } |
55 std::map<GURL, FilePath>::iterator i = responses_.find(url); | 50 std::map<GURL, FilePath>::iterator i = responses_.find(url); |
56 if (i == responses_.end()) { | 51 if (i == responses_.end()) { |
57 return NULL; | 52 return NULL; |
58 } | 53 } |
59 std::string contents; | 54 std::string contents; |
60 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); | 55 EXPECT_TRUE(file_util::ReadFileToString(i->second, &contents)); |
61 | 56 |
62 return new AutoUpdateTestRequestJob(request, contents); | 57 return new AutoUpdateTestRequestJob(request, contents); |
63 } | 58 } |
64 | 59 |
65 | 60 |
66 void AutoUpdateInterceptor::SetResponse(const std::string url, | 61 void AutoUpdateInterceptor::SetResponse(const std::string url, |
67 const FilePath& path) { | 62 const FilePath& path) { |
68 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 63 EXPECT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
69 // It's ok to do a blocking disk access on this thread; this class | |
70 // is just used for tests. | |
71 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
72 GURL gurl(url); | 64 GURL gurl(url); |
73 EXPECT_EQ("http", gurl.scheme()); | 65 EXPECT_EQ("http", gurl.scheme()); |
74 EXPECT_EQ("localhost", gurl.host()); | 66 EXPECT_EQ("localhost", gurl.host()); |
75 EXPECT_TRUE(file_util::PathExists(path)); | 67 EXPECT_TRUE(file_util::PathExists(path)); |
76 responses_[gurl] = path; | 68 responses_[gurl] = path; |
77 } | 69 } |
78 | 70 |
79 | 71 |
80 void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, | 72 void AutoUpdateInterceptor::SetResponseOnIOThread(const std::string url, |
81 const FilePath& path) { | 73 const FilePath& path) { |
82 BrowserThread::PostTask( | 74 BrowserThread::PostTask( |
83 BrowserThread::IO, FROM_HERE, | 75 BrowserThread::IO, FROM_HERE, |
84 NewRunnableMethod(this, &AutoUpdateInterceptor::SetResponse, url, path)); | 76 NewRunnableMethod(this, &AutoUpdateInterceptor::SetResponse, url, path)); |
85 } | 77 } |
OLD | NEW |