[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 | |
| 7 | #include "ash/display/display_controller.h" |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 8 | #include "ash/display/display_info.h" |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 9 | #include "ash/display/display_manager.h" |
| 10 | #include "ash/shell.h" |
[email protected] | a69b6f87 | 2013-08-24 14:38:33 | [diff] [blame] | 11 | #include "ash/system/system_notifier.h" |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
| 13 | #include "grit/ash_resources.h" |
| 14 | #include "grit/ash_strings.h" |
| 15 | #include "ui/base/l10n/l10n_util.h" |
| 16 | #include "ui/base/l10n/time_format.h" |
| 17 | #include "ui/base/resource/resource_bundle.h" |
| 18 | #include "ui/gfx/display.h" |
| 19 | #include "ui/gfx/screen.h" |
| 20 | #include "ui/message_center/message_center.h" |
| 21 | #include "ui/message_center/notification.h" |
| 22 | #include "ui/message_center/notification_delegate.h" |
| 23 | |
| 24 | using message_center::Notification; |
| 25 | |
| 26 | namespace ash { |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 27 | namespace { |
| 28 | |
| 29 | bool g_use_timer = true; |
| 30 | |
| 31 | class ResolutionChangeNotificationDelegate |
| 32 | : public message_center::NotificationDelegate { |
| 33 | public: |
| 34 | ResolutionChangeNotificationDelegate( |
| 35 | ResolutionNotificationController* controller, |
| 36 | bool has_timeout); |
| 37 | |
| 38 | protected: |
dcheng | 222b9c7 | 2015-01-16 00:48:01 | [diff] [blame^] | 39 | ~ResolutionChangeNotificationDelegate() override; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 40 | |
| 41 | private: |
| 42 | // message_center::NotificationDelegate overrides: |
dcheng | 222b9c7 | 2015-01-16 00:48:01 | [diff] [blame^] | 43 | void Close(bool by_user) override; |
| 44 | void Click() override; |
| 45 | bool HasClickedListener() override; |
| 46 | void ButtonClick(int button_index) override; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 47 | |
| 48 | ResolutionNotificationController* controller_; |
| 49 | bool has_timeout_; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate); |
| 52 | }; |
| 53 | |
| 54 | ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate( |
| 55 | ResolutionNotificationController* controller, |
| 56 | bool has_timeout) |
| 57 | : controller_(controller), |
| 58 | has_timeout_(has_timeout) { |
| 59 | DCHECK(controller_); |
| 60 | } |
| 61 | |
| 62 | ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() { |
| 63 | } |
| 64 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 65 | void ResolutionChangeNotificationDelegate::Close(bool by_user) { |
| 66 | if (by_user) |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 67 | controller_->AcceptResolutionChange(false); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void ResolutionChangeNotificationDelegate::Click() { |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 71 | controller_->AcceptResolutionChange(true); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | bool ResolutionChangeNotificationDelegate::HasClickedListener() { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) { |
| 79 | // If there's the timeout, the first button is "Accept". Otherwise the |
| 80 | // button click should be "Revert". |
| 81 | if (has_timeout_ && button_index == 0) |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 82 | controller_->AcceptResolutionChange(true); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 83 | else |
| 84 | controller_->RevertResolutionChange(); |
| 85 | } |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | // static |
| 90 | const int ResolutionNotificationController::kTimeoutInSec = 15; |
| 91 | |
| 92 | // static |
| 93 | const char ResolutionNotificationController::kNotificationId[] = |
| 94 | "chrome://settings/display/resolution"; |
| 95 | |
| 96 | struct ResolutionNotificationController::ResolutionChangeInfo { |
| 97 | ResolutionChangeInfo(int64 display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 98 | const DisplayMode& old_resolution, |
| 99 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 100 | const base::Closure& accept_callback); |
| 101 | ~ResolutionChangeInfo(); |
| 102 | |
| 103 | // The id of the display where the resolution change happens. |
| 104 | int64 display_id; |
| 105 | |
| 106 | // The resolution before the change. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 107 | DisplayMode old_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 108 | |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 109 | // The requested resolution. Note that this may be different from |
| 110 | // |current_resolution| which is the actual resolution set. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 111 | DisplayMode new_resolution; |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 112 | |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 113 | // The actual resolution after the change. |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 114 | DisplayMode current_resolution; |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 115 | |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 116 | // The callback when accept is chosen. |
| 117 | base::Closure accept_callback; |
| 118 | |
| 119 | // The remaining timeout in seconds. 0 if the change does not time out. |
| 120 | uint8 timeout_count; |
| 121 | |
| 122 | // The timer to invoke OnTimerTick() every second. This cannot be |
| 123 | // OneShotTimer since the message contains text "automatically closed in xx |
| 124 | // seconds..." which has to be updated every second. |
| 125 | base::RepeatingTimer<ResolutionNotificationController> timer; |
| 126 | |
| 127 | private: |
| 128 | DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo); |
| 129 | }; |
| 130 | |
| 131 | ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo( |
| 132 | int64 display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 133 | const DisplayMode& old_resolution, |
| 134 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 135 | const base::Closure& accept_callback) |
| 136 | : display_id(display_id), |
| 137 | old_resolution(old_resolution), |
| 138 | new_resolution(new_resolution), |
| 139 | accept_callback(accept_callback), |
| 140 | timeout_count(0) { |
| 141 | DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 142 | if (!display_manager->HasInternalDisplay() && |
| 143 | display_manager->num_connected_displays() == 1u) { |
| 144 | timeout_count = kTimeoutInSec; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | ResolutionNotificationController::ResolutionChangeInfo:: |
| 149 | ~ResolutionChangeInfo() { |
| 150 | } |
| 151 | |
| 152 | ResolutionNotificationController::ResolutionNotificationController() { |
| 153 | Shell::GetInstance()->display_controller()->AddObserver(this); |
| 154 | Shell::GetScreen()->AddObserver(this); |
| 155 | } |
| 156 | |
| 157 | ResolutionNotificationController::~ResolutionNotificationController() { |
| 158 | Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 159 | Shell::GetScreen()->RemoveObserver(this); |
| 160 | } |
| 161 | |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 162 | void ResolutionNotificationController::PrepareNotification( |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 163 | int64 display_id, |
[email protected] | 7badf7f | 2014-08-06 04:38:29 | [diff] [blame] | 164 | const DisplayMode& old_resolution, |
| 165 | const DisplayMode& new_resolution, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 166 | const base::Closure& accept_callback) { |
| 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( |
| 229 | message_center::NOTIFICATION_TYPE_SIMPLE, |
| 230 | kNotificationId, |
[email protected] | c91bd6c | 2014-01-22 11:51:10 | [diff] [blame] | 231 | message, |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 232 | timeout_message, |
[email protected] | 9bc8562 | 2014-01-22 22:29:14 | [diff] [blame] | 233 | bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY), |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 234 | base::string16() /* display_source */, |
[email protected] | b78d79a5 | 2013-09-12 01:52:21 | [diff] [blame] | 235 | message_center::NotifierId( |
[email protected] | de22ffea | 2013-12-07 03:34:29 | [diff] [blame] | 236 | message_center::NotifierId::SYSTEM_COMPONENT, |
| 237 | system_notifier::kNotifierDisplayResolutionChange), |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 238 | data, |
| 239 | new ResolutionChangeNotificationDelegate( |
| 240 | this, change_info_->timeout_count > 0))); |
| 241 | notification->SetSystemPriority(); |
| 242 | message_center->AddNotification(notification.Pass()); |
| 243 | } |
| 244 | |
| 245 | void ResolutionNotificationController::OnTimerTick() { |
| 246 | if (!change_info_) |
| 247 | return; |
| 248 | |
| 249 | --change_info_->timeout_count; |
| 250 | if (change_info_->timeout_count == 0) |
| 251 | RevertResolutionChange(); |
| 252 | else |
[email protected] | 86eee6e | 2013-08-27 01:27:15 | [diff] [blame] | 253 | CreateOrUpdateNotification(false); |
[email protected] | 6ef71d7 | 2013-08-10 18:13:44 | [diff] [blame] | 254 | } |
| 255 | |
[email protected] | 99e487e | 2013-08-13 20:58:41 | [diff] [blame] | 256 | void ResolutionNotificationController::AcceptResolutionChange( |
| 257 | bool close_notification) { |
| 258 | if (close_notification) { |
| 259 | message_center::MessageCenter::Get()->RemoveNotification( |
| 260 | kNotificationId, false /* by_user */); |
| 261 | } |
[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 */); |
| 270 | int64 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 |