resolution_notification_controller.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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"
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"
22
23using message_center::Notification;
24
25namespace ash {
26namespace internal {
27namespace {
28
29bool g_use_timer = true;
30
31class ResolutionChangeNotificationDelegate
32    : public message_center::NotificationDelegate {
33 public:
34  ResolutionChangeNotificationDelegate(
35      ResolutionNotificationController* controller,
36      bool has_timeout);
37
38 protected:
39  virtual ~ResolutionChangeNotificationDelegate();
40
41 private:
42  // message_center::NotificationDelegate overrides:
43  virtual void Display() OVERRIDE;
44  virtual void Error() OVERRIDE;
45  virtual void Close(bool by_user) OVERRIDE;
46  virtual void Click() OVERRIDE;
47  virtual bool HasClickedListener() OVERRIDE;
48  virtual void ButtonClick(int button_index) OVERRIDE;
49
50  ResolutionNotificationController* controller_;
51  bool has_timeout_;
52
53  DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
54};
55
56ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
57    ResolutionNotificationController* controller,
58    bool has_timeout)
59    : controller_(controller),
60      has_timeout_(has_timeout) {
61  DCHECK(controller_);
62}
63
64ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
65}
66
67void ResolutionChangeNotificationDelegate::Display() {
68}
69
70void ResolutionChangeNotificationDelegate::Error() {
71}
72
73void ResolutionChangeNotificationDelegate::Close(bool by_user) {
74  if (by_user)
75    controller_->AcceptResolutionChange(false);
76}
77
78void ResolutionChangeNotificationDelegate::Click() {
79  controller_->AcceptResolutionChange(true);
80}
81
82bool ResolutionChangeNotificationDelegate::HasClickedListener() {
83  return true;
84}
85
86void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) {
87  // If there's the timeout, the first button is "Accept". Otherwise the
88  // button click should be "Revert".
89  if (has_timeout_ && button_index == 0)
90    controller_->AcceptResolutionChange(true);
91  else
92    controller_->RevertResolutionChange();
93}
94
95}  // namespace
96
97// static
98const int ResolutionNotificationController::kTimeoutInSec = 15;
99
100// static
101const char ResolutionNotificationController::kNotificationId[] =
102    "chrome://settings/display/resolution";
103
104struct ResolutionNotificationController::ResolutionChangeInfo {
105  ResolutionChangeInfo(int64 display_id,
106                       const gfx::Size& old_resolution,
107                       const gfx::Size& new_resolution,
108                       const base::Closure& accept_callback);
109  ~ResolutionChangeInfo();
110
111  // The id of the display where the resolution change happens.
112  int64 display_id;
113
114  // The resolution before the change.
115  gfx::Size old_resolution;
116
117  // The requested resolution. Note that this may be different from
118  // |current_resolution| which is the actual resolution set.
119  gfx::Size new_resolution;
120
121  // The actual resolution after the change.
122  gfx::Size current_resolution;
123
124  // The callback when accept is chosen.
125  base::Closure accept_callback;
126
127  // The remaining timeout in seconds. 0 if the change does not time out.
128  uint8 timeout_count;
129
130  // The timer to invoke OnTimerTick() every second. This cannot be
131  // OneShotTimer since the message contains text "automatically closed in xx
132  // seconds..." which has to be updated every second.
133  base::RepeatingTimer<ResolutionNotificationController> timer;
134
135 private:
136  DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
137};
138
139ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
140    int64 display_id,
141    const gfx::Size& old_resolution,
142    const gfx::Size& new_resolution,
143    const base::Closure& accept_callback)
144    : display_id(display_id),
145      old_resolution(old_resolution),
146      new_resolution(new_resolution),
147      accept_callback(accept_callback),
148      timeout_count(0) {
149  DisplayManager* display_manager = Shell::GetInstance()->display_manager();
150  if (!display_manager->HasInternalDisplay() &&
151      display_manager->num_connected_displays() == 1u) {
152    timeout_count = kTimeoutInSec;
153  }
154}
155
156ResolutionNotificationController::ResolutionChangeInfo::
157    ~ResolutionChangeInfo() {
158}
159
160ResolutionNotificationController::ResolutionNotificationController() {
161  Shell::GetInstance()->display_controller()->AddObserver(this);
162  Shell::GetScreen()->AddObserver(this);
163}
164
165ResolutionNotificationController::~ResolutionNotificationController() {
166  Shell::GetInstance()->display_controller()->RemoveObserver(this);
167  Shell::GetScreen()->RemoveObserver(this);
168}
169
170void ResolutionNotificationController::SetDisplayResolutionAndNotify(
171    int64 display_id,
172    const gfx::Size& old_resolution,
173    const gfx::Size& new_resolution,
174    const base::Closure& accept_callback) {
175  // If multiple resolution changes are invoked for the same display,
176  // the original resolution for the first resolution change has to be used
177  // instead of the specified |old_resolution|.
178  gfx::Size original_resolution;
179  if (change_info_ && change_info_->display_id == display_id) {
180    DCHECK(change_info_->new_resolution == old_resolution);
181    original_resolution = change_info_->old_resolution;
182  }
183
184  change_info_.reset(new ResolutionChangeInfo(
185      display_id, old_resolution, new_resolution, accept_callback));
186  if (!original_resolution.IsEmpty())
187    change_info_->old_resolution = original_resolution;
188
189  // SetDisplayResolution() causes OnConfigurationChanged() and the notification
190  // will be shown at that point.
191  Shell::GetInstance()->display_manager()->SetDisplayResolution(
192      display_id, new_resolution);
193}
194
195bool ResolutionNotificationController::DoesNotificationTimeout() {
196  return change_info_ && change_info_->timeout_count > 0;
197}
198
199void ResolutionNotificationController::CreateOrUpdateNotification(
200    bool enable_spoken_feedback) {
201  message_center::MessageCenter* message_center =
202      message_center::MessageCenter::Get();
203  if (!change_info_) {
204    message_center->RemoveNotification(kNotificationId, false /* by_user */);
205    return;
206  }
207
208  base::string16 timeout_message;
209  message_center::RichNotificationData data;
210  if (change_info_->timeout_count > 0) {
211    data.buttons.push_back(message_center::ButtonInfo(
212        l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_ACCEPT)));
213    timeout_message = l10n_util::GetStringFUTF16(
214        IDS_ASH_DISPLAY_RESOLUTION_TIMEOUT,
215        ui::TimeFormat::TimeDurationLong(
216            base::TimeDelta::FromSeconds(change_info_->timeout_count)));
217  }
218  data.buttons.push_back(message_center::ButtonInfo(
219        l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_RESOLUTION_CHANGE_REVERT)));
220
221  data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
222
223  const base::string16 display_name = base::UTF8ToUTF16(
224      Shell::GetInstance()->display_manager()->GetDisplayNameForId(
225          change_info_->display_id));
226  const base::string16 message =
227      (change_info_->new_resolution == change_info_->current_resolution) ?
228      l10n_util::GetStringFUTF16(
229          IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
230          display_name,
231          base::UTF8ToUTF16(change_info_->new_resolution.ToString())) :
232      l10n_util::GetStringFUTF16(
233          IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
234          display_name,
235          base::UTF8ToUTF16(change_info_->new_resolution.ToString()),
236          base::UTF8ToUTF16(change_info_->current_resolution.ToString()));
237
238  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
239  scoped_ptr<Notification> notification(new Notification(
240      message_center::NOTIFICATION_TYPE_SIMPLE,
241      kNotificationId,
242      message,
243      timeout_message,
244      bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
245      base::string16() /* display_source */,
246      message_center::NotifierId(
247          message_center::NotifierId::SYSTEM_COMPONENT,
248          system_notifier::kNotifierDisplayResolutionChange),
249      data,
250      new ResolutionChangeNotificationDelegate(
251          this, change_info_->timeout_count > 0)));
252  notification->SetSystemPriority();
253  message_center->AddNotification(notification.Pass());
254}
255
256void ResolutionNotificationController::OnTimerTick() {
257  if (!change_info_)
258    return;
259
260  --change_info_->timeout_count;
261  if (change_info_->timeout_count == 0)
262    RevertResolutionChange();
263  else
264    CreateOrUpdateNotification(false);
265}
266
267void ResolutionNotificationController::AcceptResolutionChange(
268    bool close_notification) {
269  if (close_notification) {
270    message_center::MessageCenter::Get()->RemoveNotification(
271        kNotificationId, false /* by_user */);
272  }
273  base::Closure callback = change_info_->accept_callback;
274  change_info_.reset();
275  callback.Run();
276}
277
278void ResolutionNotificationController::RevertResolutionChange() {
279  message_center::MessageCenter::Get()->RemoveNotification(
280      kNotificationId, false /* by_user */);
281  int64 display_id = change_info_->display_id;
282  gfx::Size old_resolution = change_info_->old_resolution;
283  change_info_.reset();
284  Shell::GetInstance()->display_manager()->SetDisplayResolution(
285      display_id, old_resolution);
286}
287
288void ResolutionNotificationController::OnDisplayBoundsChanged(
289    const gfx::Display& display) {
290}
291
292void ResolutionNotificationController::OnDisplayAdded(
293    const gfx::Display& new_display) {
294}
295
296void ResolutionNotificationController::OnDisplayRemoved(
297    const gfx::Display& old_display) {
298  if (change_info_ && change_info_->display_id == old_display.id())
299    RevertResolutionChange();
300}
301
302void ResolutionNotificationController::OnDisplayConfigurationChanged() {
303  if (!change_info_)
304    return;
305
306  const DisplayInfo& info = Shell::GetInstance()->display_manager()->
307      GetDisplayInfo(change_info_->display_id);
308  change_info_->current_resolution = info.bounds_in_native().size();
309  CreateOrUpdateNotification(true);
310  if (g_use_timer && change_info_->timeout_count > 0) {
311    change_info_->timer.Start(FROM_HERE,
312                              base::TimeDelta::FromSeconds(1),
313                              this,
314                              &ResolutionNotificationController::OnTimerTick);
315  }
316}
317
318void ResolutionNotificationController::SuppressTimerForTest() {
319  g_use_timer = false;
320}
321
322}  // namespace internal
323}  // namespace ash
324