blob: 9f50d1c3ba9824e2ad582ea974e0d0f67e5f6213 [file] [log] [blame]
[email protected]6ef71d72013-08-10 18:13:441// 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
dcheng22724952015-12-31 03:17:547#include <utility>
8
[email protected]7badf7f2014-08-06 04:38:299#include "ash/display/display_info.h"
[email protected]6ef71d72013-08-10 18:13:4410#include "ash/display/display_manager.h"
11#include "ash/shell.h"
[email protected]a69b6f872013-08-24 14:38:3312#include "ash/system/system_notifier.h"
[email protected]6ef71d72013-08-10 18:13:4413#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
25using message_center::Notification;
26
27namespace ash {
[email protected]6ef71d72013-08-10 18:13:4428namespace {
29
30bool g_use_timer = true;
31
32class ResolutionChangeNotificationDelegate
33 : public message_center::NotificationDelegate {
34 public:
35 ResolutionChangeNotificationDelegate(
36 ResolutionNotificationController* controller,
37 bool has_timeout);
38
39 protected:
dcheng222b9c72015-01-16 00:48:0140 ~ResolutionChangeNotificationDelegate() override;
[email protected]6ef71d72013-08-10 18:13:4441
42 private:
43 // message_center::NotificationDelegate overrides:
dcheng222b9c72015-01-16 00:48:0144 void Close(bool by_user) override;
45 void Click() override;
46 bool HasClickedListener() override;
47 void ButtonClick(int button_index) override;
[email protected]6ef71d72013-08-10 18:13:4448
49 ResolutionNotificationController* controller_;
50 bool has_timeout_;
51
52 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
53};
54
55ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
56 ResolutionNotificationController* controller,
57 bool has_timeout)
58 : controller_(controller),
59 has_timeout_(has_timeout) {
60 DCHECK(controller_);
61}
62
63ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
64}
65
[email protected]6ef71d72013-08-10 18:13:4466void ResolutionChangeNotificationDelegate::Close(bool by_user) {
67 if (by_user)
[email protected]99e487e2013-08-13 20:58:4168 controller_->AcceptResolutionChange(false);
[email protected]6ef71d72013-08-10 18:13:4469}
70
71void ResolutionChangeNotificationDelegate::Click() {
[email protected]99e487e2013-08-13 20:58:4172 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4473}
74
75bool ResolutionChangeNotificationDelegate::HasClickedListener() {
76 return true;
77}
78
79void 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]99e487e2013-08-13 20:58:4183 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4484 else
85 controller_->RevertResolutionChange();
86}
87
88} // namespace
89
90// static
91const int ResolutionNotificationController::kTimeoutInSec = 15;
92
93// static
94const char ResolutionNotificationController::kNotificationId[] =
95 "chrome://settings/display/resolution";
96
97struct ResolutionNotificationController::ResolutionChangeInfo {
avidb567a8a2015-12-20 17:07:2498 ResolutionChangeInfo(int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:2999 const DisplayMode& old_resolution,
100 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44101 const base::Closure& accept_callback);
102 ~ResolutionChangeInfo();
103
104 // The id of the display where the resolution change happens.
avidb567a8a2015-12-20 17:07:24105 int64_t display_id;
[email protected]6ef71d72013-08-10 18:13:44106
107 // The resolution before the change.
[email protected]7badf7f2014-08-06 04:38:29108 DisplayMode old_resolution;
[email protected]6ef71d72013-08-10 18:13:44109
[email protected]c91bd6c2014-01-22 11:51:10110 // The requested resolution. Note that this may be different from
111 // |current_resolution| which is the actual resolution set.
[email protected]7badf7f2014-08-06 04:38:29112 DisplayMode new_resolution;
[email protected]6ef71d72013-08-10 18:13:44113
[email protected]c91bd6c2014-01-22 11:51:10114 // The actual resolution after the change.
[email protected]7badf7f2014-08-06 04:38:29115 DisplayMode current_resolution;
[email protected]c91bd6c2014-01-22 11:51:10116
[email protected]6ef71d72013-08-10 18:13:44117 // 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.
avidb567a8a2015-12-20 17:07:24121 uint8_t timeout_count;
[email protected]6ef71d72013-08-10 18:13:44122
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.
danakj8c3eb802015-09-24 07:53:00126 base::RepeatingTimer timer;
[email protected]6ef71d72013-08-10 18:13:44127
128 private:
129 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
130};
131
132ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
avidb567a8a2015-12-20 17:07:24133 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29134 const DisplayMode& old_resolution,
135 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44136 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();
oshima0b9377aa2015-04-23 08:07:43143 if (!gfx::Display::HasInternalDisplay() &&
[email protected]6ef71d72013-08-10 18:13:44144 display_manager->num_connected_displays() == 1u) {
145 timeout_count = kTimeoutInSec;
146 }
147}
148
149ResolutionNotificationController::ResolutionChangeInfo::
150 ~ResolutionChangeInfo() {
151}
152
153ResolutionNotificationController::ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52154 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
scottmge8b042972016-01-27 05:07:35155 gfx::Screen::GetScreen()->AddObserver(this);
[email protected]6ef71d72013-08-10 18:13:44156}
157
158ResolutionNotificationController::~ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52159 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
scottmge8b042972016-01-27 05:07:35160 gfx::Screen::GetScreen()->RemoveObserver(this);
[email protected]6ef71d72013-08-10 18:13:44161}
162
[email protected]7badf7f2014-08-06 04:38:29163void ResolutionNotificationController::PrepareNotification(
avidb567a8a2015-12-20 17:07:24164 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29165 const DisplayMode& old_resolution,
166 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44167 const base::Closure& accept_callback) {
mukai02322192015-10-23 01:03:13168 DCHECK(!gfx::Display::IsInternalDisplayId(display_id));
[email protected]6ef71d72013-08-10 18:13:44169 // 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]7badf7f2014-08-06 04:38:29172 DisplayMode original_resolution;
[email protected]6ef71d72013-08-10 18:13:44173 if (change_info_ && change_info_->display_id == display_id) {
[email protected]7badf7f2014-08-06 04:38:29174 DCHECK(change_info_->new_resolution.size == old_resolution.size);
[email protected]6ef71d72013-08-10 18:13:44175 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]7badf7f2014-08-06 04:38:29180 if (!original_resolution.size.IsEmpty())
[email protected]6ef71d72013-08-10 18:13:44181 change_info_->old_resolution = original_resolution;
[email protected]6ef71d72013-08-10 18:13:44182}
183
184bool ResolutionNotificationController::DoesNotificationTimeout() {
185 return change_info_ && change_info_->timeout_count > 0;
186}
187
[email protected]86eee6e2013-08-27 01:27:15188void ResolutionNotificationController::CreateOrUpdateNotification(
189 bool enable_spoken_feedback) {
[email protected]6ef71d72013-08-10 18:13:44190 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]b6cc963b2014-02-27 15:32:23204 ui::TimeFormat::Simple(
205 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
[email protected]6ef71d72013-08-10 18:13:44206 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]86eee6e2013-08-27 01:27:15211 data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
212
[email protected]c91bd6c2014-01-22 11:51:10213 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]7badf7f2014-08-06 04:38:29217 (change_info_->new_resolution.size ==
218 change_info_->current_resolution.size) ?
[email protected]c91bd6c2014-01-22 11:51:10219 l10n_util::GetStringFUTF16(
220 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
221 display_name,
[email protected]7badf7f2014-08-06 04:38:29222 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) :
[email protected]c91bd6c2014-01-22 11:51:10223 l10n_util::GetStringFUTF16(
224 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
225 display_name,
[email protected]7badf7f2014-08-06 04:38:29226 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
227 base::UTF8ToUTF16(change_info_->current_resolution.size.ToString()));
[email protected]c91bd6c2014-01-22 11:51:10228
[email protected]6ef71d72013-08-10 18:13:44229 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
230 scoped_ptr<Notification> notification(new Notification(
miguelg53a6dde52015-08-20 09:00:30231 message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, message,
232 timeout_message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
233 base::string16() /* display_source */, GURL(),
[email protected]b78d79a52013-09-12 01:52:21234 message_center::NotifierId(
[email protected]de22ffea2013-12-07 03:34:29235 message_center::NotifierId::SYSTEM_COMPONENT,
236 system_notifier::kNotifierDisplayResolutionChange),
miguelg53a6dde52015-08-20 09:00:30237 data, new ResolutionChangeNotificationDelegate(
238 this, change_info_->timeout_count > 0)));
[email protected]6ef71d72013-08-10 18:13:44239 notification->SetSystemPriority();
dcheng22724952015-12-31 03:17:54240 message_center->AddNotification(std::move(notification));
[email protected]6ef71d72013-08-10 18:13:44241}
242
243void 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]86eee6e2013-08-27 01:27:15251 CreateOrUpdateNotification(false);
[email protected]6ef71d72013-08-10 18:13:44252}
253
[email protected]99e487e2013-08-13 20:58:41254void ResolutionNotificationController::AcceptResolutionChange(
255 bool close_notification) {
256 if (close_notification) {
257 message_center::MessageCenter::Get()->RemoveNotification(
258 kNotificationId, false /* by_user */);
259 }
oshima8f0f45e42015-04-03 19:55:22260 if (!change_info_)
261 return;
[email protected]6ef71d72013-08-10 18:13:44262 base::Closure callback = change_info_->accept_callback;
263 change_info_.reset();
264 callback.Run();
265}
266
267void ResolutionNotificationController::RevertResolutionChange() {
268 message_center::MessageCenter::Get()->RemoveNotification(
269 kNotificationId, false /* by_user */);
oshima8f0f45e42015-04-03 19:55:22270 if (!change_info_)
271 return;
avidb567a8a2015-12-20 17:07:24272 int64_t display_id = change_info_->display_id;
[email protected]7badf7f2014-08-06 04:38:29273 DisplayMode old_resolution = change_info_->old_resolution;
[email protected]6ef71d72013-08-10 18:13:44274 change_info_.reset();
[email protected]7badf7f2014-08-06 04:38:29275 Shell::GetInstance()->display_manager()->SetDisplayMode(
[email protected]6ef71d72013-08-10 18:13:44276 display_id, old_resolution);
277}
278
[email protected]6ef71d72013-08-10 18:13:44279void ResolutionNotificationController::OnDisplayAdded(
280 const gfx::Display& new_display) {
281}
282
283void 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]0c5703d2014-05-22 01:26:01289void ResolutionNotificationController::OnDisplayMetricsChanged(
290 const gfx::Display&, uint32_t) {
291}
292
[email protected]6ef71d72013-08-10 18:13:44293void ResolutionNotificationController::OnDisplayConfigurationChanged() {
294 if (!change_info_)
295 return;
296
[email protected]7badf7f2014-08-06 04:38:29297 change_info_->current_resolution = Shell::GetInstance()->display_manager()->
298 GetActiveModeForDisplayId(change_info_->display_id);
[email protected]86eee6e2013-08-27 01:27:15299 CreateOrUpdateNotification(true);
[email protected]6ef71d72013-08-10 18:13:44300 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
308void ResolutionNotificationController::SuppressTimerForTest() {
309 g_use_timer = false;
310}
311
[email protected]6ef71d72013-08-10 18:13:44312} // namespace ash