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/ui/gnubby_notification.h"
6
7 #include "ash/public/cpp/notification_utils.h"
8 #include "base/location.h"
9 #include "base/strings/string16.h"
10 #include "base/task/post_task.h"
11 #include "chrome/browser/notifications/notification_display_service.h"
12 #include "chrome/browser/notifications/system_notification_helper.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/message_center/public/cpp/notification.h"
17 #include "ui/message_center/public/cpp/notification_delegate.h"
18
19 namespace {
20 constexpr base::TimeDelta kNotificationTimeout =
21 base::TimeDelta::FromSeconds(2);
22 } // namespace
23
24 namespace chromeos {
25
GnubbyNotification()26 GnubbyNotification::GnubbyNotification()
27 : update_dismiss_notification_timer_(new base::OneShotTimer()),
28 weak_ptr_factory_(this) {
29 DCHECK(DBusThreadManager::Get()->GetGnubbyClient());
30 DBusThreadManager::Get()->GetGnubbyClient()->AddObserver(this);
31 }
~GnubbyNotification()32 GnubbyNotification::~GnubbyNotification() {
33 DCHECK(DBusThreadManager::Get()->GetGnubbyClient());
34 DBusThreadManager::Get()->GetGnubbyClient()->RemoveObserver(this);
35 }
36
PromptUserAuth()37 void GnubbyNotification::PromptUserAuth() {
38 ShowNotification();
39 }
40
CreateNotification()41 void GnubbyNotification::CreateNotification() {
42 const base::string16 title =
43 l10n_util::GetStringUTF16(IDS_GNUBBY_NOTIFICATION_TITLE);
44 const base::string16 message =
45 l10n_util::GetStringUTF16(IDS_GNUBBY_NOTIFICATION_MESSAGE);
46 const message_center::SystemNotificationWarningLevel colorType =
47 message_center::SystemNotificationWarningLevel::NORMAL;
48
49 GnubbyNotification::notification_prompt_ = ash::CreateSystemNotification(
50 message_center::NOTIFICATION_TYPE_SIMPLE,
51 GnubbyNotification::kNotificationID, title, message, base::string16(),
52 GURL(), message_center::NotifierId(),
53 message_center::RichNotificationData(),
54 new message_center::HandleNotificationClickDelegate(
55 base::BindRepeating(&GnubbyNotification::DismissNotification,
56 weak_ptr_factory_.GetWeakPtr())),
57 gfx::VectorIcon(), colorType);
58 }
59
ShowNotification()60 void GnubbyNotification::ShowNotification() {
61 GnubbyNotification::update_dismiss_notification_timer_->Stop();
62
63 if (GnubbyNotification::notificationActive == false) {
64 GnubbyNotification::notification_prompt_.reset();
65 CreateNotification();
66 }
67 SystemNotificationHelper::GetInstance()->Display(
68 *GnubbyNotification::notification_prompt_);
69 GnubbyNotification::update_dismiss_notification_timer_->Start(
70 FROM_HERE, kNotificationTimeout,
71 base::BindOnce(&GnubbyNotification::DismissNotification,
72 base::Unretained(this)));
73 GnubbyNotification::notificationActive = true;
74 }
75
DismissNotification()76 void GnubbyNotification::DismissNotification() {
77 GnubbyNotification::notificationActive = false;
78 SystemNotificationHelper::GetInstance()->Close(
79 GnubbyNotification::kNotificationID);
80 }
81
82 } // namespace chromeos
83