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

Unified Diff: extensions/browser/api/test/test_api.cc

Issue 2017113002: [Extensions] DCHECK that ExtensionFunctions respond (and only once) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « extensions/browser/api/test/test_api.h ('k') | extensions/browser/api_test_utils.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/browser/api/test/test_api.cc
diff --git a/extensions/browser/api/test/test_api.cc b/extensions/browser/api/test/test_api.cc
index 376dcb96acc4972830496efbab20bfb49121eb32..676d177e77ce875171b4db8bca70b98a8e2810bf 100644
--- a/extensions/browser/api/test/test_api.cc
+++ b/extensions/browser/api/test/test_api.cc
@@ -78,27 +78,47 @@ bool TestLogFunction::RunSafe() {
return true;
}
-bool TestSendMessageFunction::RunAsync() {
+TestSendMessageFunction::TestSendMessageFunction() : waiting_(false) {}
+
+ExtensionFunction::ResponseAction TestSendMessageFunction::Run() {
std::unique_ptr<PassMessage::Params> params(
PassMessage::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());
+ bool listener_will_respond = false;
+ std::pair<std::string, bool*> details(params->message,
+ &listener_will_respond);
content::NotificationService::current()->Notify(
extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE,
content::Source<TestSendMessageFunction>(this),
- content::Details<std::string>(&params->message));
- return true;
+ content::Details<std::pair<std::string, bool*>>(&details));
+ // If the listener is not intending to respond, or has already responded,
+ // finish the function.
+ if (!listener_will_respond || response_.get()) {
+ if (!response_) {
+ response_ =
+ OneArgument(base::MakeUnique<base::StringValue>(std::string()));
+ }
+ return RespondNow(std::move(response_));
+ }
+ // Otherwise, wait for a reply.
+ waiting_ = true;
+ return RespondLater();
}
TestSendMessageFunction::~TestSendMessageFunction() {}
void TestSendMessageFunction::Reply(const std::string& message) {
- SetResult(base::MakeUnique<base::StringValue>(message));
- SendResponse(true);
+ DCHECK(!response_);
+ response_ = OneArgument(base::MakeUnique<base::StringValue>(message));
+ if (waiting_)
+ Respond(std::move(response_));
}
void TestSendMessageFunction::ReplyWithError(const std::string& error) {
- error_ = error;
- SendResponse(false);
+ DCHECK(!response_);
+ response_ = Error(error);
+ if (waiting_)
+ Respond(std::move(response_));
}
// static
« no previous file with comments | « extensions/browser/api/test/test_api.h ('k') | extensions/browser/api_test_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698