OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/test/weburl_loader_mock_factory.h" | 5 #include "content/test/weburl_loader_mock_factory.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 url_to_reponse_info_.clear(); | 69 url_to_reponse_info_.clear(); |
70 url_to_error_info_.clear(); | 70 url_to_error_info_.clear(); |
71 WebCache::clear(); | 71 WebCache::clear(); |
72 } | 72 } |
73 | 73 |
74 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { | 74 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { |
75 // Serving a request might trigger more requests, so we cannot iterate on | 75 // Serving a request might trigger more requests, so we cannot iterate on |
76 // pending_loaders_ as it might get modified. | 76 // pending_loaders_ as it might get modified. |
77 while (!pending_loaders_.empty()) { | 77 while (!pending_loaders_.empty()) { |
78 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); | 78 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
79 WebURLLoaderMock* loader = iter->first; | 79 base::WeakPtr<WebURLLoaderMock> loader(iter->first->GetWeakPtr()); |
80 const WebURLRequest& request = iter->second; | 80 const WebURLRequest request = iter->second; |
| 81 pending_loaders_.erase(loader.get()); |
| 82 |
81 WebURLResponse response; | 83 WebURLResponse response; |
82 WebURLError error; | 84 WebURLError error; |
83 WebData data; | 85 WebData data; |
84 LoadRequest(request, &response, &error, &data); | 86 LoadRequest(request, &response, &error, &data); |
85 // Follow any redirects while the loader is still active. | 87 // Follow any redirects while the loader is still active. |
86 while (response.httpStatusCode() >= 300 && | 88 while (response.httpStatusCode() >= 300 && |
87 response.httpStatusCode() < 400) { | 89 response.httpStatusCode() < 400) { |
88 WebURLRequest newRequest = loader->ServeRedirect(request, response); | 90 WebURLRequest newRequest = loader->ServeRedirect(request, response); |
89 if (!IsPending(loader) || loader->isDeferred()) | 91 if (!loader || loader->is_cancelled() || loader->is_deferred()) |
90 break; | 92 break; |
91 LoadRequest(newRequest, &response, &error, &data); | 93 LoadRequest(newRequest, &response, &error, &data); |
92 } | 94 } |
93 // Serve the request if the loader is still active. | 95 // Serve the request if the loader is still active. |
94 if (IsPending(loader) && !loader->isDeferred()) | 96 if (loader && !loader->is_cancelled() && !loader->is_deferred()) |
95 loader->ServeAsynchronousRequest(delegate_, response, data, error); | 97 loader->ServeAsynchronousRequest(delegate_, response, data, error); |
96 // The loader might have already been removed. | |
97 pending_loaders_.erase(loader); | |
98 } | 98 } |
99 base::RunLoop().RunUntilIdle(); | 99 base::RunLoop().RunUntilIdle(); |
100 } | 100 } |
101 | 101 |
102 bool WebURLLoaderMockFactory::IsMockedURL(const blink::WebURL& url) { | 102 bool WebURLLoaderMockFactory::IsMockedURL(const blink::WebURL& url) { |
103 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); | 103 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); |
104 } | 104 } |
105 | 105 |
106 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { | 106 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { |
107 pending_loaders_.erase(loader); | 107 pending_loaders_.erase(loader); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 } | 145 } |
146 | 146 |
147 if (!error->reason && !ReadFile(iter->second.file_path, data)) { | 147 if (!error->reason && !ReadFile(iter->second.file_path, data)) { |
148 NOTREACHED(); | 148 NOTREACHED(); |
149 return; | 149 return; |
150 } | 150 } |
151 | 151 |
152 *response = iter->second.response; | 152 *response = iter->second.response; |
153 } | 153 } |
154 | 154 |
155 bool WebURLLoaderMockFactory::IsPending(WebURLLoaderMock* loader) { | |
156 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); | |
157 return iter != pending_loaders_.end(); | |
158 } | |
159 | |
160 // static | 155 // static |
161 bool WebURLLoaderMockFactory::ReadFile(const base::FilePath& file_path, | 156 bool WebURLLoaderMockFactory::ReadFile(const base::FilePath& file_path, |
162 WebData* data) { | 157 WebData* data) { |
163 // If the path is empty then we return an empty file so tests can simulate | 158 // If the path is empty then we return an empty file so tests can simulate |
164 // requests without needing to actually load files. | 159 // requests without needing to actually load files. |
165 if (file_path.empty()) | 160 if (file_path.empty()) |
166 return true; | 161 return true; |
167 | 162 |
168 int64_t file_size = 0; | 163 int64_t file_size = 0; |
169 if (!base::GetFileSize(file_path, &file_size)) | 164 if (!base::GetFileSize(file_path, &file_size)) |
170 return false; | 165 return false; |
171 | 166 |
172 int size = static_cast<int>(file_size); | 167 int size = static_cast<int>(file_size); |
173 scoped_ptr<char[]> buffer(new char[size]); | 168 scoped_ptr<char[]> buffer(new char[size]); |
174 data->reset(); | 169 data->reset(); |
175 int read_count = base::ReadFile(file_path, buffer.get(), size); | 170 int read_count = base::ReadFile(file_path, buffer.get(), size); |
176 if (read_count == -1) | 171 if (read_count == -1) |
177 return false; | 172 return false; |
178 DCHECK(read_count == size); | 173 DCHECK(read_count == size); |
179 data->assign(buffer.get(), size); | 174 data->assign(buffer.get(), size); |
180 | 175 |
181 return true; | 176 return true; |
182 } | 177 } |
OLD | NEW |