Commit afed9717 authored by Hidehiko Abe's avatar Hidehiko Abe Committed by Commit Bot

Migrate ModemMessagingClient into DBusMethodCallback.

BUG=739622
TEST=Ran chromeos_unittests.

Change-Id: Id389c09d95edae181f7862174ab58de765b0ee22
Reviewed-on: https://chromium-review.googlesource.com/937741
Commit-Queue: Hidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541295}
parent 041c17d0
...@@ -35,29 +35,28 @@ void FakeModemMessagingClient::ResetSmsReceivedHandler( ...@@ -35,29 +35,28 @@ void FakeModemMessagingClient::ResetSmsReceivedHandler(
void FakeModemMessagingClient::Delete(const std::string& service_name, void FakeModemMessagingClient::Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const dbus::ObjectPath& sms_path, const dbus::ObjectPath& sms_path,
const DeleteCallback& callback) { VoidDBusMethodCallback callback) {
std::vector<dbus::ObjectPath>::iterator it( std::vector<dbus::ObjectPath>::iterator it(
find(message_paths_.begin(), message_paths_.end(), sms_path)); find(message_paths_.begin(), message_paths_.end(), sms_path));
if (it != message_paths_.end()) if (it != message_paths_.end())
message_paths_.erase(it); message_paths_.erase(it);
callback.Run(); std::move(callback).Run(true);
} }
void FakeModemMessagingClient::List(const std::string& service_name, void FakeModemMessagingClient::List(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const ListCallback& callback) { ListCallback callback) {
// This entire FakeModemMessagingClient is for testing. // This entire FakeModemMessagingClient is for testing.
// Calling List with |service_name| equal to "AddSMS" allows unit // Calling List with |service_name| equal to "AddSMS" allows unit
// tests to confirm that the sms_received_handler is functioning. // tests to confirm that the sms_received_handler is functioning.
if (service_name == "AddSMS") { if (service_name == "AddSMS") {
std::vector<dbus::ObjectPath> no_paths;
const dbus::ObjectPath kSmsPath("/SMS/0"); const dbus::ObjectPath kSmsPath("/SMS/0");
message_paths_.push_back(kSmsPath); message_paths_.push_back(kSmsPath);
if (!sms_received_handler_.is_null()) if (!sms_received_handler_.is_null())
sms_received_handler_.Run(kSmsPath, true); sms_received_handler_.Run(kSmsPath, true);
callback.Run(no_paths); std::move(callback).Run({});
} else { } else {
callback.Run(message_paths_); std::move(callback).Run(message_paths_);
} }
} }
......
...@@ -29,10 +29,10 @@ class CHROMEOS_EXPORT FakeModemMessagingClient : public ModemMessagingClient { ...@@ -29,10 +29,10 @@ class CHROMEOS_EXPORT FakeModemMessagingClient : public ModemMessagingClient {
void Delete(const std::string& service_name, void Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const dbus::ObjectPath& sms_path, const dbus::ObjectPath& sms_path,
const DeleteCallback& callback) override; VoidDBusMethodCallback 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; ListCallback callback) override;
private: private:
SmsReceivedHandler sms_received_handler_; SmsReceivedHandler sms_received_handler_;
......
...@@ -26,8 +26,7 @@ namespace { ...@@ -26,8 +26,7 @@ namespace {
class ModemMessagingProxy { class ModemMessagingProxy {
public: public:
typedef ModemMessagingClient::SmsReceivedHandler SmsReceivedHandler; typedef ModemMessagingClient::SmsReceivedHandler SmsReceivedHandler;
typedef ModemMessagingClient::ListCallback ListCallback; using ListCallback = ModemMessagingClient::ListCallback;
typedef ModemMessagingClient::DeleteCallback DeleteCallback;
ModemMessagingProxy(dbus::Bus* bus, ModemMessagingProxy(dbus::Bus* bus,
const std::string& service_name, const std::string& service_name,
...@@ -58,7 +57,7 @@ class ModemMessagingProxy { ...@@ -58,7 +57,7 @@ class ModemMessagingProxy {
// Calls Delete method. // Calls Delete method.
void Delete(const dbus::ObjectPath& message_path, void Delete(const dbus::ObjectPath& message_path,
const DeleteCallback& callback) { VoidDBusMethodCallback callback) {
dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface, dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
modemmanager::kSMSDeleteFunction); modemmanager::kSMSDeleteFunction);
dbus::MessageWriter writer(&method_call); dbus::MessageWriter writer(&method_call);
...@@ -66,17 +65,17 @@ class ModemMessagingProxy { ...@@ -66,17 +65,17 @@ class ModemMessagingProxy {
proxy_->CallMethod( proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ModemMessagingProxy::OnDelete, base::BindOnce(&ModemMessagingProxy::OnDelete,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
// Calls List method. // Calls List method.
virtual void List(const ListCallback& callback) { virtual void List(ListCallback callback) {
dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface, dbus::MethodCall method_call(modemmanager::kModemManager1MessagingInterface,
modemmanager::kSMSListFunction); modemmanager::kSMSListFunction);
proxy_->CallMethod( proxy_->CallMethod(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(&ModemMessagingProxy::OnList, base::BindOnce(&ModemMessagingProxy::OnList,
weak_ptr_factory_.GetWeakPtr(), callback)); weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
} }
private: private:
...@@ -96,21 +95,24 @@ class ModemMessagingProxy { ...@@ -96,21 +95,24 @@ class ModemMessagingProxy {
} }
// 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 List method calls. // Handles responses of List method calls.
void OnList(const ListCallback& callback, dbus::Response* response) { void OnList(ListCallback callback, dbus::Response* response) {
if (!response) if (!response) {
std::move(callback).Run(base::nullopt);
return; return;
}
dbus::MessageReader reader(response); dbus::MessageReader reader(response);
std::vector<dbus::ObjectPath> sms_paths; std::vector<dbus::ObjectPath> sms_paths;
if (!reader.PopArrayOfObjectPaths(&sms_paths)) if (!reader.PopArrayOfObjectPaths(&sms_paths)) {
LOG(WARNING) << "Invalid response: " << response->ToString(); LOG(WARNING) << "Invalid response: " << response->ToString();
callback.Run(sms_paths); std::move(callback).Run(base::nullopt);
return;
}
std::move(callback).Run(std::move(sms_paths));
} }
// Handles the result of signal connection setup. // Handles the result of signal connection setup.
...@@ -150,14 +152,14 @@ class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient { ...@@ -150,14 +152,14 @@ class CHROMEOS_EXPORT ModemMessagingClientImpl : public ModemMessagingClient {
void Delete(const std::string& service_name, void Delete(const std::string& service_name,
const dbus::ObjectPath& object_path, const dbus::ObjectPath& object_path,
const dbus::ObjectPath& sms_path, const dbus::ObjectPath& sms_path,
const DeleteCallback& callback) override { VoidDBusMethodCallback callback) override {
GetProxy(service_name, object_path)->Delete(sms_path, callback); GetProxy(service_name, object_path)->Delete(sms_path, std::move(callback));
} }
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 { ListCallback callback) override {
GetProxy(service_name, object_path)->List(callback); GetProxy(service_name, object_path)->List(std::move(callback));
} }
protected: protected:
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,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 dbus { namespace dbus {
class ObjectPath; class ObjectPath;
...@@ -25,11 +26,8 @@ namespace chromeos { ...@@ -25,11 +26,8 @@ namespace chromeos {
// initializes the DBusThreadManager instance. // initializes the DBusThreadManager instance.
class CHROMEOS_EXPORT ModemMessagingClient : public DBusClient { class CHROMEOS_EXPORT ModemMessagingClient : public DBusClient {
public: public:
typedef base::Callback<void()> DeleteCallback;
typedef base::Callback<void(const dbus::ObjectPath& message_path, typedef base::Callback<void(const dbus::ObjectPath& message_path,
bool complete)> SmsReceivedHandler; bool complete)> SmsReceivedHandler;
typedef base::Callback<void(const std::vector<dbus::ObjectPath>& paths)>
ListCallback;
~ModemMessagingClient() override; ~ModemMessagingClient() override;
...@@ -46,16 +44,17 @@ class CHROMEOS_EXPORT ModemMessagingClient : public DBusClient { ...@@ -46,16 +44,17 @@ class CHROMEOS_EXPORT ModemMessagingClient : 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 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,
const dbus::ObjectPath& sms_path, const dbus::ObjectPath& sms_path,
const DeleteCallback& callback) = 0; VoidDBusMethodCallback callback) = 0;
// Calls List method. |callback| is called after the method call succeeds. // Calls List method. |callback| is called on method completion.
using ListCallback = DBusMethodCallback<std::vector<dbus::ObjectPath>>;
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; ListCallback callback) = 0;
protected: protected:
friend class ModemMessagingClientTest; friend class ModemMessagingClientTest;
......
...@@ -185,13 +185,15 @@ TEST_F(ModemMessagingClientTest, Delete) { ...@@ -185,13 +185,15 @@ TEST_F(ModemMessagingClientTest, Delete) {
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
response_ = response.get(); response_ = response.get();
// Call Delete. // Call Delete.
bool called = false; bool success = false;
client_->Delete(kServiceName, dbus::ObjectPath(kObjectPath), kSmsPath, client_->Delete(kServiceName, dbus::ObjectPath(kObjectPath), kSmsPath,
base::Bind([](bool* called) { *called = true; }, &called)); base::BindOnce([](bool* success_out,
bool success) { *success_out = true; },
&success));
// Run the message loop. // Run the message loop.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(called); EXPECT_TRUE(success);
} }
TEST_F(ModemMessagingClientTest, List) { TEST_F(ModemMessagingClientTest, List) {
...@@ -214,18 +216,14 @@ TEST_F(ModemMessagingClientTest, List) { ...@@ -214,18 +216,14 @@ TEST_F(ModemMessagingClientTest, List) {
kServiceName, dbus::ObjectPath(kObjectPath), kServiceName, dbus::ObjectPath(kObjectPath),
base::Bind( base::Bind(
[](base::Optional<std::vector<dbus::ObjectPath>>* result_out, [](base::Optional<std::vector<dbus::ObjectPath>>* result_out,
const std::vector<dbus::ObjectPath>& result) { base::Optional<std::vector<dbus::ObjectPath>> result) {
result_out->emplace(result); *result_out = std::move(result);
}, },
&result)); &result));
// Run the message loop. // Run the message loop.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(kExpectedResult, result);
// TODO(crbug.com/784732): We should be able to skip explicit has_value()
// check.
ASSERT_TRUE(result.has_value());
EXPECT_EQ(kExpectedResult, result.value());
} }
} // namespace chromeos } // namespace chromeos
...@@ -193,10 +193,11 @@ class NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler ...@@ -193,10 +193,11 @@ class NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler
void RequestUpdate() override; void RequestUpdate() override;
private: private:
void ListCallback(const std::vector<dbus::ObjectPath>& paths); void ListCallback(base::Optional<std::vector<dbus::ObjectPath>> paths);
void SmsReceivedCallback(const dbus::ObjectPath& path, bool complete); void SmsReceivedCallback(const dbus::ObjectPath& path, bool complete);
void GetCallback(const base::DictionaryValue& dictionary); void GetCallback(const base::DictionaryValue& dictionary);
void DeleteMessages(); void DeleteMessages();
void DeleteCallback(bool success);
void GetMessages(); void GetMessages();
void MessageReceived(const base::DictionaryValue& dictionary); void MessageReceived(const base::DictionaryValue& dictionary);
...@@ -234,9 +235,9 @@ ModemManager1NetworkSmsDeviceHandler::ModemManager1NetworkSmsDeviceHandler( ...@@ -234,9 +235,9 @@ ModemManager1NetworkSmsDeviceHandler::ModemManager1NetworkSmsDeviceHandler(
// List the existing messages. // List the existing messages.
DBusThreadManager::Get()->GetModemMessagingClient()->List( DBusThreadManager::Get()->GetModemMessagingClient()->List(
service_name_, object_path_, service_name_, object_path_,
base::Bind(&NetworkSmsHandler:: base::BindOnce(&NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::
ModemManager1NetworkSmsDeviceHandler::ListCallback, ListCallback,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::RequestUpdate() { void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::RequestUpdate() {
...@@ -244,19 +245,23 @@ void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::RequestUpdate() { ...@@ -244,19 +245,23 @@ void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::RequestUpdate() {
// implementation to deliver new sms messages. // implementation to deliver new sms messages.
DBusThreadManager::Get()->GetModemMessagingClient()->List( DBusThreadManager::Get()->GetModemMessagingClient()->List(
std::string("AddSMS"), dbus::ObjectPath("/"), std::string("AddSMS"), dbus::ObjectPath("/"),
base::Bind(&NetworkSmsHandler:: base::BindOnce(&NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::
ModemManager1NetworkSmsDeviceHandler::ListCallback, ListCallback,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::ListCallback( void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::ListCallback(
const std::vector<dbus::ObjectPath>& paths) { base::Optional<std::vector<dbus::ObjectPath>> paths) {
// This receives all messages, so clear any pending gets and deletes. // This receives all messages, so clear any pending gets and deletes.
retrieval_queue_.clear(); retrieval_queue_.clear();
delete_queue_.clear(); delete_queue_.clear();
retrieval_queue_.resize(paths.size()); if (!paths.has_value())
std::copy(paths.begin(), paths.end(), retrieval_queue_.begin()); return;
retrieval_queue_.reserve(paths->size());
retrieval_queue_.assign(std::make_move_iterator(paths->begin()),
std::make_move_iterator(paths->end()));
if (!retrieving_messages_) if (!retrieving_messages_)
GetMessages(); GetMessages();
} }
...@@ -270,13 +275,20 @@ void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::DeleteMessages() { ...@@ -270,13 +275,20 @@ void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::DeleteMessages() {
return; return;
} }
deleting_messages_ = true; deleting_messages_ = true;
dbus::ObjectPath sms_path = delete_queue_.back(); dbus::ObjectPath sms_path = std::move(delete_queue_.back());
delete_queue_.pop_back(); delete_queue_.pop_back();
DBusThreadManager::Get()->GetModemMessagingClient()->Delete( DBusThreadManager::Get()->GetModemMessagingClient()->Delete(
service_name_, object_path_, sms_path, service_name_, object_path_, sms_path,
base::Bind(&NetworkSmsHandler:: base::BindOnce(&NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::
ModemManager1NetworkSmsDeviceHandler::DeleteMessages, DeleteCallback,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
}
void NetworkSmsHandler::ModemManager1NetworkSmsDeviceHandler::DeleteCallback(
bool success) {
if (!success)
return;
DeleteMessages();
} }
// Messages must be fetched one at a time, so that we do not queue too // Messages must be fetched one at a time, so that we do not queue too
......
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