Elim cr-checkbox
[chromium-blink-merge.git] / ash / display / resolution_notification_controller.cc
blob603d480a75069f3a97434ed885b27c1b1e1e6556
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, kNotificationId, message,
229 timeout_message, bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
230 base::string16() /* display_source */, GURL(),
231 message_center::NotifierId(
232 message_center::NotifierId::SYSTEM_COMPONENT,
233 system_notifier::kNotifierDisplayResolutionChange),
234 data, new ResolutionChangeNotificationDelegate(
235 this, change_info_->timeout_count > 0)));
236 notification->SetSystemPriority();
237 message_center->AddNotification(notification.Pass());
240 void ResolutionNotificationController::OnTimerTick() {
241 if (!change_info_)
242 return;
244 --change_info_->timeout_count;
245 if (change_info_->timeout_count == 0)
246 RevertResolutionChange();
247 else
248 CreateOrUpdateNotification(false);
251 void ResolutionNotificationController::AcceptResolutionChange(
252 bool close_notification) {
253 if (close_notification) {
254 message_center::MessageCenter::Get()->RemoveNotification(
255 kNotificationId, false /* by_user */);
257 if (!change_info_)
258 return;
259 base::Closure callback = change_info_->accept_callback;
260 change_info_.reset();
261 callback.Run();
264 void ResolutionNotificationController::RevertResolutionChange() {
265 message_center::MessageCenter::Get()->RemoveNotification(
266 kNotificationId, false /* by_user */);
267 if (!change_info_)
268 return;
269 int64 display_id = change_info_->display_id;
270 DisplayMode old_resolution = change_info_->old_resolution;
271 change_info_.reset();
272 Shell::GetInstance()->display_manager()->SetDisplayMode(
273 display_id, old_resolution);
276 void ResolutionNotificationController::OnDisplayAdded(
277 const gfx::Display& new_display) {
280 void ResolutionNotificationController::OnDisplayRemoved(
281 const gfx::Display& old_display) {
282 if (change_info_ && change_info_->display_id == old_display.id())
283 RevertResolutionChange();
286 void ResolutionNotificationController::OnDisplayMetricsChanged(
287 const gfx::Display&, uint32_t) {
290 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
291 if (!change_info_)
292 return;
294 change_info_->current_resolution = Shell::GetInstance()->display_manager()->
295 GetActiveModeForDisplayId(change_info_->display_id);
296 CreateOrUpdateNotification(true);
297 if (g_use_timer && change_info_->timeout_count > 0) {
298 change_info_->timer.Start(FROM_HERE,
299 base::TimeDelta::FromSeconds(1),
300 this,
301 &ResolutionNotificationController::OnTimerTick);
305 void ResolutionNotificationController::SuppressTimerForTest() {
306 g_use_timer = false;
309 } // namespace ash