Chromium Code Reviews| Index: chromeos/components/tether/keep_alive_scheduler.cc |
| diff --git a/chromeos/components/tether/keep_alive_scheduler.cc b/chromeos/components/tether/keep_alive_scheduler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..85107c39231634ff681696ea8d58a087ce8c8a18 |
| --- /dev/null |
| +++ b/chromeos/components/tether/keep_alive_scheduler.cc |
| @@ -0,0 +1,81 @@ |
| +// 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/keep_alive_scheduler.h" |
| + |
| +#include "base/bind.h" |
| + |
| +namespace chromeos { |
| + |
| +namespace tether { |
| + |
| +// static |
| +const uint32_t KeepAliveScheduler::kKeepAliveIntervalMinutes = 4; |
| + |
| +KeepAliveScheduler::KeepAliveScheduler(ActiveHost* active_host, |
| + BleConnectionManager* connection_manager) |
| + : KeepAliveScheduler(active_host, |
| + connection_manager, |
| + base::MakeUnique<base::RepeatingTimer>()) {} |
| + |
| +KeepAliveScheduler::KeepAliveScheduler(ActiveHost* active_host, |
| + BleConnectionManager* connection_manager, |
| + std::unique_ptr<base::Timer> timer) |
|
Jeremy Klein
2017/03/10 23:58:08
Should this also just be a RepeatingTimer? I know
Kyle Horimoto
2017/03/11 00:03:09
Yeah, but unfortunately MockTimer doesn't derive f
Jeremy Klein
2017/03/11 00:09:18
Gah, bummer :-/. Nah, not really. It's not a huge
|
| + : active_host_(active_host), |
| + connection_manager_(connection_manager), |
| + timer_(std::move(timer)), |
| + weak_ptr_factory_(this) { |
| + if (active_host_) { |
|
Jeremy Klein
2017/03/10 23:58:08
When would active_host_ be null here? Is that a le
Kyle Horimoto
2017/03/11 00:03:09
Done.
|
| + active_host_->AddObserver(this); |
| + } |
| +} |
| + |
| +KeepAliveScheduler::~KeepAliveScheduler() { |
| + if (active_host_) { |
| + active_host_->RemoveObserver(this); |
| + } |
| +} |
| + |
| +void KeepAliveScheduler::OnActiveHostChanged( |
| + ActiveHost::ActiveHostStatus active_host_status, |
| + std::unique_ptr<cryptauth::RemoteDevice> active_host_device, |
| + const std::string& wifi_network_id) { |
| + if (active_host_status == ActiveHost::ActiveHostStatus::DISCONNECTED) { |
| + DCHECK(!active_host_device); |
| + DCHECK(wifi_network_id.empty()); |
| + |
| + keep_alive_operation_.reset(); |
| + active_host_device_.reset(); |
| + timer_->Stop(); |
| + return; |
| + } |
| + |
| + if (active_host_status == ActiveHost::ActiveHostStatus::CONNECTED) { |
| + DCHECK(active_host_device); |
| + active_host_device_ = std::move(active_host_device); |
| + timer_->Start(FROM_HERE, |
| + base::TimeDelta::FromMinutes(kKeepAliveIntervalMinutes), |
| + base::Bind(&KeepAliveScheduler::SendKeepAliveTickle, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + SendKeepAliveTickle(); |
| + } |
| +} |
| + |
| +void KeepAliveScheduler::OnOperationFinished() { |
| + keep_alive_operation_->RemoveObserver(this); |
| + keep_alive_operation_.reset(); |
| +} |
| + |
| +void KeepAliveScheduler::SendKeepAliveTickle() { |
| + DCHECK(active_host_device_); |
| + |
| + keep_alive_operation_ = KeepAliveOperation::Factory::NewInstance( |
| + *active_host_device_, connection_manager_); |
| + keep_alive_operation_->AddObserver(this); |
| + keep_alive_operation_->Initialize(); |
| +} |
| + |
| +} // namespace tether |
| + |
| +} // namespace chromeos |