1 // Copyright 2019 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 
5 #include "chrome/browser/chromeos/wilco_dtc_supportd/wilco_dtc_supportd_notification_controller.h"
6 
7 #include <memory>
8 #include <string>
9 #include <utility>
10 
11 #include "ash/public/cpp/notification_utils.h"
12 #include "base/strings/string16.h"
13 #include "chrome/app/vector_icons/vector_icons.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/notifications/notification_display_service.h"
16 #include "chrome/browser/notifications/notification_handler.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/vector_icons/vector_icons.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/message_center/public/cpp/notification_delegate.h"
22 #include "ui/message_center/public/cpp/notification_types.h"
23 #include "ui/message_center/public/cpp/notifier_id.h"
24 
25 namespace chromeos {
26 
27 namespace {
28 
29 const char kNotifierWilco[] = "ash.wilco";
30 
31 const char kWilcoDtcSupportdNotificationIdBatteryAuth[] = "BatteryAuth";
32 const char kWilcoDtcSupportdNotificationIdNonWilcoCharger[] = "NonWilcoCharger";
33 const char kWilcoDtcSupportdNotificationIdIncompatibleDock[] =
34     "IncompatibleDock";
35 const char kWilcoDtcSupportdNotificationIdDockHardwareError[] = "DockError";
36 const char kWilcoDtcSupportdNotificationIdDockDisplay[] = "DockDisplay";
37 const char kWilcoDtcSupportdNotificationIdDockThunderbolt[] = "DockThunderbolt";
38 const char kWilcoDtcSupportdNotificationIdLowPower[] = "LowPower";
39 
40 class WilcoDtcSupportdNotificationDelegate
41     : public message_center::NotificationDelegate {
42  public:
WilcoDtcSupportdNotificationDelegate(HelpAppLauncher::HelpTopic topic)43   explicit WilcoDtcSupportdNotificationDelegate(
44       HelpAppLauncher::HelpTopic topic)
45       : topic_(topic) {}
46   WilcoDtcSupportdNotificationDelegate(
47       const WilcoDtcSupportdNotificationDelegate& other) = delete;
48   WilcoDtcSupportdNotificationDelegate& operator=(
49       const WilcoDtcSupportdNotificationDelegate& other) = delete;
50 
Click(const base::Optional<int> & button_index,const base::Optional<base::string16> & reply)51   void Click(const base::Optional<int>& button_index,
52              const base::Optional<base::string16>& reply) override {
53     if (button_index && *button_index == 0) {
54       auto help_app(
55           base::MakeRefCounted<HelpAppLauncher>(nullptr /* parent_window */));
56       help_app->ShowHelpTopic(topic_);
57     }
58   }
59 
60  private:
61   ~WilcoDtcSupportdNotificationDelegate() override = default;
62 
63   const HelpAppLauncher::HelpTopic topic_;
64 };
65 
66 }  // namespace
67 
WilcoDtcSupportdNotificationController()68 WilcoDtcSupportdNotificationController::WilcoDtcSupportdNotificationController()
69     : WilcoDtcSupportdNotificationController(
70           g_browser_process->profile_manager()) {}
71 
WilcoDtcSupportdNotificationController(ProfileManager * profile_manager)72 WilcoDtcSupportdNotificationController::WilcoDtcSupportdNotificationController(
73     ProfileManager* profile_manager)
74     : profile_manager_(profile_manager) {
75   DCHECK(profile_manager_);
76 }
77 
78 WilcoDtcSupportdNotificationController::
79     ~WilcoDtcSupportdNotificationController() = default;
80 
81 std::string
ShowBatteryAuthNotification() const82 WilcoDtcSupportdNotificationController::ShowBatteryAuthNotification() const {
83   DisplayNotification(kWilcoDtcSupportdNotificationIdBatteryAuth,
84                       IDS_WILCO_NOTIFICATION_BATTERY_AUTH_TITLE,
85                       IDS_WILCO_NOTIFICATION_BATTERY_AUTH_MESSAGE,
86                       message_center::SYSTEM_PRIORITY, kNotificationBatteryIcon,
87                       message_center::SystemNotificationWarningLevel::WARNING,
88                       HelpAppLauncher::HelpTopic::HELP_WILCO_BATTERY_CHARGER);
89   return kWilcoDtcSupportdNotificationIdBatteryAuth;
90 }
91 
92 std::string
ShowNonWilcoChargerNotification() const93 WilcoDtcSupportdNotificationController::ShowNonWilcoChargerNotification()
94     const {
95   DisplayNotification(kWilcoDtcSupportdNotificationIdNonWilcoCharger,
96                       IDS_WILCO_NOTIFICATION_NON_WILCO_CHARGER_TITLE,
97                       IDS_WILCO_NOTIFICATION_NON_WILCO_CHARGER_MESSAGE,
98                       message_center::DEFAULT_PRIORITY,
99                       kNotificationBatteryIcon,
100                       message_center::SystemNotificationWarningLevel::WARNING,
101                       HelpAppLauncher::HelpTopic::HELP_WILCO_BATTERY_CHARGER);
102   return kWilcoDtcSupportdNotificationIdNonWilcoCharger;
103 }
104 
105 std::string
ShowIncompatibleDockNotification() const106 WilcoDtcSupportdNotificationController::ShowIncompatibleDockNotification()
107     const {
108   DisplayNotification(kWilcoDtcSupportdNotificationIdIncompatibleDock,
109                       IDS_WILCO_NOTIFICATION_INCOMPATIBLE_DOCK_TITLE,
110                       IDS_WILCO_NOTIFICATION_INCOMPATIBLE_DOCK_MESSAGE,
111                       message_center::DEFAULT_PRIORITY,
112                       vector_icons::kSettingsIcon,
113                       message_center::SystemNotificationWarningLevel::NORMAL,
114                       HelpAppLauncher::HelpTopic::HELP_WILCO_DOCK);
115   return kWilcoDtcSupportdNotificationIdIncompatibleDock;
116 }
117 
ShowDockErrorNotification() const118 std::string WilcoDtcSupportdNotificationController::ShowDockErrorNotification()
119     const {
120   DisplayNotification(kWilcoDtcSupportdNotificationIdDockHardwareError,
121                       IDS_WILCO_NOTIFICATION_DOCK_ERROR_TITLE,
122                       IDS_WILCO_NOTIFICATION_DOCK_ERROR_MESSAGE,
123                       message_center::DEFAULT_PRIORITY,
124                       vector_icons::kSettingsIcon,
125                       message_center::SystemNotificationWarningLevel::NORMAL,
126                       HelpAppLauncher::HelpTopic::HELP_WILCO_DOCK);
127   return kWilcoDtcSupportdNotificationIdDockHardwareError;
128 }
129 
130 std::string
ShowDockDisplayNotification() const131 WilcoDtcSupportdNotificationController::ShowDockDisplayNotification() const {
132   DisplayNotification(kWilcoDtcSupportdNotificationIdDockDisplay,
133                       IDS_WILCO_NOTIFICATION_DOCK_DISPLAY_TITLE,
134                       IDS_WILCO_NOTIFICATION_DOCK_DISPLAY_MESSAGE,
135                       message_center::DEFAULT_PRIORITY,
136                       vector_icons::kSettingsIcon,
137                       message_center::SystemNotificationWarningLevel::NORMAL,
138                       HelpAppLauncher::HelpTopic::HELP_WILCO_DOCK);
139   return kWilcoDtcSupportdNotificationIdDockDisplay;
140 }
141 
142 std::string
ShowDockThunderboltNotification() const143 WilcoDtcSupportdNotificationController::ShowDockThunderboltNotification()
144     const {
145   DisplayNotification(kWilcoDtcSupportdNotificationIdDockThunderbolt,
146                       IDS_WILCO_NOTIFICATION_DOCK_THUNDERBOLT_TITLE,
147                       IDS_WILCO_NOTIFICATION_DOCK_THUNDERBOLT_MESSAGE,
148                       message_center::DEFAULT_PRIORITY,
149                       vector_icons::kSettingsIcon,
150                       message_center::SystemNotificationWarningLevel::NORMAL,
151                       HelpAppLauncher::HelpTopic::HELP_WILCO_DOCK);
152   return kWilcoDtcSupportdNotificationIdDockThunderbolt;
153 }
154 
155 std::string
ShowLowPowerChargerNotification() const156 WilcoDtcSupportdNotificationController::ShowLowPowerChargerNotification()
157     const {
158   DisplayNotification(kWilcoDtcSupportdNotificationIdLowPower,
159                       IDS_WILCO_LOW_POWER_CHARGER_TITLE,
160                       IDS_WILCO_LOW_POWER_CHARGER_MESSAGE,
161                       message_center::SYSTEM_PRIORITY, kNotificationBatteryIcon,
162                       message_center::SystemNotificationWarningLevel::WARNING,
163                       HelpAppLauncher::HelpTopic::HELP_WILCO_BATTERY_CHARGER);
164   return kWilcoDtcSupportdNotificationIdLowPower;
165 }
166 
DisplayNotification(const std::string & notification_id,const int title_id,const int message_id,const message_center::NotificationPriority priority,const gfx::VectorIcon & small_image,const message_center::SystemNotificationWarningLevel color_type,const HelpAppLauncher::HelpTopic topic) const167 void WilcoDtcSupportdNotificationController::DisplayNotification(
168     const std::string& notification_id,
169     const int title_id,
170     const int message_id,
171     const message_center::NotificationPriority priority,
172     const gfx::VectorIcon& small_image,
173     const message_center::SystemNotificationWarningLevel color_type,
174     const HelpAppLauncher::HelpTopic topic) const {
175   message_center::RichNotificationData rich_data;
176   rich_data.pinned = (priority == message_center::SYSTEM_PRIORITY);
177   std::unique_ptr<message_center::Notification> notification =
178       ash::CreateSystemNotification(
179           message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
180           l10n_util::GetStringUTF16(title_id),
181           l10n_util::GetStringUTF16(message_id),
182           base::string16() /* display_source */, GURL() /* origin_url */,
183           message_center::NotifierId(
184               message_center::NotifierType::SYSTEM_COMPONENT, kNotifierWilco),
185           rich_data,
186           base::MakeRefCounted<WilcoDtcSupportdNotificationDelegate>(topic),
187           small_image, color_type);
188   notification->set_buttons({message_center::ButtonInfo(
189       l10n_util::GetStringUTF16(IDS_WILCO_NOTIFICATION_LEARN_MORE))});
190   if (priority == message_center::SYSTEM_PRIORITY) {
191     notification->SetSystemPriority();
192   }
193   NotificationDisplayService::GetForProfile(
194       profile_manager_->GetLastUsedProfile(profile_manager_->user_data_dir()))
195       ->Display(NotificationHandler::Type::TRANSIENT, *notification,
196                 nullptr /* metadata */);
197 }
198 
199 }  // namespace chromeos
200