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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 URLToResponseMap::iterator iter = url_to_reponse_info_.find(url); | 54 URLToResponseMap::iterator iter = url_to_reponse_info_.find(url); |
55 DCHECK(iter != url_to_reponse_info_.end()); | 55 DCHECK(iter != url_to_reponse_info_.end()); |
56 url_to_reponse_info_.erase(iter); | 56 url_to_reponse_info_.erase(iter); |
57 } | 57 } |
58 | 58 |
59 void WebURLLoaderMockFactory::UnregisterAllURLs() { | 59 void WebURLLoaderMockFactory::UnregisterAllURLs() { |
60 url_to_reponse_info_.clear(); | 60 url_to_reponse_info_.clear(); |
61 } | 61 } |
62 | 62 |
63 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { | 63 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { |
| 64 last_handled_asynchronous_request_.reset(); |
64 // Serving a request might trigger more requests, so we cannot iterate on | 65 // Serving a request might trigger more requests, so we cannot iterate on |
65 // pending_loaders_ as it might get modified. | 66 // pending_loaders_ as it might get modified. |
66 while (!pending_loaders_.empty()) { | 67 while (!pending_loaders_.empty()) { |
67 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); | 68 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
68 WebURLLoaderMock* loader = iter->first; | 69 WebURLLoaderMock* loader = iter->first; |
69 const WebURLRequest& request = iter->second; | 70 const WebURLRequest& request = iter->second; |
70 WebURLResponse response; | 71 WebURLResponse response; |
71 WebURLError error; | 72 WebURLError error; |
72 WebData data; | 73 WebData data; |
| 74 last_handled_asynchronous_request_ = request; |
73 LoadRequest(request, &response, &error, &data); | 75 LoadRequest(request, &response, &error, &data); |
74 // Follow any redirects while the loader is still active. | 76 // Follow any redirects while the loader is still active. |
75 while (response.httpStatusCode() >= 300 && | 77 while (response.httpStatusCode() >= 300 && |
76 response.httpStatusCode() < 400) { | 78 response.httpStatusCode() < 400) { |
77 WebURLRequest newRequest = loader->ServeRedirect(response); | 79 WebURLRequest newRequest = loader->ServeRedirect(response); |
78 if (!IsPending(loader) || loader->isDeferred()) | 80 if (!IsPending(loader) || loader->isDeferred()) |
79 break; | 81 break; |
| 82 last_handled_asynchronous_request_ = newRequest; |
80 LoadRequest(newRequest, &response, &error, &data); | 83 LoadRequest(newRequest, &response, &error, &data); |
81 } | 84 } |
82 // Serve the request if the loader is still active. | 85 // Serve the request if the loader is still active. |
83 if (IsPending(loader) && !loader->isDeferred()) | 86 if (IsPending(loader) && !loader->isDeferred()) |
84 loader->ServeAsynchronousRequest(response, data, error); | 87 loader->ServeAsynchronousRequest(response, data, error); |
85 // The loader might have already been removed. | 88 // The loader might have already been removed. |
86 pending_loaders_.erase(loader); | 89 pending_loaders_.erase(loader); |
87 } | 90 } |
88 } | 91 } |
89 | 92 |
| 93 WebKit::WebURLRequest |
| 94 WebURLLoaderMockFactory::GetLastHandledAsynchronousRequest() { |
| 95 return last_handled_asynchronous_request_; |
| 96 } |
| 97 |
90 bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) { | 98 bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) { |
91 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); | 99 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); |
92 } | 100 } |
93 | 101 |
94 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { | 102 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { |
95 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); | 103 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
96 DCHECK(iter != pending_loaders_.end()); | 104 DCHECK(iter != pending_loaders_.end()); |
97 pending_loaders_.erase(iter); | 105 pending_loaders_.erase(iter); |
98 } | 106 } |
99 | 107 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 scoped_array<char> buffer(new char[size]); | 161 scoped_array<char> buffer(new char[size]); |
154 data->reset(); | 162 data->reset(); |
155 int read_count = file_util::ReadFile(file_path, buffer.get(), size); | 163 int read_count = file_util::ReadFile(file_path, buffer.get(), size); |
156 if (read_count == -1) | 164 if (read_count == -1) |
157 return false; | 165 return false; |
158 DCHECK(read_count == size); | 166 DCHECK(read_count == size); |
159 data->assign(buffer.get(), size); | 167 data->assign(buffer.get(), size); |
160 | 168 |
161 return true; | 169 return true; |
162 } | 170 } |
OLD | NEW |