Commit 122fde91 authored by Jimmy Gong's avatar Jimmy Gong Committed by Commit Bot

Add SendMessage to ConnectionManager

- ConnectionManager exposes SendMessage to send a message
  through the client channel.
- Adds the OnMessageReceived() to ConnectionManager's observer
  to notify clients of new messages from the client channel.

Bug: 1106937
Test: unit_tests
Change-Id: I9b2de4c7c05855deaf1ea8cebf2d01b2d4e3c69c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2390972
Commit-Queue: Jimmy Gong <jimmyxgong@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#805085}
parent 30744234
...@@ -23,6 +23,11 @@ void ConnectionManager::NotifyStatusChanged() { ...@@ -23,6 +23,11 @@ void ConnectionManager::NotifyStatusChanged() {
observer.OnStatusChanged(); observer.OnStatusChanged();
} }
void ConnectionManager::NotifyMessageReceived(const std::string& payload) {
for (auto& observer : observer_list_)
observer.OnMessageReceived(payload);
}
std::ostream& operator<<(std::ostream& stream, std::ostream& operator<<(std::ostream& stream,
ConnectionManager::Status status) { ConnectionManager::Status status) {
switch (status) { switch (status) {
......
...@@ -32,7 +32,10 @@ class ConnectionManager { ...@@ -32,7 +32,10 @@ class ConnectionManager {
~Observer() override = default; ~Observer() override = default;
// Called the status has changed; use GetStatus() to get the new status. // Called the status has changed; use GetStatus() to get the new status.
virtual void OnStatusChanged() = 0; virtual void OnStatusChanged() {}
// Called when a payload message has been received.
virtual void OnMessageReceived(const std::string& payload) {}
}; };
ConnectionManager(const ConnectionManager&) = delete; ConnectionManager(const ConnectionManager&) = delete;
...@@ -46,6 +49,9 @@ class ConnectionManager { ...@@ -46,6 +49,9 @@ class ConnectionManager {
// the connection. // the connection.
virtual void AttemptConnection() = 0; virtual void AttemptConnection() = 0;
// Sends a message with the specified |payload|.
virtual void SendMessage(const std::string& payload) = 0;
void AddObserver(Observer* observer); void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer); void RemoveObserver(Observer* observer);
...@@ -53,6 +59,7 @@ class ConnectionManager { ...@@ -53,6 +59,7 @@ class ConnectionManager {
ConnectionManager(); ConnectionManager();
void NotifyStatusChanged(); void NotifyStatusChanged();
void NotifyMessageReceived(const std::string& payload);
private: private:
base::ObserverList<Observer> observer_list_; base::ObserverList<Observer> observer_list_;
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chromeos/components/phonehub/connection_manager_impl.h" #include "chromeos/components/phonehub/connection_manager_impl.h"
#include "base/bind_helpers.h"
#include "chromeos/services/device_sync/public/cpp/device_sync_client.h" #include "chromeos/services/device_sync/public/cpp/device_sync_client.h"
#include "chromeos/services/multidevice_setup/public/cpp/multidevice_setup_client.h" #include "chromeos/services/multidevice_setup/public/cpp/multidevice_setup_client.h"
#include "chromeos/services/secure_channel/public/cpp/client/secure_channel_client.h" #include "chromeos/services/secure_channel/public/cpp/client/secure_channel_client.h"
...@@ -73,6 +74,15 @@ void ConnectionManagerImpl::AttemptConnection() { ...@@ -73,6 +74,15 @@ void ConnectionManagerImpl::AttemptConnection() {
NotifyStatusChanged(); NotifyStatusChanged();
} }
void ConnectionManagerImpl::SendMessage(const std::string& payload) {
if (!channel_) {
PA_LOG(ERROR) << "SendMessage() failed because channel is null.";
return;
}
channel_->SendMessage(payload, base::DoNothing());
}
void ConnectionManagerImpl::OnConnectionAttemptFailure( void ConnectionManagerImpl::OnConnectionAttemptFailure(
chromeos::secure_channel::mojom::ConnectionAttemptFailureReason reason) { chromeos::secure_channel::mojom::ConnectionAttemptFailureReason reason) {
PA_LOG(WARNING) << "AttemptConnection() failed to establish connection."; PA_LOG(WARNING) << "AttemptConnection() failed to establish connection.";
...@@ -97,7 +107,7 @@ void ConnectionManagerImpl::OnDisconnected() { ...@@ -97,7 +107,7 @@ void ConnectionManagerImpl::OnDisconnected() {
} }
void ConnectionManagerImpl::OnMessageReceived(const std::string& payload) { void ConnectionManagerImpl::OnMessageReceived(const std::string& payload) {
// TODO(jimmyxgong): Handle the payload. This is just an empty stub. NotifyMessageReceived(payload);
} }
} // namespace phonehub } // namespace phonehub
......
...@@ -45,6 +45,7 @@ class ConnectionManagerImpl ...@@ -45,6 +45,7 @@ class ConnectionManagerImpl
// ConnectionManager: // ConnectionManager:
ConnectionManager::Status GetStatus() const override; ConnectionManager::Status GetStatus() const override;
void AttemptConnection() override; void AttemptConnection() override;
void SendMessage(const std::string& payload) override;
private: private:
// chromeos::secure_channel::ConnectionAttempt::Delegate: // chromeos::secure_channel::ConnectionAttempt::Delegate:
......
...@@ -26,13 +26,22 @@ class FakeObserver : public ConnectionManager::Observer { ...@@ -26,13 +26,22 @@ class FakeObserver : public ConnectionManager::Observer {
FakeObserver() = default; FakeObserver() = default;
~FakeObserver() override = default; ~FakeObserver() override = default;
size_t num_calls() const { return num_calls_; } size_t status_num_calls() const { return status_changed_num_calls_; }
size_t message_cb_num_calls() const { return message_received_num_calls_; }
const std::string& last_message() const { return last_message_; }
// ConnectionManager::Observer: // ConnectionManager::Observer:
void OnStatusChanged() override { ++num_calls_; } void OnStatusChanged() override { ++status_changed_num_calls_; }
void OnMessageReceived(const std::string& payload) override {
last_message_ = payload;
++message_received_num_calls_;
}
private: private:
size_t num_calls_ = 0; size_t status_changed_num_calls_ = 0;
size_t message_received_num_calls_ = 0;
std::string last_message_;
}; };
} // namespace } // namespace
...@@ -73,7 +82,13 @@ class ConnectionManagerImplTest : public testing::Test { ...@@ -73,7 +82,13 @@ class ConnectionManagerImplTest : public testing::Test {
return connection_manager_->GetStatus(); return connection_manager_->GetStatus();
} }
size_t GetNumObserverCalls() const { return fake_observer_.num_calls(); } size_t GetNumStatusObserverCalls() const {
return fake_observer_.status_num_calls();
}
size_t GetNumMessageReceivedObserverCalls() const {
return fake_observer_.message_cb_num_calls();
}
void CreateFakeConnectionAttempt() { void CreateFakeConnectionAttempt() {
auto fake_connection_attempt = auto fake_connection_attempt =
...@@ -101,7 +116,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfullyAttemptConnection) { ...@@ -101,7 +116,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfullyAttemptConnection) {
// Status has been updated to connecting, verify that the status observer // Status has been updated to connecting, verify that the status observer
// has been called. // has been called.
EXPECT_EQ(1u, GetNumObserverCalls()); EXPECT_EQ(1u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus());
auto fake_client_channel = auto fake_client_channel =
...@@ -110,7 +125,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfullyAttemptConnection) { ...@@ -110,7 +125,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfullyAttemptConnection) {
// Status has been updated to connected, verify that the status observer has // Status has been updated to connected, verify that the status observer has
// been called. // been called.
EXPECT_EQ(2u, GetNumObserverCalls()); EXPECT_EQ(2u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kConnected, GetStatus());
} }
...@@ -120,7 +135,7 @@ TEST_F(ConnectionManagerImplTest, FailedToAttemptConnection) { ...@@ -120,7 +135,7 @@ TEST_F(ConnectionManagerImplTest, FailedToAttemptConnection) {
// Status has been updated to connecting, verify that the status observer // Status has been updated to connecting, verify that the status observer
// has been called. // has been called.
EXPECT_EQ(1u, GetNumObserverCalls()); EXPECT_EQ(1u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus());
fake_connection_attempt_->NotifyConnectionAttemptFailure( fake_connection_attempt_->NotifyConnectionAttemptFailure(
...@@ -129,7 +144,7 @@ TEST_F(ConnectionManagerImplTest, FailedToAttemptConnection) { ...@@ -129,7 +144,7 @@ TEST_F(ConnectionManagerImplTest, FailedToAttemptConnection) {
// Status has been updated to disconnected, verify that the status observer // Status has been updated to disconnected, verify that the status observer
// has been called. // has been called.
EXPECT_EQ(2u, GetNumObserverCalls()); EXPECT_EQ(2u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus());
} }
...@@ -139,7 +154,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfulAttemptConnectionButDisconnected) { ...@@ -139,7 +154,7 @@ TEST_F(ConnectionManagerImplTest, SuccessfulAttemptConnectionButDisconnected) {
// Status has been updated to connecting, verify that the status observer // Status has been updated to connecting, verify that the status observer
// has been called. // has been called.
EXPECT_EQ(1u, GetNumObserverCalls()); EXPECT_EQ(1u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus());
auto fake_client_channel = auto fake_client_channel =
...@@ -150,17 +165,46 @@ TEST_F(ConnectionManagerImplTest, SuccessfulAttemptConnectionButDisconnected) { ...@@ -150,17 +165,46 @@ TEST_F(ConnectionManagerImplTest, SuccessfulAttemptConnectionButDisconnected) {
// Status has been updated to connected, verify that the status observer has // Status has been updated to connected, verify that the status observer has
// been called. // been called.
EXPECT_EQ(2u, GetNumObserverCalls()); EXPECT_EQ(2u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kConnected, GetStatus());
// Simulate a disconnected channel. // Simulate a disconnected channel.
fake_client_channel_raw->NotifyDisconnected(); fake_client_channel_raw->NotifyDisconnected();
// Expect status to be updated to disconnected. // Expect status to be updated to disconnected.
EXPECT_EQ(3u, GetNumObserverCalls()); EXPECT_EQ(3u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus());
} }
TEST_F(ConnectionManagerImplTest, AttemptConnectionWithMessageReceived) {
CreateFakeConnectionAttempt();
connection_manager_->AttemptConnection();
// Status has been updated to connecting, verify that the status observer
// has been called.
EXPECT_EQ(1u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnecting, GetStatus());
auto fake_client_channel =
std::make_unique<chromeos::secure_channel::FakeClientChannel>();
chromeos::secure_channel::FakeClientChannel* fake_client_channel_raw =
fake_client_channel.get();
fake_connection_attempt_->NotifyConnection(std::move(fake_client_channel));
// Status has been updated to connected, verify that the status observer has
// been called.
EXPECT_EQ(2u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kConnected, GetStatus());
// Simulate a message being sent.
const std::string expected_payload = "payload";
fake_client_channel_raw->NotifyMessageReceived(expected_payload);
// Expected MessageReceived() callback to be called.
EXPECT_EQ(1u, GetNumMessageReceivedObserverCalls());
EXPECT_EQ(expected_payload, fake_observer_.last_message());
}
TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutLocalDevice) { TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutLocalDevice) {
// Simulate a missing local device. // Simulate a missing local device.
fake_device_sync_client_.set_local_device_metadata( fake_device_sync_client_.set_local_device_metadata(
...@@ -169,7 +213,7 @@ TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutLocalDevice) { ...@@ -169,7 +213,7 @@ TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutLocalDevice) {
// Status is still disconnected since there is a missing device, verify that // Status is still disconnected since there is a missing device, verify that
// the status observer did not get called (exited early). // the status observer did not get called (exited early).
EXPECT_EQ(0u, GetNumObserverCalls()); EXPECT_EQ(0u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus());
} }
...@@ -182,7 +226,7 @@ TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutRemoteDevice) { ...@@ -182,7 +226,7 @@ TEST_F(ConnectionManagerImplTest, AttemptConnectionWithoutRemoteDevice) {
// Status is still disconnected since there is a missing device, verify that // Status is still disconnected since there is a missing device, verify that
// the status observer did not get called (exited early). // the status observer did not get called (exited early).
EXPECT_EQ(0u, GetNumObserverCalls()); EXPECT_EQ(0u, GetNumStatusObserverCalls());
EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus()); EXPECT_EQ(ConnectionManager::Status::kDisconnected, GetStatus());
} }
......
...@@ -29,5 +29,9 @@ void FakeConnectionManager::AttemptConnection() { ...@@ -29,5 +29,9 @@ void FakeConnectionManager::AttemptConnection() {
SetStatus(Status::kConnecting); SetStatus(Status::kConnecting);
} }
void FakeConnectionManager::SendMessage(const std::string& payload) {
sent_messages_.push_back(payload);
}
} // namespace phonehub } // namespace phonehub
} // namespace chromeos } // namespace chromeos
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROMEOS_COMPONENTS_PHONEHUB_FAKE_CONNECTION_MANAGER_H_ #ifndef CHROMEOS_COMPONENTS_PHONEHUB_FAKE_CONNECTION_MANAGER_H_
#define CHROMEOS_COMPONENTS_PHONEHUB_FAKE_CONNECTION_MANAGER_H_ #define CHROMEOS_COMPONENTS_PHONEHUB_FAKE_CONNECTION_MANAGER_H_
#include <vector>
#include "chromeos/components/phonehub/connection_manager.h" #include "chromeos/components/phonehub/connection_manager.h"
namespace chromeos { namespace chromeos {
...@@ -15,14 +16,21 @@ class FakeConnectionManager : public ConnectionManager { ...@@ -15,14 +16,21 @@ class FakeConnectionManager : public ConnectionManager {
FakeConnectionManager(); FakeConnectionManager();
~FakeConnectionManager() override; ~FakeConnectionManager() override;
using ConnectionManager::NotifyMessageReceived;
void SetStatus(Status status); void SetStatus(Status status);
const std::vector<std::string>& sent_messages() const {
return sent_messages_;
}
private: private:
// ConnectionManager: // ConnectionManager:
Status GetStatus() const override; Status GetStatus() const override;
void AttemptConnection() override; void AttemptConnection() override;
void SendMessage(const std::string& payload) override;
Status status_; Status status_;
std::vector<std::string> sent_messages_;
}; };
} // namespace phonehub } // namespace phonehub
......
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