File: multidevice_feature_access_manager.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (231 lines) | stat: -rw-r--r-- 9,615 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_PHONEHUB_MULTIDEVICE_FEATURE_ACCESS_MANAGER_H_
#define CHROMEOS_ASH_COMPONENTS_PHONEHUB_MULTIDEVICE_FEATURE_ACCESS_MANAGER_H_

#include <memory>
#include <ostream>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chromeos/ash/components/phonehub/combined_access_setup_operation.h"
#include "chromeos/ash/components/phonehub/feature_setup_connection_operation.h"
#include "chromeos/ash/components/phonehub/feature_setup_response_processor.h"
#include "chromeos/ash/components/phonehub/notification_access_setup_operation.h"
#include "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"

namespace ash::phonehub {

// Tracks the status of whether the user has granted permissions for the
// following features to be enabled on the host device:
// 1. Notification
// 2. Camera roll
//
// While Phone Hub can be enabled via Chrome OS, access to
// notifications requires that the user grant access via Android settings. If a
// Phone Hub connection to the phone has never succeeded, we assume that access
// has not yet been granted. If there is no active Phone Hub connection, we
// assume that the last access value seen is the current value.
//
// This class provides two methods for requesting access permissions on the
// connected Android device:
//
// AttemptNotificationSetup() is the legacy setup flow that only supports setup
// of the Notifications feature.
//
// AttemptCombinedFeatureSetup() is the new setup flow that supports the
// Notifications and/or Camera Roll features. New features requiring setup
// should be added to this method's flow.
class MultideviceFeatureAccessManager {
 public:
  // Status of a feature's access. Numerical values are stored in prefs and
  // should not be changed or reused.
  enum class AccessStatus {
    // Access has not been granted and is prohibited from being granted (e.g.,
    // if the phone is using a Work Profile when trying to use notification
    // fearture).
    kProhibited = 0,

    // Access has not been granted, but the user is free to grant access.
    kAvailableButNotGranted = 1,

    // Access has been granted by the user.
    kAccessGranted = 2
  };

  enum class AccessProhibitedReason {
    // Access is either not prohibited or is unset. Use as a safe default value.
    kUnknown = 0,
    // Access is prohibited because the phone is using a Work Profile and on
    // Android version <N.
    kWorkProfile = 1,
    // Access is prohibited because the phone is using a Work Profile, and the
    // policy managing the phone disables access.
    kDisabledByPhonePolicy = 2
  };

  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    // Called when notification access has changed; use
    // GetNotificationAccessStatus() for the new status.
    virtual void OnNotificationAccessChanged();

    // Called when camera roll access has changed; use
    // GetCameraRollAccessStatus() for the new status.
    virtual void OnCameraRollAccessChanged();

    // Called when apps access has changed; use
    // GetAppsAccessStatus() for the new status.
    virtual void OnAppsAccessChanged();

    // Called when FeatureSetupRequestSupported has changed; use
    // GetFeatureSetupRequestSupported() for the new status.
    virtual void OnFeatureSetupRequestSupportedChanged();
  };

  MultideviceFeatureAccessManager(MultideviceFeatureAccessManager&) = delete;
  MultideviceFeatureAccessManager& operator=(MultideviceFeatureAccessManager&) =
      delete;
  virtual ~MultideviceFeatureAccessManager();

  virtual AccessStatus GetNotificationAccessStatus() const = 0;

  virtual AccessStatus GetCameraRollAccessStatus() const = 0;

  virtual AccessStatus GetAppsAccessStatus() const = 0;

  // Return true if the feature status is ready for requesting access. If the
  // feature requires further access permission from phone side, we shouldn't
  // send out the access request until the feature state is fuly synced.
  virtual bool IsAccessRequestAllowed(
      multidevice_setup::mojom::Feature feature) = 0;

  virtual bool GetFeatureSetupRequestSupported() const = 0;

  // Returns the reason notification access status is prohibited. The return
  // result is valid if the current access status (from GetAccessStatus())
  // is AccessStatus::kProhibited. Otherwise, the result is undefined and should
  // not be used.
  virtual AccessProhibitedReason GetNotificationAccessProhibitedReason()
      const = 0;

  virtual bool HasMultideviceFeatureSetupUiBeenDismissed() const = 0;

  // Disables the ability to show the banner within the PhoneHub UI.
  virtual void DismissSetupRequiredUi() = 0;

