OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/net/url_request_mock_http_job.h" | 5 #include "chrome/browser/net/url_request_mock_http_job.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/thread_restrictions.h" | 10 #include "base/thread_restrictions.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_request_filter.h" | 15 #include "net/url_request/url_request_filter.h" |
16 | 16 |
17 static const char kMockHostname[] = "mock.http"; | 17 static const char kMockHostname[] = "mock.http"; |
18 static const FilePath::CharType kMockHeaderFileSuffix[] = | 18 static const FilePath::CharType kMockHeaderFileSuffix[] = |
19 FILE_PATH_LITERAL(".mock-http-headers"); | 19 FILE_PATH_LITERAL(".mock-http-headers"); |
20 | 20 |
21 FilePath URLRequestMockHTTPJob::base_path_; | 21 FilePath URLRequestMockHTTPJob::base_path_; |
22 | 22 |
23 /* static */ | 23 /* static */ |
24 URLRequestJob* URLRequestMockHTTPJob::Factory(URLRequest* request, | 24 URLRequestJob* URLRequestMockHTTPJob::Factory(net::URLRequest* request, |
25 const std::string& scheme) { | 25 const std::string& scheme) { |
26 return new URLRequestMockHTTPJob(request, | 26 return new URLRequestMockHTTPJob(request, |
27 GetOnDiskPath(base_path_, request, scheme)); | 27 GetOnDiskPath(base_path_, request, scheme)); |
28 } | 28 } |
29 | 29 |
30 /* static */ | 30 /* static */ |
31 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { | 31 void URLRequestMockHTTPJob::AddUrlHandler(const FilePath& base_path) { |
32 base_path_ = base_path; | 32 base_path_ = base_path; |
33 | 33 |
34 // Add kMockHostname to URLRequestFilter. | 34 // Add kMockHostname to URLRequestFilter. |
(...skipping 14 matching lines...) Expand all Loading... |
49 /* static */ | 49 /* static */ |
50 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { | 50 GURL URLRequestMockHTTPJob::GetMockViewSourceUrl(const FilePath& path) { |
51 std::string url = chrome::kViewSourceScheme; | 51 std::string url = chrome::kViewSourceScheme; |
52 url.append(":"); | 52 url.append(":"); |
53 url.append(GetMockUrl(path).spec()); | 53 url.append(GetMockUrl(path).spec()); |
54 return GURL(url); | 54 return GURL(url); |
55 } | 55 } |
56 | 56 |
57 /* static */ | 57 /* static */ |
58 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, | 58 FilePath URLRequestMockHTTPJob::GetOnDiskPath(const FilePath& base_path, |
59 URLRequest* request, | 59 net::URLRequest* request, |
60 const std::string& scheme) { | 60 const std::string& scheme) { |
61 std::string file_url("file:///"); | 61 std::string file_url("file:///"); |
62 file_url += WideToUTF8(base_path.ToWStringHack()); | 62 file_url += WideToUTF8(base_path.ToWStringHack()); |
63 file_url += request->url().path(); | 63 file_url += request->url().path(); |
64 | 64 |
65 // Convert the file:/// URL to a path on disk. | 65 // Convert the file:/// URL to a path on disk. |
66 FilePath file_path; | 66 FilePath file_path; |
67 net::FileURLToFilePath(GURL(file_url), &file_path); | 67 net::FileURLToFilePath(GURL(file_url), &file_path); |
68 return file_path; | 68 return file_path; |
69 } | 69 } |
70 | 70 |
71 URLRequestMockHTTPJob::URLRequestMockHTTPJob(URLRequest* request, | 71 URLRequestMockHTTPJob::URLRequestMockHTTPJob(net::URLRequest* request, |
72 const FilePath& file_path) | 72 const FilePath& file_path) |
73 : URLRequestFileJob(request, file_path) { } | 73 : URLRequestFileJob(request, file_path) { } |
74 | 74 |
75 // Public virtual version. | 75 // Public virtual version. |
76 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 76 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
77 // Forward to private const version. | 77 // Forward to private const version. |
78 GetResponseInfoConst(info); | 78 GetResponseInfoConst(info); |
79 } | 79 } |
80 | 80 |
81 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 81 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
(...skipping 24 matching lines...) Expand all Loading... |
106 net::HttpResponseInfo info; | 106 net::HttpResponseInfo info; |
107 GetResponseInfoConst(&info); | 107 GetResponseInfoConst(&info); |
108 return info.headers && info.headers->GetMimeType(mime_type); | 108 return info.headers && info.headers->GetMimeType(mime_type); |
109 } | 109 } |
110 | 110 |
111 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 111 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
112 net::HttpResponseInfo info; | 112 net::HttpResponseInfo info; |
113 GetResponseInfo(&info); | 113 GetResponseInfo(&info); |
114 return info.headers && info.headers->GetCharset(charset); | 114 return info.headers && info.headers->GetCharset(charset); |
115 } | 115 } |
OLD | NEW |