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) | |
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
| |
25 : active_host_(active_host), | |
26 connection_manager_(connection_manager), | |
27 timer_(std::move(timer)), | |
28 weak_ptr_factory_(this) { | |
29 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.
| |
30 active_host_->AddObserver(this); | |
31 } | |
32 } | |
33 | |
34 KeepAliveScheduler::~KeepAliveScheduler() { | |
35 if (active_host_) { | |
36 active_host_->RemoveObserver(this); | |
37 } | |
38 } | |
39 | |
40 void KeepAliveScheduler::OnActiveHostChanged( | |
41 ActiveHost::ActiveHostStatus active_host_status, | |
42 std::unique_ptr<cryptauth::RemoteDevice> active_host_device, | |
43 const std::string& wifi_network_id) { | |
44 if (active_host_status == ActiveHost::ActiveHostStatus::DISCONNECTED) { | |
45 DCHECK(!active_host_device); | |
46 DCHECK(wifi_network_id.empty()); | |
47 | |
48 keep_alive_operation_.reset(); | |
49 active_host_device_.reset(); | |
50 timer_->Stop(); | |
51 return; | |
52 } | |
53 | |
54 if (active_host_status == ActiveHost::ActiveHostStatus::CONNECTED) { | |
55 DCHECK(active_host_device); | |
56 active_host_device_ = std::move(active_host_device); | |
57 timer_->Start(FROM_HERE, | |
58 base::TimeDelta::FromMinutes(kKeepAliveIntervalMinutes), | |
59 base::Bind(&KeepAliveScheduler::SendKeepAliveTickle, | |
60 weak_ptr_factory_.GetWeakPtr())); | |
61 SendKeepAliveTickle(); | |
62 } | |
63 } | |
64 | |
65 void KeepAliveScheduler::OnOperationFinished() { | |
66 keep_alive_operation_->RemoveObserver(this); | |
67 keep_alive_operation_.reset(); | |
68 } | |
69 | |
70 void KeepAliveScheduler::SendKeepAliveTickle() { | |
71 DCHECK(active_host_device_); | |
72 | |
73 keep_alive_operation_ = KeepAliveOperation::Factory::NewInstance( | |
74 *active_host_device_, connection_manager_); | |
75 keep_alive_operation_->AddObserver(this); | |
76 keep_alive_operation_->Initialize(); | |
77 } | |
78 | |
79 } // namespace tether | |
80 | |
81 } // namespace chromeos | |
OLD | NEW |