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/keep_alive_scheduler.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 |
| 9 namespace chromeos { |
| 10 |
| 11 namespace tether { |
| 12 |
| 13 // static |
| 14 const uint32_t KeepAliveScheduler::kKeepAliveIntervalMinutes = 4; |
| 15 |
| 16 KeepAliveScheduler::KeepAliveScheduler(ActiveHost* active_host, |
| 17 BleConnectionManager* connection_manager) |
| 18 : KeepAliveScheduler(active_host, |
| 19 connection_manager, |
| 20 base::MakeUnique<base::RepeatingTimer>()) {} |
| 21 |
| 22 KeepAliveScheduler::KeepAliveScheduler(ActiveHost* active_host, |
| 23 BleConnectionManager* connection_manager, |
| 24 std::unique_ptr<base::Timer> timer) |
| 25 : active_host_(active_host), |
| 26 connection_manager_(connection_manager), |
| 27 timer_(std::move(timer)), |
| 28 weak_ptr_factory_(this) { |
| 29 active_host_->AddObserver(this); |
| 30 } |
| 31 |
| 32 KeepAliveScheduler::~KeepAliveScheduler() { |
| 33 active_host_->RemoveObserver(this); |
| 34 } |
| 35 |
| 36 void KeepAliveScheduler::OnActiveHostChanged( |
| 37 ActiveHost::ActiveHostStatus active_host_status, |
| 38 std::unique_ptr<cryptauth::RemoteDevice> active_host_device, |
| 39 const std::string& wifi_network_id) { |
| 40 if (active_host_status == ActiveHost::ActiveHostStatus::DISCONNECTED) { |
| 41 DCHECK(!active_host_device); |
| 42 DCHECK(wifi_network_id.empty()); |
| 43 |
| 44 keep_alive_operation_.reset(); |
| 45 active_host_device_.reset(); |
| 46 timer_->Stop(); |
| 47 return; |
| 48 } |
| 49 |
| 50 if (active_host_status == ActiveHost::ActiveHostStatus::CONNECTED) { |
| 51 DCHECK(active_host_device); |
| 52 active_host_device_ = std::move(active_host_device); |
| 53 timer_->Start(FROM_HERE, |
| 54 base::TimeDelta::FromMinutes(kKeepAliveIntervalMinutes), |
| 55 base::Bind(&KeepAliveScheduler::SendKeepAliveTickle, |
| 56 weak_ptr_factory_.GetWeakPtr())); |
| 57 SendKeepAliveTickle(); |
| 58 } |
| 59 } |
| 60 |
| 61 void KeepAliveScheduler::OnOperationFinished() { |
| 62 keep_alive_operation_->RemoveObserver(this); |
| 63 keep_alive_operation_.reset(); |
| 64 } |
| 65 |
| 66 void KeepAliveScheduler::SendKeepAliveTickle() { |
| 67 DCHECK(active_host_device_); |
| 68 |
| 69 keep_alive_operation_ = KeepAliveOperation::Factory::NewInstance( |
| 70 *active_host_device_, connection_manager_); |
| 71 keep_alive_operation_->AddObserver(this); |
| 72 keep_alive_operation_->Initialize(); |
| 73 } |
| 74 |
| 75 } // namespace tether |
| 76 |
| 77 } // namespace chromeos |
OLD | NEW |