blob: bd31c958ec15f8e80657638a3b64530e9df0af25 [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
[email protected]7badf7f2014-08-06 04:38:297#include "ash/display/display_info.h"
[email protected]6ef71d72013-08-10 18:13:448#include "ash/display/display_manager.h"
9#include "ash/shell.h"
[email protected]a69b6f872013-08-24 14:38:3310#include "ash/system/system_notifier.h"
[email protected]6ef71d72013-08-10 18:13:4411#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
23using message_center::Notification;
24
25namespace ash {
[email protected]6ef71d72013-08-10 18:13:4426namespace {
27
28bool g_use_timer = true;
29
30class ResolutionChangeNotificationDelegate
31 : public message_center::NotificationDelegate {
32 public:
33 ResolutionChangeNotificationDelegate(
34 ResolutionNotificationController* controller,
35 bool has_timeout);
36
37 protected:
dcheng222b9c72015-01-16 00:48:0138 ~ResolutionChangeNotificationDelegate() override;
[email protected]6ef71d72013-08-10 18:13:4439
40 private:
41 // message_center::NotificationDelegate overrides:
dcheng222b9c72015-01-16 00:48:0142 void Close(bool by_user) override;
43 void Click() override;
44 bool HasClickedListener() override;
45 void ButtonClick(int button_index) override;
[email protected]6ef71d72013-08-10 18:13:4446
47 ResolutionNotificationController* controller_;
48 bool has_timeout_;
49
50 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
51};
52
53ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
54 ResolutionNotificationController* controller,
55 bool has_timeout)
56 : controller_(controller),
57 has_timeout_(has_timeout) {
58 DCHECK(controller_);
59}
60
61ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
62}
63
[email protected]6ef71d72013-08-10 18:13:4464void ResolutionChangeNotificationDelegate::Close(bool by_user) {
65 if (by_user)
[email protected]99e487e2013-08-13 20:58:4166 controller_->AcceptResolutionChange(false);
[email protected]6ef71d72013-08-10 18:13:4467}
68
69void ResolutionChangeNotificationDelegate::Click() {
[email protected]99e487e2013-08-13 20:58:4170 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4471}
72
73bool ResolutionChangeNotificationDelegate::HasClickedListener() {
74 return true;
75}
76
77void 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]99e487e2013-08-13 20:58:4181 controller_->AcceptResolutionChange(true);
[email protected]6ef71d72013-08-10 18:13:4482 else
83 controller_->RevertResolutionChange();
84}
85
86} // namespace
87
88// static
89const int ResolutionNotificationController::kTimeoutInSec = 15;
90
91// static
92const char ResolutionNotificationController::kNotificationId[] =
93 "chrome://settings/display/resolution";
94
95struct ResolutionNotificationController::ResolutionChangeInfo {
avidb567a8a2015-12-20 17:07:2496 ResolutionChangeInfo(int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:2997 const DisplayMode& old_resolution,
98 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:4499 const base::Closure& accept_callback);
100 ~ResolutionChangeInfo();
101
102 // The id of the display where the resolution change happens.
avidb567a8a2015-12-20 17:07:24103 int64_t display_id;
[email protected]6ef71d72013-08-10 18:13:44104
105 // The resolution before the change.
[email protected]7badf7f2014-08-06 04:38:29106 DisplayMode old_resolution;
[email protected]6ef71d72013-08-10 18:13:44107
[email protected]c91bd6c2014-01-22 11:51:10108 // The requested resolution. Note that this may be different from
109 // |current_resolution| which is the actual resolution set.
[email protected]7badf7f2014-08-06 04:38:29110 DisplayMode new_resolution;
[email protected]6ef71d72013-08-10 18:13:44111
[email protected]c91bd6c2014-01-22 11:51:10112 // The actual resolution after the change.
[email protected]7badf7f2014-08-06 04:38:29113 DisplayMode current_resolution;
[email protected]c91bd6c2014-01-22 11:51:10114
[email protected]6ef71d72013-08-10 18:13:44115 // 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.
avidb567a8a2015-12-20 17:07:24119 uint8_t timeout_count;
[email protected]6ef71d72013-08-10 18:13:44120
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.
danakj8c3eb802015-09-24 07:53:00124 base::RepeatingTimer timer;
[email protected]6ef71d72013-08-10 18:13:44125
126 private:
127 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
128};
129
130ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
avidb567a8a2015-12-20 17:07:24131 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29132 const DisplayMode& old_resolution,
133 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44134 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();
oshima0b9377aa2015-04-23 08:07:43141 if (!gfx::Display::HasInternalDisplay() &&
[email protected]6ef71d72013-08-10 18:13:44142 display_manager->num_connected_displays() == 1u) {
143 timeout_count = kTimeoutInSec;
144 }
145}
146
147ResolutionNotificationController::ResolutionChangeInfo::
148 ~ResolutionChangeInfo() {
149}
150
151ResolutionNotificationController::ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52152 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
[email protected]6ef71d72013-08-10 18:13:44153 Shell::GetScreen()->AddObserver(this);
154}
155
156ResolutionNotificationController::~ResolutionNotificationController() {
oshimae2818922015-07-28 01:18:52157 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
[email protected]6ef71d72013-08-10 18:13:44158 Shell::GetScreen()->RemoveObserver(this);
159}
160
[email protected]7badf7f2014-08-06 04:38:29161void ResolutionNotificationController::PrepareNotification(
avidb567a8a2015-12-20 17:07:24162 int64_t display_id,
[email protected]7badf7f2014-08-06 04:38:29163 const DisplayMode& old_resolution,
164 const DisplayMode& new_resolution,
[email protected]6ef71d72013-08-10 18:13:44165 const base::Closure& accept_callback) {
mukai02322192015-10-23 01:03:13166 DCHECK(!gfx::Display::IsInternalDisplayId(display_id));
[email protected]6ef71d72013-08-10 18:13:44167 // 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]7badf7f2014-08-06 04:38:29170 DisplayMode original_resolution;
[email protected]6ef71d72013-08-10 18:13:44171 if (change_info_ && change_info_->display_id == display_id) {
[email protected]7badf7f2014-08-06 04:38:29172 DCHECK(change_info_->new_resolution.size == old_resolution.size);
[email protected]6ef71d72013-08-10 18:13:44173 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]7badf7f2014-08-06 04:38:29178 if (!original_resolution.size.IsEmpty())
[email protected]6ef71d72013-08-10 18:13:44179 change_info_->old_resolution = original_resolution;
[email protected]6ef71d72013-08-10 18:13:44180}
181
182bool ResolutionNotificationController::DoesNotificationTimeout() {
183 return change_info_ && change_info_->timeout_count > 0;
184}
185
[email protected]86eee6e2013-08-27 01:27:15186void ResolutionNotificationController::CreateOrUpdateNotification(
187 bool enable_spoken_feedback) {
[email protected]6ef71d72013-08-10 18:13:44188 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]b6cc963b2014-02-27 15:32:23202 ui::TimeFormat::Simple(
203 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
[email protected]6ef71d72013-08-10 18:13:44204 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]86eee6e2013-08-27 01:27:15209 data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
210
[email protected]c91bd6c2014-01-22 11:51:10211 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]7badf7f2014-08-06 04:38:29215 (change_info_->new_resolution.size ==
216 change_info_->current_resolution.size) ?
[email protected]c91bd6c2014-01-22 11:51:10217 l10n_util::GetStringFUTF16(
218 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
219 display_name,
[email protected]7badf7f2014-08-06 04:38:29220 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) :
[email protected]c91bd6c2014-01-22 11:51:10221 l10n_util::GetStringFUTF16(
222 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
223 display_name,
[email protected]7badf7f2014-08-06 04:38:29224 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
225 base::UTF8ToUTF16(change_info_->current_resolution.size.ToString()));
[email protected]c91bd6c2014-01-22 11:51:10226
[email protected]6ef71d72013-08-10 18:13:44227 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
228 scoped_ptr<Notification> notification(new Notification(
miguelg53a6dde52015-08-20 09:00:30229 message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId, message,
230 timeout_message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
231 base::string16() /* display_source */, GURL(),
[email protected]b78d79a52013-09-12 01:52:21232 message_center::NotifierId(
[email protected]de22ffea2013-12-07 03:34:29233 message_center::NotifierId::SYSTEM_COMPONENT,
234 system_notifier::kNotifierDisplayResolutionChange),
miguelg53a6dde52015-08-20 09:00:30235 data, new ResolutionChangeNotificationDelegate(
236 this, change_info_->timeout_count > 0)));
[email protected]6ef71d72013-08-10 18:13:44237 notification->SetSystemPriority();
238 message_center->AddNotification(notification.Pass());
239}
240
241void 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]86eee6e2013-08-27 01:27:15249 CreateOrUpdateNotification(false);
[email protected]6ef71d72013-08-10 18:13:44250}
251
[email protected]99e487e2013-08-13 20:58:41252void ResolutionNotificationController::AcceptResolutionChange(
253 bool close_notification) {
254 if (close_notification) {
255 message_center::MessageCenter::Get()->RemoveNotification(
256 kNotificationId, false /* by_user */);
257 }
oshima8f0f45e42015-04-03 19:55:22258 if (!change_info_)
259 return;
[email protected]6ef71d72013-08-10 18:13:44260 base::Closure callback = change_info_->accept_callback;
261 change_info_.reset();
262 callback.Run();
263}
264
265void ResolutionNotificationController::RevertResolutionChange() {
266 message_center::MessageCenter::Get()->RemoveNotification(
267 kNotificationId, false /* by_user */);
oshima8f0f45e42015-04-03 19:55:22268 if (!change_info_)
269 return;
avidb567a8a2015-12-20 17:07:24270 int64_t display_id = change_info_->display_id;
[email protected]7badf7f2014-08-06 04:38:29271 DisplayMode old_resolution = change_info_->old_resolution;
[email protected]6ef71d72013-08-10 18:13:44272 change_info_.reset();
[email protected]7badf7f2014-08-06 04:38:29273 Shell::GetInstance()->display_manager()->SetDisplayMode(
[email protected]6ef71d72013-08-10 18:13:44274 display_id, old_resolution);
275}
276
[email protected]6ef71d72013-08-10 18:13:44277void ResolutionNotificationController::OnDisplayAdded(
278 const gfx::Display& new_display) {
279}
280
281void 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]0c5703d2014-05-22 01:26:01287void ResolutionNotificationController::OnDisplayMetricsChanged(
288 const gfx::Display&, uint32_t) {
289}
290
[email protected]6ef71d72013-08-10 18:13:44291void ResolutionNotificationController::OnDisplayConfigurationChanged() {
292 if (!change_info_)
293 return;
294
[email protected]7badf7f2014-08-06 04:38:29295 change_info_->current_resolution = Shell::GetInstance()->display_manager()->
296 GetActiveModeForDisplayId(change_info_->display_id);
[email protected]86eee6e2013-08-27 01:27:15297 CreateOrUpdateNotification(true);
[email protected]6ef71d72013-08-10 18:13:44298 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
306void ResolutionNotificationController::SuppressTimerForTest() {
307 g_use_timer = false;
308}
309
[email protected]6ef71d72013-08-10 18:13:44310} // namespace ash