| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/api/test/test_api.h" | 5 #include "extensions/browser/api/test/test_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 | 71 |
| 72 TestLogFunction::~TestLogFunction() {} | 72 TestLogFunction::~TestLogFunction() {} |
| 73 | 73 |
| 74 bool TestLogFunction::RunSafe() { | 74 bool TestLogFunction::RunSafe() { |
| 75 std::unique_ptr<Log::Params> params(Log::Params::Create(*args_)); | 75 std::unique_ptr<Log::Params> params(Log::Params::Create(*args_)); |
| 76 EXTENSION_FUNCTION_VALIDATE(params.get()); | 76 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 77 VLOG(1) << params->message; | 77 VLOG(1) << params->message; |
| 78 return true; | 78 return true; |
| 79 } | 79 } |
| 80 | 80 |
| 81 bool TestSendMessageFunction::RunAsync() { | 81 TestSendMessageFunction::TestSendMessageFunction() : waiting_(false) {} |
| 82 |
| 83 ExtensionFunction::ResponseAction TestSendMessageFunction::Run() { |
| 82 std::unique_ptr<PassMessage::Params> params( | 84 std::unique_ptr<PassMessage::Params> params( |
| 83 PassMessage::Params::Create(*args_)); | 85 PassMessage::Params::Create(*args_)); |
| 84 EXTENSION_FUNCTION_VALIDATE(params.get()); | 86 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 87 bool listener_will_respond = false; |
| 88 std::pair<std::string, bool*> details(params->message, |
| 89 &listener_will_respond); |
| 85 content::NotificationService::current()->Notify( | 90 content::NotificationService::current()->Notify( |
| 86 extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE, | 91 extensions::NOTIFICATION_EXTENSION_TEST_MESSAGE, |
| 87 content::Source<TestSendMessageFunction>(this), | 92 content::Source<TestSendMessageFunction>(this), |
| 88 content::Details<std::string>(¶ms->message)); | 93 content::Details<std::pair<std::string, bool*>>(&details)); |
| 89 return true; | 94 // If the listener is not intending to respond, or has already responded, |
| 95 // finish the function. |
| 96 if (!listener_will_respond || response_.get()) { |
| 97 if (!response_) { |
| 98 response_ = |
| 99 OneArgument(base::MakeUnique<base::StringValue>(std::string())); |
| 100 } |
| 101 return RespondNow(std::move(response_)); |
| 102 } |
| 103 // Otherwise, wait for a reply. |
| 104 waiting_ = true; |
| 105 return RespondLater(); |
| 90 } | 106 } |
| 91 | 107 |
| 92 TestSendMessageFunction::~TestSendMessageFunction() {} | 108 TestSendMessageFunction::~TestSendMessageFunction() {} |
| 93 | 109 |
| 94 void TestSendMessageFunction::Reply(const std::string& message) { | 110 void TestSendMessageFunction::Reply(const std::string& message) { |
| 95 SetResult(base::MakeUnique<base::StringValue>(message)); | 111 DCHECK(!response_); |
| 96 SendResponse(true); | 112 response_ = OneArgument(base::MakeUnique<base::StringValue>(message)); |
| 113 if (waiting_) |
| 114 Respond(std::move(response_)); |
| 97 } | 115 } |
| 98 | 116 |
| 99 void TestSendMessageFunction::ReplyWithError(const std::string& error) { | 117 void TestSendMessageFunction::ReplyWithError(const std::string& error) { |
| 100 error_ = error; | 118 DCHECK(!response_); |
| 101 SendResponse(false); | 119 response_ = Error(error); |
| 120 if (waiting_) |
| 121 Respond(std::move(response_)); |
| 102 } | 122 } |
| 103 | 123 |
| 104 // static | 124 // static |
| 105 void TestGetConfigFunction::set_test_config_state( | 125 void TestGetConfigFunction::set_test_config_state( |
| 106 base::DictionaryValue* value) { | 126 base::DictionaryValue* value) { |
| 107 TestConfigState* test_config_state = TestConfigState::GetInstance(); | 127 TestConfigState* test_config_state = TestConfigState::GetInstance(); |
| 108 test_config_state->set_config_state(value); | 128 test_config_state->set_config_state(value); |
| 109 } | 129 } |
| 110 | 130 |
| 111 TestGetConfigFunction::TestConfigState::TestConfigState() | 131 TestGetConfigFunction::TestConfigState::TestConfigState() |
| (...skipping 22 matching lines...) Expand all Loading... |
| 134 TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {} | 154 TestWaitForRoundTripFunction::~TestWaitForRoundTripFunction() {} |
| 135 | 155 |
| 136 bool TestWaitForRoundTripFunction::RunSafe() { | 156 bool TestWaitForRoundTripFunction::RunSafe() { |
| 137 std::unique_ptr<WaitForRoundTrip::Params> params( | 157 std::unique_ptr<WaitForRoundTrip::Params> params( |
| 138 WaitForRoundTrip::Params::Create(*args_)); | 158 WaitForRoundTrip::Params::Create(*args_)); |
| 139 SetResult(base::MakeUnique<base::StringValue>(params->message)); | 159 SetResult(base::MakeUnique<base::StringValue>(params->message)); |
| 140 return true; | 160 return true; |
| 141 } | 161 } |
| 142 | 162 |
| 143 } // namespace extensions | 163 } // namespace extensions |
| OLD | NEW |