notification_access_setup_operation.h 4.1 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_NOTIFICATION_ACCESS_SETUP_OPERATION_H_
  5. #define ASH_COMPONENTS_PHONEHUB_NOTIFICATION_ACCESS_SETUP_OPERATION_H_
  6. #include <ostream>
  7. #include "base/callback.h"
  8. #include "base/time/time.h"
  9. #include "third_party/abseil-cpp/absl/types/optional.h"
  10. namespace ash {
  11. namespace phonehub {
  12. // Implements the notification access setup flow. This flow involves:
  13. // (1) Creating a connection to the phone if one does not already exist.
  14. // (2) Sending a message to the phone which asks it to begin the setup flow;
  15. // upon receipt of the message, the phone displays a UI which asks the
  16. // user to enable notification access for Phone Hub.
  17. // (3) Waiting for the user to complete the flow; once the flow is complete, the
  18. // phone sends a message back to this device which indicates that
  19. // notification access has been granted.
  20. //
  21. // If an instance of this class exists, the flow continues until the status
  22. // changes to a "final" status (i.e., a success or a fatal error). To cancel the
  23. // ongoing setup operation, simply delete the instance of this class.
  24. class NotificationAccessSetupOperation {
  25. public:
  26. // Note: Numerical values should not be changed because they must stay in
  27. // sync with multidevice_notification_access_setup_dialog.js, with the
  28. // exception of NOT_STARTED, which has a value of 0. Also, these values are
  29. // persisted to logs. Entries should not be renumbered and numeric values
  30. // should never be reused. If entries are added, kMaxValue should be updated.
  31. enum class Status {
  32. // Connecting to the phone in order to set up notification access.
  33. kConnecting = 1,
  34. // No connection was able to be made to the phone within the expected time
  35. // period.
  36. kTimedOutConnecting = 2,
  37. // A connection to the phone was successful, but it unexpectedly became
  38. // disconnected before the setup flow could complete.
  39. kConnectionDisconnected = 3,
  40. // A connection to the phone has succeeded, and a message has been sent to
  41. // the phone to start the notification access opt-in flow. However, the user
  42. // has not yet completed the flow phone-side.
  43. kSentMessageToPhoneAndWaitingForResponse = 4,
  44. // The user has completed the phone-side opt-in flow.
  45. kCompletedSuccessfully = 5,
  46. // The user's phone is prohibited from granting notification access (e.g.,
  47. // the user could be using a Work Profile).
  48. kProhibitedFromProvidingAccess = 6,
  49. kMaxValue = kProhibitedFromProvidingAccess
  50. };
  51. // Returns true if the provided status is the final one for this operation,
  52. // indicating either success or failure.
  53. static bool IsFinalStatus(Status status);
  54. class Delegate {
  55. public:
  56. virtual ~Delegate() = default;
  57. // Called when status of the setup flow has changed.
  58. virtual void OnNotificationStatusChange(Status new_status) = 0;
  59. };
  60. NotificationAccessSetupOperation(const NotificationAccessSetupOperation&) =
  61. delete;
  62. NotificationAccessSetupOperation& operator=(
  63. const NotificationAccessSetupOperation&) = delete;
  64. virtual ~NotificationAccessSetupOperation();
  65. private:
  66. friend class MultideviceFeatureAccessManager;
  67. NotificationAccessSetupOperation(Delegate* delegate,
  68. base::OnceClosure destructor_callback);
  69. void NotifyNotificationStatusChanged(Status new_status);
  70. absl::optional<Status> current_status_;
  71. const base::TimeTicks start_timestamp_ = base::TimeTicks::Now();
  72. Delegate* const delegate_;
  73. base::OnceClosure destructor_callback_;
  74. };
  75. std::ostream& operator<<(std::ostream& stream,
  76. NotificationAccessSetupOperation::Status status);
  77. } // namespace phonehub
  78. } // namespace ash
  79. // TODO(https://crbug.com/1164001): remove after the migration is finished.
  80. namespace chromeos {
  81. namespace phonehub {
  82. using ::ash::phonehub::NotificationAccessSetupOperation;
  83. }
  84. } // namespace chromeos
  85. #endif // ASH_COMPONENTS_PHONEHUB_NOTIFICATION_ACCESS_SETUP_OPERATION_H_