| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 "content/renderer/pepper/pepper_url_loader_host.h" | 5 #include "content/renderer/pepper/pepper_url_loader_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" |
| 9 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" | 10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" |
| 10 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" | 11 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" |
| 11 #include "content/renderer/pepper/url_request_info_util.h" | 12 #include "content/renderer/pepper/url_request_info_util.h" |
| 12 #include "content/renderer/pepper/url_response_info_util.h" | 13 #include "content/renderer/pepper/url_response_info_util.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/host/dispatch_host_message.h" | 16 #include "ppapi/host/dispatch_host_message.h" |
| 16 #include "ppapi/host/host_message_context.h" | 17 #include "ppapi/host/host_message_context.h" |
| 17 #include "ppapi/host/ppapi_host.h" | 18 #include "ppapi/host/ppapi_host.h" |
| 18 #include "ppapi/proxy/ppapi_messages.h" | 19 #include "ppapi/proxy/ppapi_messages.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 void PepperURLLoaderHost::didDownloadData(int data_length) { | 156 void PepperURLLoaderHost::didDownloadData(int data_length) { |
| 156 bytes_received_ += data_length; | 157 bytes_received_ += data_length; |
| 157 UpdateProgress(); | 158 UpdateProgress(); |
| 158 } | 159 } |
| 159 | 160 |
| 160 void PepperURLLoaderHost::didReceiveData(const char* data, int data_length) { | 161 void PepperURLLoaderHost::didReceiveData(const char* data, int data_length) { |
| 161 // Note that |loader| will be NULL for document loads. | 162 // Note that |loader| will be NULL for document loads. |
| 162 bytes_received_ += data_length; | 163 bytes_received_ += data_length; |
| 163 UpdateProgress(); | 164 UpdateProgress(); |
| 164 | 165 |
| 165 PpapiPluginMsg_URLLoader_SendData* message = | 166 auto message = base::MakeUnique<PpapiPluginMsg_URLLoader_SendData>(); |
| 166 new PpapiPluginMsg_URLLoader_SendData; | |
| 167 message->WriteData(data, data_length); | 167 message->WriteData(data, data_length); |
| 168 SendUpdateToPlugin(message); | 168 SendUpdateToPlugin(std::move(message)); |
| 169 } | 169 } |
| 170 | 170 |
| 171 void PepperURLLoaderHost::didFinishLoading(double finish_time) { | 171 void PepperURLLoaderHost::didFinishLoading(double finish_time) { |
| 172 // Note that |loader| will be NULL for document loads. | 172 // Note that |loader| will be NULL for document loads. |
| 173 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(PP_OK)); | 173 SendUpdateToPlugin( |
| 174 base::MakeUnique<PpapiPluginMsg_URLLoader_FinishedLoading>(PP_OK)); |
| 174 } | 175 } |
| 175 | 176 |
| 176 void PepperURLLoaderHost::didFail(const WebURLError& error) { | 177 void PepperURLLoaderHost::didFail(const WebURLError& error) { |
| 177 // Note that |loader| will be NULL for document loads. | 178 // Note that |loader| will be NULL for document loads. |
| 178 int32_t pp_error = PP_ERROR_FAILED; | 179 int32_t pp_error = PP_ERROR_FAILED; |
| 179 if (error.domain.equals(WebString::fromUTF8(net::kErrorDomain))) { | 180 if (error.domain.equals(WebString::fromUTF8(net::kErrorDomain))) { |
| 180 // TODO(bbudge): Extend pp_errors.h to cover interesting network errors | 181 // TODO(bbudge): Extend pp_errors.h to cover interesting network errors |
| 181 // from the net error domain. | 182 // from the net error domain. |
| 182 switch (error.reason) { | 183 switch (error.reason) { |
| 183 case net::ERR_ACCESS_DENIED: | 184 case net::ERR_ACCESS_DENIED: |
| 184 case net::ERR_NETWORK_ACCESS_DENIED: | 185 case net::ERR_NETWORK_ACCESS_DENIED: |
| 185 pp_error = PP_ERROR_NOACCESS; | 186 pp_error = PP_ERROR_NOACCESS; |
| 186 break; | 187 break; |
| 187 } | 188 } |
| 188 } else { | 189 } else { |
| 189 // It's a WebKit error. | 190 // It's a WebKit error. |
| 190 pp_error = PP_ERROR_NOACCESS; | 191 pp_error = PP_ERROR_NOACCESS; |
| 191 } | 192 } |
| 192 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(pp_error)); | 193 SendUpdateToPlugin( |
| 194 base::MakeUnique<PpapiPluginMsg_URLLoader_FinishedLoading>(pp_error)); |
| 193 } | 195 } |
| 194 | 196 |
| 195 void PepperURLLoaderHost::DidConnectPendingHostToResource() { | 197 void PepperURLLoaderHost::DidConnectPendingHostToResource() { |
| 196 for (size_t i = 0; i < pending_replies_.size(); i++) | 198 for (const auto& reply : pending_replies_) |
| 197 host()->SendUnsolicitedReply(pp_resource(), *pending_replies_[i]); | 199 host()->SendUnsolicitedReply(pp_resource(), *reply); |
| 198 pending_replies_.clear(); | 200 pending_replies_.clear(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 int32_t PepperURLLoaderHost::OnHostMsgOpen( | 203 int32_t PepperURLLoaderHost::OnHostMsgOpen( |
| 202 ppapi::host::HostMessageContext* context, | 204 ppapi::host::HostMessageContext* context, |
| 203 const ppapi::URLRequestInfoData& request_data) { | 205 const ppapi::URLRequestInfoData& request_data) { |
| 204 // An "Open" isn't a resource Call so has no reply, but failure to open | 206 // An "Open" isn't a resource Call so has no reply, but failure to open |
| 205 // implies a load failure. To make it harder to forget to send the load | 207 // implies a load failure. To make it harder to forget to send the load |
| 206 // failed reply from the open handler, we instead catch errors and convert | 208 // failed reply from the open handler, we instead catch errors and convert |
| 207 // them to load failed messages. | 209 // them to load failed messages. |
| 208 int32_t ret = InternalOnHostMsgOpen(context, request_data); | 210 int32_t ret = InternalOnHostMsgOpen(context, request_data); |
| 209 DCHECK(ret != PP_OK_COMPLETIONPENDING); | 211 DCHECK(ret != PP_OK_COMPLETIONPENDING); |
| 210 | 212 |
| 211 if (ret != PP_OK) | 213 if (ret != PP_OK) |
| 212 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_FinishedLoading(ret)); | 214 SendUpdateToPlugin( |
| 215 base::MakeUnique<PpapiPluginMsg_URLLoader_FinishedLoading>(ret)); |
| 213 return PP_OK; | 216 return PP_OK; |
| 214 } | 217 } |
| 215 | 218 |
| 216 // Since this is wrapped by OnHostMsgOpen, we can return errors here and they | 219 // Since this is wrapped by OnHostMsgOpen, we can return errors here and they |
| 217 // will be translated into a FinishedLoading call automatically. | 220 // will be translated into a FinishedLoading call automatically. |
| 218 int32_t PepperURLLoaderHost::InternalOnHostMsgOpen( | 221 int32_t PepperURLLoaderHost::InternalOnHostMsgOpen( |
| 219 ppapi::host::HostMessageContext* context, | 222 ppapi::host::HostMessageContext* context, |
| 220 const ppapi::URLRequestInfoData& request_data) { | 223 const ppapi::URLRequestInfoData& request_data) { |
| 221 // Main document loads are already open, so don't allow people to open them | 224 // Main document loads are already open, so don't allow people to open them |
| 222 // again. | 225 // again. |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 313 |
| 311 int32_t PepperURLLoaderHost::OnHostMsgGrantUniversalAccess( | 314 int32_t PepperURLLoaderHost::OnHostMsgGrantUniversalAccess( |
| 312 ppapi::host::HostMessageContext* context) { | 315 ppapi::host::HostMessageContext* context) { |
| 313 // Only plugins with private permission can bypass same origin. | 316 // Only plugins with private permission can bypass same origin. |
| 314 if (!host()->permissions().HasPermission(ppapi::PERMISSION_PRIVATE)) | 317 if (!host()->permissions().HasPermission(ppapi::PERMISSION_PRIVATE)) |
| 315 return PP_ERROR_FAILED; | 318 return PP_ERROR_FAILED; |
| 316 has_universal_access_ = true; | 319 has_universal_access_ = true; |
| 317 return PP_OK; | 320 return PP_OK; |
| 318 } | 321 } |
| 319 | 322 |
| 320 void PepperURLLoaderHost::SendUpdateToPlugin(IPC::Message* message) { | 323 void PepperURLLoaderHost::SendUpdateToPlugin( |
| 324 std::unique_ptr<IPC::Message> message) { |
| 321 // We must send messages to the plugin in the order that the responses are | 325 // We must send messages to the plugin in the order that the responses are |
| 322 // received from webkit, even when the host isn't ready to send messages or | 326 // received from webkit, even when the host isn't ready to send messages or |
| 323 // when the host performs an asynchronous operation. | 327 // when the host performs an asynchronous operation. |
| 324 // | 328 // |
| 325 // Only {FinishedLoading, ReceivedResponse, SendData} have ordering | 329 // Only {FinishedLoading, ReceivedResponse, SendData} have ordering |
| 326 // contraints; all other messages are immediately added to pending_replies_. | 330 // contraints; all other messages are immediately added to pending_replies_. |
| 327 // | 331 // |
| 328 // Accepted orderings for {FinishedLoading, ReceivedResponse, SendData} are: | 332 // Accepted orderings for {FinishedLoading, ReceivedResponse, SendData} are: |
| 329 // - {ReceivedResponse, SendData (zero or more times), FinishedLoading} | 333 // - {ReceivedResponse, SendData (zero or more times), FinishedLoading} |
| 330 // - {FinishedLoading (when status != PP_OK)} | 334 // - {FinishedLoading (when status != PP_OK)} |
| 331 if (message->type() == PpapiPluginMsg_URLLoader_SendData::ID || | 335 if (message->type() == PpapiPluginMsg_URLLoader_SendData::ID || |
| 332 message->type() == PpapiPluginMsg_URLLoader_FinishedLoading::ID) { | 336 message->type() == PpapiPluginMsg_URLLoader_FinishedLoading::ID) { |
| 333 // Messages that must be sent after ReceivedResponse. | 337 // Messages that must be sent after ReceivedResponse. |
| 334 if (pending_response_) { | 338 if (pending_response_) { |
| 335 out_of_order_replies_.push_back(message); | 339 out_of_order_replies_.push_back(std::move(message)); |
| 336 } else { | 340 } else { |
| 337 SendOrderedUpdateToPlugin(message); | 341 SendOrderedUpdateToPlugin(std::move(message)); |
| 338 } | 342 } |
| 339 } else if (message->type() == PpapiPluginMsg_URLLoader_ReceivedResponse::ID) { | 343 } else if (message->type() == PpapiPluginMsg_URLLoader_ReceivedResponse::ID) { |
| 340 // Allow SendData and FinishedLoading into the ordered queue. | 344 // Allow SendData and FinishedLoading into the ordered queue. |
| 341 DCHECK(pending_response_); | 345 DCHECK(pending_response_); |
| 342 SendOrderedUpdateToPlugin(message); | 346 SendOrderedUpdateToPlugin(std::move(message)); |
| 343 for (size_t i = 0; i < out_of_order_replies_.size(); i++) | 347 for (auto& reply : out_of_order_replies_) |
| 344 SendOrderedUpdateToPlugin(out_of_order_replies_[i]); | 348 SendOrderedUpdateToPlugin(std::move(reply)); |
| 345 // SendOrderedUpdateToPlugin destroys the messages for us. | 349 out_of_order_replies_.clear(); |
| 346 out_of_order_replies_.weak_clear(); | |
| 347 pending_response_ = false; | 350 pending_response_ = false; |
| 348 } else { | 351 } else { |
| 349 // Messages without ordering constraints. | 352 // Messages without ordering constraints. |
| 350 SendOrderedUpdateToPlugin(message); | 353 SendOrderedUpdateToPlugin(std::move(message)); |
| 351 } | 354 } |
| 352 } | 355 } |
| 353 | 356 |
| 354 void PepperURLLoaderHost::SendOrderedUpdateToPlugin(IPC::Message* message) { | 357 void PepperURLLoaderHost::SendOrderedUpdateToPlugin( |
| 358 std::unique_ptr<IPC::Message> message) { |
| 355 if (pp_resource() == 0) { | 359 if (pp_resource() == 0) { |
| 356 pending_replies_.push_back(message); | 360 pending_replies_.push_back(std::move(message)); |
| 357 } else { | 361 } else { |
| 358 host()->SendUnsolicitedReply(pp_resource(), *message); | 362 host()->SendUnsolicitedReply(pp_resource(), *message); |
| 359 delete message; | |
| 360 } | 363 } |
| 361 } | 364 } |
| 362 | 365 |
| 363 void PepperURLLoaderHost::Close() { | 366 void PepperURLLoaderHost::Close() { |
| 364 if (loader_.get()) { | 367 if (loader_.get()) { |
| 365 loader_->cancel(); | 368 loader_->cancel(); |
| 366 } else if (main_document_loader_) { | 369 } else if (main_document_loader_) { |
| 367 // TODO(raymes): Calling WebLocalFrame::stopLoading here is incorrect as it | 370 // TODO(raymes): Calling WebLocalFrame::stopLoading here is incorrect as it |
| 368 // cancels all URL loaders associated with the frame. If a client has opened | 371 // cancels all URL loaders associated with the frame. If a client has opened |
| 369 // other URLLoaders and then closes the main one, the others should still | 372 // other URLLoaders and then closes the main one, the others should still |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 renderer_ppapi_host_, | 412 renderer_ppapi_host_, |
| 410 pp_instance(), | 413 pp_instance(), |
| 411 response, | 414 response, |
| 412 base::Bind(&PepperURLLoaderHost::DidDataFromWebURLResponse, | 415 base::Bind(&PepperURLLoaderHost::DidDataFromWebURLResponse, |
| 413 weak_factory_.GetWeakPtr())); | 416 weak_factory_.GetWeakPtr())); |
| 414 } | 417 } |
| 415 } | 418 } |
| 416 | 419 |
| 417 void PepperURLLoaderHost::DidDataFromWebURLResponse( | 420 void PepperURLLoaderHost::DidDataFromWebURLResponse( |
| 418 const ppapi::URLResponseInfoData& data) { | 421 const ppapi::URLResponseInfoData& data) { |
| 419 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_ReceivedResponse(data)); | 422 SendUpdateToPlugin( |
| 423 base::MakeUnique<PpapiPluginMsg_URLLoader_ReceivedResponse>(data)); |
| 420 } | 424 } |
| 421 | 425 |
| 422 void PepperURLLoaderHost::UpdateProgress() { | 426 void PepperURLLoaderHost::UpdateProgress() { |
| 423 bool record_download = request_data_.record_download_progress; | 427 bool record_download = request_data_.record_download_progress; |
| 424 bool record_upload = request_data_.record_upload_progress; | 428 bool record_upload = request_data_.record_upload_progress; |
| 425 if (record_download || record_upload) { | 429 if (record_download || record_upload) { |
| 426 // Here we go through some effort to only send the exact information that | 430 // Here we go through some effort to only send the exact information that |
| 427 // the requestor wanted in the request flags. It would be just as | 431 // the requestor wanted in the request flags. It would be just as |
| 428 // efficient to send all of it, but we don't want people to rely on | 432 // efficient to send all of it, but we don't want people to rely on |
| 429 // getting download progress when they happen to set the upload progress | 433 // getting download progress when they happen to set the upload progress |
| 430 // flag. | 434 // flag. |
| 431 ppapi::proxy::ResourceMessageReplyParams params; | 435 ppapi::proxy::ResourceMessageReplyParams params; |
| 432 SendUpdateToPlugin(new PpapiPluginMsg_URLLoader_UpdateProgress( | 436 SendUpdateToPlugin( |
| 433 record_upload ? bytes_sent_ : -1, | 437 base::MakeUnique<PpapiPluginMsg_URLLoader_UpdateProgress>( |
| 434 record_upload ? total_bytes_to_be_sent_ : -1, | 438 record_upload ? bytes_sent_ : -1, |
| 435 record_download ? bytes_received_ : -1, | 439 record_upload ? total_bytes_to_be_sent_ : -1, |
| 436 record_download ? total_bytes_to_be_received_ : -1)); | 440 record_download ? bytes_received_ : -1, |
| 441 record_download ? total_bytes_to_be_received_ : -1)); |
| 437 } | 442 } |
| 438 } | 443 } |
| 439 | 444 |
| 440 } // namespace content | 445 } // namespace content |
| OLD | NEW |