File: it2me_confirmation_dialog_chromeos.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 (445 lines) | stat: -rw-r--r-- 15,657 bytes parent folder | download | duplicates (5)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/host/it2me/it2me_confirmation_dialog_chromeos.h"

#include <memory>
#include <string>
#include <utility>

#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/check_is_test.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/i18n/message_formatter.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chromeos/ui/vector_icons/vector_icons.h"
#include "components/session_manager/session_manager_types.h"
#include "remoting/base/string_resources.h"
#include "remoting/host/chromeos/features.h"
#include "remoting/host/chromeos/message_box.h"
#include "remoting/host/it2me/it2me_confirmation_dialog.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/ui_base_types.h"
#include "ui/gfx/vector_icon_types.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/views/window/dialog_delegate.h"

namespace remoting {

namespace {

using session_manager::SessionState;

constexpr char kConfirmationNotificationId[] = "CRD_CONFIRMATION_NOTIFICATION";
constexpr char kConfirmationNotifierId[] = "crd.confirmation_notification";

std::u16string FormatMessage(const std::string& remote_user_email,
                             It2MeConfirmationDialog::DialogStyle style) {
  int message_id = (style == It2MeConfirmationDialog::DialogStyle::kEnterprise
                        ? IDS_SHARE_CONFIRM_DIALOG_MESSAGE_ADMIN_INITIATED
                        : IDS_SHARE_CONFIRM_DIALOG_MESSAGE_WITH_USERNAME);

  return base::i18n::MessageFormatter::FormatWithNumberedArgs(
      l10n_util::GetStringUTF16(message_id),
      base::UTF8ToUTF16(remote_user_email),
      l10n_util::GetStringUTF16(IDS_SHARE_CONFIRM_DIALOG_DECLINE),
      l10n_util::GetStringUTF16(IDS_SHARE_CONFIRM_DIALOG_CONFIRM));
}

std::u16string GetAutoAcceptMessage(const std::string& remote_user_email,
                                    base::TimeDelta time_left) {
  std::u16string auto_accept_message =
      base::i18n::MessageFormatter::FormatWithNumberedArgs(
          l10n_util::GetStringUTF16(
              IDS_SHARE_CONFIRM_DIALOG_MESSAGE_ADMIN_INITIATED_CRD_UNATTENDED),
          remote_user_email);
  auto_accept_message.append(u"\n\n");
  auto_accept_message.append(l10n_util::GetPluralStringFUTF16(
      IDS_CRD_AUTO_ACCEPT_COUNTDOWN, time_left.InSeconds()));

  return auto_accept_message;
}

std::u16string GetTitle() {
  return l10n_util::GetStringUTF16(IDS_MODE_IT2ME);
}

std::u16string GetConfirmButtonLabel() {
  return l10n_util::GetStringUTF16(IDS_SHARE_CONFIRM_DIALOG_CONFIRM);
}

std::u16string GetAutoAcceptConfirmButtonLabel() {
  return l10n_util::GetStringUTF16(
      IDS_SHARE_CONFIRM_DIALOG_CONFIRM_CRD_UNATTENDED);
}

std::u16string GetDeclineButtonLabel() {
  return l10n_util::GetStringUTF16(IDS_SHARE_CONFIRM_DIALOG_DECLINE);
}

ash::SessionControllerImpl* session_controller() {
  return ash::Shell::Get()->session_controller();
}

ash::ShellWindowId GetParentContainerId() {
  switch (session_controller()->GetSessionState()) {
    case SessionState::LOCKED:
    case SessionState::LOGIN_PRIMARY:
    case SessionState::LOGIN_SECONDARY:
    case SessionState::LOGGED_IN_NOT_ACTIVE:
      return ash::kShellWindowId_LockSystemModalContainer;

    case SessionState::ACTIVE:
      return ash::kShellWindowId_SystemModalContainer;

    case SessionState::OOBE:
    case SessionState::RMA:
    case SessionState::UNKNOWN:
      NOTREACHED() << "CRD is not supported for the session state:"
                   << static_cast<int>(session_controller()->GetSessionState());
  }
}

gfx::NativeView GetParentContainer() {
  return ash::Shell::GetContainer(ash::Shell::GetPrimaryRootWindow(),
                                  GetParentContainerId());
}

class CountdownTimer {
 public:
  using CountdownCallback = base::RepeatingCallback<void(base::TimeDelta)>;

