Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "extensions/browser/extension_function.h" | 5 #include "extensions/browser/extension_function.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 442 } | 442 } |
| 443 | 443 |
| 444 bool ExtensionFunction::HasOptionalArgument(size_t index) { | 444 bool ExtensionFunction::HasOptionalArgument(size_t index) { |
| 445 base::Value* value; | 445 base::Value* value; |
| 446 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); | 446 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); |
| 447 } | 447 } |
| 448 | 448 |
| 449 void ExtensionFunction::SendResponseImpl(bool success) { | 449 void ExtensionFunction::SendResponseImpl(bool success) { |
| 450 DCHECK(!response_callback_.is_null()); | 450 DCHECK(!response_callback_.is_null()); |
| 451 | 451 |
| 452 ResponseType type = success ? SUCCEEDED : FAILED; | 452 ResponseType response = success ? SUCCEEDED : FAILED; |
| 453 if (bad_message_) { | 453 if (bad_message_) { |
| 454 type = BAD_MESSAGE; | 454 response = BAD_MESSAGE; |
| 455 LOG(ERROR) << "Bad extension message " << name_; | 455 LOG(ERROR) << "Bad extension message " << name_; |
| 456 } | 456 } |
| 457 response_ = base::MakeUnique<ResponseType>(response); | |
|
lazyboy
2016/09/19 18:08:56
I'd name this response_type_ (and the getter). Bec
Devlin
2016/09/19 20:14:20
SG, done.
| |
| 457 | 458 |
| 458 // If results were never set, we send an empty argument list. | 459 // If results were never set, we send an empty argument list. |
| 459 if (!results_) | 460 if (!results_) |
| 460 results_.reset(new base::ListValue()); | 461 results_.reset(new base::ListValue()); |
| 461 | 462 |
| 462 response_callback_.Run(type, *results_, GetError(), histogram_value()); | 463 response_callback_.Run(response, *results_, GetError(), histogram_value()); |
| 463 LogUma(success, timer_.Elapsed(), histogram_value_); | 464 LogUma(success, timer_.Elapsed(), histogram_value_); |
| 464 } | 465 } |
| 465 | 466 |
| 466 void ExtensionFunction::OnRespondingLater(ResponseValue value) { | 467 void ExtensionFunction::OnRespondingLater(ResponseValue value) { |
| 467 SendResponse(value->Apply()); | 468 SendResponse(value->Apply()); |
| 468 } | 469 } |
| 469 | 470 |
| 470 UIThreadExtensionFunction::UIThreadExtensionFunction() | 471 UIThreadExtensionFunction::UIThreadExtensionFunction() |
| 471 : context_(nullptr), | 472 : context_(nullptr), |
| 472 render_frame_host_(nullptr), | 473 render_frame_host_(nullptr), |
| 473 is_from_service_worker_(false), | 474 is_from_service_worker_(false) {} |
| 474 delegate_(nullptr) {} | |
| 475 | 475 |
| 476 UIThreadExtensionFunction::~UIThreadExtensionFunction() { | 476 UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
| 477 if (dispatcher() && render_frame_host()) | 477 if (dispatcher() && render_frame_host()) |
| 478 dispatcher()->OnExtensionFunctionCompleted(extension()); | 478 dispatcher()->OnExtensionFunctionCompleted(extension()); |
| 479 // The extension function should always respond to avoid leaks in the | 479 // The extension function should always respond to avoid leaks in the |
| 480 // renderer, dangling callbacks, etc. The exception is if the system is | 480 // renderer, dangling callbacks, etc. The exception is if the system is |
| 481 // shutting down. | 481 // shutting down. |
| 482 // TODO(devlin): Duplicate this check in IOThreadExtensionFunction. It's | 482 // TODO(devlin): Duplicate this check in IOThreadExtensionFunction. It's |
| 483 // tricky because checking IsShuttingDown has to be called from the UI thread. | 483 // tricky because checking IsShuttingDown has to be called from the UI thread. |
| 484 extensions::ExtensionsBrowserClient* browser_client = | 484 extensions::ExtensionsBrowserClient* browser_client = |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 544 } | 544 } |
| 545 | 545 |
| 546 content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() { | 546 content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() { |
| 547 return render_frame_host_ ? | 547 return render_frame_host_ ? |
| 548 content::WebContents::FromRenderFrameHost(render_frame_host_) : nullptr; | 548 content::WebContents::FromRenderFrameHost(render_frame_host_) : nullptr; |
| 549 } | 549 } |
| 550 | 550 |
| 551 void UIThreadExtensionFunction::SendResponse(bool success) { | 551 void UIThreadExtensionFunction::SendResponse(bool success) { |
| 552 DCHECK(!did_respond_) << name_; | 552 DCHECK(!did_respond_) << name_; |
| 553 did_respond_ = true; | 553 did_respond_ = true; |
| 554 if (delegate_) | 554 SendResponseImpl(success); |
| 555 delegate_->OnSendResponse(this, success, bad_message_); | |
| 556 else | |
| 557 SendResponseImpl(success); | |
| 558 | 555 |
| 559 if (!transferred_blob_uuids_.empty()) { | 556 if (!transferred_blob_uuids_.empty()) { |
| 560 DCHECK(!delegate_) << "Blob transfer not supported with test delegate."; | |
| 561 render_frame_host_->Send( | 557 render_frame_host_->Send( |
| 562 new ExtensionMsg_TransferBlobs(transferred_blob_uuids_)); | 558 new ExtensionMsg_TransferBlobs(transferred_blob_uuids_)); |
| 563 } | 559 } |
| 564 } | 560 } |
| 565 | 561 |
| 566 void UIThreadExtensionFunction::SetTransferredBlobUUIDs( | 562 void UIThreadExtensionFunction::SetTransferredBlobUUIDs( |
| 567 const std::vector<std::string>& blob_uuids) { | 563 const std::vector<std::string>& blob_uuids) { |
| 568 DCHECK(transferred_blob_uuids_.empty()); // Should only be called once. | 564 DCHECK(transferred_blob_uuids_.empty()); // Should only be called once. |
| 569 transferred_blob_uuids_ = blob_uuids; | 565 transferred_blob_uuids_ = blob_uuids; |
| 570 } | 566 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 616 | 612 |
| 617 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { | 613 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { |
| 618 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); | 614 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); |
| 619 } | 615 } |
| 620 | 616 |
| 621 // static | 617 // static |
| 622 bool AsyncExtensionFunction::ValidationFailure( | 618 bool AsyncExtensionFunction::ValidationFailure( |
| 623 AsyncExtensionFunction* function) { | 619 AsyncExtensionFunction* function) { |
| 624 return false; | 620 return false; |
| 625 } | 621 } |
| OLD | NEW |