OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/components/tether/host_scan_scheduler.h" | 5 #include "chromeos/components/tether/host_scan_scheduler.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "chromeos/components/tether/host_scanner.h" | 10 #include "chromeos/components/tether/host_scanner.h" |
11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
12 #include "chromeos/network/network_handler.h" | 12 #include "chromeos/network/network_handler.h" |
13 #include "chromeos/network/network_state.h" | 13 #include "chromeos/network/network_state.h" |
14 #include "chromeos/network/network_state_handler.h" | 14 #include "chromeos/network/network_state_handler.h" |
15 #include "components/proximity_auth/logging/logging.h" | 15 #include "components/proximity_auth/logging/logging.h" |
16 | 16 |
17 namespace chromeos { | 17 namespace chromeos { |
18 | 18 |
19 namespace tether { | 19 namespace tether { |
20 | 20 |
21 HostScanScheduler::ContextImpl::ContextImpl( | 21 HostScanScheduler::DelegateImpl::DelegateImpl( |
22 const content::BrowserContext* browser_context) { | 22 const content::BrowserContext* browser_context) { |
23 // TODO(khorimoto): Use browser_context to get a CryptAuthDeviceManager. | 23 // TODO(khorimoto): Use browser_context to get a CryptAuthDeviceManager. |
24 } | 24 } |
25 | 25 |
26 void HostScanScheduler::ContextImpl::AddObserver( | 26 void HostScanScheduler::DelegateImpl::AddObserver( |
27 HostScanScheduler* host_scan_scheduler) { | 27 HostScanScheduler* host_scan_scheduler) { |
28 LoginState::Get()->AddObserver(host_scan_scheduler); | 28 LoginState::Get()->AddObserver(host_scan_scheduler); |
29 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | 29 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
30 host_scan_scheduler); | 30 host_scan_scheduler); |
31 NetworkHandler::Get()->network_state_handler()->AddObserver( | 31 NetworkHandler::Get()->network_state_handler()->AddObserver( |
32 host_scan_scheduler, FROM_HERE); | 32 host_scan_scheduler, FROM_HERE); |
33 // TODO(khorimoto): Add listener for CryptAuthDeviceManager. | 33 // TODO(khorimoto): Add listener for CryptAuthDeviceManager. |
34 } | 34 } |
35 | 35 |
36 void HostScanScheduler::ContextImpl::RemoveObserver( | 36 void HostScanScheduler::DelegateImpl::RemoveObserver( |
37 HostScanScheduler* host_scan_scheduler) { | 37 HostScanScheduler* host_scan_scheduler) { |
38 LoginState::Get()->RemoveObserver(host_scan_scheduler); | 38 LoginState::Get()->RemoveObserver(host_scan_scheduler); |
39 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | 39 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
40 host_scan_scheduler); | 40 host_scan_scheduler); |
41 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | 41 NetworkHandler::Get()->network_state_handler()->RemoveObserver( |
42 host_scan_scheduler, FROM_HERE); | 42 host_scan_scheduler, FROM_HERE); |
43 // TODO(khorimoto): Add observer of CryptAuthDeviceManager. | 43 // TODO(khorimoto): Add observer of CryptAuthDeviceManager. |
44 } | 44 } |
45 | 45 |
46 bool HostScanScheduler::ContextImpl::IsAuthenticatedUserLoggedIn() const { | 46 bool HostScanScheduler::DelegateImpl::IsAuthenticatedUserLoggedIn() const { |
47 LoginState* login_state = LoginState::Get(); | 47 LoginState* login_state = LoginState::Get(); |
48 return login_state && login_state->IsUserLoggedIn() && | 48 return login_state && login_state->IsUserLoggedIn() && |
49 login_state->IsUserAuthenticated(); | 49 login_state->IsUserAuthenticated(); |
50 } | 50 } |
51 | 51 |
52 bool HostScanScheduler::ContextImpl::IsNetworkConnectedOrConnecting() const { | 52 bool HostScanScheduler::DelegateImpl::IsNetworkConnectedOrConnecting() const { |
53 const NetworkState* network_state = | 53 const NetworkState* network_state = |
54 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | 54 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); |
55 return network_state && (network_state->IsConnectedState() || | 55 return network_state && (network_state->IsConnectedState() || |
56 network_state->IsConnectingState()); | 56 network_state->IsConnectingState()); |
57 } | 57 } |
58 | 58 |
59 bool HostScanScheduler::ContextImpl::AreTetherHostsSynced() const { | 59 bool HostScanScheduler::DelegateImpl::AreTetherHostsSynced() const { |
60 // TODO(khorimoto): Return CryptAuthDeviceManager->GetTetherHosts().empty(). | 60 // TODO(khorimoto): Return CryptAuthDeviceManager->GetTetherHosts().empty(). |
61 return true; | 61 return true; |
62 } | 62 } |
63 | 63 |
64 HostScanScheduler::HostScanScheduler( | 64 HostScanScheduler::HostScanScheduler( |
65 const content::BrowserContext* browser_context, | 65 const content::BrowserContext* browser_context, |
66 std::unique_ptr<HostScanner> host_scanner) | 66 std::unique_ptr<HostScanner> host_scanner) |
67 : HostScanScheduler(base::MakeUnique<ContextImpl>(browser_context), | 67 : HostScanScheduler(base::MakeUnique<DelegateImpl>(browser_context), |
68 std::move(host_scanner)) {} | 68 std::move(host_scanner)) {} |
69 | 69 |
70 HostScanScheduler::~HostScanScheduler() { | 70 HostScanScheduler::~HostScanScheduler() { |
71 if (initialized_) { | 71 if (initialized_) { |
72 context_->RemoveObserver(this); | 72 delegate_->RemoveObserver(this); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 HostScanScheduler::HostScanScheduler(std::unique_ptr<Context> context, | 76 HostScanScheduler::HostScanScheduler(std::unique_ptr<Delegate> delegate, |
77 std::unique_ptr<HostScanner> host_scanner) | 77 std::unique_ptr<HostScanner> host_scanner) |
78 : context_(std::move(context)), | 78 : delegate_(std::move(delegate)), |
79 host_scanner_(std::move(host_scanner)), | 79 host_scanner_(std::move(host_scanner)), |
80 initialized_(false) {} | 80 initialized_(false) {} |
81 | 81 |
82 void HostScanScheduler::InitializeAutomaticScans() { | 82 void HostScanScheduler::InitializeAutomaticScans() { |
83 if (initialized_) { | 83 if (initialized_) { |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 initialized_ = true; | 87 initialized_ = true; |
88 context_->AddObserver(this); | 88 delegate_->AddObserver(this); |
89 } | 89 } |
90 | 90 |
91 bool HostScanScheduler::ScheduleScanNowIfPossible() { | 91 bool HostScanScheduler::ScheduleScanNowIfPossible() { |
92 if (!context_->IsAuthenticatedUserLoggedIn()) { | 92 if (!delegate_->IsAuthenticatedUserLoggedIn()) { |
93 PA_LOG(INFO) << "Authenticated user not logged in; not starting scan."; | 93 PA_LOG(INFO) << "Authenticated user not logged in; not starting scan."; |
94 return false; | 94 return false; |
95 } | 95 } |
96 | 96 |
97 if (context_->IsNetworkConnectedOrConnecting()) { | 97 if (delegate_->IsNetworkConnectedOrConnecting()) { |
98 PA_LOG(INFO) | 98 PA_LOG(INFO) |
99 << "Network is already connected/connecting; not starting scan."; | 99 << "Network is already connected/connecting; not starting scan."; |
100 return false; | 100 return false; |
101 } | 101 } |
102 | 102 |
103 if (!context_->AreTetherHostsSynced()) { | 103 if (!delegate_->AreTetherHostsSynced()) { |
104 PA_LOG(INFO) << "No tether hosts available on account; not starting scan."; | 104 PA_LOG(INFO) << "No tether hosts available on account; not starting scan."; |
105 return false; | 105 return false; |
106 } | 106 } |
107 | 107 |
108 host_scanner_->StartScan(); | 108 host_scanner_->StartScan(); |
109 return true; | 109 return true; |
110 } | 110 } |
111 | 111 |
112 void HostScanScheduler::LoggedInStateChanged() { | 112 void HostScanScheduler::LoggedInStateChanged() { |
113 PA_LOG(INFO) << "Received login state change."; | 113 PA_LOG(INFO) << "Received login state change."; |
(...skipping 15 matching lines...) Expand all Loading... |
129 cryptauth::CryptAuthDeviceManager::SyncResult sync_result, | 129 cryptauth::CryptAuthDeviceManager::SyncResult sync_result, |
130 cryptauth::CryptAuthDeviceManager::DeviceChangeResult | 130 cryptauth::CryptAuthDeviceManager::DeviceChangeResult |
131 device_change_result) { | 131 device_change_result) { |
132 PA_LOG(INFO) << "CryptAuth device sync finished."; | 132 PA_LOG(INFO) << "CryptAuth device sync finished."; |
133 ScheduleScanNowIfPossible(); | 133 ScheduleScanNowIfPossible(); |
134 } | 134 } |
135 | 135 |
136 } // namespace tether | 136 } // namespace tether |
137 | 137 |
138 } // namespace chromeos | 138 } // namespace chromeos |
OLD | NEW |