Commit 052227ed authored by khorimoto's avatar khorimoto Committed by Commit bot

[CrOS Tether] Create DisconnectTetheringOperation, which sends a...

[CrOS Tether] Create DisconnectTetheringOperation, which sends a DisconnectTetheringRequest to a remote tether host.

BUG=672263

Review-Url: https://codereview.chromium.org/2861603002
Cr-Commit-Position: refs/heads/master@{#468872}
parent a0f8ac5d
...@@ -24,6 +24,8 @@ static_library("tether") { ...@@ -24,6 +24,8 @@ static_library("tether") {
"connect_tethering_operation.h", "connect_tethering_operation.h",
"device_id_tether_network_guid_map.cc", "device_id_tether_network_guid_map.cc",
"device_id_tether_network_guid_map.h", "device_id_tether_network_guid_map.h",
"disconnect_tethering_operation.cc",
"disconnect_tethering_operation.h",
"host_scan_cache.cc", "host_scan_cache.cc",
"host_scan_cache.h", "host_scan_cache.h",
"host_scan_device_prioritizer.cc", "host_scan_device_prioritizer.cc",
...@@ -131,6 +133,7 @@ source_set("unit_tests") { ...@@ -131,6 +133,7 @@ source_set("unit_tests") {
"ble_connection_manager_unittest.cc", "ble_connection_manager_unittest.cc",
"ble_scanner_unittest.cc", "ble_scanner_unittest.cc",
"connect_tethering_operation_unittest.cc", "connect_tethering_operation_unittest.cc",
"disconnect_tethering_operation_unittest.cc",
"host_scan_cache_unittest.cc", "host_scan_cache_unittest.cc",
"host_scan_device_prioritizer_unittest.cc", "host_scan_device_prioritizer_unittest.cc",
"host_scan_scheduler_unittest.cc", "host_scan_scheduler_unittest.cc",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/disconnect_tethering_operation.h"
#include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/proto/tether.pb.h"
#include "components/proximity_auth/logging/logging.h"
namespace chromeos {
namespace tether {
// static
DisconnectTetheringOperation::Factory*
DisconnectTetheringOperation::Factory::factory_instance_ = nullptr;
// static
std::unique_ptr<DisconnectTetheringOperation>
DisconnectTetheringOperation::Factory::NewInstance(
const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager) {
if (!factory_instance_) {
factory_instance_ = new Factory();
}
return factory_instance_->BuildInstance(device_to_connect,
connection_manager);
}
// static
void DisconnectTetheringOperation::Factory::SetInstanceForTesting(
Factory* factory) {
factory_instance_ = factory;
}
std::unique_ptr<DisconnectTetheringOperation>
DisconnectTetheringOperation::Factory::BuildInstance(
const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager) {
return base::MakeUnique<DisconnectTetheringOperation>(device_to_connect,
connection_manager);
}
DisconnectTetheringOperation::DisconnectTetheringOperation(
const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager)
: MessageTransferOperation(
std::vector<cryptauth::RemoteDevice>{device_to_connect},
connection_manager),
has_authenticated_(false) {}
DisconnectTetheringOperation::~DisconnectTetheringOperation() {}
void DisconnectTetheringOperation::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void DisconnectTetheringOperation::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void DisconnectTetheringOperation::OnDeviceAuthenticated(
const cryptauth::RemoteDevice& remote_device) {
DCHECK(remote_devices().size() == 1u && remote_devices()[0] == remote_device);
has_authenticated_ = true;
SendMessageToDevice(remote_device, base::MakeUnique<MessageWrapper>(
DisconnectTetheringRequest()));
UnregisterDevice(remote_device);
}
void DisconnectTetheringOperation::OnOperationFinished() {
for (auto& observer : observer_list_) {
observer.OnOperationFinished(has_authenticated_);
}
}
MessageType DisconnectTetheringOperation::GetMessageTypeForConnection() {
return MessageType::DISCONNECT_TETHERING_REQUEST;
}
} // namespace tether
} // namespace chromeos
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_TETHER_DISCONNECT_TETHERING_OPERATION_H_
#define CHROMEOS_COMPONENTS_TETHER_DISCONNECT_TETHERING_OPERATION_H_
#include "base/observer_list.h"
#include "chromeos/components/tether/message_transfer_operation.h"
namespace chromeos {
namespace tether {
class BleConnectionManager;
// Operation which sends a disconnect message to a tether host.
class DisconnectTetheringOperation : public MessageTransferOperation {
public:
class Factory {
public:
static std::unique_ptr<DisconnectTetheringOperation> NewInstance(
const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager);
static void SetInstanceForTesting(Factory* factory);
protected:
virtual std::unique_ptr<DisconnectTetheringOperation> BuildInstance(
const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager);
private:
static Factory* factory_instance_;
};
class Observer {
public:
// Alerts observers when the operation has finished. |success| is true when
// the operation successfully sends the message and false otherwise.
virtual void OnOperationFinished(bool success) = 0;
};
DisconnectTetheringOperation(const cryptauth::RemoteDevice& device_to_connect,
BleConnectionManager* connection_manager);
~DisconnectTetheringOperation() override;
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
protected:
// MessageTransferOperation:
void OnDeviceAuthenticated(
const cryptauth::RemoteDevice& remote_device) override;
void OnOperationFinished() override;
MessageType GetMessageTypeForConnection() override;
private:
friend class DisconnectTetheringOperationTest;
base::ObserverList<Observer> observer_list_;
bool has_authenticated_;
DISALLOW_COPY_AND_ASSIGN(DisconnectTetheringOperation);
};
} // namespace tether
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_TETHER_DISCONNECT_TETHERING_OPERATION_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/tether/disconnect_tethering_operation.h"
#include <memory>
#include <vector>
#include "chromeos/components/tether/fake_ble_connection_manager.h"
#include "chromeos/components/tether/message_wrapper.h"
#include "chromeos/components/tether/proto/tether.pb.h"
#include "components/cryptauth/remote_device_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace tether {
namespace {
class TestObserver : public DisconnectTetheringOperation::Observer {
public:
TestObserver() : has_run_callback_(false), success_(false) {}
virtual ~TestObserver() {}
bool WasLastOperationSuccessful() {
EXPECT_TRUE(has_run_callback_);
return success_;
}
// DisconnectTetheringOperation::Observer:
void OnOperationFinished(bool success) override {
has_run_callback_ = true;
success_ = success;
}
private:
bool has_run_callback_;
bool success_;
};
std::string CreateDisconnectTetheringString() {
DisconnectTetheringRequest request;
return MessageWrapper(request).ToRawMessage();
}
} // namespace
class DisconnectTetheringOperationTest : public testing::Test {
protected:
DisconnectTetheringOperationTest()
: disconnect_tethering_request_string_(CreateDisconnectTetheringString()),
test_device_(cryptauth::GenerateTestRemoteDevices(1)[0]) {}
void SetUp() override {
fake_ble_connection_manager_ = base::MakeUnique<FakeBleConnectionManager>();
operation_ = base::WrapUnique(new DisconnectTetheringOperation(
test_device_, fake_ble_connection_manager_.get()));
test_observer_ = base::WrapUnique(new TestObserver());
operation_->AddObserver(test_observer_.get());
operation_->Initialize();
}
void SimulateDeviceAuthenticationAndVerifyMessageSent() {
operation_->OnDeviceAuthenticated(test_device_);
// Verify that the message was sent successfully.
std::vector<FakeBleConnectionManager::SentMessage>& sent_messages =
fake_ble_connection_manager_->sent_messages();
ASSERT_EQ(1u, sent_messages.size());
EXPECT_EQ(test_device_, sent_messages[0].remote_device);
EXPECT_EQ(disconnect_tethering_request_string_, sent_messages[0].message);
}
void SimulateConnectionTimeout() {
operation_->UnregisterDevice(test_device_);
}
const std::string disconnect_tethering_request_string_;
const cryptauth::RemoteDevice test_device_;
std::unique_ptr<FakeBleConnectionManager> fake_ble_connection_manager_;
std::unique_ptr<TestObserver> test_observer_;
std::unique_ptr<DisconnectTetheringOperation> operation_;
private:
DISALLOW_COPY_AND_ASSIGN(DisconnectTetheringOperationTest);
};
TEST_F(DisconnectTetheringOperationTest, TestSuccess) {
SimulateDeviceAuthenticationAndVerifyMessageSent();
EXPECT_TRUE(test_observer_->WasLastOperationSuccessful());
}
TEST_F(DisconnectTetheringOperationTest, TestFailure) {
SimulateConnectionTimeout();
EXPECT_FALSE(test_observer_->WasLastOperationSuccessful());
}
} // namespace tether
} // namespace cryptauth
...@@ -76,6 +76,7 @@ class MessageTransferOperation : public BleConnectionManager::Observer { ...@@ -76,6 +76,7 @@ class MessageTransferOperation : public BleConnectionManager::Observer {
private: private:
friend class ConnectTetheringOperationTest; friend class ConnectTetheringOperationTest;
friend class DisconnectTetheringOperationTest;
friend class HostScannerOperationTest; friend class HostScannerOperationTest;
friend class MessageTransferOperationTest; friend class MessageTransferOperationTest;
......
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