File: deprecation_notification_controller.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 (155 lines) | stat: -rw-r--r-- 5,723 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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/notifications/deprecation_notification_controller.h"

#include <string>

#include "ash/accelerators/accelerator_controller_impl.h"
#include "ash/constants/notifier_catalogs.h"
#include "ash/public/cpp/notification_utils.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/events/ash/keyboard_capability.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_types.h"

namespace ash {

namespace {

const char kNotifierId[] = "event_rewriter_deprecation_controller";
const char kNotificationIdPrefix[] = "alt_event_rewrite_deprecation.";

}  // namespace

DeprecationNotificationController::DeprecationNotificationController(
    message_center::MessageCenter* message_center)
    : message_center_(message_center) {
  DCHECK(message_center_);
}

DeprecationNotificationController::~DeprecationNotificationController() =
    default;

bool DeprecationNotificationController::NotifyDeprecatedRightClickRewrite() {
  if (right_click_notification_shown_) {
    return false;
  }

  const std::string id = std::string(kNotificationIdPrefix) + "right_click";
  ShowNotificationFromIdWithLauncherKey(id,
                                        IDS_ASH_SHORTCUT_DEPRECATION_ALT_CLICK);

  // Don't show the notification again.
  right_click_notification_shown_ = true;
  return true;
}

bool DeprecationNotificationController::NotifyDeprecatedSixPackKeyRewrite(
    ui::KeyboardCode key_code) {
  if (!ShouldShowSixPackKeyDeprecationNotification(key_code)) {
    return false;
  }

  // The notification id is not user visible.
  const std::string id =
      std::string(kNotificationIdPrefix) + base::NumberToString(key_code);
  const int message_id = GetSixPackKeyDeprecationMessageId(key_code);
  ShowNotificationFromIdWithLauncherKey(id, message_id);

  // Keep track that the notification was shown to decide whether to show it
  // again in future.
  RecordSixPackKeyDeprecationNotificationShown(key_code);
  return true;
}

void DeprecationNotificationController::ResetStateForTesting() {
  shown_key_notifications_.clear();
  right_click_notification_shown_ = false;
}

void DeprecationNotificationController::ShowNotificationFromIdWithLauncherKey(
    const std::string& id,
    int message_id) {
  const int launcher_key_name_id =
      Shell::Get()->keyboard_capability()->HasLauncherButtonOnAnyKeyboard()
          ? IDS_ASH_SHORTCUT_MODIFIER_LAUNCHER
          : IDS_ASH_SHORTCUT_MODIFIER_SEARCH;
  const std::u16string launcher_key_name =
      l10n_util::GetStringUTF16(launcher_key_name_id);
  const std::u16string message_body =
      l10n_util::GetStringFUTF16(message_id, launcher_key_name);

  ShowNotification(id, message_body);
}

void DeprecationNotificationController::ShowNotification(
    const std::string& id,
    const std::u16string& message_body) {
  auto on_click_handler =
      base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
          base::BindRepeating([]() {
            if (!Shell::Get()->session_controller()->IsUserSessionBlocked())
              Shell::Get()->shell_delegate()->OpenKeyboardShortcutHelpPage();
          }));

  auto notification = CreateSystemNotificationPtr(
      message_center::NOTIFICATION_TYPE_SIMPLE, id,
      l10n_util::GetStringUTF16(IDS_DEPRECATED_SHORTCUT_TITLE), message_body,
      std::u16string(), GURL(),
      message_center::NotifierId(
          message_center::NotifierType::SYSTEM_COMPONENT, kNotifierId,
          NotificationCatalogName::kEventRewriterDeprecation),
      message_center::RichNotificationData(), std::move(on_click_handler),
      kNotificationKeyboardIcon,
      message_center::SystemNotificationWarningLevel::NORMAL);
  message_center_->AddNotification(std::move(notification));
}

bool DeprecationNotificationController::
    ShouldShowSixPackKeyDeprecationNotification(ui::KeyboardCode key_code) {
  const auto* accelerator_controller = Shell::Get()->accelerator_controller();
  DCHECK(accelerator_controller);

  // Six pack key notification should not show if accelerators are being blocked
  // as the user does not expect these keys to be interpreted as a six pack key.
  return !accelerator_controller->ShouldPreventProcessingAccelerators() &&
         !shown_key_notifications_.contains(key_code);
}

void DeprecationNotificationController::
    RecordSixPackKeyDeprecationNotificationShown(ui::KeyboardCode key_code) {
  DCHECK(!shown_key_notifications_.contains(key_code));
  shown_key_notifications_.insert(key_code);
}

int DeprecationNotificationController::GetSixPackKeyDeprecationMessageId(
    ui::KeyboardCode key_code) {
  switch (key_code) {
    case ui::VKEY_DELETE:
      return IDS_ASH_SHORTCUT_DEPRECATION_ALT_BASED_DELETE;
    case ui::VKEY_INSERT:
      return IDS_ASH_SHORTCUT_DEPRECATION_SEARCH_PERIOD_INSERT;
    case ui::VKEY_HOME:
      return IDS_ASH_SHORTCUT_DEPRECATION_ALT_BASED_HOME;
    case ui::VKEY_END:
      return IDS_ASH_SHORTCUT_DEPRECATION_ALT_BASED_END;
    case ui::VKEY_PRIOR:
      return IDS_ASH_SHORTCUT_DEPRECATION_ALT_BASED_PAGE_UP;
    case ui::VKEY_NEXT:
      return IDS_ASH_SHORTCUT_DEPRECATION_ALT_BASED_PAGE_DOWN;
    default:
      NOTREACHED();
  }
}

}  // namespace ash