OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/glue/plugins/pepper_url_loader.h" | 5 #include "webkit/glue/plugins/pepper_url_loader.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "third_party/ppapi/c/pp_completion_callback.h" | 8 #include "third_party/ppapi/c/pp_completion_callback.h" |
9 #include "third_party/ppapi/c/pp_errors.h" | 9 #include "third_party/ppapi/c/pp_errors.h" |
10 #include "third_party/ppapi/c/ppb_url_loader.h" | 10 #include "third_party/ppapi/c/ppb_url_loader.h" |
| 11 #include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" |
| 12 #include "third_party/WebKit/WebKit/chromium/public/WebElement.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebFrame.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebKit.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebKitClient.h" |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebPluginContainer.h" |
11 #include "webkit/glue/plugins/pepper_plugin_instance.h" | 17 #include "webkit/glue/plugins/pepper_plugin_instance.h" |
12 #include "webkit/glue/plugins/pepper_url_request_info.h" | 18 #include "webkit/glue/plugins/pepper_url_request_info.h" |
13 #include "webkit/glue/plugins/pepper_url_response_info.h" | 19 #include "webkit/glue/plugins/pepper_url_response_info.h" |
14 | 20 |
| 21 using WebKit::WebFrame; |
| 22 using WebKit::WebURL; |
| 23 using WebKit::WebURLError; |
| 24 using WebKit::WebURLLoader; |
| 25 using WebKit::WebURLRequest; |
| 26 using WebKit::WebURLResponse; |
| 27 |
| 28 #ifdef _MSC_VER |
| 29 // Do not warn about use of std::copy with raw pointers. |
| 30 #pragma warning(disable : 4996) |
| 31 #endif |
| 32 |
15 namespace pepper { | 33 namespace pepper { |
16 | 34 |
17 namespace { | 35 namespace { |
18 | 36 |
19 PP_Resource Create(PP_Instance instance_id) { | 37 PP_Resource Create(PP_Instance instance_id) { |
20 PluginInstance* instance = PluginInstance::FromPPInstance(instance_id); | 38 PluginInstance* instance = PluginInstance::FromPPInstance(instance_id); |
21 if (!instance) | 39 if (!instance) |
22 return 0; | 40 return 0; |
23 | 41 |
24 URLLoader* loader = new URLLoader(instance); | 42 URLLoader* loader = new URLLoader(instance); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 &GetDownloadProgress, | 138 &GetDownloadProgress, |
121 &GetResponseInfo, | 139 &GetResponseInfo, |
122 &ReadResponseBody, | 140 &ReadResponseBody, |
123 &Close | 141 &Close |
124 }; | 142 }; |
125 | 143 |
126 } // namespace | 144 } // namespace |
127 | 145 |
128 URLLoader::URLLoader(PluginInstance* instance) | 146 URLLoader::URLLoader(PluginInstance* instance) |
129 : Resource(instance->module()), | 147 : Resource(instance->module()), |
| 148 instance_(instance), |
| 149 pending_callback_(), |
130 bytes_sent_(0), | 150 bytes_sent_(0), |
131 total_bytes_to_be_sent_(0), | 151 total_bytes_to_be_sent_(0), |
132 bytes_received_(0), | 152 bytes_received_(0), |
133 total_bytes_to_be_received_(0) { | 153 total_bytes_to_be_received_(0) { |
134 } | 154 } |
135 | 155 |
136 URLLoader::~URLLoader() { | 156 URLLoader::~URLLoader() { |
137 } | 157 } |
138 | 158 |
| 159 // static |
| 160 const PPB_URLLoader* URLLoader::GetInterface() { |
| 161 return &ppb_urlloader; |
| 162 } |
| 163 |
139 int32_t URLLoader::Open(URLRequestInfo* request, | 164 int32_t URLLoader::Open(URLRequestInfo* request, |
140 PP_CompletionCallback callback) { | 165 PP_CompletionCallback callback) { |
141 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 166 if (loader_.get()) |
142 return PP_Error_Failed; | 167 return PP_Error_InProgress; |
| 168 |
| 169 // We only support non-blocking calls. |
| 170 if (!callback.func) |
| 171 return PP_Error_BadArgument; |
| 172 |
| 173 WebURLRequest web_request(request->web_request()); |
| 174 |
| 175 WebFrame* frame = instance_->container()->element().document().frame(); |
| 176 if (!frame) |
| 177 return PP_Error_Failed; |
| 178 frame->setReferrerForRequest(web_request, WebURL()); // Use default. |
| 179 frame->dispatchWillSendRequest(web_request); |
| 180 |
| 181 loader_.reset(WebKit::webKitClient()->createURLLoader()); |
| 182 if (!loader_.get()) { |
| 183 loader_.reset(); |
| 184 return PP_Error_Failed; |
| 185 } |
| 186 loader_->loadAsynchronously(web_request, this); |
| 187 |
| 188 pending_callback_ = callback; |
| 189 |
| 190 // Notify completion when we receive a redirect or response headers. |
| 191 return PP_Error_WouldBlock; |
143 } | 192 } |
144 | 193 |
145 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { | 194 int32_t URLLoader::FollowRedirect(PP_CompletionCallback callback) { |
146 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 195 NOTIMPLEMENTED(); // TODO(darin): Implement me. |
147 return PP_Error_Failed; | 196 return PP_Error_Failed; |
148 } | 197 } |
149 | 198 |
150 int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, | 199 int32_t URLLoader::ReadResponseBody(char* buffer, int32_t bytes_to_read, |
151 PP_CompletionCallback callback) { | 200 PP_CompletionCallback callback) { |
152 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 201 if (bytes_to_read <= 0 || !buffer) |
153 return PP_Error_Failed; | 202 return PP_Error_BadArgument; |
| 203 if (pending_callback_.func) |
| 204 return PP_Error_InProgress; |
| 205 |
| 206 // We only support non-blocking calls. |
| 207 if (!callback.func) |
| 208 return PP_Error_BadArgument; |
| 209 |
| 210 user_buffer_ = buffer; |
| 211 user_buffer_size_ = bytes_to_read; |
| 212 |
| 213 if (!buffer_.empty()) |
| 214 return FillUserBuffer(); |
| 215 |
| 216 pending_callback_ = callback; |
| 217 return PP_Error_WouldBlock; |
154 } | 218 } |
155 | 219 |
156 void URLLoader::Close() { | 220 void URLLoader::Close() { |
157 NOTIMPLEMENTED(); // TODO(darin): Implement me. | 221 NOTIMPLEMENTED(); // TODO(darin): Implement me. |
158 } | 222 } |
159 | 223 |
160 // static | 224 void URLLoader::RunCallback(int32_t result) { |
161 const PPB_URLLoader* URLLoader::GetInterface() { | 225 if (!pending_callback_.func) |
162 return &ppb_urlloader; | 226 return; |
| 227 |
| 228 PP_CompletionCallback callback = {0}; |
| 229 std::swap(callback, pending_callback_); |
| 230 PP_RunCompletionCallback(&callback, result); |
| 231 } |
| 232 |
| 233 size_t URLLoader::FillUserBuffer() { |
| 234 DCHECK(user_buffer_); |
| 235 DCHECK(user_buffer_size_); |
| 236 |
| 237 size_t bytes_to_copy = std::min(buffer_.size(), user_buffer_size_); |
| 238 std::copy(buffer_.begin(), buffer_.begin() + bytes_to_copy, user_buffer_); |
| 239 buffer_.erase(buffer_.begin(), buffer_.begin() + bytes_to_copy); |
| 240 |
| 241 // Reset for next time. |
| 242 user_buffer_ = NULL; |
| 243 user_buffer_size_ = 0; |
| 244 return bytes_to_copy; |
| 245 } |
| 246 |
| 247 void URLLoader::willSendRequest(WebURLLoader* loader, |
| 248 WebURLRequest& new_request, |
| 249 const WebURLResponse& redirect_response) { |
| 250 NOTIMPLEMENTED(); // TODO(darin): Allow the plugin to inspect redirects. |
| 251 } |
| 252 |
| 253 void URLLoader::didSendData(WebURLLoader* loader, |
| 254 unsigned long long bytes_sent, |
| 255 unsigned long long total_bytes_to_be_sent) { |
| 256 // TODO(darin): Bounds check input? |
| 257 bytes_sent_ = static_cast<int64_t>(bytes_sent); |
| 258 total_bytes_to_be_sent_ = static_cast<int64_t>(total_bytes_to_be_sent); |
| 259 } |
| 260 |
| 261 void URLLoader::didReceiveResponse(WebURLLoader* loader, |
| 262 const WebURLResponse& response) { |
| 263 // TODO(darin): Initialize response_info_. |
| 264 RunCallback(PP_OK); |
| 265 } |
| 266 |
| 267 void URLLoader::didReceiveData(WebURLLoader* loader, |
| 268 const char* data, |
| 269 int data_length) { |
| 270 buffer_.insert(buffer_.end(), data, data + data_length); |
| 271 if (user_buffer_) { |
| 272 RunCallback(FillUserBuffer()); |
| 273 } else { |
| 274 DCHECK(!pending_callback_.func); |
| 275 } |
| 276 } |
| 277 |
| 278 void URLLoader::didFinishLoading(WebURLLoader* loader) { |
| 279 RunCallback(PP_OK); |
| 280 } |
| 281 |
| 282 void URLLoader::didFail(WebURLLoader* loader, const WebURLError& error) { |
| 283 // TODO(darin): Provide more detailed error information. |
| 284 RunCallback(PP_Error_Failed); |
163 } | 285 } |
164 | 286 |
165 } // namespace pepper | 287 } // namespace pepper |
OLD | NEW |