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::kTetherNetworkSsid, ""); | |
35 } | |
36 | |
37 void ActiveHost::SetActiveHostDisconnected() { | |
38 SetActiveHost(ActiveHostStatus::DISCONNECTED, "" /* active_host_device_id */, | |
39 "" /* tether_network_ssid */); | |
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 "" /* tether_network_ssid */); | |
48 } | |
49 | |
50 void ActiveHost::SetActiveHostConnected( | |
51 const std::string& active_host_device_id, | |
52 const std::string& tether_network_ssid) { | |
53 DCHECK(!active_host_device_id.empty()); | |
54 DCHECK(!tether_network_ssid.empty()); | |
55 | |
56 SetActiveHost(ActiveHostStatus::CONNECTED, active_host_device_id, | |
57 tether_network_ssid); | |
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, ""); | |
65 return; | |
66 } | |
67 | |
68 std::string active_host_device_id = GetActiveHostDeviceId(); | |
69 DCHECK(!active_host_device_id.empty()); | |
70 | |
71 tether_host_fetcher_->FetchTetherHost( | |
72 active_host_device_id, | |
73 base::Bind(&ActiveHost::OnTetherHostFetched, | |
74 weak_ptr_factory_.GetWeakPtr(), active_host_callback)); | |
75 } | |
76 | |
77 ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const { | |
78 return static_cast<ActiveHostStatus>( | |
79 pref_service_->GetInteger(prefs::kActiveHostStatus)); | |
80 } | |
81 | |
82 const std::string ActiveHost::GetActiveHostDeviceId() const { | |
83 return pref_service_->GetString(prefs::kActiveHostDeviceId); | |
84 } | |
85 | |
86 const std::string ActiveHost::GetTetherNetworkSsid() const { | |
87 return pref_service_->GetString(prefs::kTetherNetworkSsid); | |
88 } | |
89 | |
90 void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status, | |
91 const std::string& active_host_device_id, | |
92 const std::string& tether_network_ssid) { | |
93 pref_service_->Set(prefs::kActiveHostStatus, | |
94 base::Value(static_cast<int>(active_host_status))); | |
95 pref_service_->Set(prefs::kActiveHostDeviceId, | |
96 base::Value(active_host_device_id)); | |
97 pref_service_->Set(prefs::kTetherNetworkSsid, | |
98 base::Value(tether_network_ssid)); | |
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(GetTetherNetworkSsid().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, nullptr, ""); | |
111 return; | |
112 } | |
113 | |
114 if (GetActiveHostDeviceId() != remote_device->GetDeviceId()) { | |
115 // If the active host has changed while the tether host was being fetched, | |
116 // perform the fetch again. | |
117 GetActiveHost(active_host_callback); | |
118 return; | |
119 } | |
120 | |
121 if (GetActiveHostStatus() == ActiveHostStatus::CONNECTING) { | |
122 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
| |
123 active_host_callback.Run(ActiveHostStatus::CONNECTING, | |
124 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.
| |
125 return; | |
126 } | |
127 | |
128 DCHECK(GetActiveHostStatus() == ActiveHostStatus::CONNECTED); | |
129 DCHECK(!GetTetherNetworkSsid().empty()); | |
130 active_host_callback.Run(ActiveHostStatus::CONNECTED, | |
131 std::move(remote_device), GetTetherNetworkSsid()); | |
132 } | |
133 | |
134 } // namespace tether | |
135 | |
136 } // namespace chromeos | |
OLD | NEW |