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/active_host.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/values.h" |
| 9 #include "chromeos/components/tether/pref_names.h" |
| 10 #include "chromeos/components/tether/tether_host_fetcher.h" |
| 11 #include "components/cryptauth/remote_device.h" |
| 12 #include "components/prefs/pref_registry_simple.h" |
| 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/proximity_auth/logging/logging.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 namespace tether { |
| 19 |
| 20 ActiveHost::ActiveHost(TetherHostFetcher* tether_host_fetcher, |
| 21 PrefService* pref_service) |
| 22 : tether_host_fetcher_(tether_host_fetcher), |
| 23 pref_service_(pref_service), |
| 24 weak_ptr_factory_(this) {} |
| 25 |
| 26 ActiveHost::~ActiveHost() {} |
| 27 |
| 28 // static |
| 29 void ActiveHost::RegisterPrefs(PrefRegistrySimple* registry) { |
| 30 registry->RegisterIntegerPref( |
| 31 prefs::kActiveHostStatus, |
| 32 static_cast<int>(ActiveHostStatus::DISCONNECTED)); |
| 33 registry->RegisterStringPref(prefs::kActiveHostDeviceId, ""); |
| 34 registry->RegisterStringPref(prefs::kWifiNetworkId, ""); |
| 35 } |
| 36 |
| 37 void ActiveHost::SetActiveHostDisconnected() { |
| 38 SetActiveHost(ActiveHostStatus::DISCONNECTED, "" /* active_host_device_id */, |
| 39 "" /* wifi_network_id */); |
| 40 } |
| 41 |
| 42 void ActiveHost::SetActiveHostConnecting( |
| 43 const std::string& active_host_device_id) { |
| 44 DCHECK(!active_host_device_id.empty()); |
| 45 |
| 46 SetActiveHost(ActiveHostStatus::CONNECTING, active_host_device_id, |
| 47 "" /* wifi_network_id */); |
| 48 } |
| 49 |
| 50 void ActiveHost::SetActiveHostConnected( |
| 51 const std::string& active_host_device_id, |
| 52 const std::string& wifi_network_id) { |
| 53 DCHECK(!active_host_device_id.empty()); |
| 54 DCHECK(!wifi_network_id.empty()); |
| 55 |
| 56 SetActiveHost(ActiveHostStatus::CONNECTED, active_host_device_id, |
| 57 wifi_network_id); |
| 58 } |
| 59 |
| 60 void ActiveHost::GetActiveHost(const ActiveHostCallback& active_host_callback) { |
| 61 ActiveHostStatus status = GetActiveHostStatus(); |
| 62 |
| 63 if (status == ActiveHostStatus::DISCONNECTED) { |
| 64 active_host_callback.Run(status, nullptr /* active_host */, |
| 65 "" /* wifi_network_id */); |
| 66 return; |
| 67 } |
| 68 |
| 69 std::string active_host_device_id = GetActiveHostDeviceId(); |
| 70 DCHECK(!active_host_device_id.empty()); |
| 71 |
| 72 tether_host_fetcher_->FetchTetherHost( |
| 73 active_host_device_id, |
| 74 base::Bind(&ActiveHost::OnTetherHostFetched, |
| 75 weak_ptr_factory_.GetWeakPtr(), active_host_callback)); |
| 76 } |
| 77 |
| 78 ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const { |
| 79 return static_cast<ActiveHostStatus>( |
| 80 pref_service_->GetInteger(prefs::kActiveHostStatus)); |
| 81 } |
| 82 |
| 83 const std::string ActiveHost::GetActiveHostDeviceId() const { |
| 84 return pref_service_->GetString(prefs::kActiveHostDeviceId); |
| 85 } |
| 86 |
| 87 const std::string ActiveHost::GetWifiNetworkId() const { |
| 88 return pref_service_->GetString(prefs::kWifiNetworkId); |
| 89 } |
| 90 |
| 91 void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status, |
| 92 const std::string& active_host_device_id, |
| 93 const std::string& wifi_network_id) { |
| 94 pref_service_->Set(prefs::kActiveHostStatus, |
| 95 base::Value(static_cast<int>(active_host_status))); |
| 96 pref_service_->Set(prefs::kActiveHostDeviceId, |
| 97 base::Value(active_host_device_id)); |
| 98 pref_service_->Set(prefs::kWifiNetworkId, base::Value(wifi_network_id)); |
| 99 } |
| 100 |
| 101 void ActiveHost::OnTetherHostFetched( |
| 102 const ActiveHostCallback& active_host_callback, |
| 103 std::unique_ptr<cryptauth::RemoteDevice> remote_device) { |
| 104 if (GetActiveHostDeviceId().empty() || !remote_device) { |
| 105 DCHECK(GetActiveHostStatus() == ActiveHostStatus::DISCONNECTED); |
| 106 DCHECK(GetWifiNetworkId().empty()); |
| 107 |
| 108 // If the active host became disconnected while the tether host was being |
| 109 // fetched, forward this information to the callback. |
| 110 active_host_callback.Run(ActiveHostStatus::DISCONNECTED, |
| 111 nullptr /* active_host */, |
| 112 "" /* wifi_network_id */); |
| 113 return; |
| 114 } |
| 115 |
| 116 if (GetActiveHostDeviceId() != remote_device->GetDeviceId()) { |
| 117 // If the active host has changed while the tether host was being fetched, |
| 118 // perform the fetch again. |
| 119 GetActiveHost(active_host_callback); |
| 120 return; |
| 121 } |
| 122 |
| 123 if (GetActiveHostStatus() == ActiveHostStatus::CONNECTING) { |
| 124 DCHECK(GetWifiNetworkId().empty()); |
| 125 active_host_callback.Run(ActiveHostStatus::CONNECTING, |
| 126 std::move(remote_device), |
| 127 "" /* wifi_network_id */); |
| 128 return; |
| 129 } |
| 130 |
| 131 DCHECK(GetActiveHostStatus() == ActiveHostStatus::CONNECTED); |
| 132 DCHECK(!GetWifiNetworkId().empty()); |
| 133 active_host_callback.Run(ActiveHostStatus::CONNECTED, |
| 134 std::move(remote_device), GetWifiNetworkId()); |
| 135 } |
| 136 |
| 137 } // namespace tether |
| 138 |
| 139 } // namespace chromeos |
OLD | NEW |