Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / display / resolution_notification_controller.cc
blobb2238c884395c2b149cd963922ff8c9a5bbfb11e
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.
5 #include "ash/display/resolution_notification_controller.h"
7 #include "ash/display/display_info.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/shell.h"
10 #include "ash/system/system_notifier.h"
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"
23 using message_center::Notification;
25 namespace ash {
26 namespace {
28 bool g_use_timer = true;
30 class ResolutionChangeNotificationDelegate
31 : public message_center::NotificationDelegate {
32 public:
33 ResolutionChangeNotificationDelegate(
34 ResolutionNotificationController* controller,
35 bool has_timeout);
37 protected:
38 ~ResolutionChangeNotificationDelegate() override;
40 private:
41 // message_center::NotificationDelegate overrides:
42 void Close(bool by_user) override;
43 void Click() override;
44 bool HasClickedListener() override;
45 void ButtonClick(int button_index) override;
47 ResolutionNotificationController* controller_;
48 bool has_timeout_;
50 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
53 ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
54 ResolutionNotificationController* controller,
55 bool has_timeout)
56 : controller_(controller),
57 has_timeout_(has_timeout) {
58 DCHECK(controller_);
61 ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
64 void ResolutionChangeNotificationDelegate::Close(bool by_user) {
65 if (by_user)
66 controller_->AcceptResolutionChange(false);
69 void ResolutionChangeNotificationDelegate::Click() {
70 controller_->AcceptResolutionChange(true);
73 bool ResolutionChangeNotificationDelegate::HasClickedListener() {
74 return true;
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)
81 controller_->AcceptResolutionChange(true);
82 else
83 controller_->RevertResolutionChange();
86 } // namespace
88 // static
89 const int ResolutionNotificationController::kTimeoutInSec = 15;
91 // static
92 const char ResolutionNotificationController::kNotificationId[] =
93 "chrome://settings/display/resolution";
95 struct ResolutionNotificationController::ResolutionChangeInfo {
96 ResolutionChangeInfo(int64 display_id,
97 const DisplayMode& old_resolution,
98 const DisplayMode& new_resolution,
99 const base::Closure& accept_callback);
100 ~ResolutionChangeInfo();
102 // The id of the display where the resolution change happens.
103 int64 display_id;
105 // The resolution before the change.
106 DisplayMode old_resolution;
108 // The requested resolution. Note that this may be different from
109 // |current_resolution| which is the actual resolution set.
110 DisplayMode new_resolution;
112 // The actual resolution after the change.
113 DisplayMode current_resolution;
115 // The callback when accept is chosen.
116 base::Closure accept_callback;
118 // The remaining timeout in seconds. 0 if the change does not time out.
119 uint8 timeout_count;
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.
124 base::RepeatingTimer<ResolutionNotificationController> timer;
126 private:
127 DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
130 ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
131 int64 display_id,
132 const DisplayMode& old_resolution,
133 const DisplayMode& new_resolution,
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();
141 if (!gfx::Display::HasInternalDisplay() &&
142 display_manager->num_connected_displays() == 1u) {
143 timeout_count = kTimeoutInSec;
147 ResolutionNotificationController::ResolutionChangeInfo::
148 ~ResolutionChangeInfo() {
151 ResolutionNotificationController::ResolutionNotificationController() {
152 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
153 Shell::GetScreen()->AddObserver(this);
156 ResolutionNotificationController::~ResolutionNotificationController() {
157 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
158 Shell::GetScreen()->RemoveObserver(this);
161 void ResolutionNotificationController::PrepareNotification(
162 int64 display_id,
163 const DisplayMode& old_resolution,
164 const DisplayMode& new_resolution,
165 const base::Closure& accept_callback) {
166 // If multiple resolution changes are invoked for the same display,
167 // the original resolution for the first resolution change has to be used
168 // instead of the specified |old_resolution|.
169 DisplayMode original_resolution;
170 if (change_info_ && change_info_->display_id == display_id) {
171 DCHECK(change_info_->new_resolution.size == old_resolution.size);
172 original_resolution = change_info_->old_resolution;
175 change_info_.reset(new ResolutionChangeInfo(
176 display_id, old_resolution, new_resolution, accept_callback));
177 if (!original_resolution.size.IsEmpty())
178 change_info_->old_resolution = original_resolution;
181 bool ResolutionNotificationController::DoesNotificationTimeout() {
182 return change_info_ && change_info_->timeout_count > 0;
185 void ResolutionNotificationController::CreateOrUpdateNotification(
186 bool enable_spoken_feedback) {
187 message_center::MessageCenter* message_center =
188 message_center::MessageCenter::Get();
189 if (!change_info_) {
190 message_center->RemoveNotification(kNotificationId, false /* by_user */);
191 return;
194 base::string16 timeout_message;
195 message_center::RichNotificationData data;
196 if (change_info_->timeout_count > 0) {
197 data.buttons.push_back(message_center::ButtonInfo(
198 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT)));
199 timeout_message = l10n_util::GetStringFUTF16(
200 IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT,
201 ui::TimeFormat::Simple(
202 ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
203 base::TimeDelta::FromSeconds(change_info_->timeout_count)));
205 data.buttons.push_back(message_center::ButtonInfo(
206 l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT)));
208 data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
210 const base::string16 display_name = base::UTF8ToUTF16(
211 Shell::GetInstance()->display_manager()->GetDisplayNameForId(
212 change_info_->display_id));
213 const base::string16 message =
214 (change_info_->new_resolution.size ==
215 change_info_->current_resolution.size) ?
216 l10n_util::GetStringFUTF16(
217 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
218 display_name,
219 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) :
220 l10n_util::GetStringFUTF16(
221 IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
222 display_name,
223 base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
224 base::UTF8ToUTF16(change_info_->current_resolution.size.ToString()));
226 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
227 scoped_ptr<Notification> notification(new Notification(
228 message_center::NOTIFICATION_TYPE_SIMPLE,
229 kNotificationId,
230 message,
231 timeout_message,
232 bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
233 base::string16() /* display_source */,
234 message_center::NotifierId(
235 message_center::NotifierId::SYSTEM_COMPONENT,
236 system_notifier::kNotifierDisplayResolutionChange),
237 data,
238 new ResolutionChangeNotificationDelegate(
239 this, change_info_->timeout_count > 0)));
240 notification->SetSystemPriority();
241 message_center->AddNotification(notification.Pass());
244 void ResolutionNotificationController::OnTimerTick() {
245 if (!change_info_)
246 return;
248 --change_info_->timeout_count;
249 if (change_info_->timeout_count == 0)
250 RevertResolutionChange();
251 else
252 CreateOrUpdateNotification(false);
255 void ResolutionNotificationController::AcceptResolutionChange(
256 bool close_notification) {
257 if (close_notification) {
258 message_center::MessageCenter::Get()->RemoveNotification(
259 kNotificationId, false /* by_user */);
261 if (!change_info_)
262 return;
263 base::Closure callback = change_info_->accept_callback;
264 change_info_.reset();
265 callback.Run();
268 void ResolutionNotificationController::RevertResolutionChange() {
269 message_center::MessageCenter::Get()->RemoveNotification(
270 kNotificationId, false /* by_user */);
271 if (!change_info_)
272 return;
273 int64 display_id = change_info_->display_id;
274 DisplayMode old_resolution = change_info_->old_resolution;
275 change_info_.reset();
276 Shell::GetInstance()->display_manager()->SetDisplayMode(
277 display_id, old_resolution);
280 void ResolutionNotificationController::OnDisplayAdded(
281 const gfx::Display& new_display) {
284 void ResolutionNotificationController::OnDisplayRemoved(
285 const gfx::Display& old_display) {
286 if (change_info_ && change_info_->display_id == old_display.id())
287 RevertResolutionChange();
290 void ResolutionNotificationController::OnDisplayMetricsChanged(
291 const gfx::Display&, uint32_t) {
294 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
295 if (!change_info_)
296 return;
298 change_info_->current_resolution = Shell::GetInstance()->display_manager()->
299 GetActiveModeForDisplayId(change_info_->display_id);
300 CreateOrUpdateNotification(true);
301 if (g_use_timer && change_info_->timeout_count > 0) {
302 change_info_->timer.Start(FROM_HERE,
303 base::TimeDelta::FromSeconds(1),
304 this,
305 &ResolutionNotificationController::OnTimerTick);
309 void ResolutionNotificationController::SuppressTimerForTest() {
310 g_use_timer = false;
313 } // namespace ash