Chromium Code Reviews| Index: chromeos/components/tether/active_host.cc |
| diff --git a/chromeos/components/tether/active_host.cc b/chromeos/components/tether/active_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c54dcc93469f7e1ade1bd4e8e9f5adc834cf54ca |
| --- /dev/null |
| +++ b/chromeos/components/tether/active_host.cc |
| @@ -0,0 +1,136 @@ |
| +// 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/active_host.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/values.h" |
| +#include "chromeos/components/tether/pref_names.h" |
| +#include "chromeos/components/tether/tether_host_fetcher.h" |
| +#include "components/cryptauth/remote_device.h" |
| +#include "components/prefs/pref_registry_simple.h" |
| +#include "components/prefs/pref_service.h" |
| +#include "components/proximity_auth/logging/logging.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace tether { |
| + |
| +ActiveHost::ActiveHost(TetherHostFetcher* tether_host_fetcher, |
| + PrefService* pref_service) |
| + : tether_host_fetcher_(tether_host_fetcher), |
| + pref_service_(pref_service), |
| + weak_ptr_factory_(this) {} |
| + |
| +ActiveHost::~ActiveHost() {} |
| + |
| +// static |
| +void ActiveHost::RegisterPrefs(PrefRegistrySimple* registry) { |
| + registry->RegisterIntegerPref( |
| + prefs::kActiveHostStatus, |
| + static_cast<int>(ActiveHostStatus::DISCONNECTED)); |
| + registry->RegisterStringPref(prefs::kActiveHostDeviceId, ""); |
| + registry->RegisterStringPref(prefs::kTetherNetworkSsid, ""); |
| +} |
| + |
| +void ActiveHost::SetActiveHostDisconnected() { |
| + SetActiveHost(ActiveHostStatus::DISCONNECTED, "" /* active_host_device_id */, |
| + "" /* tether_network_ssid */); |
| +} |
| + |
| +void ActiveHost::SetActiveHostConnecting( |
| + const std::string& active_host_device_id) { |
| + DCHECK(!active_host_device_id.empty()); |
| + |
| + SetActiveHost(ActiveHostStatus::CONNECTING, active_host_device_id, |
| + "" /* tether_network_ssid */); |
| +} |
| + |
| +void ActiveHost::SetActiveHostConnected( |
| + const std::string& active_host_device_id, |
| + const std::string& tether_network_ssid) { |
| + DCHECK(!active_host_device_id.empty()); |
| + DCHECK(!tether_network_ssid.empty()); |
| + |
| + SetActiveHost(ActiveHostStatus::CONNECTED, active_host_device_id, |
| + tether_network_ssid); |
| +} |
| + |
| +void ActiveHost::GetActiveHost(const ActiveHostCallback& active_host_callback) { |
| + ActiveHostStatus status = GetActiveHostStatus(); |
| + |
| + if (status == ActiveHostStatus::DISCONNECTED) { |
| + active_host_callback.Run(status, nullptr, ""); |
| + return; |
| + } |
| + |
| + std::string active_host_device_id = GetActiveHostDeviceId(); |
| + DCHECK(!active_host_device_id.empty()); |
| + |
| + tether_host_fetcher_->FetchTetherHost( |
| + active_host_device_id, |
| + base::Bind(&ActiveHost::OnTetherHostFetched, |
| + weak_ptr_factory_.GetWeakPtr(), active_host_callback)); |
| +} |
| + |
| +ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const { |
| + return static_cast<ActiveHostStatus>( |
| + pref_service_->GetInteger(prefs::kActiveHostStatus)); |
| +} |
| + |
| +const std::string ActiveHost::GetActiveHostDeviceId() const { |
| + return pref_service_->GetString(prefs::kActiveHostDeviceId); |
| +} |
| + |
| +const std::string ActiveHost::GetTetherNetworkSsid() const { |
| + return pref_service_->GetString(prefs::kTetherNetworkSsid); |
| +} |
| + |
| +void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status, |
| + const std::string& active_host_device_id, |
| + const std::string& tether_network_ssid) { |
| + pref_service_->Set(prefs::kActiveHostStatus, |
| + base::Value(static_cast<int>(active_host_status))); |
| + pref_service_->Set(prefs::kActiveHostDeviceId, |
| + base::Value(active_host_device_id)); |
| + pref_service_->Set(prefs::kTetherNetworkSsid, |
| + base::Value(tether_network_ssid)); |
| +} |
| + |
| +void ActiveHost::OnTetherHostFetched( |
| + const ActiveHostCallback& active_host_callback, |
| + std::unique_ptr<cryptauth::RemoteDevice> remote_device) { |
| + if (GetActiveHostDeviceId().empty() || !remote_device) { |
| + DCHECK(GetActiveHostStatus() == ActiveHostStatus::DISCONNECTED); |
| + DCHECK(GetTetherNetworkSsid().empty()); |
| + |
| + // If the active host became disconnected while the tether host was being |
| + // fetched, forward this information to the callback. |
| + active_host_callback.Run(ActiveHostStatus::DISCONNECTED, nullptr, ""); |
| + return; |
| + } |
| + |
| + if (GetActiveHostDeviceId() != remote_device->GetDeviceId()) { |
| + // If the active host has changed while the tether host was being fetched, |
| + // perform the fetch again. |
| + GetActiveHost(active_host_callback); |
| + return; |
| + } |
| + |
| + if (GetActiveHostStatus() == ActiveHostStatus::CONNECTING) { |
| + DCHECK(GetTetherNetworkSsid().empty()); |
|
Ryan Hansberry
2017/03/09 17:20:48
It's possible that the network_id is known (non-em
Kyle Horimoto
2017/03/09 18:42:00
As discussed offline, this is an implementation de
|
| + active_host_callback.Run(ActiveHostStatus::CONNECTING, |
| + std::move(remote_device), ""); |
|
Ryan Hansberry
2017/03/09 17:20:48
add a /* argument comment */
Kyle Horimoto
2017/03/09 18:42:00
Done.
|
| + return; |
| + } |
| + |
| + DCHECK(GetActiveHostStatus() == ActiveHostStatus::CONNECTED); |
| + DCHECK(!GetTetherNetworkSsid().empty()); |
| + active_host_callback.Run(ActiveHostStatus::CONNECTED, |
| + std::move(remote_device), GetTetherNetworkSsid()); |
| +} |
| + |
| +} // namespace tether |
| + |
| +} // namespace chromeos |