| 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 #ifndef CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H | 5 #ifndef CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H |
| 6 #define CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H | 6 #define CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H |
| 7 | 7 |
| 8 #include "chromeos/components/tether/host_scanner.h" |
| 8 #include "chromeos/dbus/power_manager_client.h" | 9 #include "chromeos/dbus/power_manager_client.h" |
| 9 #include "chromeos/login/login_state.h" | |
| 10 #include "chromeos/network/network_state_handler_observer.h" | 10 #include "chromeos/network/network_state_handler_observer.h" |
| 11 #include "components/cryptauth/cryptauth_device_manager.h" | |
| 12 | |
| 13 namespace content { | |
| 14 class BrowserContext; | |
| 15 } | |
| 16 | |
| 17 namespace cryptauth { | |
| 18 class CryptAuthDeviceManager; | |
| 19 } | |
| 20 | 11 |
| 21 namespace chromeos { | 12 namespace chromeos { |
| 22 | 13 |
| 23 class LoginState; | |
| 24 class PowerManagerClient; | 14 class PowerManagerClient; |
| 15 class NetworkStateHandler; |
| 25 | 16 |
| 26 namespace tether { | 17 namespace tether { |
| 27 | 18 |
| 28 class HostScanner; | 19 // Schedules scans for Tether hosts. One of three events begin a scan attempt: |
| 29 | |
| 30 // Schedules scans for tether hosts. To start a scan attempt, three conditions | |
| 31 // must be true: | |
| 32 // | 20 // |
| 33 // (1) The user has just logged in or has just resumed using the device after | 21 // (1) NetworkStateHandler requests a Tether network scan. |
| 34 // it had been sleeping/suspended. | 22 // (2) The device loses its Internet connection. |
| 35 // (2) The device does not have an Internet connection. | 23 // (3) The user has just logged in or has just resumed using the device after |
| 36 // (3) The device has synced data about other devices belonging to the user's | 24 // it had been sleeping/suspended, and the device does not have an |
| 37 // account, and at least one of those devices is capable of being a tether | 25 // Internet connection. Note: Because HostScanScheduler is created on user |
| 38 // host (i.e., it has a mobile data connection). | 26 // login, it will attempt a scan upon creation. |
| 39 // | 27 class HostScanScheduler : public PowerManagerClient::Observer, |
| 40 // When one of those conditions changes, this class checks the conditions and | |
| 41 // starts a scan automatically. Alternatively, a scan can be explicitly | |
| 42 // triggered via ScheduleScanNowIfPossible(). | |
| 43 class HostScanScheduler : public LoginState::Observer, | |
| 44 public PowerManagerClient::Observer, | |
| 45 public NetworkStateHandlerObserver, | 28 public NetworkStateHandlerObserver, |
| 46 public cryptauth::CryptAuthDeviceManager::Observer { | 29 public HostScanner::Observer { |
| 47 public: | 30 public: |
| 48 HostScanScheduler(const content::BrowserContext* browser_context, | 31 // TODO (hansberry): Inject a PowerManagerClient. |
| 49 std::unique_ptr<HostScanner> host_scanner); | 32 HostScanScheduler(NetworkStateHandler* network_state_handler, |
| 33 HostScanner* host_scanner); |
| 50 ~HostScanScheduler() override; | 34 ~HostScanScheduler() override; |
| 51 | 35 |
| 52 // Sets up listeners so that scans can be automatically triggered when | 36 // PowerManagerClient::Observer: |
| 53 // needed. | |
| 54 void InitializeAutomaticScans(); | |
| 55 | |
| 56 // Schedules a scan now if the three conditions described above are met and | |
| 57 // returns whether the scan was started. | |
| 58 bool ScheduleScanNowIfPossible(); | |
| 59 | |
| 60 // LoginState::Observer | |
| 61 void LoggedInStateChanged() override; | |
| 62 | |
| 63 // PowerManagerClient::Observer | |
| 64 void SuspendDone(const base::TimeDelta& sleep_duration) override; | 37 void SuspendDone(const base::TimeDelta& sleep_duration) override; |
| 65 | 38 |
| 66 // NetworkStateHandlerObserver | 39 // NetworkStateHandlerObserver: |
| 67 void NetworkConnectionStateChanged(const NetworkState* network) override; | 40 void DefaultNetworkChanged(const NetworkState* network) override; |
| 41 void ScanRequested() override; |
| 68 | 42 |
| 69 // cryptauth::CryptAuthDeviceManager::Observer | 43 // HostScanner::Observer: |
| 70 void OnSyncFinished(cryptauth::CryptAuthDeviceManager::SyncResult sync_result, | 44 void ScanFinished() override; |
| 71 cryptauth::CryptAuthDeviceManager::DeviceChangeResult | |
| 72 device_change_result) override; | |
| 73 | 45 |
| 74 private: | 46 private: |
| 75 friend class HostScanSchedulerTest; | 47 friend class HostScanSchedulerTest; |
| 76 | 48 |
| 77 class Delegate { | 49 void AttemptScan(); |
| 78 public: | |
| 79 virtual void AddObserver(HostScanScheduler* host_scan_scheduler) = 0; | |
| 80 virtual void RemoveObserver(HostScanScheduler* host_scan_scheduler) = 0; | |
| 81 virtual bool IsAuthenticatedUserLoggedIn() const = 0; | |
| 82 virtual bool IsNetworkConnectedOrConnecting() const = 0; | |
| 83 virtual bool AreTetherHostsSynced() const = 0; | |
| 84 }; | |
| 85 | 50 |
| 86 class DelegateImpl : public Delegate { | 51 NetworkStateHandler* network_state_handler_; |
| 87 public: | 52 HostScanner* host_scanner_; |
| 88 DelegateImpl(const content::BrowserContext* browser_context); | |
| 89 | |
| 90 void AddObserver(HostScanScheduler* host_scan_scheduler) override; | |
| 91 void RemoveObserver(HostScanScheduler* host_scan_scheduler) override; | |
| 92 bool IsAuthenticatedUserLoggedIn() const override; | |
| 93 bool IsNetworkConnectedOrConnecting() const override; | |
| 94 bool AreTetherHostsSynced() const override; | |
| 95 }; | |
| 96 | |
| 97 HostScanScheduler(std::unique_ptr<Delegate> delegate, | |
| 98 std::unique_ptr<HostScanner> host_scanner); | |
| 99 | |
| 100 std::unique_ptr<Delegate> delegate_; | |
| 101 std::unique_ptr<HostScanner> host_scanner_; | |
| 102 bool initialized_; | |
| 103 | 53 |
| 104 DISALLOW_COPY_AND_ASSIGN(HostScanScheduler); | 54 DISALLOW_COPY_AND_ASSIGN(HostScanScheduler); |
| 105 }; | 55 }; |
| 106 | 56 |
| 107 } // namespace tether | 57 } // namespace tether |
| 108 | 58 |
| 109 } // namespace chromeos | 59 } // namespace chromeos |
| 110 | 60 |
| 111 #endif // CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H | 61 #endif // CHROMEOS_COMPONENTS_TETHER_HOST_SCAN_SCHEDULER_H |
| OLD | NEW |