xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 1 | // 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" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 18 | #include "ui/base/l10n/l10n_util.h" |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 19 | #include "ui/gfx/color_palette.h" |
| 20 | #include "ui/gfx/paint_vector_icon.h" |
| 21 | #include "ui/gfx/vector_icons_public.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 22 | |
| 23 | using message_center::MessageCenter; |
| 24 | |
| 25 | namespace chromeos { |
| 26 | namespace { |
| 27 | |
| 28 | const char kEolNotificationId[] = "eol"; |
| 29 | const char kDelegateId[] = "eol_delegate"; |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 30 | const SkColor kButtonIconColor = SkColorSetRGB(150, 150, 152); |
| 31 | const SkColor kNotificationIconColor = SkColorSetRGB(219, 68, 55); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 32 | |
| 33 | class EolNotificationDelegate : public NotificationDelegate { |
| 34 | public: |
| 35 | explicit EolNotificationDelegate(Profile* profile); |
| 36 | |
| 37 | private: |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 38 | // Buttons that appear in notifications. |
| 39 | enum EOL_Button { BUTTON_MORE_INFO = 0, BUTTON_DISMISS }; |
| 40 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 41 | ~EolNotificationDelegate() override; |
| 42 | |
| 43 | // NotificationDelegate overrides: |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 44 | void ButtonClick(int button_index) override; |
| 45 | std::string id() const override; |
| 46 | |
| 47 | Profile* const profile_; |
| 48 | |
| 49 | void OpenMoreInfoPage(); |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 50 | void CancelNotification(); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(EolNotificationDelegate); |
| 53 | }; |
| 54 | |
| 55 | EolNotificationDelegate::EolNotificationDelegate(Profile* profile) |
| 56 | : profile_(profile) {} |
| 57 | |
| 58 | EolNotificationDelegate::~EolNotificationDelegate() {} |
| 59 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 60 | void EolNotificationDelegate::ButtonClick(int button_index) { |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 61 | 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(); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | std::string EolNotificationDelegate::id() const { |
| 77 | return kDelegateId; |
| 78 | } |
| 79 | |
| 80 | void EolNotificationDelegate::OpenMoreInfoPage() { |
| 81 | chrome::NavigateParams params(profile_, GURL(chrome::kEolNotificationURL), |
| 82 | ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 83 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 84 | params.window_action = chrome::NavigateParams::SHOW_WINDOW; |
| 85 | chrome::Navigate(¶ms); |
| 86 | } |
| 87 | |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 88 | void EolNotificationDelegate::CancelNotification() { |
| 89 | // Clean up the notification |
| 90 | g_browser_process->notification_ui_manager()->CancelById( |
| 91 | id(), NotificationUIManager::GetProfileID(profile_)); |
| 92 | } |
| 93 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 94 | } // namespace |
| 95 | |
| 96 | EolNotification::EolNotification(Profile* profile) |
| 97 | : profile_(profile), |
| 98 | status_(update_engine::EndOfLifeStatus::kSupported), |
| 99 | weak_factory_(this) {} |
| 100 | |
| 101 | EolNotification::~EolNotification() {} |
| 102 | |
| 103 | void 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 | |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 112 | void EolNotification::OnEolStatus(update_engine::EndOfLifeStatus status) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 113 | status_ = status; |
| 114 | |
| 115 | const int pre_eol_status = |
| 116 | profile_->GetPrefs()->GetInteger(prefs::kEolStatus); |
| 117 | profile_->GetPrefs()->SetInteger(prefs::kEolStatus, status_); |
| 118 | |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame^] | 119 | // Security only state is no longer supported. |
| 120 | if (status_ == update_engine::EndOfLifeStatus::kSupported || |
| 121 | status_ == update_engine::EndOfLifeStatus::kSecurityOnly) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 122 | return; |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame^] | 123 | } |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 124 | |
| 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 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 136 | Update(); |
| 137 | } |
| 138 | |
| 139 | void EolNotification::Update() { |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 140 | 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 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 149 | message_center::RichNotificationData data; |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 150 | data.buttons.push_back(learn_more); |
| 151 | data.buttons.push_back(dismiss); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 152 | |
| 153 | Notification notification( |
| 154 | message_center::NOTIFICATION_TYPE_SIMPLE, |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame^] | 155 | l10n_util::GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
| 156 | l10n_util::GetStringUTF16(IDS_EOL_NOTIFICATION_EOL), |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 157 | gfx::Image( |
| 158 | CreateVectorIcon(gfx::VectorIconId::EOL, kNotificationIconColor)), |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 159 | 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 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 166 | } // namespace chromeos |