File: multidevice_feature_access_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (287 lines) | stat: -rw-r--r-- 9,843 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// 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.

#include "chromeos/ash/components/phonehub/multidevice_feature_access_manager.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/components/phonehub/feature_setup_connection_operation.h"

namespace ash::phonehub {

MultideviceFeatureAccessManager::MultideviceFeatureAccessManager() = default;

MultideviceFeatureAccessManager::~MultideviceFeatureAccessManager() = default;

std::unique_ptr<NotificationAccessSetupOperation>
MultideviceFeatureAccessManager::AttemptNotificationSetup(
    NotificationAccessSetupOperation::Delegate* delegate) {
  // Should only be able to start the setup process if notification access is
  // available but not yet granted.
  // Legacy setup flow used when FeatureSetupRequest is not supported.
  if (GetNotificationAccessStatus() != AccessStatus::kAvailableButNotGranted)
    return nullptr;

  int operation_id = next_operation_id_;
  ++next_operation_id_;

  auto operation = base::WrapUnique(new NotificationAccessSetupOperation(
      delegate,
      base::BindOnce(
          &MultideviceFeatureAccessManager::OnNotificationSetupOperationDeleted,
          weak_ptr_factory_.GetWeakPtr(), operation_id)));
  id_to_notification_operation_map_.emplace(operation_id, operation.get());

  OnNotificationSetupRequested();
  return operation;
}

std::unique_ptr<CombinedAccessSetupOperation>
MultideviceFeatureAccessManager::AttemptCombinedFeatureSetup(
    bool camera_roll,
    bool notifications,
    CombinedAccessSetupOperation::Delegate* delegate) {
  // New setup flow for combined Camera Roll and/or Notifications setup using
  // FeatureSetupRequest message type.
  if (!GetFeatureSetupRequestSupported()) {
    return nullptr;
  }
  if (GetCameraRollAccessStatus() != AccessStatus::kAvailableButNotGranted &&
      camera_roll) {
    return nullptr;
  }
  if (GetNotificationAccessStatus() != AccessStatus::kAvailableButNotGranted &&
      notifications) {
    return nullptr;
  }

  int operation_id = next_operation_id_;
  ++next_operation_id_;

  auto operation = base::WrapUnique(new CombinedAccessSetupOperation(
      delegate,
      base::BindOnce(
          &MultideviceFeatureAccessManager::OnCombinedSetupOperationDeleted,
          weak_ptr_factory_.GetWeakPtr(), operation_id)));
  id_to_combined_operation_map_.emplace(operation_id, operation.get());

  OnCombinedSetupRequested(camera_roll, notifications);
  return operation;
}

std::unique_ptr<FeatureSetupConnectionOperation>
MultideviceFeatureAccessManager::AttemptFeatureSetupConnection(
    FeatureSetupConnectionOperation::Delegate* delegate) {
  int operation_id = next_operation_id_;
  ++next_operation_id_;

  auto operation = base::WrapUnique(new FeatureSetupConnectionOperation(
      delegate, base::BindOnce(&MultideviceFeatureAccessManager::
                                   OnFeatureSetupConnectionOperationDeleted,
                               weak_ptr_factory_.GetWeakPtr(), operation_id)));
  id_to_connection_operation_map_.emplace(operation_id, operation.get());

  OnFeatureSetupConnectionRequested();
  return operation;
}

void MultideviceFeatureAccessManager::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void MultideviceFeatureAccessManager::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void MultideviceFeatureAccessManager::NotifyNotificationAccessChanged() {
  for (auto& observer : observer_list_)
    observer.OnNotificationAccessChanged();
}

void MultideviceFeatureAccessManager::NotifyCameraRollAccessChanged() {
  for (auto& observer : observer_list_)
    observer.OnCameraRollAccessChanged();
}

void MultideviceFeatureAccessManager::NotifyAppsAccessChanged() {
  for (auto& observer : observer_list_)
    observer.OnAppsAccessChanged();
}

void MultideviceFeatureAccessManager::
    NotifyFeatureSetupRequestSupportedChanged() {
  for (auto& observer : observer_list_)
    observer.OnFeatureSetupRequestSupportedChanged();
}

void MultideviceFeatureAccessManager::SetNotificationSetupOperationStatus(
    NotificationAccessSetupOperation::Status new_status) {
  DCHECK(IsNotificationSetupOperationInProgress());

  PA_LOG(INFO) << "Notification access setup flow - new status: " << new_status;

  for (auto& it : id_to_notification_operation_map_)
    it.second->NotifyNotificationStatusChanged(new_status);

  if (NotificationAccessSetupOperation::IsFinalStatus(new_status))
    id_to_notification_operation_map_.clear();
}

bool MultideviceFeatureAccessManager::IsNotificationSetupOperationInProgress()
    const {
  return !id_to_notification_operation_map_.empty();
}

void MultideviceFeatureAccessManager::OnNotificationSetupOperationDeleted(
    int operation_id) {
  auto it = id_to_notification_operation_map_.find(operation_id);
  if (it == id_to_notification_operation_map_.end())
    return;

  id_to_notification_operation_map_.erase(it);

  if (id_to_notification_operation_map_.empty())
    PA_LOG(INFO) << "Notification access setup operation has ended.";
}

void MultideviceFeatureAccessManager::SetCombinedSetupOperationStatus(
    CombinedAccessSetupOperation::Status new_status) {
  DCHECK(IsCombinedSetupOperationInProgress());

  PA_LOG(INFO) << "Combined access setup flow - new status: " << new_status;

  for (auto& it : id_to_combined_operation_map_)
    it.second->NotifyCombinedStatusChanged(new_status);

  if (CombinedAccessSetupOperation::IsFinalStatus(new_status))
    id_to_combined_operation_map_.clear();
}

void MultideviceFeatureAccessManager::SetFeatureSetupConnectionOperationStatus(
    FeatureSetupConnectionOperation::Status new_status) {
  DCHECK(IsFeatureSetupConnectionOperationInProgress());

  PA_LOG(INFO) << "Feature setup connection status - new status: "
               << new_status;

  for (auto& it : id_to_connection_operation_map_)
    it.second->NotifyFeatureSetupConnectionStatusChanged(new_status);

  if (FeatureSetupConnectionOperation::IsFinalStatus(new_status))
    id_to_connection_operation_map_.clear();
}

bool MultideviceFeatureAccessManager::IsCombinedSetupOperationInProgress()
    const {
  return !id_to_combined_operation_map_.empty();
}

bool MultideviceFeatureAccessManager::
    IsFeatureSetupConnectionOperationInProgress() const {
  return !id_to_connection_operation_map_.empty();
}

void MultideviceFeatureAccessManager::OnNotificationSetupRequested() {}

void MultideviceFeatureAccessManager::OnCombinedSetupRequested(
    bool camera_roll,
    bool notifications) {}

void MultideviceFeatureAccessManager::OnFeatureSetupConnectionRequested() {}

void MultideviceFeatureAccessManager::
    UpdatedFeatureSetupConnectionStatusIfNeeded() {}

void MultideviceFeatureAccessManager::OnCombinedSetupOperationDeleted(
    int operation_id) {
  auto it = id_to_combined_operation_map_.find(operation_id);
  if (it == id_to_combined_operation_map_.end())
    return;

  id_to_combined_operation_map_.erase(it);

  if (id_to_combined_operation_map_.empty())
    PA_LOG(INFO) << "Combined access setup operation has ended.";
}

void MultideviceFeatureAccessManager::OnFeatureSetupConnectionOperationDeleted(
    int operation_id) {
  auto it = id_to_connection_operation_map_.find(operation_id);
  if (it == id_to_connection_operation_map_.end())
    return;

  id_to_connection_operation_map_.erase(it);

  if (id_to_connection_operation_map_.empty())
    PA_LOG(INFO) << "Feature setup connection operation has ended.";
}

void MultideviceFeatureAccessManager::Observer::OnNotificationAccessChanged() {
  // Optional method, inherit class doesn't have to implement this
}

void MultideviceFeatureAccessManager::Observer::OnCameraRollAccessChanged() {
  // Optional method, inherit class doesn't have to implement this
}

void MultideviceFeatureAccessManager::Observer::OnAppsAccessChanged() {
  // Optional method, inherit class doesn't have to implement this
}

void MultideviceFeatureAccessManager::Observer::
    OnFeatureSetupRequestSupportedChanged() {
  // Optional method, inherit class doesn't have to implement this
}

std::ostream& operator<<(std::ostream& stream,
                         MultideviceFeatureAccessManager::AccessStatus status) {
  switch (status) {
    case MultideviceFeatureAccessManager::AccessStatus::kProhibited:
      stream << "[Access prohibited]";
      break;
    case MultideviceFeatureAccessManager::AccessStatus::kAvailableButNotGranted:
      stream << "[Access available but not granted]";
      break;
    case MultideviceFeatureAccessManager::AccessStatus::kAccessGranted:
      stream << "[Access granted]";
      break;
  }
  return stream;
}

std::ostream& operator<<(
    std::ostream& stream,
    MultideviceFeatureAccessManager::AccessProhibitedReason reason) {
  switch (reason) {
    case MultideviceFeatureAccessManager::AccessProhibitedReason::kUnknown:
      stream << "[Unknown]";
      break;
    case MultideviceFeatureAccessManager::AccessProhibitedReason::kWorkProfile:
      stream << "[Work Profile]";
      break;
    case MultideviceFeatureAccessManager::AccessProhibitedReason::
        kDisabledByPhonePolicy:
      stream << "[Admin Policy]";
      break;
  }
  return stream;
}

std::ostream& operator<<(
    std::ostream& stream,
    std::pair<MultideviceFeatureAccessManager::AccessStatus,
              MultideviceFeatureAccessManager::AccessProhibitedReason>
        status_reason) {
  stream << status_reason.first;
  if (status_reason.first ==
      MultideviceFeatureAccessManager::AccessStatus::kProhibited) {
    stream << "," << status_reason.second;
  }
  return stream;
}

}  // namespace ash::phonehub