  explicit CountdownTimer(base::TimeDelta time,
                          CountdownCallback countdown_callback,
                          base::OnceClosure completion_callback)
      : time_left_(time),
        countdown_callback_(std::move(countdown_callback)),
        completion_callback_(std::move(completion_callback)) {}

  CountdownTimer(const CountdownTimer&) = delete;
  CountdownTimer& operator=(const CountdownTimer&) = delete;

  void Start() {
    countdown_timer_.Start(FROM_HERE, /*delay=*/base::Seconds(1),
                           base::BindRepeating(&CountdownTimer::RunEverySecond,
                                               weak_factory_.GetWeakPtr()));
  }

 private:
  void RunEverySecond() {
    time_left_ = time_left_ - base::Seconds(1);

    if (time_left_ < base::Seconds(1)) {
      countdown_timer_.Stop();
      std::move(completion_callback_).Run();
      return;
    }

    countdown_callback_.Run(time_left_);
  }

  base::RepeatingTimer countdown_timer_;

  base::TimeDelta time_left_;
  CountdownCallback countdown_callback_;
  base::OnceClosure completion_callback_;

  base::WeakPtrFactory<CountdownTimer> weak_factory_{this};
};

class MessageLabelProvider {
 public:
  MessageLabelProvider(const std::string& remote_user_email,
                       It2MeConfirmationDialog::DialogStyle style,
                       base::TimeDelta auto_accept_timeout)
      : remote_user_email_(remote_user_email),
        style_(style),
        auto_accept_timeout_(auto_accept_timeout) {}
  ~MessageLabelProvider() = default;

  std::u16string GetMessageLabel() const {
    if (!auto_accept_timeout_.is_zero()) {
      return GetAutoAcceptMessage(remote_user_email_,
                                  /*time_left=*/auto_accept_timeout_);
    }

    return FormatMessage(remote_user_email_, style_);
  }

  std::u16string GetAutoAcceptMessageLabel(base::TimeDelta time_left) const {
    return GetAutoAcceptMessage(remote_user_email_, time_left);
  }

 private:
  const std::string remote_user_email_;
  const It2MeConfirmationDialog::DialogStyle style_;
  const base::TimeDelta auto_accept_timeout_;
};

}  // namespace

class It2MeConfirmationDialogChromeOS::Core : public ash::SessionObserver {
 public:
  explicit Core(const std::u16string& title_label,
                const MessageLabelProvider message_label_provider_,
                const std::u16string& ok_label,
                const std::u16string& cancel_label,
                const std::optional<ui::ImageModel> icon,
                const base::TimeDelta auto_accept_timeout,
                ResultCallback callback);
  ~Core() override;

  void ShowConfirmationDialog();

  views::DialogDelegate& GetDialogDelegate();

 private:
  void OnConfirmationDialogResult(MessageBox::Result result);

  // Implements ash::SessionObserver:
  void OnSessionStateChanged(session_manager::SessionState state) override;

  bool HasAutoAcceptTimeout();
  void StartAutoAcceptCountDown();
  void UpdateCountdownInDialogUI(base::TimeDelta count);
  void OnCountdownComplete();

  std::unique_ptr<CountdownTimer> countdown_timer_;

  ResultCallback callback_;
  std::unique_ptr<MessageBox> message_box_;

  const MessageLabelProvider message_label_provider_;
  const base::TimeDelta auto_accept_timeout_;

