Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(292)

Side by Side Diff: extensions/browser/extension_function.cc

Issue 2351823004: [Extensions] Consolidate ExtensionFunction::SendResponse()s (Closed)
Patch Set: ready Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 294
295 bool ExtensionFunction::HasPermission() { 295 bool ExtensionFunction::HasPermission() {
296 Feature::Availability availability = 296 Feature::Availability availability =
297 ExtensionAPI::GetSharedInstance()->IsAvailable( 297 ExtensionAPI::GetSharedInstance()->IsAvailable(
298 name_, extension_.get(), source_context_type_, source_url()); 298 name_, extension_.get(), source_context_type_, source_url());
299 return availability.is_available(); 299 return availability.is_available();
300 } 300 }
301 301
302 void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) { 302 void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) {
303 error_ = violation_error; 303 error_ = violation_error;
304 SendResponse(false); 304 SendResponseImpl(false);
305 } 305 }
306 306
307 void ExtensionFunction::SetArgs(const base::ListValue* args) { 307 void ExtensionFunction::SetArgs(const base::ListValue* args) {
308 DCHECK(!args_.get()); // Should only be called once. 308 DCHECK(!args_.get()); // Should only be called once.
309 args_ = args->CreateDeepCopy(); 309 args_ = args->CreateDeepCopy();
310 } 310 }
311 311
312 void ExtensionFunction::SetResult(std::unique_ptr<base::Value> result) { 312 void ExtensionFunction::SetResult(std::unique_ptr<base::Value> result) {
313 results_.reset(new base::ListValue()); 313 results_.reset(new base::ListValue());
314 results_->Append(std::move(result)); 314 results_->Append(std::move(result));
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 name(), "ErrorWithArguments", this, std::move(args), error)); 400 name(), "ErrorWithArguments", this, std::move(args), error));
401 } 401 }
402 402
403 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { 403 ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() {
404 return ResponseValue(new BadMessageResponseValue(this)); 404 return ResponseValue(new BadMessageResponseValue(this));
405 } 405 }
406 406
407 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( 407 ExtensionFunction::ResponseAction ExtensionFunction::RespondNow(
408 ResponseValue result) { 408 ResponseValue result) {
409 return ResponseAction(new RespondNowAction( 409 return ResponseAction(new RespondNowAction(
410 std::move(result), base::Bind(&ExtensionFunction::SendResponse, this))); 410 std::move(result),
411 base::Bind(&ExtensionFunction::SendResponseImpl, this)));
411 } 412 }
412 413
413 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { 414 ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() {
414 return ResponseAction(new RespondLaterAction()); 415 return ResponseAction(new RespondLaterAction());
415 } 416 }
416 417
417 // static 418 // static
418 ExtensionFunction::ResponseAction ExtensionFunction::ValidationFailure( 419 ExtensionFunction::ResponseAction ExtensionFunction::ValidationFailure(
419 ExtensionFunction* function) { 420 ExtensionFunction* function) {
420 return function->RespondNow(function->BadMessage()); 421 return function->RespondNow(function->BadMessage());
421 } 422 }
422 423
423 void ExtensionFunction::Respond(ResponseValue result) { 424 void ExtensionFunction::Respond(ResponseValue result) {
424 SendResponse(result->Apply()); 425 SendResponseImpl(result->Apply());
425 } 426 }
426 427
427 bool ExtensionFunction::PreRunValidation(std::string* error) { 428 bool ExtensionFunction::PreRunValidation(std::string* error) {
428 return true; 429 return true;
429 } 430 }
430 431
431 ExtensionFunction::ResponseAction ExtensionFunction::RunWithValidation() { 432 ExtensionFunction::ResponseAction ExtensionFunction::RunWithValidation() {
432 std::string error; 433 std::string error;
433 if (!PreRunValidation(&error)) { 434 if (!PreRunValidation(&error)) {
434 DCHECK(!error.empty() || bad_message_); 435 DCHECK(!error.empty() || bad_message_);
435 return bad_message_ ? ValidationFailure(this) : RespondNow(Error(error)); 436 return bad_message_ ? ValidationFailure(this) : RespondNow(Error(error));
436 } 437 }
437 return Run(); 438 return Run();
438 } 439 }
439 440
440 bool ExtensionFunction::ShouldSkipQuotaLimiting() const { 441 bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
441 return false; 442 return false;
442 } 443 }
443 444
444 bool ExtensionFunction::HasOptionalArgument(size_t index) { 445 bool ExtensionFunction::HasOptionalArgument(size_t index) {
445 base::Value* value; 446 base::Value* value;
446 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); 447 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL);
447 } 448 }
448 449
449 void ExtensionFunction::SendResponseImpl(bool success) { 450 void ExtensionFunction::SendResponseImpl(bool success) {
450 DCHECK(!response_callback_.is_null()); 451 DCHECK(!response_callback_.is_null());
452 DCHECK(!did_respond_) << name_;
453 did_respond_ = true;
451 454
452 ResponseType response = success ? SUCCEEDED : FAILED; 455 ResponseType response = success ? SUCCEEDED : FAILED;
453 if (bad_message_) { 456 if (bad_message_) {
454 response = BAD_MESSAGE; 457 response = BAD_MESSAGE;
455 LOG(ERROR) << "Bad extension message " << name_; 458 LOG(ERROR) << "Bad extension message " << name_;
456 } 459 }
457 response_type_ = base::MakeUnique<ResponseType>(response); 460 response_type_ = base::MakeUnique<ResponseType>(response);
458 461
459 // If results were never set, we send an empty argument list. 462 // If results were never set, we send an empty argument list.
460 if (!results_) 463 if (!results_)
461 results_.reset(new base::ListValue()); 464 results_.reset(new base::ListValue());
462 465
463 response_callback_.Run(response, *results_, GetError(), histogram_value()); 466 response_callback_.Run(response, *results_, GetError(), histogram_value());
464 LogUma(success, timer_.Elapsed(), histogram_value_); 467 LogUma(success, timer_.Elapsed(), histogram_value_);
468
469 OnResponded();
465 } 470 }
466 471
467 void ExtensionFunction::OnRespondingLater(ResponseValue value) { 472 void ExtensionFunction::OnRespondingLater(ResponseValue value) {
lazyboy 2016/09/20 20:21:33 Is OnRespondingLater used still? (I can't find any
Devlin 2016/09/20 21:51:04 Looks like no - good catch!
468 SendResponse(value->Apply()); 473 SendResponseImpl(value->Apply());
469 } 474 }
470 475
471 UIThreadExtensionFunction::UIThreadExtensionFunction() 476 UIThreadExtensionFunction::UIThreadExtensionFunction()
472 : context_(nullptr), 477 : context_(nullptr),
473 render_frame_host_(nullptr), 478 render_frame_host_(nullptr),
474 is_from_service_worker_(false) {} 479 is_from_service_worker_(false) {}
475 480
476 UIThreadExtensionFunction::~UIThreadExtensionFunction() { 481 UIThreadExtensionFunction::~UIThreadExtensionFunction() {
477 if (dispatcher() && render_frame_host()) 482 if (dispatcher() && render_frame_host())
478 dispatcher()->OnExtensionFunctionCompleted(extension()); 483 dispatcher()->OnExtensionFunctionCompleted(extension());
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 web_contents = dispatcher()->GetAssociatedWebContents(); 546 web_contents = dispatcher()->GetAssociatedWebContents();
542 547
543 return web_contents; 548 return web_contents;
544 } 549 }
545 550
546 content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() { 551 content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() {
547 return render_frame_host_ ? 552 return render_frame_host_ ?
548 content::WebContents::FromRenderFrameHost(render_frame_host_) : nullptr; 553 content::WebContents::FromRenderFrameHost(render_frame_host_) : nullptr;
549 } 554 }
550 555
551 void UIThreadExtensionFunction::SendResponse(bool success) { 556 void UIThreadExtensionFunction::OnResponded() {
552 DCHECK(!did_respond_) << name_;
553 did_respond_ = true;
554 SendResponseImpl(success);
555
556 if (!transferred_blob_uuids_.empty()) { 557 if (!transferred_blob_uuids_.empty()) {
557 render_frame_host_->Send( 558 render_frame_host_->Send(
558 new ExtensionMsg_TransferBlobs(transferred_blob_uuids_)); 559 new ExtensionMsg_TransferBlobs(transferred_blob_uuids_));
559 } 560 }
560 } 561 }
561 562
562 void UIThreadExtensionFunction::SetTransferredBlobUUIDs( 563 void UIThreadExtensionFunction::SetTransferredBlobUUIDs(
563 const std::vector<std::string>& blob_uuids) { 564 const std::vector<std::string>& blob_uuids) {
564 DCHECK(transferred_blob_uuids_.empty()); // Should only be called once. 565 DCHECK(transferred_blob_uuids_.empty()); // Should only be called once.
565 transferred_blob_uuids_ = blob_uuids; 566 transferred_blob_uuids_ = blob_uuids;
(...skipping 17 matching lines...) Expand all
583 584
584 IOThreadExtensionFunction* 585 IOThreadExtensionFunction*
585 IOThreadExtensionFunction::AsIOThreadExtensionFunction() { 586 IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
586 return this; 587 return this;
587 } 588 }
588 589
589 void IOThreadExtensionFunction::Destruct() const { 590 void IOThreadExtensionFunction::Destruct() const {
590 BrowserThread::DeleteOnIOThread::Destruct(this); 591 BrowserThread::DeleteOnIOThread::Destruct(this);
591 } 592 }
592 593
593 void IOThreadExtensionFunction::SendResponse(bool success) {
594 DCHECK(!did_respond_) << name_;
595 did_respond_ = true;
596 SendResponseImpl(success);
597 }
598
599 AsyncExtensionFunction::AsyncExtensionFunction() { 594 AsyncExtensionFunction::AsyncExtensionFunction() {
600 } 595 }
601 596
602 AsyncExtensionFunction::~AsyncExtensionFunction() { 597 AsyncExtensionFunction::~AsyncExtensionFunction() {
603 } 598 }
604 599
605 ExtensionFunction::ScopedUserGestureForTests::ScopedUserGestureForTests() { 600 ExtensionFunction::ScopedUserGestureForTests::ScopedUserGestureForTests() {
606 UserGestureForTests::GetInstance()->IncrementCount(); 601 UserGestureForTests::GetInstance()->IncrementCount();
607 } 602 }
608 603
609 ExtensionFunction::ScopedUserGestureForTests::~ScopedUserGestureForTests() { 604 ExtensionFunction::ScopedUserGestureForTests::~ScopedUserGestureForTests() {
610 UserGestureForTests::GetInstance()->DecrementCount(); 605 UserGestureForTests::GetInstance()->DecrementCount();
611 } 606 }
612 607
613 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { 608 ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() {
614 return RunAsync() ? RespondLater() : RespondNow(Error(error_)); 609 return RunAsync() ? RespondLater() : RespondNow(Error(error_));
615 } 610 }
616 611
617 // static 612 // static
618 bool AsyncExtensionFunction::ValidationFailure( 613 bool AsyncExtensionFunction::ValidationFailure(
619 AsyncExtensionFunction* function) { 614 AsyncExtensionFunction* function) {
620 return false; 615 return false;
621 } 616 }
617
618 void AsyncExtensionFunction::SendResponse(bool success) {
619 Respond(success ? ArgumentList(std::move(results_)) : Error(error_));
620 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698