Commit 5c7391f9 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Migrate GsmSMSClient to DBusCallback.

BUG=739622
TEST=Ran chromeos_unittests.

Change-Id: Ic79e5491ccf36e2cb83642545dd5b29763c0eedc
Reviewed-on: https://chromium-review.googlesource.com/903607Reviewed-by: default avatarDan Erat <derat@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#534711}
parent 008ceb3e
...@@ -50,28 +50,31 @@ void FakeGsmSMSClient::ResetSmsReceivedHandler( ...@@ -50,28 +50,31 @@ void FakeGsmSMSClient::ResetSmsReceivedHandler(
void FakeGsmSMSClient::Delete(const std::string& service_name, void FakeGsmSMSClient::Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const DeleteCallback& callback) { VoidDBusMethodCallback callback) {
message_list_.Remove(index, NULL); message_list_.Remove(index, nullptr);
callback.Run(); std::move(callback).Run(true);
} }
void FakeGsmSMSClient::Get(const std::string& service_name, void FakeGsmSMSClient::Get(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const GetCallback& callback) { DBusMethodCallback<base::DictionaryValue> callback) {
base::DictionaryValue* dictionary = NULL; base::DictionaryValue* dictionary = nullptr;
if (message_list_.GetDictionary(index, &dictionary)) { if (!message_list_.GetDictionary(index, &dictionary)) {
callback.Run(*dictionary); std::move(callback).Run(base::nullopt);
return; return;
} }
base::DictionaryValue empty_dictionary;
callback.Run(empty_dictionary); // TODO(crbug.com/646113): Once migration is done, this can be simplified.
base::DictionaryValue copy;
copy.MergeDictionary(dictionary);
std::move(callback).Run(std::move(copy));
} }
void FakeGsmSMSClient::List(const std::string& service_name, void FakeGsmSMSClient::List(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const ListCallback& callback) { DBusMethodCallback<base::ListValue> callback) {
callback.Run(message_list_); std::move(callback).Run(base::ListValue(message_list_.GetList()));
} }
void FakeGsmSMSClient::RequestUpdate(const std::string& service_name, void FakeGsmSMSClient::RequestUpdate(const std::string& service_name,
......
...@@ -34,14 +34,14 @@ class CHROMEOS_EXPORT FakeGsmSMSClient : public GsmSMSClient { ...@@ -34,14 +34,14 @@ class CHROMEOS_EXPORT FakeGsmSMSClient : public GsmSMSClient {
void Delete(const std::string& service_name, void Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const DeleteCallback& callback) override; VoidDBusMethodCallback callback) override;
void Get(const std::string& service_name, void Get(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const GetCallback& callback) override; DBusMethodCallback<base::DictionaryValue> callback) override;
void List(const std::string& service_name, void List(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const ListCallback& callback) override; DBusMethodCallback<base::ListValue> callback) override;
void RequestUpdate(const std::string& service_name, void RequestUpdate(const std::string& service_name,
const dbus::ObjectPath& object_path) override; const dbus::ObjectPath& object_path) override;
......
...@@ -31,9 +31,6 @@ namespace { ...@@ -31,9 +31,6 @@ namespace {
class SMSProxy { class SMSProxy {
public: public:
typedef GsmSMSClient::SmsReceivedHandler SmsReceivedHandler; typedef GsmSMSClient::SmsReceivedHandler SmsReceivedHandler;
typedef GsmSMSClient::DeleteCallback DeleteCallback;
typedef GsmSMSClient::GetCallback GetCallback;
typedef GsmSMSClient::ListCallback ListCallback;
SMSProxy(dbus::Bus* bus, SMSProxy(dbus::Bus* bus,
const std::string& service_name, const std::string& service_name,
...@@ -60,7 +57,7 @@ class SMSProxy { ...@@ -60,7 +57,7 @@ class SMSProxy {
} }
// Calls Delete method. // Calls Delete method.
void Delete(uint32_t index, const DeleteCallback& callback) { void Delete(uint32_t index, VoidDBusMethodCallback callback) {
dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface, dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface,
modemmanager::kSMSDeleteFunction); modemmanager::kSMSDeleteFunction);
dbus::MessageWriter writer(&method_call); dbus::MessageWriter writer(&method_call);
...@@ -68,11 +65,11 @@ class SMSProxy { ...@@ -68,11 +65,11 @@ class SMSProxy {
proxy_->CallMethod( proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SMSProxy::OnDelete, weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&SMSProxy::OnDelete, weak_ptr_factory_.GetWeakPtr(),
callback)); std::move(callback)));
} }
// Calls Get method. // Calls Get method.
void Get(uint32_t index, const GetCallback& callback) { void Get(uint32_t index, DBusMethodCallback<base::DictionaryValue> callback) {
dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface, dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface,
modemmanager::kSMSGetFunction); modemmanager::kSMSGetFunction);
dbus::MessageWriter writer(&method_call); dbus::MessageWriter writer(&method_call);
...@@ -80,17 +77,17 @@ class SMSProxy { ...@@ -80,17 +77,17 @@ class SMSProxy {
proxy_->CallMethod( proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SMSProxy::OnGet, weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&SMSProxy::OnGet, weak_ptr_factory_.GetWeakPtr(),
callback)); std::move(callback)));
} }
// Calls List method. // Calls List method.
void List(const ListCallback& callback) { void List(DBusMethodCallback<base::ListValue> callback) {
dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface, dbus::MethodCall method_call(modemmanager::kModemManagerSMSInterface,
modemmanager::kSMSListFunction); modemmanager::kSMSListFunction);
proxy_->CallMethod( proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&SMSProxy::OnList, weak_ptr_factory_.GetWeakPtr(), base::BindOnce(&SMSProxy::OnList, weak_ptr_factory_.GetWeakPtr(),
callback)); std::move(callback)));
} }
private: private:
...@@ -117,38 +114,42 @@ class SMSProxy { ...@@ -117,38 +114,42 @@ class SMSProxy {
} }
// Handles responses of Delete method calls. // Handles responses of Delete method calls.
void OnDelete(const DeleteCallback& callback, dbus::Response* response) { void OnDelete(VoidDBusMethodCallback callback, dbus::Response* response) {
if (!response) std::move(callback).Run(response);
return;
callback.Run();
} }
// Handles responses of Get method calls. // Handles responses of Get method calls.
void OnGet(const GetCallback& callback, dbus::Response* response) { void OnGet(DBusMethodCallback<base::DictionaryValue> callback,
if (!response) dbus::Response* response) {
if (!response) {
std::move(callback).Run(base::nullopt);
return; return;
}
dbus::MessageReader reader(response); dbus::MessageReader reader(response);
std::unique_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); auto value = base::DictionaryValue::From(dbus::PopDataAsValue(&reader));
base::DictionaryValue* dictionary_value = NULL; if (!value) {
if (!value.get() || !value->GetAsDictionary(&dictionary_value)) {
LOG(WARNING) << "Invalid response: " << response->ToString(); LOG(WARNING) << "Invalid response: " << response->ToString();
std::move(callback).Run(base::nullopt);
return; return;
} }
callback.Run(*dictionary_value); std::move(callback).Run(std::move(*value));
} }
// Handles responses of List method calls. // Handles responses of List method calls.
void OnList(const ListCallback& callback, dbus::Response* response) { void OnList(DBusMethodCallback<base::ListValue> callback,
if (!response) dbus::Response* response) {
if (!response) {
std::move(callback).Run(base::nullopt);
return; return;
}
dbus::MessageReader reader(response); dbus::MessageReader reader(response);
std::unique_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); auto value = base::ListValue::From(dbus::PopDataAsValue(&reader));
base::ListValue* list_value = NULL; if (!value) {
if (!value.get() || !value->GetAsList(&list_value)) {
LOG(WARNING) << "Invalid response: " << response->ToString(); LOG(WARNING) << "Invalid response: " << response->ToString();
std::move(callback).Run(base::nullopt);
return; return;
} }
callback.Run(*list_value); std::move(callback).Run(std::move(*value));
} }
dbus::ObjectProxy* proxy_; dbus::ObjectProxy* proxy_;
...@@ -183,23 +184,23 @@ class GsmSMSClientImpl : public GsmSMSClient { ...@@ -183,23 +184,23 @@ class GsmSMSClientImpl : public GsmSMSClient {
void Delete(const std::string& service_name, void Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const DeleteCallback& callback) override { VoidDBusMethodCallback callback) override {
GetProxy(service_name, object_path)->Delete(index, callback); GetProxy(service_name, object_path)->Delete(index, std::move(callback));
} }
// GsmSMSClient override. // GsmSMSClient override.
void Get(const std::string& service_name, void Get(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const GetCallback& callback) override { DBusMethodCallback<base::DictionaryValue> callback) override {
GetProxy(service_name, object_path)->Get(index, callback); GetProxy(service_name, object_path)->Get(index, std::move(callback));
} }
// GsmSMSClient override. // GsmSMSClient override.
void List(const std::string& service_name, void List(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const ListCallback& callback) override { DBusMethodCallback<base::ListValue> callback) override {
GetProxy(service_name, object_path)->List(callback); GetProxy(service_name, object_path)->List(std::move(callback));
} }
// GsmSMSClient override. // GsmSMSClient override.
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "chromeos/chromeos_export.h" #include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_client.h" #include "chromeos/dbus/dbus_client.h"
#include "chromeos/dbus/dbus_method_call_status.h"
namespace base { namespace base {
class DictionaryValue; class DictionaryValue;
...@@ -33,9 +34,6 @@ class CHROMEOS_EXPORT GsmSMSClient : public DBusClient { ...@@ -33,9 +34,6 @@ class CHROMEOS_EXPORT GsmSMSClient : public DBusClient {
public: public:
typedef base::Callback<void(uint32_t index, bool complete)> typedef base::Callback<void(uint32_t index, bool complete)>
SmsReceivedHandler; SmsReceivedHandler;
typedef base::Callback<void()> DeleteCallback;
typedef base::Callback<void(const base::DictionaryValue& sms)> GetCallback;
typedef base::Callback<void(const base::ListValue& result)> ListCallback;
~GsmSMSClient() override; ~GsmSMSClient() override;
...@@ -52,22 +50,22 @@ class CHROMEOS_EXPORT GsmSMSClient : public DBusClient { ...@@ -52,22 +50,22 @@ class CHROMEOS_EXPORT GsmSMSClient : public DBusClient {
virtual void ResetSmsReceivedHandler(const std::string& service_name, virtual void ResetSmsReceivedHandler(const std::string& service_name,
const dbus::ObjectPath& object_path) = 0; const dbus::ObjectPath& object_path) = 0;
// Calls Delete method. |callback| is called after the method call succeeds. // Calls Delete method. |callback| is called on method call completion.
virtual void Delete(const std::string& service_name, virtual void Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const DeleteCallback& callback) = 0; VoidDBusMethodCallback callback) = 0;
// Calls Get method. |callback| is called after the method call succeeds. // Calls Get method. |callback| is called on method call completion.
virtual void Get(const std::string& service_name, virtual void Get(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
uint32_t index, uint32_t index,
const GetCallback& callback) = 0; DBusMethodCallback<base::DictionaryValue> callback) = 0;
// Calls List method. |callback| is called after the method call succeeds. // Calls List method. |callback| is called on method call completion.
virtual void List(const std::string& service_name, virtual void List(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const ListCallback& callback) = 0; DBusMethodCallback<base::ListValue> callback) = 0;
// Requests a check for new messages. In shill this does nothing. The // Requests a check for new messages. In shill this does nothing. The
// stub implementation uses it to generate a sequence of test messages. // stub implementation uses it to generate a sequence of test messages.
......
...@@ -222,13 +222,16 @@ TEST_F(GsmSMSClientTest, Delete) { ...@@ -222,13 +222,16 @@ TEST_F(GsmSMSClientTest, Delete) {
response_ = response.get(); response_ = response.get();
// Call Delete. // Call Delete.
bool called = false; base::Optional<bool> success;
client_->Delete(kServiceName, dbus::ObjectPath(kObjectPath), kIndex, client_->Delete(
base::Bind([](bool* called) { *called = true; }, &called)); kServiceName, dbus::ObjectPath(kObjectPath), kIndex,
base::BindOnce([](base::Optional<bool>* success_out,
bool success) { success_out->emplace(success); },
&success));
// Run the message loop. // Run the message loop.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called); EXPECT_EQ(true, success);
} }
TEST_F(GsmSMSClientTest, Get) { TEST_F(GsmSMSClientTest, Get) {
...@@ -255,32 +258,23 @@ TEST_F(GsmSMSClientTest, Get) { ...@@ -255,32 +258,23 @@ TEST_F(GsmSMSClientTest, Get) {
response_ = response.get(); response_ = response.get();
// Call Get. // Call Get.
base::Value raw_result; base::Optional<base::DictionaryValue> result;
client_->Get( client_->Get(kServiceName, dbus::ObjectPath(kObjectPath), kIndex,
kServiceName, dbus::ObjectPath(kObjectPath), kIndex, base::BindOnce(
base::Bind( [](base::Optional<base::DictionaryValue>* result_out,
[](base::Value* result_out, const base::DictionaryValue& result) { base::Optional<base::DictionaryValue> result) {
*result_out = result.Clone(); *result_out = std::move(result);
}, },
&raw_result)); &result));
// Run the message loop. // Run the message loop.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Verify the result. // Verify the result.
const auto result = base::DictionaryValue::From( base::DictionaryValue expected_result;
base::Value::ToUniquePtrValue(std::move(raw_result))); expected_result.SetKey(kNumberKey, base::Value(kExampleNumber));
ASSERT_TRUE(result); expected_result.SetKey(kTextKey, base::Value(kExampleText));
EXPECT_EQ(2u, result->size()); EXPECT_EQ(expected_result, result);
const base::Value* number =
result->FindKeyOfType(kNumberKey, base::Value::Type::STRING);
ASSERT_TRUE(number);
EXPECT_EQ(kExampleNumber, number->GetString());
const base::Value* text =
result->FindKeyOfType(kTextKey, base::Value::Type::STRING);
ASSERT_TRUE(text);
EXPECT_EQ(kExampleText, text->GetString());
} }
TEST_F(GsmSMSClientTest, List) { TEST_F(GsmSMSClientTest, List) {
...@@ -308,11 +302,12 @@ TEST_F(GsmSMSClientTest, List) { ...@@ -308,11 +302,12 @@ TEST_F(GsmSMSClientTest, List) {
response_ = response.get(); response_ = response.get();
// Call List. // Call List.
base::Value result; base::Optional<base::ListValue> result;
client_->List(kServiceName, dbus::ObjectPath(kObjectPath), client_->List(kServiceName, dbus::ObjectPath(kObjectPath),
base::Bind( base::BindOnce(
[](base::Value* result_out, const base::ListValue& result) { [](base::Optional<base::ListValue>* result_out,
*result_out = result.Clone(); base::Optional<base::ListValue> result) {
*result_out = std::move(result);
}, },
&result)); &result));
......
...@@ -59,10 +59,12 @@ class NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler ...@@ -59,10 +59,12 @@ class NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler
void RequestUpdate() override; void RequestUpdate() override;
private: private:
void ListCallback(const base::ListValue& message_list); void ListCallback(base::Optional<base::ListValue> message_list);
void SmsReceivedCallback(uint32_t index, bool complete); void SmsReceivedCallback(uint32_t index, bool complete);
void GetCallback(uint32_t index, const base::DictionaryValue& dictionary); void GetCallback(uint32_t index,
base::Optional<base::DictionaryValue> dictionary);
void DeleteMessages(); void DeleteMessages();
void DeleteCallback(bool success);
void MessageReceived(const base::DictionaryValue& dictionary); void MessageReceived(const base::DictionaryValue& dictionary);
NetworkSmsHandler* host_; NetworkSmsHandler* host_;
...@@ -105,13 +107,15 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::RequestUpdate() { ...@@ -105,13 +107,15 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::RequestUpdate() {
} }
void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::ListCallback( void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::ListCallback(
const base::ListValue& message_list) { base::Optional<base::ListValue> message_list) {
if (!message_list.has_value())
return;
// This receives all messages, so clear any pending deletes. // This receives all messages, so clear any pending deletes.
delete_queue_.clear(); delete_queue_.clear();
for (base::ListValue::const_iterator iter = message_list.begin(); for (const auto& entry : message_list.value()) {
iter != message_list.end(); ++iter) { const base::DictionaryValue* message = nullptr;
const base::DictionaryValue* message = NULL; if (entry.GetAsDictionary(&message))
if (iter->GetAsDictionary(&message))
continue; continue;
MessageReceived(*message); MessageReceived(*message);
double index = 0; double index = 0;
...@@ -134,9 +138,16 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::DeleteMessages() { ...@@ -134,9 +138,16 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::DeleteMessages() {
delete_queue_.pop_back(); delete_queue_.pop_back();
DBusThreadManager::Get()->GetGsmSMSClient()->Delete( DBusThreadManager::Get()->GetGsmSMSClient()->Delete(
service_name_, object_path_, index, service_name_, object_path_, index,
base::Bind(&NetworkSmsHandler:: base::BindOnce(&NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::
ModemManagerNetworkSmsDeviceHandler::DeleteMessages, DeleteCallback,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
}
void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::DeleteCallback(
bool success) {
if (!success)
return;
DeleteMessages();
} }
void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler:: void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::
...@@ -146,15 +157,18 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler:: ...@@ -146,15 +157,18 @@ void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::
return; return;
DBusThreadManager::Get()->GetGsmSMSClient()->Get( DBusThreadManager::Get()->GetGsmSMSClient()->Get(
service_name_, object_path_, index, service_name_, object_path_, index,
base::Bind(&NetworkSmsHandler:: base::BindOnce(
ModemManagerNetworkSmsDeviceHandler::GetCallback, &NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::GetCallback,
weak_ptr_factory_.GetWeakPtr(), index)); weak_ptr_factory_.GetWeakPtr(), index));
} }
void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::GetCallback( void NetworkSmsHandler::ModemManagerNetworkSmsDeviceHandler::GetCallback(
uint32_t index, uint32_t index,
const base::DictionaryValue& dictionary) { base::Optional<base::DictionaryValue> dictionary) {
MessageReceived(dictionary); if (!dictionary.has_value())
return;
MessageReceived(dictionary.value());
delete_queue_.push_back(index); delete_queue_.push_back(index);
if (!deleting_messages_) if (!deleting_messages_)
DeleteMessages(); DeleteMessages();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment