OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/components/tether/active_host.h" | 5 #include "chromeos/components/tether/active_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "chromeos/components/tether/pref_names.h" | 10 #include "chromeos/components/tether/pref_names.h" |
10 #include "chromeos/components/tether/tether_host_fetcher.h" | 11 #include "chromeos/components/tether/tether_host_fetcher.h" |
11 #include "components/cryptauth/remote_device.h" | 12 #include "components/cryptauth/remote_device.h" |
12 #include "components/prefs/pref_registry_simple.h" | 13 #include "components/prefs/pref_registry_simple.h" |
13 #include "components/prefs/pref_service.h" | 14 #include "components/prefs/pref_service.h" |
14 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
15 | 16 |
16 namespace chromeos { | 17 namespace chromeos { |
17 | 18 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 active_host_device_id, | 74 active_host_device_id, |
74 base::Bind(&ActiveHost::OnTetherHostFetched, | 75 base::Bind(&ActiveHost::OnTetherHostFetched, |
75 weak_ptr_factory_.GetWeakPtr(), active_host_callback)); | 76 weak_ptr_factory_.GetWeakPtr(), active_host_callback)); |
76 } | 77 } |
77 | 78 |
78 ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const { | 79 ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const { |
79 return static_cast<ActiveHostStatus>( | 80 return static_cast<ActiveHostStatus>( |
80 pref_service_->GetInteger(prefs::kActiveHostStatus)); | 81 pref_service_->GetInteger(prefs::kActiveHostStatus)); |
81 } | 82 } |
82 | 83 |
83 const std::string ActiveHost::GetActiveHostDeviceId() const { | 84 std::string ActiveHost::GetActiveHostDeviceId() const { |
84 return pref_service_->GetString(prefs::kActiveHostDeviceId); | 85 return pref_service_->GetString(prefs::kActiveHostDeviceId); |
85 } | 86 } |
86 | 87 |
87 const std::string ActiveHost::GetWifiNetworkId() const { | 88 std::string ActiveHost::GetWifiNetworkId() const { |
88 return pref_service_->GetString(prefs::kWifiNetworkId); | 89 return pref_service_->GetString(prefs::kWifiNetworkId); |
89 } | 90 } |
90 | 91 |
| 92 void ActiveHost::AddObserver(Observer* observer) { |
| 93 observer_list_.AddObserver(observer); |
| 94 } |
| 95 |
| 96 void ActiveHost::RemoveObserver(Observer* observer) { |
| 97 observer_list_.RemoveObserver(observer); |
| 98 } |
| 99 |
91 void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status, | 100 void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status, |
92 const std::string& active_host_device_id, | 101 const std::string& active_host_device_id, |
93 const std::string& wifi_network_id) { | 102 const std::string& wifi_network_id) { |
| 103 bool status_changed = GetActiveHostStatus() != active_host_status; |
| 104 bool device_changed = GetActiveHostDeviceId() != active_host_device_id; |
| 105 bool network_id_changed = GetWifiNetworkId() != wifi_network_id; |
| 106 |
| 107 if (!status_changed && !device_changed && !network_id_changed) { |
| 108 // If nothing has changed, return early. |
| 109 return; |
| 110 } |
| 111 |
94 pref_service_->Set(prefs::kActiveHostStatus, | 112 pref_service_->Set(prefs::kActiveHostStatus, |
95 base::Value(static_cast<int>(active_host_status))); | 113 base::Value(static_cast<int>(active_host_status))); |
96 pref_service_->Set(prefs::kActiveHostDeviceId, | 114 pref_service_->Set(prefs::kActiveHostDeviceId, |
97 base::Value(active_host_device_id)); | 115 base::Value(active_host_device_id)); |
98 pref_service_->Set(prefs::kWifiNetworkId, base::Value(wifi_network_id)); | 116 pref_service_->Set(prefs::kWifiNetworkId, base::Value(wifi_network_id)); |
| 117 |
| 118 // Now, send an active host changed update. |
| 119 GetActiveHost(base::Bind(&ActiveHost::SendActiveHostChangedUpdate, |
| 120 weak_ptr_factory_.GetWeakPtr())); |
99 } | 121 } |
100 | 122 |
101 void ActiveHost::OnTetherHostFetched( | 123 void ActiveHost::OnTetherHostFetched( |
102 const ActiveHostCallback& active_host_callback, | 124 const ActiveHostCallback& active_host_callback, |
103 std::unique_ptr<cryptauth::RemoteDevice> remote_device) { | 125 std::unique_ptr<cryptauth::RemoteDevice> remote_device) { |
104 if (GetActiveHostDeviceId().empty() || !remote_device) { | 126 if (GetActiveHostDeviceId().empty() || !remote_device) { |
105 DCHECK(GetActiveHostStatus() == ActiveHostStatus::DISCONNECTED); | 127 DCHECK(GetActiveHostStatus() == ActiveHostStatus::DISCONNECTED); |
106 DCHECK(GetWifiNetworkId().empty()); | 128 DCHECK(GetWifiNetworkId().empty()); |
107 | 129 |
108 // If the active host became disconnected while the tether host was being | 130 // If the active host became disconnected while the tether host was being |
(...skipping 18 matching lines...) Expand all Loading... |
127 "" /* wifi_network_id */); | 149 "" /* wifi_network_id */); |
128 return; | 150 return; |
129 } | 151 } |
130 | 152 |
131 DCHECK(GetActiveHostStatus() == ActiveHostStatus::CONNECTED); | 153 DCHECK(GetActiveHostStatus() == ActiveHostStatus::CONNECTED); |
132 DCHECK(!GetWifiNetworkId().empty()); | 154 DCHECK(!GetWifiNetworkId().empty()); |
133 active_host_callback.Run(ActiveHostStatus::CONNECTED, | 155 active_host_callback.Run(ActiveHostStatus::CONNECTED, |
134 std::move(remote_device), GetWifiNetworkId()); | 156 std::move(remote_device), GetWifiNetworkId()); |
135 } | 157 } |
136 | 158 |
| 159 void ActiveHost::SendActiveHostChangedUpdate( |
| 160 ActiveHostStatus active_host_status, |
| 161 std::unique_ptr<cryptauth::RemoteDevice> active_host, |
| 162 const std::string& wifi_network_id) { |
| 163 for (auto& observer : observer_list_) { |
| 164 std::unique_ptr<cryptauth::RemoteDevice> unique_remote_device = |
| 165 active_host ? base::MakeUnique<cryptauth::RemoteDevice>(*active_host) |
| 166 : nullptr; |
| 167 observer.OnActiveHostChanged( |
| 168 active_host_status, std::move(unique_remote_device), wifi_network_id); |
| 169 } |
| 170 } |
| 171 |
137 } // namespace tether | 172 } // namespace tether |
138 | 173 |
139 } // namespace chromeos | 174 } // namespace chromeos |
OLD | NEW |