blob: c9da0b449e35fb98ed15ab0f0fc6bb537018751c [file] [log] [blame]
xiaoyinhf39e3dd2016-06-18 04:50:231// Copyright 2016 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/eol_notification.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/notifications/notification.h"
9#include "chrome/browser/notifications/notification_ui_manager.h"
10#include "chrome/browser/ui/browser_navigator.h"
11#include "chrome/browser/ui/browser_navigator_params.h"
12#include "chrome/common/pref_names.h"
13#include "chrome/common/url_constants.h"
14#include "chrome/grit/generated_resources.h"
15#include "chromeos/dbus/dbus_thread_manager.h"
16#include "chromeos/dbus/update_engine_client.h"
17#include "components/prefs/pref_service.h"
xiaoyinhf39e3dd2016-06-18 04:50:2318#include "ui/base/l10n/l10n_util.h"
xiaoyinhaa17d8342016-06-30 22:56:0219#include "ui/gfx/color_palette.h"
20#include "ui/gfx/paint_vector_icon.h"
21#include "ui/gfx/vector_icons_public.h"
xiaoyinhf39e3dd2016-06-18 04:50:2322
23using message_center::MessageCenter;
24
25namespace chromeos {
26namespace {
27
28const char kEolNotificationId[] = "eol";
29const char kDelegateId[] = "eol_delegate";
xiaoyinhaa17d8342016-06-30 22:56:0230const SkColor kButtonIconColor = SkColorSetRGB(150, 150, 152);
31const SkColor kNotificationIconColor = SkColorSetRGB(219, 68, 55);
xiaoyinhf39e3dd2016-06-18 04:50:2332
33class EolNotificationDelegate : public NotificationDelegate {
34 public:
35 explicit EolNotificationDelegate(Profile* profile);
36
37 private:
xiaoyinhaa17d8342016-06-30 22:56:0238 // Buttons that appear in notifications.
39 enum EOL_Button { BUTTON_MORE_INFO = 0, BUTTON_DISMISS };
40
xiaoyinhf39e3dd2016-06-18 04:50:2341 ~EolNotificationDelegate() override;
42
43 // NotificationDelegate overrides:
xiaoyinhf39e3dd2016-06-18 04:50:2344 void ButtonClick(int button_index) override;
45 std::string id() const override;
46
47 Profile* const profile_;
48
49 void OpenMoreInfoPage();
xiaoyinhaa17d8342016-06-30 22:56:0250 void CancelNotification();
xiaoyinhf39e3dd2016-06-18 04:50:2351
52 DISALLOW_COPY_AND_ASSIGN(EolNotificationDelegate);
53};
54
55EolNotificationDelegate::EolNotificationDelegate(Profile* profile)
56 : profile_(profile) {}
57
58EolNotificationDelegate::~EolNotificationDelegate() {}
59
xiaoyinhf39e3dd2016-06-18 04:50:2360void EolNotificationDelegate::ButtonClick(int button_index) {
xiaoyinhaa17d8342016-06-30 22:56:0261 switch (button_index) {
62 case BUTTON_MORE_INFO:
63 // show eol link
64 OpenMoreInfoPage();
65 break;
66 case BUTTON_DISMISS:
67 // set dismiss pref.
68 profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, true);
69 break;
70 default:
71 NOTREACHED();
72 }
73 CancelNotification();
xiaoyinhf39e3dd2016-06-18 04:50:2374}
75
76std::string EolNotificationDelegate::id() const {
77 return kDelegateId;
78}
79
80void EolNotificationDelegate::OpenMoreInfoPage() {
81 chrome::NavigateParams params(profile_, GURL(chrome::kEolNotificationURL),
82 ui::PAGE_TRANSITION_LINK);
nick3b04f322016-08-31 19:29:1983 params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
xiaoyinhf39e3dd2016-06-18 04:50:2384 params.window_action = chrome::NavigateParams::SHOW_WINDOW;
85 chrome::Navigate(&params);
86}
87
xiaoyinhaa17d8342016-06-30 22:56:0288void EolNotificationDelegate::CancelNotification() {
89 // Clean up the notification
90 g_browser_process->notification_ui_manager()->CancelById(
91 id(), NotificationUIManager::GetProfileID(profile_));
92}
93
xiaoyinhf39e3dd2016-06-18 04:50:2394} // namespace
95
96EolNotification::EolNotification(Profile* profile)
97 : profile_(profile),
98 status_(update_engine::EndOfLifeStatus::kSupported),
99 weak_factory_(this) {}
100
101EolNotification::~EolNotification() {}
102
103void EolNotification::CheckEolStatus() {
104 UpdateEngineClient* update_engine_client =
105 DBusThreadManager::Get()->GetUpdateEngineClient();
106
107 // Request the Eol Status.
108 update_engine_client->GetEolStatus(
109 base::Bind(&EolNotification::OnEolStatus, weak_factory_.GetWeakPtr()));
110}
111
xiaoyinhaa17d8342016-06-30 22:56:02112void EolNotification::OnEolStatus(update_engine::EndOfLifeStatus status) {
xiaoyinhf39e3dd2016-06-18 04:50:23113 status_ = status;
114
115 const int pre_eol_status =
116 profile_->GetPrefs()->GetInteger(prefs::kEolStatus);
117 profile_->GetPrefs()->SetInteger(prefs::kEolStatus, status_);
118
xiaoyinhf4e5b5b2017-02-27 23:22:46119 // Security only state is no longer supported.
120 if (status_ == update_engine::EndOfLifeStatus::kSupported ||
121 status_ == update_engine::EndOfLifeStatus::kSecurityOnly) {
xiaoyinhf39e3dd2016-06-18 04:50:23122 return;
xiaoyinhf4e5b5b2017-02-27 23:22:46123 }
xiaoyinhf39e3dd2016-06-18 04:50:23124
125 if (pre_eol_status != status_) {
126 // If Eol status has changed, we should reset
127 // kEolNotificationDismissed and show notification.
128 profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false);
129 }
130
131 bool user_dismissed_eol_notification =
132 profile_->GetPrefs()->GetBoolean(prefs::kEolNotificationDismissed);
133 if (user_dismissed_eol_notification)
134 return;
135
xiaoyinhf39e3dd2016-06-18 04:50:23136 Update();
137}
138
139void EolNotification::Update() {
xiaoyinhaa17d8342016-06-30 22:56:02140 message_center::ButtonInfo learn_more(
141 l10n_util::GetStringUTF16(IDS_EOL_MORE_INFO_BUTTON));
142 learn_more.icon = gfx::Image(
143 CreateVectorIcon(gfx::VectorIconId::INFO_OUTLINE, kButtonIconColor));
144 message_center::ButtonInfo dismiss(
145 l10n_util::GetStringUTF16(IDS_EOL_DISMISS_BUTTON));
146 dismiss.icon = gfx::Image(
147 CreateVectorIcon(gfx::VectorIconId::NOTIFICATIONS_OFF, kButtonIconColor));
148
xiaoyinhf39e3dd2016-06-18 04:50:23149 message_center::RichNotificationData data;
xiaoyinhaa17d8342016-06-30 22:56:02150 data.buttons.push_back(learn_more);
151 data.buttons.push_back(dismiss);
xiaoyinhf39e3dd2016-06-18 04:50:23152
153 Notification notification(
154 message_center::NOTIFICATION_TYPE_SIMPLE,
xiaoyinhf4e5b5b2017-02-27 23:22:46155 l10n_util::GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE),
156 l10n_util::GetStringUTF16(IDS_EOL_NOTIFICATION_EOL),
xiaoyinhaa17d8342016-06-30 22:56:02157 gfx::Image(
158 CreateVectorIcon(gfx::VectorIconId::EOL, kNotificationIconColor)),
xiaoyinhf39e3dd2016-06-18 04:50:23159 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
160 kEolNotificationId),
161 base::string16(), // display_source
162 GURL(), kEolNotificationId, data, new EolNotificationDelegate(profile_));
163 g_browser_process->notification_ui_manager()->Add(notification, profile_);
164}
165
xiaoyinhf39e3dd2016-06-18 04:50:23166} // namespace chromeos