[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 1 | // Copyright 2013 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 "ash/display/resolution_notification_controller.h" |
| 6 | |
dcheng | 2272495 | 2015-12-31 03:17:54 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 9 | #include "ash/display/display_info.h" |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 10 | #include "ash/display/display_manager.h" |
| 11 | #include "ash/shell.h" |
[email protected] | a69b6f87 | 2013-08-24 14:38:33 | [diff] [blame] | 12 | #include "ash/system/system_notifier.h" |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
| 14 | #include "grit/ash_resources.h" |
| 15 | #include "grit/ash_strings.h" |
| 16 | #include "ui/base/l10n/l10n_util.h" |
| 17 | #include "ui/base/l10n/time_format.h" |
| 18 | #include "ui/base/resource/resource_bundle.h" |
| 19 | #include "ui/gfx/display.h" |
| 20 | #include "ui/gfx/screen.h" |
| 21 | #include "ui/message_center/message_center.h" |
| 22 | #include "ui/message_center/notification.h" |
| 23 | #include "ui/message_center/notification_delegate.h" |
| 24 | |
| 25 | using message_center::Notification; |
| 26 | |
| 27 | namespace ash { |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 28 | namespace { |
| 29 | |
| 30 | bool g_use_timer = true; |
| 31 | |
| 32 | class ResolutionChangeNotificationDelegate |
| 33 | : public message_center::NotificationDelegate { |
| 34 | public: |
| 35 | ResolutionChangeNotificationDelegate( |
| 36 | ResolutionNotificationController* controller, |
| 37 | bool has_timeout); |
| 38 | |
| 39 | protected: |
dcheng | 222b9c7 | 2015-01-16 00:48:01 | [diff] [blame] | 40 | ~ResolutionChangeNotificationDelegate() override; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | // message_center::NotificationDelegate overrides: |
dcheng | 222b9c7 | 2015-01-16 00:48:01 | [diff] [blame] | 44 | void Close(bool by_user) override; |
| 45 | void Click() override; |
| 46 | bool HasClickedListener() override; |
| 47 | void ButtonClick(int button_index) override; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 48 | |
| 49 | ResolutionNotificationController* controller_; |
| 50 | bool has_timeout_; |
| 51 | |
| 52 | DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate); |
| 53 | }; |
| 54 | |
| 55 | ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate( |
| 56 | ResolutionNotificationController* controller, |
| 57 | bool has_timeout) |
| 58 | : controller_(controller), |
| 59 | has_timeout_(has_timeout) { |
| 60 | DCHECK(controller_); |
| 61 | } |
| 62 | |
| 63 | ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() { |
| 64 | } |
| 65 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 66 | void ResolutionChangeNotificationDelegate::Close(bool by_user) { |
| 67 | if (by_user) |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 68 | controller_->AcceptResolutionChange(false); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void ResolutionChangeNotificationDelegate::Click() { |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 72 | controller_->AcceptResolutionChange(true); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool ResolutionChangeNotificationDelegate::HasClickedListener() { |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) { |
| 80 | // If there's the timeout, the first button is "Accept". Otherwise the |
| 81 | // button click should be "Revert". |
| 82 | if (has_timeout_ && button_index == 0) |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 83 | controller_->AcceptResolutionChange(true); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 84 | else |
| 85 | controller_->RevertResolutionChange(); |
| 86 | } |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | // static |
| 91 | const int ResolutionNotificationController::kTimeoutInSec = 15; |
| 92 | |
| 93 | // static |
| 94 | const char ResolutionNotificationController::kNotificationId[] = |
| 95 | "chrome://settings/display/resolution"; |
| 96 | |
| 97 | struct ResolutionNotificationController::ResolutionChangeInfo { |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 98 | ResolutionChangeInfo(int64_t display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 99 | const DisplayMode& old_resolution, |
| 100 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 101 | const base::Closure& accept_callback); |
| 102 | ~ResolutionChangeInfo(); |
| 103 | |
| 104 | // The id of the display where the resolution change happens. |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 105 | int64_t display_id; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 106 | |
| 107 | // The resolution before the change. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 108 | DisplayMode old_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 109 | |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 110 | // The requested resolution. Note that this may be different from |
| 111 | // |current_resolution| which is the actual resolution set. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 112 | DisplayMode new_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 113 | |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 114 | // The actual resolution after the change. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 115 | DisplayMode current_resolution; |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 116 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 117 | // The callback when accept is chosen. |
| 118 | base::Closure accept_callback; |
| 119 | |
| 120 | // The remaining timeout in seconds. 0 if the change does not time out. |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 121 | uint8_t timeout_count; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 122 | |
| 123 | // The timer to invoke OnTimerTick() every second. This cannot be |
| 124 | // OneShotTimer since the message contains text "automatically closed in xx |
| 125 | // seconds..." which has to be updated every second. |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 126 | base::RepeatingTimer timer; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 127 | |
| 128 | private: |
| 129 | DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo); |
| 130 | }; |
| 131 | |
| 132 | ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo( |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 133 | int64_t display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 134 | const DisplayMode& old_resolution, |
| 135 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 136 | const base::Closure& accept_callback) |
| 137 | : display_id(display_id), |
| 138 | old_resolution(old_resolution), |
| 139 | new_resolution(new_resolution), |
| 140 | accept_callback(accept_callback), |
| 141 | timeout_count(0) { |
| 142 | DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
oshima | 0b9377aa | 2015-04-23 08:07:43 | [diff] [blame] | 143 | if (!gfx::Display::HasInternalDisplay() && |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 144 | display_manager->num_connected_displays() == 1u) { |
| 145 | timeout_count = kTimeoutInSec; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | ResolutionNotificationController::ResolutionChangeInfo:: |
| 150 | ~ResolutionChangeInfo() { |
| 151 | } |
| 152 | |
| 153 | ResolutionNotificationController::ResolutionNotificationController() { |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 154 | Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); |
scottmg | e8b04297 | 2016-01-27 05:07:35 | [diff] [blame^] | 155 | gfx::Screen::GetScreen()->AddObserver(this); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | ResolutionNotificationController::~ResolutionNotificationController() { |
oshima | e281892 | 2015-07-28 01:18:52 | [diff] [blame] | 159 | Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); |
scottmg | e8b04297 | 2016-01-27 05:07:35 | [diff] [blame^] | 160 | gfx::Screen::GetScreen()->RemoveObserver(this); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 163 | void ResolutionNotificationController::PrepareNotification( |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 164 | int64_t display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 165 | const DisplayMode& old_resolution, |
| 166 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 167 | const base::Closure& accept_callback) { |
mukai | 0232219 | 2015-10-23 01:03:13 | [diff] [blame] | 168 | DCHECK(!gfx::Display::IsInternalDisplayId(display_id)); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 169 | // If multiple resolution changes are invoked for the same display, |
| 170 | // the original resolution for the first resolution change has to be used |
| 171 | // instead of the specified |old_resolution|. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 172 | DisplayMode original_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 173 | if (change_info_ && change_info_->display_id == display_id) { |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 174 | DCHECK(change_info_->new_resolution.size == old_resolution.size); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 175 | original_resolution = change_info_->old_resolution; |
| 176 | } |
| 177 | |
| 178 | change_info_.reset(new ResolutionChangeInfo( |
| 179 | display_id, old_resolution, new_resolution, accept_callback)); |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 180 | if (!original_resolution.size.IsEmpty()) |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 181 | change_info_->old_resolution = original_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | bool ResolutionNotificationController::DoesNotificationTimeout() { |
| 185 | return change_info_ && change_info_->timeout_count > 0; |
| 186 | } |
| 187 | |
[email protected] | 86eee6e | 2013-08-27 01:27:15 | [diff] [blame] | 188 | void ResolutionNotificationController::CreateOrUpdateNotification( |
| 189 | bool enable_spoken_feedback) { |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 190 | message_center::MessageCenter* message_center = |
| 191 | message_center::MessageCenter::Get(); |
| 192 | if (!change_info_) { |
| 193 | message_center->RemoveNotification(kNotificationId, false /* by_user */); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | base::string16 timeout_message; |
| 198 | message_center::RichNotificationData data; |
| 199 | if (change_info_->timeout_count > 0) { |
| 200 | data.buttons.push_back(message_center::ButtonInfo( |
| 201 | l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT))); |
| 202 | timeout_message = l10n_util::GetStringFUTF16( |
| 203 | IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT, |
[email protected] | b6cc963b | 2014-02-27 15:32:23 | [diff] [blame] | 204 | ui::TimeFormat::Simple( |
| 205 | ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 206 | base::TimeDelta::FromSeconds(change_info_->timeout_count))); |
| 207 | } |
| 208 | data.buttons.push_back(message_center::ButtonInfo( |
| 209 | l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT))); |
| 210 | |
[email protected] | 86eee6e | 2013-08-27 01:27:15 | [diff] [blame] | 211 | data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback; |
| 212 | |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 213 | const base::string16 display_name = base::UTF8ToUTF16( |
| 214 | Shell::GetInstance()->display_manager()->GetDisplayNameForId( |
| 215 | change_info_->display_id)); |
| 216 | const base::string16 message = |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 217 | (change_info_->new_resolution.size == |
| 218 | change_info_->current_resolution.size) ? |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 219 | l10n_util::GetStringFUTF16( |
| 220 | IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED, |
| 221 | display_name, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 222 | base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) : |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 223 | l10n_util::GetStringFUTF16( |
| 224 | IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED, |
| 225 | display_name, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 226 | base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()), |
| 227 | base::UTF8ToUTF16(change_info_->current_resolution.size.ToString())); |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 228 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 229 | ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); |
| 230 | scoped_ptr<Notification> notification(new Notification( |
miguelg | 53a6dde5 | 2015-08-20 09:00:30 | [diff] [blame] | 231 | message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, message, |
| 232 | timeout_message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY), |
| 233 | base::string16() /* display_source */, GURL(), |
[email protected] | b78d79a5 | 2013-09-12 01:52:21 | [diff] [blame] | 234 | message_center::NotifierId( |
[email protected] | de22ffea | 2013-12-07 03:34:29 | [diff] [blame] | 235 | message_center::NotifierId::SYSTEM_COMPONENT, |
| 236 | system_notifier::kNotifierDisplayResolutionChange), |
miguelg | 53a6dde5 | 2015-08-20 09:00:30 | [diff] [blame] | 237 | data, new ResolutionChangeNotificationDelegate( |
| 238 | this, change_info_->timeout_count > 0))); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 239 | notification->SetSystemPriority(); |
dcheng | 2272495 | 2015-12-31 03:17:54 | [diff] [blame] | 240 | message_center->AddNotification(std::move(notification)); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void ResolutionNotificationController::OnTimerTick() { |
| 244 | if (!change_info_) |
| 245 | return; |
| 246 | |
| 247 | --change_info_->timeout_count; |
| 248 | if (change_info_->timeout_count == 0) |
| 249 | RevertResolutionChange(); |
| 250 | else |
[email protected] | 86eee6e | 2013-08-27 01:27:15 | [diff] [blame] | 251 | CreateOrUpdateNotification(false); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 254 | void ResolutionNotificationController::AcceptResolutionChange( |
| 255 | bool close_notification) { |
| 256 | if (close_notification) { |
| 257 | message_center::MessageCenter::Get()->RemoveNotification( |
| 258 | kNotificationId, false /* by_user */); |
| 259 | } |
oshima | 8f0f45e4 | 2015-04-03 19:55:22 | [diff] [blame] | 260 | if (!change_info_) |
| 261 | return; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 262 | base::Closure callback = change_info_->accept_callback; |
| 263 | change_info_.reset(); |
| 264 | callback.Run(); |
| 265 | } |
| 266 | |
| 267 | void ResolutionNotificationController::RevertResolutionChange() { |
| 268 | message_center::MessageCenter::Get()->RemoveNotification( |
| 269 | kNotificationId, false /* by_user */); |
oshima | 8f0f45e4 | 2015-04-03 19:55:22 | [diff] [blame] | 270 | if (!change_info_) |
| 271 | return; |
avi | db567a8a | 2015-12-20 17:07:24 | [diff] [blame] | 272 | int64_t display_id = change_info_->display_id; |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 273 | DisplayMode old_resolution = change_info_->old_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 274 | change_info_.reset(); |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 275 | Shell::GetInstance()->display_manager()->SetDisplayMode( |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 276 | display_id, old_resolution); |
| 277 | } |
| 278 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 279 | void ResolutionNotificationController::OnDisplayAdded( |
| 280 | const gfx::Display& new_display) { |
| 281 | } |
| 282 | |
| 283 | void ResolutionNotificationController::OnDisplayRemoved( |
| 284 | const gfx::Display& old_display) { |
| 285 | if (change_info_ && change_info_->display_id == old_display.id()) |
| 286 | RevertResolutionChange(); |
| 287 | } |
| 288 | |
[email protected] | 0c5703d | 2014-05-22 01:26:01 | [diff] [blame] | 289 | void ResolutionNotificationController::OnDisplayMetricsChanged( |
| 290 | const gfx::Display&, uint32_t) { |
| 291 | } |
| 292 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 293 | void ResolutionNotificationController::OnDisplayConfigurationChanged() { |
| 294 | if (!change_info_) |
| 295 | return; |
| 296 | |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 297 | change_info_->current_resolution = Shell::GetInstance()->display_manager()-> |
| 298 | GetActiveModeForDisplayId(change_info_->display_id); |
[email protected] | 86eee6e | 2013-08-27 01:27:15 | [diff] [blame] | 299 | CreateOrUpdateNotification(true); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 300 | if (g_use_timer && change_info_->timeout_count > 0) { |
| 301 | change_info_->timer.Start(FROM_HERE, |
| 302 | base::TimeDelta::FromSeconds(1), |
| 303 | this, |
| 304 | &ResolutionNotificationController::OnTimerTick); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void ResolutionNotificationController::SuppressTimerForTest() { |
| 309 | g_use_timer = false; |
| 310 | } |
| 311 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 312 | } // namespace ash |