OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 "content/renderer/pepper/pepper_url_loader_host.h" |
| 6 |
| 7 #include "content/public/renderer/renderer_ppapi_host.h" |
| 8 #include "net/base/net_errors.h" |
| 9 #include "ppapi/c/pp_errors.h" |
| 10 #include "ppapi/host/dispatch_host_message.h" |
| 11 #include "ppapi/host/host_message_context.h" |
| 12 #include "ppapi/host/ppapi_host.h" |
| 13 #include "ppapi/proxy/ppapi_messages.h" |
| 14 #include "ppapi/shared_impl/ppapi_globals.h" |
| 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLError.h" |
| 16 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLLoader.h" |
| 17 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h" |
| 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLLoaderOptions.h
" |
| 26 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
| 27 #include "webkit/plugins/ppapi/url_request_info_util.h" |
| 28 #include "webkit/plugins/ppapi/url_response_info_util.h" |
| 29 |
| 30 using WebKit::WebFrame; |
| 31 using WebKit::WebString; |
| 32 using WebKit::WebURL; |
| 33 using WebKit::WebURLError; |
| 34 using WebKit::WebURLLoader; |
| 35 using WebKit::WebURLLoaderOptions; |
| 36 using WebKit::WebURLRequest; |
| 37 using WebKit::WebURLResponse; |
| 38 |
| 39 #ifdef _MSC_VER |
| 40 // Do not warn about use of std::copy with raw pointers. |
| 41 #pragma warning(disable : 4996) |
| 42 #endif |
| 43 |
| 44 namespace content { |
| 45 |
| 46 PepperURLLoaderHost::PepperURLLoaderHost(RendererPpapiHost* host, |
| 47 bool main_document_loader, |
| 48 PP_Instance instance, |
| 49 PP_Resource resource) |
| 50 : ResourceHost(host->GetPpapiHost(), instance, resource), |
| 51 renderer_ppapi_host_(host), |
| 52 main_document_loader_(main_document_loader), |
| 53 has_universal_access_(false), |
| 54 bytes_sent_(0), |
| 55 total_bytes_to_be_sent_(-1), |
| 56 bytes_received_(0), |
| 57 total_bytes_to_be_received_(-1) { |
| 58 DCHECK((main_document_loader && !resource) || |
| 59 (!main_document_loader && resource)); |
| 60 } |
| 61 |
| 62 PepperURLLoaderHost::~PepperURLLoaderHost() { |
| 63 // Normally deleting this object will delete the loader which will implicitly |
| 64 // cancel the load. But this won't happen for the main document loader. So it |
| 65 // would be nice to issue a Close() here. |
| 66 // |
| 67 // However, the PDF plugin will cancel the document load and then close the |
| 68 // resource (which is reasonable). It then makes a second request to load the |
| 69 // document so it can set the "want progress" flags (which is unreasonable -- |
| 70 // we should probably provide download progress on document loads). |
| 71 // |
| 72 // But a Close() on the main document (even if the request is already |
| 73 // canceled) will cancel all pending subresources, of which the second |
| 74 // request is one, and the load will fail. Even if we fixed the PDF reader to |
| 75 // change the timing or to send progress events to avoid the second request, |
| 76 // we don't want to cancel other loads when the main one is closed. |
| 77 // |
| 78 // "Leaking" the main document load here by not closing it will only affect |
| 79 // plugins handling main document loads (which are very few, mostly only PDF) |
| 80 // that dereference without explicitly closing the main document load (which |
| 81 // PDF doesn't do -- it explicitly closes it before issuing the second |
| 82 // request). And the worst thing that will happen is that any remaining data |
| 83 // will get queued inside WebKit. |
| 84 if (main_document_loader_) { |
| 85 // The PluginInstance has a non-owning pointer to us. |
| 86 webkit::ppapi::PluginInstance* instance_object = |
| 87 renderer_ppapi_host_->GetPluginInstance(pp_instance()); |
| 88 if (instance_object) { |
| 89 DCHECK(instance_object->document_loader() == this); |
| 90 instance_object->set_document_loader(NULL); |
| 91 } |
| 92 } |
| 93 |
| 94 // There is a path whereby the destructor for the loader_ member can |
| 95 // invoke InstanceWasDeleted() upon this URLLoaderResource, thereby |
| 96 // re-entering the scoped_ptr destructor with the same scoped_ptr object |
| 97 // via loader_.reset(). Be sure that loader_ is first NULL then destroy |
| 98 // the scoped_ptr. See http://crbug.com/159429. |
| 99 scoped_ptr<WebKit::WebURLLoader> for_destruction_only(loader_.release()); |
| 100 |
| 101 for (size_t i = 0; i < pending_replies_.size(); i++) |
| 102 delete pending_replies_[i]; |
| 103 } |
| 104 |
| 105 int32_t PepperURLLoaderHost::OnResourceMessageReceived( |
| 106 const IPC::Message& msg, |
| 107 ppapi::host::HostMessageContext* context) { |
| 108 IPC_BEGIN_MESSAGE_MAP(PepperURLLoaderHost, msg) |
| 109 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 110 PpapiHostMsg_URLLoader_Open, |
| 111 OnHostMsgOpen) |
| 112 PPAPI_DISPATCH_HOST_RESOURCE_CALL( |
| 113 PpapiHostMsg_URLLoader_SetDeferLoading, |
| 114 OnHostMsgSetDeferLoading) |
| 115 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 116 PpapiHostMsg_URLLoader_Close, |
| 117 OnHostMsgClose); |
| 118 PPAPI_DISPATCH_HOST_RESOURCE_CALL_0( |
| 119 PpapiHostMsg_URLLoader_GrantUniversalAccess, |
| 120 OnHostMsgGrantUniversalAccess) |
| 121 IPC_END_MESSAGE_MAP() |
| 122 return PP_ERROR_FAILED; |
| 123 } |
| 124 |
| 125 void PepperURLLoaderHost::willSendRequest( |
| 126 WebURLLoader* loader, |
| 127 WebURLRequest& new_request, |
| 128 const WebURLResponse& redirect_response) { |
| 129 if (!request_data_.follow_redirects) { |
| 130 SaveResponse(redirect_response); |
| 131 SetDefersLoading(true); |
| 132 } |
| 133 } |
| 134 |
| 135 void PepperURLLoaderHost::didSendData( |
| 136 WebURLLoader* loader, |
| 137 unsigned long long bytes_sent, |
| 138 unsigned long long total_bytes_to_be_sent) { |
| 139 // TODO(darin): Bounds check input? |
| 140 bytes_sent_ = static_cast<int64_t>(bytes_sent); |
| 141 total_bytes_to_be_sent_ = static_cast<int64_t>(total_bytes_to_be_sent); |
| 142 UpdateProgress(); |
| 143 } |
| 144 |
| 145 void PepperURLLoaderHost::didReceiveResponse(WebURLLoader* loader, |
| 146 const WebURLResponse& response) { |
| 147 // Sets -1 if the content length is unknown. Send before issuing callback. |
| 148 total_bytes_to_be_received_ = response.expectedContentLength(); |
| 149 UpdateProgress(); |
| 150 |
| 151 SaveResponse(response); |
| 152 } |
| 153 |
| 154 void PepperURLLoaderHost::didDownloadData(WebURLLoader* loader, |
| 155 int data_length) { |
| 156 bytes_received_ += data_length; |
| 157 UpdateProgress(); |
| 158 } |
| 159 |
| 160 void PepperURLLoaderHost::didReceiveData(WebURLLoader* loader, |
| 161 const char* data, |
| 162 int data_length, |
| 163 int encoded_data_length) { |
| 164 // Note that |loader| will be NULL for document loads. |
| 165 bytes_received_ += data_length; |
| 166 UpdateProgress(); |
| 167 |
| 168 PpapiPluginMsg_URLLoader_SendData* message = |
| 169 new PpapiPluginMsg_URLLoader_SendData; |
| 170 message->WriteData(data, data_length); |
| 171 SendUpdateToPlugin(message); |
| 172 } |
| 173 |
| 174 void PepperURLLoaderHost::didFinishLoading(WebURLLoader* loader, |
| 175 double finish_time) { |
| 176 // Note that |loader| will be NULL for document loads. |
| 177 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(PP_OK)); |
| 178 } |
| 179 |
| 180 void PepperURLLoaderHost::didFail(WebURLLoader* loader, |
| 181 const WebURLError& error) { |
| 182 // Note that |loader| will be NULL for document loads. |
| 183 int32_t pp_error = PP_ERROR_FAILED; |
| 184 if (error.domain.equals(WebString::fromUTF8(net::kErrorDomain))) { |
| 185 // TODO(bbudge): Extend pp_errors.h to cover interesting network errors |
| 186 // from the net error domain. |
| 187 switch (error.reason) { |
| 188 case net::ERR_ACCESS_DENIED: |
| 189 case net::ERR_NETWORK_ACCESS_DENIED: |
| 190 pp_error = PP_ERROR_NOACCESS; |
| 191 break; |
| 192 } |
| 193 } else { |
| 194 // It's a WebKit error. |
| 195 pp_error = PP_ERROR_NOACCESS; |
| 196 } |
| 197 |
| 198 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(pp_error)); |
| 199 } |
| 200 |
| 201 void PepperURLLoaderHost::DidConnectPendingHostToResource() { |
| 202 for (size_t i = 0; i < pending_replies_.size(); i++) { |
| 203 host()->SendUnsolicitedReply(pp_resource(), *pending_replies_[i]); |
| 204 delete pending_replies_[i]; |
| 205 } |
| 206 pending_replies_.clear(); |
| 207 } |
| 208 |
| 209 int32_t PepperURLLoaderHost::OnHostMsgOpen( |
| 210 ppapi::host::HostMessageContext* context, |
| 211 const ppapi::URLRequestInfoData& request_data) { |
| 212 // An "Open" isn't a resource Call so has no reply, but failure to open |
| 213 // implies a load failure. To make it harder to forget to send the load |
| 214 // failed reply from the open handler, we instead catch errors and convert |
| 215 // them to load failed messages. |
| 216 int32_t ret = InternalOnHostMsgOpen(context, request_data); |
| 217 DCHECK(ret != PP_OK_COMPLETIONPENDING); |
| 218 |
| 219 if (ret != PP_OK) |
| 220 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(ret)); |
| 221 return PP_OK; |
| 222 } |
| 223 |
| 224 // Since this is wrapped by OnHostMsgOpen, we can return errors here and they |
| 225 // will be translated into a FinishedLoading call automatically. |
| 226 int32_t PepperURLLoaderHost::InternalOnHostMsgOpen( |
| 227 ppapi::host::HostMessageContext* context, |
| 228 const ppapi::URLRequestInfoData& request_data) { |
| 229 // Main document loads are already open, so don't allow people to open them |
| 230 // again. |
| 231 if (main_document_loader_) |
| 232 return PP_ERROR_INPROGRESS; |
| 233 |
| 234 // Create a copy of the request data since CreateWebURLRequest will populate |
| 235 // the file refs. |
| 236 ppapi::URLRequestInfoData filled_in_request_data = request_data; |
| 237 |
| 238 if (webkit::ppapi::URLRequestRequiresUniversalAccess( |
| 239 filled_in_request_data) && |
| 240 !has_universal_access_) { |
| 241 ppapi::PpapiGlobals::Get()->LogWithSource( |
| 242 pp_instance(), PP_LOGLEVEL_ERROR, std::string(), |
| 243 "PPB_URLLoader.Open: The URL you're requesting is " |
| 244 " on a different security origin than your plugin. To request " |
| 245 " cross-origin resources, see " |
| 246 " PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS."); |
| 247 return PP_ERROR_NOACCESS; |
| 248 } |
| 249 |
| 250 if (loader_.get()) |
| 251 return PP_ERROR_INPROGRESS; |
| 252 |
| 253 WebFrame* frame = GetFrame(); |
| 254 if (!frame) |
| 255 return PP_ERROR_FAILED; |
| 256 WebURLRequest web_request; |
| 257 if (!webkit::ppapi::CreateWebURLRequest(&filled_in_request_data, frame, |
| 258 &web_request)) |
| 259 return PP_ERROR_FAILED; |
| 260 web_request.setRequestorProcessID(renderer_ppapi_host_->GetPluginPID()); |
| 261 |
| 262 WebURLLoaderOptions options; |
| 263 if (has_universal_access_) { |
| 264 options.allowCredentials = true; |
| 265 options.crossOriginRequestPolicy = |
| 266 WebURLLoaderOptions::CrossOriginRequestPolicyAllow; |
| 267 } else { |
| 268 // All other HTTP requests are untrusted. |
| 269 options.untrustedHTTP = true; |
| 270 if (filled_in_request_data.allow_cross_origin_requests) { |
| 271 // Allow cross-origin requests with access control. The request specifies |
| 272 // if credentials are to be sent. |
| 273 options.allowCredentials = filled_in_request_data.allow_credentials; |
| 274 options.crossOriginRequestPolicy = |
| 275 WebURLLoaderOptions::CrossOriginRequestPolicyUseAccessControl; |
| 276 } else { |
| 277 // Same-origin requests can always send credentials. |
| 278 options.allowCredentials = true; |
| 279 } |
| 280 } |
| 281 |
| 282 loader_.reset(frame->createAssociatedURLLoader(options)); |
| 283 if (!loader_.get()) |
| 284 return PP_ERROR_FAILED; |
| 285 |
| 286 // Don't actually save the request until we know we're going to load. |
| 287 request_data_ = filled_in_request_data; |
| 288 loader_->loadAsynchronously(web_request, this); |
| 289 |
| 290 // Although the request is technically pending, this is not a "Call" message |
| 291 // so we don't return COMPLETIONPENDING. |
| 292 return PP_OK; |
| 293 } |
| 294 |
| 295 int32_t PepperURLLoaderHost::OnHostMsgSetDeferLoading( |
| 296 ppapi::host::HostMessageContext* context, |
| 297 bool defers_loading) { |
| 298 SetDefersLoading(defers_loading); |
| 299 return PP_OK; |
| 300 } |
| 301 |
| 302 int32_t PepperURLLoaderHost::OnHostMsgClose( |
| 303 ppapi::host::HostMessageContext* context) { |
| 304 Close(); |
| 305 return PP_OK; |
| 306 } |
| 307 |
| 308 int32_t PepperURLLoaderHost::OnHostMsgGrantUniversalAccess( |
| 309 ppapi::host::HostMessageContext* context) { |
| 310 // Only plugins with private permission can bypass same origin. |
| 311 if (!host()->permissions().HasPermission(ppapi::PERMISSION_PRIVATE)) |
| 312 return PP_ERROR_FAILED; |
| 313 has_universal_access_ = true; |
| 314 return PP_OK; |
| 315 } |
| 316 |
| 317 void PepperURLLoaderHost::SendUpdateToPlugin(IPC::Message* msg) { |
| 318 if (pp_resource()) { |
| 319 host()->SendUnsolicitedReply(pp_resource(), *msg); |
| 320 delete msg; |
| 321 } else { |
| 322 pending_replies_.push_back(msg); |
| 323 } |
| 324 } |
| 325 |
| 326 void PepperURLLoaderHost::Close() { |
| 327 if (loader_.get()) |
| 328 loader_->cancel(); |
| 329 else if (main_document_loader_) |
| 330 GetFrame()->stopLoading(); |
| 331 } |
| 332 |
| 333 WebKit::WebFrame* PepperURLLoaderHost::GetFrame() { |
| 334 webkit::ppapi::PluginInstance* instance_object = |
| 335 renderer_ppapi_host_->GetPluginInstance(pp_instance()); |
| 336 if (!instance_object) |
| 337 return NULL; |
| 338 return instance_object->container()->element().document().frame(); |
| 339 } |
| 340 |
| 341 void PepperURLLoaderHost::SetDefersLoading(bool defers_loading) { |
| 342 if (loader_.get()) |
| 343 loader_->setDefersLoading(defers_loading); |
| 344 |
| 345 // TODO(brettw) bug 96770: We need a way to set the defers loading flag on |
| 346 // main document loads (when the loader_ is null). |
| 347 } |
| 348 |
| 349 void PepperURLLoaderHost::SaveResponse(const WebURLResponse& response) { |
| 350 if (!main_document_loader_) { |
| 351 // When we're the main document loader, we send the response data up front, |
| 352 // so we don't want to trigger any callbacks in the plugin which aren't |
| 353 // expected. We should not be getting redirects so the response sent |
| 354 // up-front should be valid (plugin document loads happen after all |
| 355 // redirects are processed since WebKit has to know the MIME type). |
| 356 SendUpdateToPlugin( |
| 357 new PpapiPluginMsg_URLLoader_ReceivedResponse( |
| 358 webkit::ppapi::DataFromWebURLResponse(pp_instance(), response))); |
| 359 } |
| 360 } |
| 361 |
| 362 void PepperURLLoaderHost::UpdateProgress() { |
| 363 bool record_download = request_data_.record_download_progress; |
| 364 bool record_upload = request_data_.record_upload_progress; |
| 365 if (record_download || record_upload) { |
| 366 // Here we go through some effort to only send the exact information that |
| 367 // the requestor wanted in the request flags. It would be just as |
| 368 // efficient to send all of it, but we don't want people to rely on |
| 369 // getting download progress when they happen to set the upload progress |
| 370 // flag. |
| 371 ppapi::proxy::ResourceMessageReplyParams params; |
| 372 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_UpdateProgress( |
| 373 record_upload ? bytes_sent_ : -1, |
| 374 record_upload ? total_bytes_to_be_sent_ : -1, |
| 375 record_download ? bytes_received_ : -1, |
| 376 record_download ? total_bytes_to_be_received_ : -1)); |
| 377 } |
| 378 } |
| 379 |
| 380 } // namespace content |
OLD | NEW |