Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / display / resolution_notification_controller.cc
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_info.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/shell.h"
11 #include "ash/system/system_notifier.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "grit/ash_resources.h"
14 #include "grit/ash_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/time_format.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/message_center/message_center.h"
21 #include "ui/message_center/notification.h"
22 #include "ui/message_center/notification_delegate.h"
23
24 using message_center::Notification;
25
26 namespace ash {
27 namespace {
28
29 bool g_use_timer = true;
30
31 class 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 Close(bool by_user) override;
44   virtual void Click() override;
45   virtual bool HasClickedListener() override;
46   virtual void ButtonClick(int button_index) override;
47
48   ResolutionNotificationController* controller_;
49   bool has_timeout_;
50
51   DISALLOW_COPY_AND_ASSIGN(ResolutionChangeNotificationDelegate);
52 };
53
54 ResolutionChangeNotificationDelegate::ResolutionChangeNotificationDelegate(
55     ResolutionNotificationController* controller,
56     bool has_timeout)
57     : controller_(controller),
58       has_timeout_(has_timeout) {
59   DCHECK(controller_);
60 }
61
62 ResolutionChangeNotificationDelegate::~ResolutionChangeNotificationDelegate() {
63 }
64
65 void ResolutionChangeNotificationDelegate::Close(bool by_user) {
66   if (by_user)
67     controller_->AcceptResolutionChange(false);
68 }
69
70 void ResolutionChangeNotificationDelegate::Click() {
71   controller_->AcceptResolutionChange(true);
72 }
73
74 bool ResolutionChangeNotificationDelegate::HasClickedListener() {
75   return true;
76 }
77
78 void ResolutionChangeNotificationDelegate::ButtonClick(int button_index) {
79   // If there's the timeout, the first button is "Accept". Otherwise the
80   // button click should be "Revert".
81   if (has_timeout_ && button_index == 0)
82     controller_->AcceptResolutionChange(true);
83   else
84     controller_->RevertResolutionChange();
85 }
86
87 }  // namespace
88
89 // static
90 const int ResolutionNotificationController::kTimeoutInSec = 15;
91
92 // static
93 const char ResolutionNotificationController::kNotificationId[] =
94     "chrome://settings/display/resolution";
95
96 struct ResolutionNotificationController::ResolutionChangeInfo {
97   ResolutionChangeInfo(int64 display_id,
98                        const DisplayMode& old_resolution,
99                        const DisplayMode& new_resolution,
100                        const base::Closure& accept_callback);
101   ~ResolutionChangeInfo();
102
103   // The id of the display where the resolution change happens.
104   int64 display_id;
105
106   // The resolution before the change.
107   DisplayMode old_resolution;
108
109   // The requested resolution. Note that this may be different from
110   // |current_resolution| which is the actual resolution set.
111   DisplayMode new_resolution;
112
113   // The actual resolution after the change.
114   DisplayMode current_resolution;
115
116   // The callback when accept is chosen.
117   base::Closure accept_callback;
118
119   // The remaining timeout in seconds. 0 if the change does not time out.
120   uint8 timeout_count;
121
122   // The timer to invoke OnTimerTick() every second. This cannot be
123   // OneShotTimer since the message contains text "automatically closed in xx
124   // seconds..." which has to be updated every second.
125   base::RepeatingTimer<ResolutionNotificationController> timer;
126
127  private:
128   DISALLOW_COPY_AND_ASSIGN(ResolutionChangeInfo);
129 };
130
131 ResolutionNotificationController::ResolutionChangeInfo::ResolutionChangeInfo(
132     int64 display_id,
133     const DisplayMode& old_resolution,
134     const DisplayMode& new_resolution,
135     const base::Closure& accept_callback)
136     : display_id(display_id),
137       old_resolution(old_resolution),
138       new_resolution(new_resolution),
139       accept_callback(accept_callback),
140       timeout_count(0) {
141   DisplayManager* display_manager = Shell::GetInstance()->display_manager();
142   if (!display_manager->HasInternalDisplay() &&
143       display_manager->num_connected_displays() == 1u) {
144     timeout_count = kTimeoutInSec;
145   }
146 }
147
148 ResolutionNotificationController::ResolutionChangeInfo::
149     ~ResolutionChangeInfo() {
150 }
151
152 ResolutionNotificationController::ResolutionNotificationController() {
153   Shell::GetInstance()->display_controller()->AddObserver(this);
154   Shell::GetScreen()->AddObserver(this);
155 }
156
157 ResolutionNotificationController::~ResolutionNotificationController() {
158   Shell::GetInstance()->display_controller()->RemoveObserver(this);
159   Shell::GetScreen()->RemoveObserver(this);
160 }
161
162 void ResolutionNotificationController::PrepareNotification(
163     int64 display_id,
164     const DisplayMode& old_resolution,
165     const DisplayMode& new_resolution,
166     const base::Closure& accept_callback) {
167   // 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|.
170   DisplayMode original_resolution;
171   if (change_info_ && change_info_->display_id == display_id) {
172     DCHECK(change_info_->new_resolution.size == old_resolution.size);
173     original_resolution = change_info_->old_resolution;
174   }
175
176   change_info_.reset(new ResolutionChangeInfo(
177       display_id, old_resolution, new_resolution, accept_callback));
178   if (!original_resolution.size.IsEmpty())
179     change_info_->old_resolution = original_resolution;
180 }
181
182 bool ResolutionNotificationController::DoesNotificationTimeout() {
183   return change_info_ && change_info_->timeout_count > 0;
184 }
185
186 void ResolutionNotificationController::CreateOrUpdateNotification(
187     bool enable_spoken_feedback) {
188   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,
202         ui::TimeFormat::Simple(
203             ui::TimeFormat::FORMAT_DURATION, ui::TimeFormat::LENGTH_LONG,
204             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
209   data.should_make_spoken_feedback_for_popup_updates = enable_spoken_feedback;
210
211   const base::string16 display_name = base::UTF8ToUTF16(
212       Shell::GetInstance()->display_manager()->GetDisplayNameForId(
213           change_info_->display_id));
214   const base::string16 message =
215       (change_info_->new_resolution.size ==
216        change_info_->current_resolution.size) ?
217       l10n_util::GetStringFUTF16(
218           IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED,
219           display_name,
220           base::UTF8ToUTF16(change_info_->new_resolution.size.ToString())) :
221       l10n_util::GetStringFUTF16(
222           IDS_ASH_STATUS_TRAY_DISPLAY_RESOLUTION_CHANGED_TO_UNSUPPORTED,
223           display_name,
224           base::UTF8ToUTF16(change_info_->new_resolution.size.ToString()),
225           base::UTF8ToUTF16(change_info_->current_resolution.size.ToString()));
226
227   ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
228   scoped_ptr<Notification> notification(new Notification(
229       message_center::NOTIFICATION_TYPE_SIMPLE,
230       kNotificationId,
231       message,
232       timeout_message,
233       bundle.GetImageNamed(IDR_AURA_NOTIFICATION_DISPLAY),
234       base::string16() /* display_source */,
235       message_center::NotifierId(
236           message_center::NotifierId::SYSTEM_COMPONENT,
237           system_notifier::kNotifierDisplayResolutionChange),
238       data,
239       new ResolutionChangeNotificationDelegate(
240           this, change_info_->timeout_count > 0)));
241   notification->SetSystemPriority();
242   message_center->AddNotification(notification.Pass());
243 }
244
245 void ResolutionNotificationController::OnTimerTick() {
246   if (!change_info_)
247     return;
248
249   --change_info_->timeout_count;
250   if (change_info_->timeout_count == 0)
251     RevertResolutionChange();
252   else
253     CreateOrUpdateNotification(false);
254 }
255
256 void ResolutionNotificationController::AcceptResolutionChange(
257     bool close_notification) {
258   if (close_notification) {
259     message_center::MessageCenter::Get()->RemoveNotification(
260         kNotificationId, false /* by_user */);
261   }
262   base::Closure callback = change_info_->accept_callback;
263   change_info_.reset();
264   callback.Run();
265 }
266
267 void ResolutionNotificationController::RevertResolutionChange() {
268   message_center::MessageCenter::Get()->RemoveNotification(
269       kNotificationId, false /* by_user */);
270   int64 display_id = change_info_->display_id;
271   DisplayMode old_resolution = change_info_->old_resolution;
272   change_info_.reset();
273   Shell::GetInstance()->display_manager()->SetDisplayMode(
274       display_id, old_resolution);
275 }
276
277 void ResolutionNotificationController::OnDisplayAdded(
278     const gfx::Display& new_display) {
279 }
280
281 void ResolutionNotificationController::OnDisplayRemoved(
282     const gfx::Display& old_display) {
283   if (change_info_ && change_info_->display_id == old_display.id())
284     RevertResolutionChange();
285 }
286
287 void ResolutionNotificationController::OnDisplayMetricsChanged(
288     const gfx::Display&, uint32_t) {
289 }
290
291 void ResolutionNotificationController::OnDisplayConfigurationChanged() {
292   if (!change_info_)
293     return;
294
295   change_info_->current_resolution = Shell::GetInstance()->display_manager()->
296       GetActiveModeForDisplayId(change_info_->display_id);
297   CreateOrUpdateNotification(true);
298   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
306 void ResolutionNotificationController::SuppressTimerForTest() {
307   g_use_timer = false;
308 }
309
310 }  // namespace ash