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 | |
Evan Stade | 6a09578 | 2018-02-14 03:55:00 | [diff] [blame] | 7 | #include "ash/public/cpp/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 8 | #include "chrome/browser/browser_process.h" |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 9 | #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 10 | #include "chrome/browser/notifications/notification_display_service.h" |
| 11 | #include "chrome/browser/notifications/notification_display_service_factory.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 12 | #include "chrome/browser/ui/browser_navigator.h" |
| 13 | #include "chrome/browser/ui/browser_navigator_params.h" |
| 14 | #include "chrome/common/pref_names.h" |
| 15 | #include "chrome/common/url_constants.h" |
| 16 | #include "chrome/grit/generated_resources.h" |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 17 | #include "chromeos/chromeos_switches.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 18 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 | #include "chromeos/dbus/update_engine_client.h" |
| 20 | #include "components/prefs/pref_service.h" |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 21 | #include "components/strings/grit/components_strings.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 22 | #include "ui/base/l10n/l10n_util.h" |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 23 | #include "ui/gfx/color_palette.h" |
| 24 | #include "ui/gfx/paint_vector_icon.h" |
Evan Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 25 | #include "ui/message_center/public/cpp/notification.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 26 | |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 27 | using l10n_util::GetStringUTF16; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 28 | |
| 29 | namespace chromeos { |
| 30 | namespace { |
| 31 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 32 | const char kEolNotificationId[] = "chrome://product_eol"; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 33 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 34 | // Buttons that appear in notifications. |
| 35 | enum ButtonIndex { |
| 36 | BUTTON_MORE_INFO = 0, |
| 37 | BUTTON_DISMISS, |
| 38 | BUTTON_SIZE = BUTTON_DISMISS |
| 39 | }; |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 40 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 41 | class EolNotificationDelegate : public message_center::NotificationDelegate { |
| 42 | public: |
| 43 | explicit EolNotificationDelegate(Profile* profile) : profile_(profile) {} |
| 44 | |
| 45 | private: |
| 46 | ~EolNotificationDelegate() override = default; |
| 47 | |
| 48 | // NotificationDelegate overrides: |
Evan Stade | e092109 | 2018-04-04 21:13:09 | [diff] [blame^] | 49 | void Click(const base::Optional<int>& button_index, |
| 50 | const base::Optional<base::string16>& reply) override { |
| 51 | if (!button_index) |
| 52 | return; |
| 53 | |
| 54 | switch (*button_index) { |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 55 | case BUTTON_MORE_INFO: { |
| 56 | // show eol link |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 57 | NavigateParams params(profile_, GURL(chrome::kEolNotificationURL), |
| 58 | ui::PAGE_TRANSITION_LINK); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 59 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 60 | params.window_action = NavigateParams::SHOW_WINDOW; |
| 61 | Navigate(¶ms); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | case BUTTON_DISMISS: |
| 65 | // set dismiss pref. |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 66 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, |
| 67 | true); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 68 | break; |
| 69 | } |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 70 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Close( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 71 | NotificationHandler::Type::TRANSIENT, kEolNotificationId); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 72 | } |
| 73 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 74 | Profile* const profile_; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 75 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 76 | DISALLOW_COPY_AND_ASSIGN(EolNotificationDelegate); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 77 | }; |
| 78 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 79 | } // namespace |
| 80 | |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 81 | // static |
| 82 | bool EolNotification::ShouldShowEolNotification() { |
| 83 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 84 | chromeos::switches::kDisableEolNotification)) { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | // Do not show end of life notification if this device is managed by |
| 89 | // enterprise user. |
| 90 | if (g_browser_process->platform_part() |
| 91 | ->browser_policy_connector_chromeos() |
| 92 | ->IsEnterpriseManaged()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 99 | EolNotification::EolNotification(Profile* profile) |
| 100 | : profile_(profile), |
| 101 | status_(update_engine::EndOfLifeStatus::kSupported), |
| 102 | weak_factory_(this) {} |
| 103 | |
| 104 | EolNotification::~EolNotification() {} |
| 105 | |
| 106 | void EolNotification::CheckEolStatus() { |
| 107 | UpdateEngineClient* update_engine_client = |
| 108 | DBusThreadManager::Get()->GetUpdateEngineClient(); |
| 109 | |
| 110 | // Request the Eol Status. |
Sarah Hu | 32816fa7 | 2018-02-28 02:20:38 | [diff] [blame] | 111 | update_engine_client->GetEolStatus(base::BindOnce( |
| 112 | &EolNotification::OnEolStatus, weak_factory_.GetWeakPtr())); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 113 | } |
| 114 | |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 115 | void EolNotification::OnEolStatus(update_engine::EndOfLifeStatus status) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 116 | status_ = status; |
| 117 | |
| 118 | const int pre_eol_status = |
| 119 | profile_->GetPrefs()->GetInteger(prefs::kEolStatus); |
| 120 | profile_->GetPrefs()->SetInteger(prefs::kEolStatus, status_); |
| 121 | |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame] | 122 | // Security only state is no longer supported. |
| 123 | if (status_ == update_engine::EndOfLifeStatus::kSupported || |
| 124 | status_ == update_engine::EndOfLifeStatus::kSecurityOnly) { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 125 | return; |
xiaoyinh | f4e5b5b | 2017-02-27 23:22:46 | [diff] [blame] | 126 | } |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 127 | |
| 128 | if (pre_eol_status != status_) { |
| 129 | // If Eol status has changed, we should reset |
| 130 | // kEolNotificationDismissed and show notification. |
| 131 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, false); |
| 132 | } |
| 133 | |
| 134 | bool user_dismissed_eol_notification = |
| 135 | profile_->GetPrefs()->GetBoolean(prefs::kEolNotificationDismissed); |
| 136 | if (user_dismissed_eol_notification) |
| 137 | return; |
| 138 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 139 | Update(); |
| 140 | } |
| 141 | |
| 142 | void EolNotification::Update() { |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 143 | message_center::RichNotificationData data; |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 144 | |
| 145 | DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size()); |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 146 | data.buttons.emplace_back(GetStringUTF16(IDS_LEARN_MORE)); |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 147 | |
| 148 | DCHECK_EQ(BUTTON_DISMISS, data.buttons.size()); |
| 149 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON)); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 150 | |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 151 | std::unique_ptr<message_center::Notification> notification = |
| 152 | message_center::Notification::CreateSystemNotification( |
| 153 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
| 154 | GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
| 155 | GetStringUTF16(IDS_EOL_NOTIFICATION_EOL), gfx::Image(), |
| 156 | GetStringUTF16(IDS_EOL_NOTIFICATION_DISPLAY_SOURCE), |
| 157 | GURL(kEolNotificationId), |
| 158 | message_center::NotifierId( |
| 159 | message_center::NotifierId::SYSTEM_COMPONENT, kEolNotificationId), |
| 160 | data, new EolNotificationDelegate(profile_), |
Evan Stade | 6a09578 | 2018-02-14 03:55:00 | [diff] [blame] | 161 | ash::kNotificationEndOfSupportIcon, |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 162 | message_center::SystemNotificationWarningLevel::CRITICAL_WARNING); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 163 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 164 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 165 | NotificationHandler::Type::TRANSIENT, *notification); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 166 | } |
| 167 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 168 | } // namespace chromeos |