Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chromeos/components/tether/host_scan_scheduler.h" | |
| 6 | |
| 7 #include "base/location.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "chromeos/components/tether/host_scanner.h" | |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 12 #include "chromeos/network/network_handler.h" | |
| 13 #include "chromeos/network/network_state.h" | |
| 14 #include "chromeos/network/network_state_handler.h" | |
| 15 #include "components/proximity_auth/logging/logging.h" | |
| 16 | |
| 17 namespace chromeos { | |
| 18 | |
| 19 namespace tether { | |
| 20 | |
| 21 namespace {} // namespace | |
|
Ryan Hansberry
2016/12/17 00:57:11
Is this needed?
Kyle Horimoto
2016/12/17 01:18:13
Done.
| |
| 22 | |
| 23 HostScanScheduler::ContextImpl::ContextImpl( | |
| 24 const content::BrowserContext* browser_context) { | |
| 25 // TODO(khorimoto): Use browser_context to get a CryptAuthDeviceManager. | |
| 26 } | |
| 27 | |
| 28 void HostScanScheduler::ContextImpl::AddObserver( | |
| 29 HostScanScheduler* host_scan_scheduler) { | |
| 30 LoginState::Get()->AddObserver(host_scan_scheduler); | |
| 31 DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( | |
| 32 host_scan_scheduler); | |
| 33 NetworkHandler::Get()->network_state_handler()->AddObserver( | |
| 34 host_scan_scheduler, FROM_HERE); | |
| 35 // TODO(khorimoto): Add listener for CryptAuthDeviceManager. | |
| 36 } | |
| 37 | |
| 38 void HostScanScheduler::ContextImpl::RemoveObserver( | |
| 39 HostScanScheduler* host_scan_scheduler) { | |
| 40 LoginState::Get()->RemoveObserver(host_scan_scheduler); | |
| 41 DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( | |
| 42 host_scan_scheduler); | |
| 43 NetworkHandler::Get()->network_state_handler()->RemoveObserver( | |
| 44 host_scan_scheduler, FROM_HERE); | |
| 45 // TODO(khorimoto): Add observer of CryptAuthDeviceManager. | |
| 46 } | |
| 47 | |
| 48 bool HostScanScheduler::ContextImpl::IsAuthenticatedUserLoggedIn() const { | |
| 49 LoginState* login_state = LoginState::Get(); | |
| 50 return login_state && login_state->IsUserLoggedIn() && | |
| 51 login_state->IsUserAuthenticated(); | |
| 52 } | |
| 53 | |
| 54 bool HostScanScheduler::ContextImpl::IsNetworkConnectedOrConnecting() const { | |
| 55 const NetworkState* network_state = | |
| 56 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | |
| 57 return network_state && (network_state->IsConnectedState() || | |
| 58 network_state->IsConnectingState()); | |
| 59 } | |
| 60 | |
| 61 bool HostScanScheduler::ContextImpl::AreTetherHostsSynced() const { | |
| 62 // TODO(khorimoto): Return CryptAuthDeviceManager->GetTetherHosts().empty(). | |
| 63 return true; | |
| 64 } | |
| 65 | |
| 66 HostScanScheduler::HostScanScheduler( | |
| 67 const content::BrowserContext* browser_context, | |
| 68 std::unique_ptr<HostScanner> host_scanner) | |
| 69 : HostScanScheduler(base::MakeUnique<ContextImpl>(browser_context), | |
| 70 std::move(host_scanner)) {} | |
| 71 | |
| 72 HostScanScheduler::~HostScanScheduler() { | |
| 73 if (initialized_) { | |
| 74 context_->RemoveObserver(this); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 HostScanScheduler::HostScanScheduler(std::unique_ptr<Context> context, | |
| 79 std::unique_ptr<HostScanner> host_scanner) | |
| 80 : context_(std::move(context)), | |
| 81 host_scanner_(std::move(host_scanner)), | |
| 82 initialized_(false) {} | |
| 83 | |
| 84 void HostScanScheduler::InitializeAutomaticScans() { | |
| 85 if (initialized_) { | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 initialized_ = true; | |
| 90 context_->AddObserver(this); | |
| 91 } | |
| 92 | |
| 93 bool HostScanScheduler::ScheduleScanNowIfPossible() { | |
| 94 if (!context_->IsAuthenticatedUserLoggedIn()) { | |
| 95 PA_LOG(INFO) << "Authenticated user not logged in; not starting scan."; | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 if (context_->IsNetworkConnectedOrConnecting()) { | |
| 100 PA_LOG(INFO) | |
| 101 << "Network is already connected/connecting; not starting scan."; | |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 if (!context_->AreTetherHostsSynced()) { | |
| 106 PA_LOG(INFO) << "No tether hosts available on account; not starting scan."; | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 host_scanner_->StartScan(); | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 void HostScanScheduler::LoggedInStateChanged() { | |
| 115 PA_LOG(INFO) << "Received login state change."; | |
| 116 ScheduleScanNowIfPossible(); | |
| 117 } | |
| 118 | |
| 119 void HostScanScheduler::SuspendDone(const base::TimeDelta& sleep_duration) { | |
| 120 PA_LOG(INFO) << "Device has resumed from sleeping."; | |
| 121 ScheduleScanNowIfPossible(); | |
| 122 } | |
| 123 | |
| 124 void HostScanScheduler::NetworkConnectionStateChanged( | |
| 125 const NetworkState* network) { | |
| 126 PA_LOG(INFO) << "Received network connection state change."; | |
| 127 ScheduleScanNowIfPossible(); | |
| 128 } | |
| 129 | |
| 130 void HostScanScheduler::OnSyncFinished( | |
| 131 cryptauth::CryptAuthDeviceManager::SyncResult sync_result, | |
| 132 cryptauth::CryptAuthDeviceManager::DeviceChangeResult | |
| 133 device_change_result) { | |
| 134 PA_LOG(INFO) << "CryptAuth device sync finished."; | |
| 135 ScheduleScanNowIfPossible(); | |
| 136 } | |
| 137 | |
| 138 } // namespace tether | |
| 139 | |
| 140 } // namespace chromeos | |
| OLD | NEW |