OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chromeos/components/tether/fake_active_host.h" |
| 6 |
| 7 #include "base/base64.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "components/cryptauth/remote_device.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 namespace tether { |
| 16 |
| 17 FakeActiveHost::FakeActiveHost() |
| 18 : ActiveHost(nullptr, nullptr), |
| 19 active_host_status_(ActiveHost::ActiveHostStatus::DISCONNECTED), |
| 20 active_host_device_id_(std::string()), |
| 21 wifi_network_id_(std::string()) {} |
| 22 |
| 23 FakeActiveHost::~FakeActiveHost() {} |
| 24 |
| 25 void FakeActiveHost::SetActiveHostDisconnected() { |
| 26 SetActiveHost(ActiveHost::ActiveHostStatus::DISCONNECTED, "", ""); |
| 27 } |
| 28 |
| 29 void FakeActiveHost::SetActiveHostConnecting( |
| 30 const std::string& active_host_device_id) { |
| 31 SetActiveHost(ActiveHost::ActiveHostStatus::CONNECTING, active_host_device_id, |
| 32 ""); |
| 33 } |
| 34 |
| 35 void FakeActiveHost::SetActiveHostConnected( |
| 36 const std::string& active_host_device_id, |
| 37 const std::string& wifi_network_id) { |
| 38 SetActiveHost(ActiveHost::ActiveHostStatus::CONNECTED, active_host_device_id, |
| 39 wifi_network_id); |
| 40 } |
| 41 |
| 42 void FakeActiveHost::GetActiveHost( |
| 43 const ActiveHost::ActiveHostCallback& active_host_callback) { |
| 44 std::unique_ptr<cryptauth::RemoteDevice> remote_device; |
| 45 if (GetActiveHostStatus() == ActiveHost::ActiveHostStatus::DISCONNECTED) { |
| 46 remote_device = nullptr; |
| 47 } else { |
| 48 // Convert the active host ID to a public key. |
| 49 std::string public_key; |
| 50 ASSERT_TRUE(base::Base64Decode(GetActiveHostDeviceId(), &public_key)); |
| 51 |
| 52 // Create a new RemoteDevice and set its public key. |
| 53 remote_device = base::MakeUnique<cryptauth::RemoteDevice>(); |
| 54 remote_device->public_key = public_key; |
| 55 } |
| 56 |
| 57 active_host_callback.Run(GetActiveHostStatus(), std::move(remote_device), |
| 58 GetWifiNetworkId()); |
| 59 } |
| 60 |
| 61 ActiveHost::ActiveHostStatus FakeActiveHost::GetActiveHostStatus() const { |
| 62 return active_host_status_; |
| 63 } |
| 64 |
| 65 std::string FakeActiveHost::GetActiveHostDeviceId() const { |
| 66 return active_host_device_id_; |
| 67 } |
| 68 |
| 69 std::string FakeActiveHost::GetWifiNetworkId() const { |
| 70 return wifi_network_id_; |
| 71 } |
| 72 |
| 73 void FakeActiveHost::SetActiveHost(ActiveHostStatus active_host_status, |
| 74 const std::string& active_host_device_id, |
| 75 const std::string& wifi_network_id) { |
| 76 bool status_changed = GetActiveHostStatus() != active_host_status; |
| 77 bool device_changed = GetActiveHostDeviceId() != active_host_device_id; |
| 78 bool network_id_changed = GetWifiNetworkId() != wifi_network_id; |
| 79 |
| 80 if (!status_changed && !device_changed && !network_id_changed) { |
| 81 // If nothing has changed, return early. |
| 82 return; |
| 83 } |
| 84 |
| 85 active_host_status_ = active_host_status; |
| 86 active_host_device_id_ = active_host_device_id; |
| 87 wifi_network_id_ = wifi_network_id; |
| 88 |
| 89 GetActiveHost(base::Bind(&FakeActiveHost::SendActiveHostChangedUpdate, |
| 90 base::Unretained(this))); |
| 91 } |
| 92 |
| 93 } // namespace tether |
| 94 |
| 95 } // namespace chromeos |
OLD | NEW |