OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "webkit/support/weburl_loader_mock_factory.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
| 9 #include "third_party/WebKit/WebKit/chromium/public/WebURLError.h" |
| 10 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebURLResponse.h" |
| 12 #include "webkit/support/weburl_loader_mock.h" |
| 13 |
| 14 using WebKit::WebData; |
| 15 using WebKit::WebString; |
| 16 using WebKit::WebURL; |
| 17 using WebKit::WebURLError; |
| 18 using WebKit::WebURLLoader; |
| 19 using WebKit::WebURLRequest; |
| 20 using WebKit::WebURLResponse; |
| 21 |
| 22 void WebURLLoaderMockFactory::RegisterURL(const WebURL& url, |
| 23 const WebURLResponse& response, |
| 24 const WebString& file_path) { |
| 25 ResponseInfo response_info; |
| 26 response_info.response = response; |
| 27 if (!file_path.isNull() && !file_path.isEmpty()) { |
| 28 #if defined(OS_POSIX) |
| 29 // TODO(jcivelli): On Linux, UTF8 might not be correct. |
| 30 response_info.file_path = |
| 31 FilePath(static_cast<std::string>(file_path.utf8())); |
| 32 #elif defined(OS_WIN) |
| 33 response_info.file_path = |
| 34 FilePath(std::wstring(file_path.data(), file_path.length())); |
| 35 #endif |
| 36 DCHECK(file_util::PathExists(response_info.file_path)); |
| 37 } |
| 38 |
| 39 DCHECK(url_to_reponse_info_.find(url) == url_to_reponse_info_.end()); |
| 40 url_to_reponse_info_[url] = response_info; |
| 41 } |
| 42 |
| 43 void WebURLLoaderMockFactory::UnregisterURL(const WebKit::WebURL& url) { |
| 44 URLToResponseMap::iterator iter = url_to_reponse_info_.find(url); |
| 45 DCHECK(iter != url_to_reponse_info_.end()); |
| 46 url_to_reponse_info_.erase(iter); |
| 47 } |
| 48 |
| 49 void WebURLLoaderMockFactory::UnregisterAllURLs() { |
| 50 url_to_reponse_info_.clear(); |
| 51 } |
| 52 |
| 53 void WebURLLoaderMockFactory::ServeAsynchronousRequests() { |
| 54 // Serving a request might trigger more requests, so we cannot iterate on |
| 55 // pending_loaders_ as it might get modified. |
| 56 while (!pending_loaders_.empty()) { |
| 57 LoaderToRequestMap::iterator iter = pending_loaders_.begin(); |
| 58 WebURLLoaderMock* loader = iter->first; |
| 59 const WebURLRequest& request = iter->second; |
| 60 WebURLResponse response; |
| 61 WebURLError error; |
| 62 WebData data; |
| 63 LoadRequest(request, &response, &error, &data); |
| 64 loader->ServeAsynchronousRequest(response, data, error); |
| 65 pending_loaders_.erase(iter); |
| 66 } |
| 67 } |
| 68 |
| 69 bool WebURLLoaderMockFactory::IsMockedURL(const WebKit::WebURL& url) { |
| 70 return url_to_reponse_info_.find(url) != url_to_reponse_info_.end(); |
| 71 } |
| 72 |
| 73 void WebURLLoaderMockFactory::CancelLoad(WebURLLoaderMock* loader) { |
| 74 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
| 75 DCHECK(iter != pending_loaders_.end()); |
| 76 pending_loaders_.erase(iter); |
| 77 } |
| 78 |
| 79 WebURLLoader* WebURLLoaderMockFactory::CreateURLLoader( |
| 80 WebURLLoader* default_loader) { |
| 81 DCHECK(default_loader); |
| 82 return new WebURLLoaderMock(this, default_loader); |
| 83 } |
| 84 |
| 85 void WebURLLoaderMockFactory::LoadSynchronously(const WebURLRequest& request, |
| 86 WebURLResponse* response, |
| 87 WebURLError* error, |
| 88 WebData* data) { |
| 89 LoadRequest(request, response, error, data); |
| 90 } |
| 91 |
| 92 void WebURLLoaderMockFactory::LoadAsynchronouly(const WebURLRequest& request, |
| 93 WebURLLoaderMock* loader) { |
| 94 LoaderToRequestMap::iterator iter = pending_loaders_.find(loader); |
| 95 DCHECK(iter == pending_loaders_.end()); |
| 96 pending_loaders_[loader] = request; |
| 97 } |
| 98 |
| 99 void WebURLLoaderMockFactory::LoadRequest(const WebURLRequest& request, |
| 100 WebURLResponse* response, |
| 101 WebURLError* error, |
| 102 WebData* data) { |
| 103 URLToResponseMap::const_iterator iter = |
| 104 url_to_reponse_info_.find(request.url()); |
| 105 if (iter == url_to_reponse_info_.end()) { |
| 106 // Non mocked URLs should not have been passed to the default URLLoader. |
| 107 NOTREACHED(); |
| 108 return; |
| 109 } |
| 110 |
| 111 if (!ReadFile(iter->second.file_path, data)) { |
| 112 NOTREACHED(); |
| 113 return; |
| 114 } |
| 115 |
| 116 *response = iter->second.response; |
| 117 } |
| 118 |
| 119 // static |
| 120 bool WebURLLoaderMockFactory::ReadFile(const FilePath& file_path, |
| 121 WebData* data) { |
| 122 int64 file_size = 0; |
| 123 if (!file_util::GetFileSize(file_path, &file_size)) |
| 124 return false; |
| 125 |
| 126 int size = static_cast<int>(file_size); |
| 127 scoped_array<char> buffer(new char[size]); |
| 128 data->reset(); |
| 129 int read_count = file_util::ReadFile(file_path, buffer.get(), size); |
| 130 if (read_count == -1) |
| 131 return false; |
| 132 DCHECK(read_count == size); |
| 133 data->assign(buffer.get(), size); |
| 134 |
| 135 return true; |
| 136 } |
OLD | NEW |