  base::WeakPtrFactory<It2MeConfirmationDialogChromeOS::Core> weak_factory_{
      this};
};

It2MeConfirmationDialogChromeOS::Core::Core(
    const std::u16string& title_label,
    const MessageLabelProvider message_label_provider,
    const std::u16string& ok_label,
    const std::u16string& cancel_label,
    const std::optional<ui::ImageModel> icon,
    const base::TimeDelta auto_accept_timeout,
    ResultCallback callback)
    : message_label_provider_(message_label_provider),
      auto_accept_timeout_(auto_accept_timeout) {
  session_controller()->AddObserver(this);
  message_box_ = std::make_unique<MessageBox>(
      /*title_label=*/title_label,
      /*message_label=*/message_label_provider_.GetMessageLabel(),
      /*ok_label=*/ok_label,
      /*cancel_label=*/cancel_label,
      /*icon=*/icon,
      /*result_callback=*/
      base::BindOnce(
          &It2MeConfirmationDialogChromeOS::Core::OnConfirmationDialogResult,
          weak_factory_.GetWeakPtr()));
  callback_ = std::move(callback);
}

It2MeConfirmationDialogChromeOS::Core::~Core() {
  session_controller()->RemoveObserver(this);
}

void It2MeConfirmationDialogChromeOS::Core::ShowConfirmationDialog() {
  // Ensure the message box remains visible when the user logs in/out.
  message_box_->ShowInParentContainer(GetParentContainer());

  if (HasAutoAcceptTimeout()) {
    StartAutoAcceptCountDown();
  }
}

void It2MeConfirmationDialogChromeOS::Core::OnConfirmationDialogResult(
    MessageBox::Result result) {
  std::move(callback_).Run(result == MessageBox::Result::OK ? Result::OK
                                                            : Result::CANCEL);
}

void It2MeConfirmationDialogChromeOS::Core::OnSessionStateChanged(
    session_manager::SessionState state) {
  message_box_->ChangeParentContainer(GetParentContainer());
}

views::DialogDelegate&
It2MeConfirmationDialogChromeOS::Core::GetDialogDelegate() {
  return message_box_->GetDialogDelegate();
}

bool It2MeConfirmationDialogChromeOS::Core::HasAutoAcceptTimeout() {
  return !auto_accept_timeout_.is_zero();
}

void It2MeConfirmationDialogChromeOS::Core::StartAutoAcceptCountDown() {
  countdown_timer_ = std::make_unique<CountdownTimer>(
      /*time=*/auto_accept_timeout_,
      /*countdown_callback=*/
      base::BindRepeating(
          &It2MeConfirmationDialogChromeOS::Core::UpdateCountdownInDialogUI,
          weak_factory_.GetWeakPtr()),
      /*completion_callback=*/
      base::BindOnce(
          &It2MeConfirmationDialogChromeOS::Core::OnCountdownComplete,
          weak_factory_.GetWeakPtr()));
  countdown_timer_->Start();
}

void It2MeConfirmationDialogChromeOS::Core::UpdateCountdownInDialogUI(
    base::TimeDelta time_left) {
  message_box_->SetMessageLabel(
      message_label_provider_.GetAutoAcceptMessageLabel(time_left));
}

void It2MeConfirmationDialogChromeOS::Core::OnCountdownComplete() {
  weak_factory_.InvalidateWeakPtrs();
  countdown_timer_.reset();
  // `callback_` will lead to destruction of `this`, so no member fields should
  // be accessed after this point.
  std::move(callback_).Run(Result::OK);
}

It2MeConfirmationDialogChromeOS::It2MeConfirmationDialogChromeOS(
    DialogStyle style,
    base::TimeDelta auto_accept_timeout)
    : style_(style), auto_accept_timeout_(auto_accept_timeout) {}

It2MeConfirmationDialogChromeOS::~It2MeConfirmationDialogChromeOS() {
  message_center::MessageCenter::Get()->RemoveNotification(
      kConfirmationNotificationId,
      /*by_user=*/false);
}

void It2MeConfirmationDialogChromeOS::Show(const std::string& remote_user_email,
                                           ResultCallback callback) {
  DCHECK(!remote_user_email.empty());
  callback_ = std::move(callback);

  if (base::FeatureList::IsEnabled(
          remoting::features::kEnableCrdSharedSessionToUnattendedDevice)) {
    core_ = std::make_unique<It2MeConfirmationDialogChromeOS::Core>(
        /*title_label=*/GetTitle(),
        /*message_label_provider=*/
        MessageLabelProvider(remote_user_email, style_, auto_accept_timeout_),
        /*ok_label=*/GetShareButtonLabel(),
        /*cancel_label=*/GetDeclineButtonLabel(),
        /*icon=*/GetDialogIcon(),
        /*auto_accept_timeout=*/auto_accept_timeout_,
        /*callback=*/
        base::BindOnce(
            &It2MeConfirmationDialogChromeOS::OnConfirmationDialogResult,
            base::Unretained(this)));
    core_->ShowConfirmationDialog();
  } else {
    ShowConfirmationNotification(remote_user_email);
  }
}

void It2MeConfirmationDialogChromeOS::ShowConfirmationNotification(
    const std::string& remote_user_email) {
  message_center::RichNotificationData data;
  data.pinned = false;

  data.buttons.emplace_back(GetDeclineButtonLabel());
  data.buttons.emplace_back(GetConfirmButtonLabel());

  std::unique_ptr<message_center::Notification> notification =
      ash::CreateSystemNotificationPtr(
          message_center::NOTIFICATION_TYPE_SIMPLE, kConfirmationNotificationId,
          GetTitle(), FormatMessage(remote_user_email, style_), u"", GURL(),
          message_center::NotifierId(
              message_center::NotifierType::SYSTEM_COMPONENT,
              kConfirmationNotifierId,
              ash::NotificationCatalogName::kIt2MeConfirmation),
          data,
          base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
              base::BindRepeating(&It2MeConfirmationDialogChromeOS::
                                      OnConfirmationNotificationResult,
                                  base::Unretained(this))),
          GetIcon(),
          // Warning level must be set to CRITICAL_WARNING to ensure this
          // notification is always shown, even when the user enabled
          // do-not-disturb mode.
          message_center::SystemNotificationWarningLevel::CRITICAL_WARNING);

