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#ifndef ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_
6#define ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_
7
8#include "ash/ash_export.h"
9#include "ash/display/display_controller.h"
10#include "base/callback.h"
11#include "base/gtest_prod_util.h"
12#include "base/timer/timer.h"
13#include "ui/gfx/display_observer.h"
14#include "ui/gfx/size.h"
15
16namespace chromeos {
17FORWARD_DECLARE_TEST(DisplayPreferencesTest, PreventStore);
18}  // namespace chromeos
19
20namespace views {
21class Label;
22class Widget;
23}  // namespace views
24
25namespace ash {
26
27struct DisplayMode;
28
29// A class which manages the notification of display resolution change and
30// also manages the timeout in case the new resolution is unusable.
31class ASH_EXPORT ResolutionNotificationController
32    : public gfx::DisplayObserver,
33      public DisplayController::Observer {
34 public:
35  ResolutionNotificationController();
36  virtual ~ResolutionNotificationController();
37
38  // Prepare a resolution change notification for |display_id| from
39  // |old_resolution| to |new_resolution|, which offers a button to revert the
40  // change in case something goes wrong. The notification times out if there's
41  // only one display connected and the user is trying to modify its resolution.
42  // In that case, the timeout has to be set since the user cannot make any
43  // changes if something goes wrong.
44  //
45  // This method does not create a notification itself. The notification will be
46  // created the next OnDisplayConfigurationChanged(), which will be called
47  // asynchronously after the resolution change is requested. So typically this
48  // method will be combined with resolution change methods like
49  // DisplayManager::SetDisplayMode().
50  void PrepareNotification(int64 display_id,
51                           const DisplayMode& old_resolution,
52                           const DisplayMode& new_resolution,
53                           const base::Closure& accept_callback);
54
55  // Returns true if the notification is visible or scheduled to be visible and
56  // the notification times out.
57  bool DoesNotificationTimeout();
58
59  // Called by the notification delegate when the user accepts the display
60  // resolution change. Set |close_notification| to true when the notification
61  // should be removed.
62  void AcceptResolutionChange(bool close_notification);
63
64  // Called by the notification delegate when the user wants to revert the
65  // display resolution change.
66  void RevertResolutionChange();
67
68 private:
69  friend class ResolutionNotificationControllerTest;
70  FRIEND_TEST_ALL_PREFIXES(ResolutionNotificationControllerTest, Timeout);
71  FRIEND_TEST_ALL_PREFIXES(chromeos::DisplayPreferencesTest, PreventStore);
72
73  // A struct to bundle the data for a single resolution change.
74  struct ResolutionChangeInfo;
75
76  static const int kTimeoutInSec;
77  static const char kNotificationId[];
78
79  // Create a new notification, or update its content if it already exists.
80  // |enable_spoken_feedback| is set to false when the notification is updated
81  // during the countdown so the update isn't necessarily read by the spoken
82  // feedback.
83  void CreateOrUpdateNotification(bool enable_spoken_feedback);
84
85  // Called every second for timeout.
86  void OnTimerTick();
87
88  // gfx::DisplayObserver overrides:
89  virtual void OnDisplayAdded(const gfx::Display& new_display) OVERRIDE;
90  virtual void OnDisplayRemoved(const gfx::Display& old_display) OVERRIDE;
91  virtual void OnDisplayMetricsChanged(const gfx::Display& display,
92                                       uint32_t metrics) OVERRIDE;
93
94  // DisplayController::Observer overrides:
95  virtual void OnDisplayConfigurationChanged() OVERRIDE;
96
97  static void SuppressTimerForTest();
98
99  scoped_ptr<ResolutionChangeInfo> change_info_;
100
101  DISALLOW_COPY_AND_ASSIGN(ResolutionNotificationController);
102};
103
104}  // namespace ash
105
106#endif  // ASH_DISPLAY_RESOLUTION_NOTIFICATION_CONTROLLER_H_
107