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/net/url_request_mock_net_error_job.h" | |
6 | |
7 #include <string> | |
8 #include <vector> | |
9 | |
10 #include "base/compiler_specific.h" | |
11 #include "base/file_util.h" | |
12 #include "base/message_loop.h" | |
13 #include "base/task.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/base/net_util.h" | |
17 #include "net/base/x509_certificate.h" | |
18 #include "net/url_request/url_request_filter.h" | |
19 | |
20 // static | |
21 URLRequestMockNetErrorJob::URLMockInfoMap | |
22 URLRequestMockNetErrorJob::url_mock_info_map_; | |
23 | |
24 struct URLRequestMockNetErrorJob::MockInfo { | |
25 MockInfo() : ssl_cert(NULL) { } | |
26 MockInfo(std::wstring base, | |
27 std::vector<int> errors, | |
28 net::X509Certificate* ssl_cert) | |
29 : base(base), | |
30 errors(errors), | |
31 ssl_cert(ssl_cert) { } | |
32 | |
33 std::wstring base; | |
34 std::vector<int> errors; | |
35 scoped_refptr<net::X509Certificate> ssl_cert; | |
36 }; | |
37 | |
38 // static | |
39 void URLRequestMockNetErrorJob::AddMockedURL(const GURL& url, | |
40 const std::wstring& base, | |
41 const std::vector<int>& errors, | |
42 net::X509Certificate* ssl_cert) { | |
43 #ifndef NDEBUG | |
44 URLMockInfoMap::const_iterator iter = url_mock_info_map_.find(url); | |
45 DCHECK(iter == url_mock_info_map_.end()); | |
46 #endif | |
47 | |
48 url_mock_info_map_[url] = MockInfo(base, errors, ssl_cert); | |
49 net::URLRequestFilter::GetInstance() | |
50 ->AddUrlHandler(url, &URLRequestMockNetErrorJob::Factory); | |
51 } | |
52 | |
53 // static | |
54 void URLRequestMockNetErrorJob::RemoveMockedURL(const GURL& url) { | |
55 URLMockInfoMap::iterator iter = url_mock_info_map_.find(url); | |
56 DCHECK(iter != url_mock_info_map_.end()); | |
57 url_mock_info_map_.erase(iter); | |
58 net::URLRequestFilter::GetInstance()->RemoveUrlHandler(url); | |
59 } | |
60 | |
61 // static | |
62 net::URLRequestJob* URLRequestMockNetErrorJob::Factory( | |
63 net::URLRequest* request, | |
64 const std::string& scheme) { | |
65 GURL url = request->url(); | |
66 | |
67 URLMockInfoMap::const_iterator iter = url_mock_info_map_.find(url); | |
68 DCHECK(iter != url_mock_info_map_.end()); | |
69 | |
70 MockInfo mock_info = iter->second; | |
71 | |
72 // URLRequestMockNetErrorJob derives from net::URLRequestFileJob. We pass a | |
73 // FilePath so that the net::URLRequestFileJob methods will do the loading | |
74 // from the files. | |
75 std::wstring file_url(L"file:///"); | |
76 file_url.append(mock_info.base); | |
77 file_url.append(UTF8ToWide(url.path())); | |
78 // Convert the file:/// URL to a path on disk. | |
79 FilePath file_path; | |
80 net::FileURLToFilePath(GURL(WideToUTF8(file_url)), &file_path); | |
81 return new URLRequestMockNetErrorJob(request, mock_info.errors, | |
82 mock_info.ssl_cert, | |
83 file_path); | |
84 } | |
85 | |
86 URLRequestMockNetErrorJob::URLRequestMockNetErrorJob(net::URLRequest* request, | |
87 const std::vector<int>& errors, net::X509Certificate* cert, | |
88 const FilePath& file_path) | |
89 : URLRequestMockHTTPJob(request, file_path), | |
90 errors_(errors), | |
91 ssl_cert_(cert), | |
92 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
93 } | |
94 | |
95 URLRequestMockNetErrorJob::~URLRequestMockNetErrorJob() { | |
96 } | |
97 | |
98 void URLRequestMockNetErrorJob::Start() { | |
99 MessageLoop::current()->PostTask( | |
100 FROM_HERE, | |
101 method_factory_.NewRunnableMethod( | |
102 &URLRequestMockNetErrorJob::StartAsync)); | |
103 } | |
104 | |
105 void URLRequestMockNetErrorJob::StartAsync() { | |
106 if (errors_.empty()) { | |
107 URLRequestMockHTTPJob::Start(); | |
108 } else { | |
109 int error = errors_[0]; | |
110 errors_.erase(errors_.begin()); | |
111 | |
112 if (net::IsCertificateError(error)) { | |
113 DCHECK(ssl_cert_); | |
114 request_->delegate()->OnSSLCertificateError(request_, error, | |
115 ssl_cert_.get()); | |
116 } else { | |
117 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | |
118 error)); | |
119 } | |
120 } | |
121 } | |
122 | |
123 void URLRequestMockNetErrorJob::ContinueDespiteLastError() { | |
124 Start(); | |
125 } | |
OLD | NEW |