  // Set system priority so the notification is always shown and it will never
  // time out.
  notification->SetSystemPriority();
  message_center::MessageCenter::Get()->AddNotification(
      std::move(notification));
}

void It2MeConfirmationDialogChromeOS::OnConfirmationNotificationResult(
    std::optional<int> button_index) {
  if (!button_index.has_value()) {
    return;  // This happens when the user clicks the notification itself.
  }

  // Note: |by_user| must be false, otherwise the notification will not actually
  // be removed but instead it will be moved into the message center bubble
  // (because the message was pinned).
  message_center::MessageCenter::Get()->RemoveNotification(
      kConfirmationNotificationId,
      /*by_user=*/false);

  std::move(callback_).Run(*button_index == 0 ? Result::CANCEL : Result::OK);
}

void It2MeConfirmationDialogChromeOS::OnConfirmationDialogResult(
    Result result) {
  core_.reset();
  std::move(callback_).Run(result);
}

const gfx::VectorIcon& It2MeConfirmationDialogChromeOS::GetIcon() const {
  switch (style_) {
    case It2MeConfirmationDialog::DialogStyle::kConsumer:
      return gfx::VectorIcon::EmptyIcon();
    case It2MeConfirmationDialog::DialogStyle::kEnterprise:
      return chromeos::kEnterpriseIcon;
  }
}

const ui::ImageModel It2MeConfirmationDialogChromeOS::GetDialogIcon() const {
  return ui::ImageModel::FromVectorIcon(GetIcon());
}

std::u16string It2MeConfirmationDialogChromeOS::GetShareButtonLabel() const {
  if (!auto_accept_timeout_.is_zero()) {
    return GetAutoAcceptConfirmButtonLabel();
  }

  return GetConfirmButtonLabel();
}

views::DialogDelegate&
It2MeConfirmationDialogChromeOS::GetDialogDelegateForTest() {
  CHECK_IS_TEST();
  return core_->GetDialogDelegate();
}

std::unique_ptr<It2MeConfirmationDialog>
It2MeConfirmationDialogFactory::Create() {
  return std::make_unique<It2MeConfirmationDialogChromeOS>(
      dialog_style_, auto_accept_timeout_);
}

}  // namespace remoting