combined_access_setup_operation.h 4.2 KB

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