  // Starts an attempt to enable the notification access. |delegate| will be
  // updated with the status of the flow as long as the operation object
  // returned by this function remains instantiated.
  //
  // To cancel an ongoing setup attempt, delete the operation. If a setup
  // attempt fails, clients can retry by calling AttemptNotificationSetup()
  // again to start a new attempt.
  //
  // If notification access has already been granted, this function returns null
  // since there is nothing to set up.
  std::unique_ptr<NotificationAccessSetupOperation> AttemptNotificationSetup(
      NotificationAccessSetupOperation::Delegate* delegate);

  // Starts an attempt to enable the access for multiple features. |delegate|
  // will be updated with the status of the flow as long as the operation object
  // returned by this function remains instantiated.
  //
  // To cancel an ongoing setup attempt, delete the operation. If a setup
  // attempt fails, clients can retry by calling AttemptCombinedFeatureSetup()
  // again to start a new attempt.
  //
  // If a requested feature's access has already been granted, or the
  // FeatureSetupRequest message is not supported on the phone, this function
  // returns null.
  std::unique_ptr<CombinedAccessSetupOperation> AttemptCombinedFeatureSetup(
      bool camera_roll,
      bool notifications,
      CombinedAccessSetupOperation::Delegate* delegate);

  std::unique_ptr<FeatureSetupConnectionOperation>
  AttemptFeatureSetupConnection(
      FeatureSetupConnectionOperation::Delegate* delegate);

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 protected:
  MultideviceFeatureAccessManager();

  void NotifyNotificationAccessChanged();
  void NotifyCameraRollAccessChanged();
  void NotifyAppsAccessChanged();
  void NotifyFeatureSetupRequestSupportedChanged();
  void SetNotificationSetupOperationStatus(
      NotificationAccessSetupOperation::Status new_status);
  void SetCombinedSetupOperationStatus(
      CombinedAccessSetupOperation::Status new_status);
  void SetFeatureSetupConnectionOperationStatus(
      FeatureSetupConnectionOperation::Status new_status);

  bool IsNotificationSetupOperationInProgress() const;
  bool IsCombinedSetupOperationInProgress() const;
  bool IsFeatureSetupConnectionOperationInProgress() const;

  virtual void OnNotificationSetupRequested();
  virtual void OnCombinedSetupRequested(bool camera_roll, bool notifications);
  virtual void OnFeatureSetupConnectionRequested();

 private:
  friend class MultideviceFeatureAccessManagerImplTest;
  friend class PhoneStatusProcessor;
  friend class FeatureSetupResponseProcessor;

  // Sets the internal AccessStatus but does not send a request for
  // a new status to the remote phone device.
  virtual void SetNotificationAccessStatusInternal(
      AccessStatus access_status,
      AccessProhibitedReason reason) = 0;
  // Sets the internal AccessStatus but does not send a request for
  // a new status to the remote phone device.
  virtual void SetCameraRollAccessStatusInternal(
      AccessStatus access_status) = 0;
  // Sets internal status tracking if feature setup request message is
  // supported by connected phone.
  virtual void SetFeatureSetupRequestSupportedInternal(bool supported) = 0;

  virtual void UpdatedFeatureSetupConnectionStatusIfNeeded();

  void OnNotificationSetupOperationDeleted(int operation_id);
  void OnCombinedSetupOperationDeleted(int operation_id);
  void OnFeatureSetupConnectionOperationDeleted(int operation_id);

  int next_operation_id_ = 0;
  base::flat_map<int,
                 raw_ptr<NotificationAccessSetupOperation, CtnExperimental>>
      id_to_notification_operation_map_;
  base::flat_map<int, raw_ptr<CombinedAccessSetupOperation, CtnExperimental>>
      id_to_combined_operation_map_;
  base::flat_map<int, raw_ptr<FeatureSetupConnectionOperation, CtnExperimental>>
      id_to_connection_operation_map_;
  base::ObserverList<Observer> observer_list_;
  base::WeakPtrFactory<MultideviceFeatureAccessManager> weak_ptr_factory_{this};
};

std::ostream& operator<<(std::ostream& stream,
                         MultideviceFeatureAccessManager::AccessStatus status);
std::ostream& operator<<(
    std::ostream& stream,
    MultideviceFeatureAccessManager::AccessProhibitedReason reason);
std::ostream& operator<<(
    std::ostream& stream,
    std::pair<MultideviceFeatureAccessManager::AccessStatus,
              MultideviceFeatureAccessManager::AccessProhibitedReason>
        status_reason);

}  // namespace ash::phonehub

#endif  // CHROMEOS_ASH_COMPONENTS_PHONEHUB_MULTIDEVICE_FEATURE_ACCESS_MANAGER_H_