OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/support/weburl_loader_mock_factory.h" | 5 #include "webkit/support/weburl_loader_mock_factory.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLError.
h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // Serving a request might trigger more requests, so we cannot iterate on | 64 // Serving a request might trigger more requests, so we cannot iterate on |
65 // pending_loaders_ as it might get modified. | 65 // pending_loaders_ as it might get modified. |
66 while (!pending_loaders_.empty()) { | 66 while (!pending_loaders_.empty()) { |
67 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); | 67 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
68 WebURLLoaderMock* loader = iter->first; | 68 WebURLLoaderMock* loader = iter->first; |
69 const WebURLRequest& request = iter->second; | 69 const WebURLRequest& request = iter->second; |
70 WebURLResponse response; | 70 WebURLResponse response; |
71 WebURLError error; | 71 WebURLError error; |
72 WebData data; | 72 WebData data; |
73 LoadRequest(request, &response, &error, &data); | 73 LoadRequest(request, &response, &error, &data); |
74 // Follow any redirect chain. | 74 // Follow any redirects while the loader is still active. |
75 while (response.httpStatusCode() >= 300 && | 75 while (response.httpStatusCode() >= 300 && |
76 response.httpStatusCode() < 400) { | 76 response.httpStatusCode() < 400) { |
77 WebURLRequest newRequest = loader->ServeRedirect(response); | 77 WebURLRequest newRequest = loader->ServeRedirect(response); |
78 if (loader->isDeferred()) | 78 if (!IsPending(loader) || loader->isDeferred()) |
79 break; | 79 break; |
80 LoadRequest(newRequest, &response, &error, &data); | 80 LoadRequest(newRequest, &response, &error, &data); |
81 } | 81 } |
82 if (!loader->isDeferred()) | 82 // Serve the request if the loader is still active. |
| 83 if (IsPending(loader) && !loader->isDeferred()) |
83 loader->ServeAsynchronousRequest(response, data, error); | 84 loader->ServeAsynchronousRequest(response, data, error); |
84 // If the load has been canceled, the loader may not be in the map. | 85 // The loader might have already been removed. |
85 pending_loaders_.erase(loader); | 86 pending_loaders_.erase(loader); |
86 } | 87 } |
87 } | 88 } |
88 | 89 |
89 bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) { | 90 bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) { |
90 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); | 91 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); |
91 } | 92 } |
92 | 93 |
93 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { | 94 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { |
94 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); | 95 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } | 130 } |
130 | 131 |
131 if (!ReadFile(iter->second.file_path, data)) { | 132 if (!ReadFile(iter->second.file_path, data)) { |
132 NOTREACHED(); | 133 NOTREACHED(); |
133 return; | 134 return; |
134 } | 135 } |
135 | 136 |
136 *response = iter->second.response; | 137 *response = iter->second.response; |
137 } | 138 } |
138 | 139 |
| 140 bool WebURLLoaderMockFactory::IsPending(WebURLLoaderMock* loader) { |
| 141 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
| 142 return iter != pending_loaders_.end(); |
| 143 } |
| 144 |
139 // static | 145 // static |
140 bool WebURLLoaderMockFactory::ReadFile(const FilePath& file_path, | 146 bool WebURLLoaderMockFactory::ReadFile(const FilePath& file_path, |
141 WebData* data) { | 147 WebData* data) { |
142 int64 file_size = 0; | 148 int64 file_size = 0; |
143 if (!file_util::GetFileSize(file_path, &file_size)) | 149 if (!file_util::GetFileSize(file_path, &file_size)) |
144 return false; | 150 return false; |
145 | 151 |
146 int size = static_cast<int>(file_size); | 152 int size = static_cast<int>(file_size); |
147 scoped_array<char> buffer(new char[size]); | 153 scoped_array<char> buffer(new char[size]); |
148 data->reset(); | 154 data->reset(); |
149 int read_count = file_util::ReadFile(file_path, buffer.get(), size); | 155 int read_count = file_util::ReadFile(file_path, buffer.get(), size); |
150 if (read_count == -1) | 156 if (read_count == -1) |
151 return false; | 157 return false; |
152 DCHECK(read_count == size); | 158 DCHECK(read_count == size); |
153 data->assign(buffer.get(), size); | 159 data->assign(buffer.get(), size); |
154 | 160 |
155 return true; | 161 return true; |
156 } | 162 } |
OLD | NEW |