Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Unified Diff: chromeos/components/tether/active_host.cc

Issue 2741253002: [CrOS Tether] Create KeepAliveScheduler, a class which schedules keep-alive tickles to be sent to a… (Closed)
Patch Set: Added tests for other modified files. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chromeos/components/tether/active_host.cc
diff --git a/chromeos/components/tether/active_host.cc b/chromeos/components/tether/active_host.cc
index ddfa0fe86aefa2834ccb83484b7229d2c3302236..2e73898664cbd87ac88abe2921b2d930e1968d4e 100644
--- a/chromeos/components/tether/active_host.cc
+++ b/chromeos/components/tether/active_host.cc
@@ -5,6 +5,7 @@
#include "chromeos/components/tether/active_host.h"
#include "base/bind.h"
+#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chromeos/components/tether/pref_names.h"
#include "chromeos/components/tether/tether_host_fetcher.h"
@@ -80,22 +81,43 @@ ActiveHost::ActiveHostStatus ActiveHost::GetActiveHostStatus() const {
pref_service_->GetInteger(prefs::kActiveHostStatus));
}
-const std::string ActiveHost::GetActiveHostDeviceId() const {
+std::string ActiveHost::GetActiveHostDeviceId() const {
return pref_service_->GetString(prefs::kActiveHostDeviceId);
}
-const std::string ActiveHost::GetWifiNetworkId() const {
+std::string ActiveHost::GetWifiNetworkId() const {
return pref_service_->GetString(prefs::kWifiNetworkId);
}
+void ActiveHost::AddObserver(Observer* observer) {
+ observer_list_.AddObserver(observer);
+}
+
+void ActiveHost::RemoveObserver(Observer* observer) {
+ observer_list_.RemoveObserver(observer);
+}
+
void ActiveHost::SetActiveHost(ActiveHostStatus active_host_status,
const std::string& active_host_device_id,
const std::string& wifi_network_id) {
+ bool status_changed = GetActiveHostStatus() != active_host_status;
+ bool device_changed = GetActiveHostDeviceId() != active_host_device_id;
+ bool network_id_changed = GetWifiNetworkId() != wifi_network_id;
+
+ if (!status_changed && !device_changed && !network_id_changed) {
+ // If nothing has changed, return early.
+ return;
+ }
+
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::kWifiNetworkId, base::Value(wifi_network_id));
+
+ // Now, send an active host changed update.
+ GetActiveHost(base::Bind(&ActiveHost::SendActiveHostChangedUpdate,
+ weak_ptr_factory_.GetWeakPtr()));
}
void ActiveHost::OnTetherHostFetched(
@@ -134,6 +156,19 @@ void ActiveHost::OnTetherHostFetched(
std::move(remote_device), GetWifiNetworkId());
}
+void ActiveHost::SendActiveHostChangedUpdate(
+ ActiveHostStatus active_host_status,
+ std::unique_ptr<cryptauth::RemoteDevice> active_host,
+ const std::string& wifi_network_id) {
+ for (auto& observer : observer_list_) {
+ std::unique_ptr<cryptauth::RemoteDevice> unique_remote_device =
+ active_host ? base::MakeUnique<cryptauth::RemoteDevice>(*active_host)
+ : nullptr;
+ observer.OnActiveHostChanged(
+ active_host_status, std::move(unique_remote_device), wifi_network_id);
+ }
+}
+
} // namespace tether
} // namespace chromeos

Powered by Google App Engine
This is Rietveld 408576698