tether_controller.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2020 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. #ifndef ASH_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_H_
  5. #define ASH_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_H_
  6. #include <ostream>
  7. #include "base/observer_list.h"
  8. #include "base/observer_list_types.h"
  9. namespace ash {
  10. namespace phonehub {
  11. // Exposes Instant Tethering functionality to Phone Hub.
  12. class TetherController {
  13. public:
  14. // Note: Numerical values should stay in sync with JS enums within the debug
  15. // UI at //chrome/browser/resources/chromeos/multidevice_internals/types.js.
  16. enum class Status {
  17. // The device is ineligible for Instant Tethering, potentially due to the
  18. // flag being disabled (on Chrome OS or on the phone) or due to an
  19. // enterprise policy.
  20. kIneligibleForFeature = 0,
  21. // Instant Tethering is available for use, but currently a connection is
  22. // unavailable. There are a variety of reasons why this may be the case:
  23. // the feature could have been disabled in settings, or the phone may not
  24. // have Google Play Services notifications enabled, which are required
  25. // for the feature. Note that the phone having no reception or no SIM card
  26. // does not count in this case; instead, kNoReception is used.
  27. kConnectionUnavailable = 1,
  28. // It is possible to connect, but no connection is active or in progress.
  29. // This state can occur if a previously-active connection has been
  30. // disconnected.
  31. kConnectionAvailable = 2,
  32. // Initiating an Instant Tethering connection.
  33. kConnecting = 3,
  34. // Connected via Instant Tethering.
  35. kConnected = 4,
  36. // The phone has no reception (including no SIM).
  37. kNoReception = 5,
  38. };
  39. class Observer : public base::CheckedObserver {
  40. public:
  41. ~Observer() override = default;
  42. // Called the status has changed; use GetStatus() to get the new status.
  43. virtual void OnTetherStatusChanged() = 0;
  44. // Called when AttemptConnection() is called, a scan for a tether network is
  45. // requested, but no tether network was found.
  46. virtual void OnAttemptConnectionScanFailed() {}
  47. };
  48. TetherController(const TetherController&) = delete;
  49. TetherController& operator=(const TetherController&) = delete;
  50. virtual ~TetherController();
  51. virtual Status GetStatus() const = 0;
  52. // Attempts to find an available Instant Tethering connection. For a
  53. // connection to be available, the phone must be nearby, have reception, and
  54. // have Google Play Services notifications enabled. This function is a no-op
  55. // if the state is not kConnectionUnavailable.
  56. virtual void ScanForAvailableConnection() = 0;
  57. // Initiates an Instant Tethering connection. This function is a no-op if the
  58. // state is not one of kConnectionUnavailable or kConnectionAvailable.
  59. virtual void AttemptConnection() = 0;
  60. // Disconnects from an active Instant Tethering connection or connection
  61. // attempt. This function is a no-op if the state is not one of kConnecting or
  62. // kConnected.
  63. virtual void Disconnect() = 0;
  64. void AddObserver(Observer* observer);
  65. void RemoveObserver(Observer* observer);
  66. protected:
  67. TetherController();
  68. void NotifyStatusChanged();
  69. void NotifyAttemptConnectionScanFailed();
  70. private:
  71. base::ObserverList<Observer> observer_list_;
  72. };
  73. std::ostream& operator<<(std::ostream& stream, TetherController::Status status);
  74. } // namespace phonehub
  75. } // namespace ash
  76. // TODO(https://crbug.com/1164001): remove after the migration is finished.
  77. namespace chromeos {
  78. namespace phonehub {
  79. using ::ash::phonehub::TetherController;
  80. }
  81. } // namespace chromeos
  82. #endif // ASH_COMPONENTS_PHONEHUB_TETHER_CONTROLLER_H_