host_backend_delegate.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2018 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_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_H_
  5. #define ASH_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_H_
  6. #include "ash/components/multidevice/remote_device_ref.h"
  7. #include "base/observer_list.h"
  8. #include "third_party/abseil-cpp/absl/types/optional.h"
  9. namespace ash {
  10. namespace multidevice_setup {
  11. // Delegate for setting and receiving the MultiDevice host from the back-end.
  12. // This class is considered the source of truth for the most recent snapshot of
  13. // what the server knows about.
  14. class HostBackendDelegate {
  15. public:
  16. class Observer {
  17. public:
  18. virtual ~Observer() = default;
  19. // Invoked when the host has changed. The new host can be retrieved via
  20. // GetMultiDeviceHostFromBackend().
  21. //
  22. // Note that this function is invoked when the host changes from one device
  23. // to another, from a device to no device at all, or from no device at all
  24. // to a device. The function is not invoked when an individual property of
  25. // the host device changes (i.e., this function is only invoked when the
  26. // previous host's device ID is different fro the new host's device ID).
  27. virtual void OnHostChangedOnBackend() {}
  28. // Invoked when an attempt to set the MultiDevice host failed. The device
  29. // whose attempt failed can be retrieved via GetPendingHostRequest().
  30. virtual void OnBackendRequestFailed() {}
  31. // Invoked when the pending host request has changed. Note that this
  32. // callback is also fired when a HasPendingHostRequest() changes from true
  33. // to false.
  34. virtual void OnPendingHostRequestChange() {}
  35. };
  36. HostBackendDelegate(const HostBackendDelegate&) = delete;
  37. HostBackendDelegate& operator=(const HostBackendDelegate&) = delete;
  38. virtual ~HostBackendDelegate();
  39. // Attempts to set |host_device| as the host on the back-end. If |host_device|
  40. // is null, this function attempts to remove the active host device.
  41. //
  42. // If the request is successful, the OnHostChangedOnBackend() observer
  43. // function is invoked.
  44. //
  45. // If a the request fails (e.g., the device is offline or the server is down),
  46. // the OnBackendRequestFailed() observer function is invoked, but this
  47. // object continues to attempt the request until the request succeeds or until
  48. // AttemptToSetMultiDeviceHostOnBackend() is called with a new device.
  49. //
  50. // If there is already a pending request and this function is called with the
  51. // same request, a retry will be attempted immediately.
  52. virtual void AttemptToSetMultiDeviceHostOnBackend(
  53. const absl::optional<multidevice::RemoteDeviceRef>& host_device) = 0;
  54. // Returns whether there is a pending request to set the host on the back-end
  55. // which has not yet completed.
  56. virtual bool HasPendingHostRequest() = 0;
  57. // Returns the device which is pending to be set as the host device. If null
  58. // is returned, this means that the current host is pending removal.
  59. //
  60. // This function invokes a crash if called when HasPendingHostRequest()
  61. // returns false.
  62. virtual absl::optional<multidevice::RemoteDeviceRef> GetPendingHostRequest()
  63. const = 0;
  64. // Provides the host from the most recent device sync. If the return value is
  65. // null, there is no host set on the back-end.
  66. virtual absl::optional<multidevice::RemoteDeviceRef>
  67. GetMultiDeviceHostFromBackend() const = 0;
  68. void AddObserver(Observer* observer);
  69. void RemoveObserver(Observer* observer);
  70. protected:
  71. HostBackendDelegate();
  72. void NotifyHostChangedOnBackend();
  73. void NotifyBackendRequestFailed();
  74. void NotifyPendingHostRequestChange();
  75. private:
  76. base::ObserverList<Observer>::Unchecked observer_list_;
  77. };
  78. } // namespace multidevice_setup
  79. } // namespace ash
  80. #endif // ASH_SERVICES_MULTIDEVICE_SETUP_HOST_BACKEND_DELEGATE_H_