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