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 | 70e2ed4 | 2018-11-08 06:23:05 | [diff] [blame] | 7 | #include "ash/public/cpp/notification_utils.h" |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 8 | #include "base/bind.h" |
Evan Stade | 82ba4b6 | 2019-07-11 01:58:01 | [diff] [blame^] | 9 | #include "chrome/app/vector_icons/vector_icons.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 10 | #include "chrome/browser/browser_process.h" |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 11 | #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 12 | #include "chrome/browser/notifications/notification_display_service.h" |
| 13 | #include "chrome/browser/notifications/notification_display_service_factory.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 14 | #include "chrome/browser/ui/browser_navigator.h" |
| 15 | #include "chrome/browser/ui/browser_navigator_params.h" |
| 16 | #include "chrome/common/pref_names.h" |
| 17 | #include "chrome/common/url_constants.h" |
| 18 | #include "chrome/grit/generated_resources.h" |
| 19 | #include "chromeos/dbus/dbus_thread_manager.h" |
| 20 | #include "chromeos/dbus/update_engine_client.h" |
| 21 | #include "components/prefs/pref_service.h" |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 22 | #include "components/strings/grit/components_strings.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 23 | #include "ui/base/l10n/l10n_util.h" |
Vladislav Kaznacheev | e980522 | 2019-05-28 19:44:40 | [diff] [blame] | 24 | #include "ui/chromeos/devicetype_utils.h" |
xiaoyinh | aa17d834 | 2016-06-30 22:56:02 | [diff] [blame] | 25 | #include "ui/gfx/color_palette.h" |
| 26 | #include "ui/gfx/paint_vector_icon.h" |
Evan Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 27 | #include "ui/message_center/public/cpp/notification.h" |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 28 | |
Tetsui Ohkubo | e8f95e8 | 2017-08-23 06:10:47 | [diff] [blame] | 29 | using l10n_util::GetStringUTF16; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 30 | |
| 31 | namespace chromeos { |
| 32 | namespace { |
| 33 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 34 | const char kEolNotificationId[] = "chrome://product_eol"; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 35 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 36 | // Buttons that appear in notifications. |
| 37 | enum ButtonIndex { |
| 38 | BUTTON_MORE_INFO = 0, |
| 39 | BUTTON_DISMISS, |
| 40 | BUTTON_SIZE = BUTTON_DISMISS |
| 41 | }; |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 42 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 43 | class EolNotificationDelegate : public message_center::NotificationDelegate { |
| 44 | public: |
| 45 | explicit EolNotificationDelegate(Profile* profile) : profile_(profile) {} |
| 46 | |
| 47 | private: |
| 48 | ~EolNotificationDelegate() override = default; |
| 49 | |
| 50 | // NotificationDelegate overrides: |
Evan Stade | e092109 | 2018-04-04 21:13:09 | [diff] [blame] | 51 | void Click(const base::Optional<int>& button_index, |
| 52 | const base::Optional<base::string16>& reply) override { |
| 53 | if (!button_index) |
| 54 | return; |
| 55 | |
| 56 | switch (*button_index) { |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 57 | case BUTTON_MORE_INFO: { |
| 58 | // show eol link |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 59 | NavigateParams params(profile_, GURL(chrome::kEolNotificationURL), |
| 60 | ui::PAGE_TRANSITION_LINK); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 61 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
cm.sanchi | 2522bc9 | 2017-12-04 08:04:13 | [diff] [blame] | 62 | params.window_action = NavigateParams::SHOW_WINDOW; |
| 63 | Navigate(¶ms); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 64 | break; |
| 65 | } |
| 66 | case BUTTON_DISMISS: |
| 67 | // set dismiss pref. |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 68 | profile_->GetPrefs()->SetBoolean(prefs::kEolNotificationDismissed, |
| 69 | true); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 70 | break; |
| 71 | } |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 72 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Close( |
Peter Beverloo | 6dba2e10 | 2017-11-23 17:46:33 | [diff] [blame] | 73 | NotificationHandler::Type::TRANSIENT, kEolNotificationId); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 74 | } |
| 75 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 76 | Profile* const profile_; |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 77 | |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 78 | DISALLOW_COPY_AND_ASSIGN(EolNotificationDelegate); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 79 | }; |
| 80 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 81 | } // namespace |
| 82 | |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 83 | // static |
| 84 | bool EolNotification::ShouldShowEolNotification() { |
Sarah Hu | 4ad394b | 2017-11-27 19:03:00 | [diff] [blame] | 85 | // Do not show end of life notification if this device is managed by |
| 86 | // enterprise user. |
| 87 | if (g_browser_process->platform_part() |
| 88 | ->browser_policy_connector_chromeos() |
| 89 | ->IsEnterpriseManaged()) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | return true; |
| 94 | } |
| 95 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 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. |
Sarah Hu | 32816fa7 | 2018-02-28 02:20:38 | [diff] [blame] | 108 | update_engine_client->GetEolStatus(base::BindOnce( |
| 109 | &EolNotification::OnEolStatus, weak_factory_.GetWeakPtr())); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 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 | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 140 | message_center::RichNotificationData data; |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 141 | |
| 142 | DCHECK_EQ(BUTTON_MORE_INFO, data.buttons.size()); |
dpapad | 943ef18 | 2018-01-20 02:58:20 | [diff] [blame] | 143 | data.buttons.emplace_back(GetStringUTF16(IDS_LEARN_MORE)); |
Evan Stade | cf27dae1 | 2017-11-07 23:57:51 | [diff] [blame] | 144 | |
| 145 | DCHECK_EQ(BUTTON_DISMISS, data.buttons.size()); |
| 146 | data.buttons.emplace_back(GetStringUTF16(IDS_EOL_DISMISS_BUTTON)); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 147 | |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 148 | std::unique_ptr<message_center::Notification> notification = |
Evan Stade | 70e2ed4 | 2018-11-08 06:23:05 | [diff] [blame] | 149 | ash::CreateSystemNotification( |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 150 | message_center::NOTIFICATION_TYPE_SIMPLE, kEolNotificationId, |
| 151 | GetStringUTF16(IDS_EOL_NOTIFICATION_TITLE), |
Vladislav Kaznacheev | e980522 | 2019-05-28 19:44:40 | [diff] [blame] | 152 | l10n_util::GetStringFUTF16(IDS_EOL_NOTIFICATION_EOL, |
| 153 | ui::GetChromeOSDeviceName()), |
| 154 | base::string16() /* display_source */, GURL(kEolNotificationId), |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 155 | message_center::NotifierId( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 156 | message_center::NotifierType::SYSTEM_COMPONENT, |
| 157 | kEolNotificationId), |
Tetsui Ohkubo | f24e7a94 | 2017-12-19 03:15:00 | [diff] [blame] | 158 | data, new EolNotificationDelegate(profile_), |
Evan Stade | 82ba4b6 | 2019-07-11 01:58:01 | [diff] [blame^] | 159 | kNotificationEndOfSupportIcon, |
Vladislav Kaznacheev | e980522 | 2019-05-28 19:44:40 | [diff] [blame] | 160 | message_center::SystemNotificationWarningLevel::NORMAL); |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 161 | |
Evan Stade | cc63b18 | 2017-09-26 16:05:12 | [diff] [blame] | 162 | NotificationDisplayServiceFactory::GetForProfile(profile_)->Display( |
Lei Zhang | 03c6b78 | 2019-03-21 05:22:24 | [diff] [blame] | 163 | NotificationHandler::Type::TRANSIENT, *notification, |
| 164 | /*metadata=*/nullptr); |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 165 | } |
| 166 | |
xiaoyinh | f39e3dd | 2016-06-18 04:50:23 | [diff] [blame] | 167 | } // namespace chromeos |