Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / wm / maximize_mode / maximize_mode_controller.h
1 // Copyright 2014 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_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
6 #define ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_
7
8 #include "ash/accelerometer/accelerometer_observer.h"
9 #include "ash/ash_export.h"
10 #include "ash/display/display_controller.h"
11 #include "ash/display/display_manager.h"
12 #include "ash/shell_observer.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "ui/gfx/display.h"
17
18 #if defined(OS_CHROMEOS)
19 #include "chromeos/dbus/power_manager_client.h"
20 #endif  // OS_CHROMEOS
21
22 namespace base {
23 class TickClock;
24 }
25
26 namespace ui {
27 class EventHandler;
28 }
29
30 namespace ash {
31
32 class MaximizeModeControllerTest;
33 class ScopedDisableInternalMouseAndKeyboard;
34 class MaximizeModeWindowManager;
35 class MaximizeModeWindowManagerTest;
36 namespace test {
37 class MultiUserWindowManagerChromeOSTest;
38 }
39
40 // MaximizeModeController listens to accelerometer events and automatically
41 // enters and exits maximize mode when the lid is opened beyond the triggering
42 // angle and rotates the display to match the device when in maximize mode.
43 class ASH_EXPORT MaximizeModeController
44     : public AccelerometerObserver,
45 #if defined(OS_CHROMEOS)
46       public chromeos::PowerManagerClient::Observer,
47 #endif  // OS_CHROMEOS
48       public ShellObserver,
49       public DisplayController::Observer {
50  public:
51   // Observer that reports changes to the state of MaximizeModeController's
52   // rotation lock.
53   class Observer {
54    public:
55     // Invoked whenever |rotation_locked_| is changed.
56     virtual void OnRotationLockChanged(bool rotation_locked) {}
57
58    protected:
59     virtual ~Observer() {}
60   };
61
62   MaximizeModeController();
63   ~MaximizeModeController() override;
64
65   bool ignore_display_configuration_updates() const {
66     return ignore_display_configuration_updates_;
67   }
68
69   // True if |rotation_lock_| has been set, and OnAccelerometerUpdated will not
70   // change the display rotation.
71   bool rotation_locked() {
72     return rotation_locked_;
73   }
74
75   // If |rotation_locked| future calls to OnAccelerometerUpdated will not
76   // change the display rotation.
77   void SetRotationLocked(bool rotation_locked);
78
79   // Add/Remove observers.
80   void AddObserver(Observer* observer);
81   void RemoveObserver(Observer* observer);
82
83   // True if it is possible to enter maximize mode in the current
84   // configuration. If this returns false, it should never be the case that
85   // maximize mode becomes enabled.
86   bool CanEnterMaximizeMode();
87
88   // TODO(jonross): Merge this with EnterMaximizeMode. Currently these are
89   // separate for several reasons: there is no internal display when running
90   // unittests; the event blocker prevents keyboard input when running ChromeOS
91   // on linux. http://crbug.com/362881
92   // Turn the always maximize mode window manager on or off.
93   void EnableMaximizeModeWindowManager(bool enable);
94
95   // Test if the MaximizeModeWindowManager is enabled or not.
96   bool IsMaximizeModeWindowManagerEnabled() const;
97
98   // Add a special window to the MaximizeModeWindowManager for tracking. This is
99   // only required for special windows which are handled by other window
100   // managers like the |MultiUserWindowManager|.
101   // If the maximize mode is not enabled no action will be performed.
102   void AddWindow(aura::Window* window);
103
104   // TODO(jonross): move this into the destructor. Currently separated as
105   // ShellOberver notifies of maximize mode ending, and the observers end up
106   // attempting to access MaximizeModeController via the Shell. If done in
107   // destructor the controller is null, and the observers segfault.
108   // Shuts down down the MaximizeModeWindowManager and notifies all observers.
109   void Shutdown();
110
111   // AccelerometerObserver:
112   void OnAccelerometerUpdated(const ui::AccelerometerUpdate& update) override;
113
114   // ShellObserver:
115   void OnAppTerminating() override;
116   void OnMaximizeModeStarted() override;
117   void OnMaximizeModeEnded() override;
118
119   // DisplayController::Observer:
120   void OnDisplayConfigurationChanged() override;
121
122 #if defined(OS_CHROMEOS)
123   // PowerManagerClient::Observer:
124   virtual void LidEventReceived(bool open,
125                                 const base::TimeTicks& time) override;
126   virtual void SuspendImminent() override;
127   virtual void SuspendDone(const base::TimeDelta& sleep_duration) override;
128 #endif  // OS_CHROMEOS
129
130  private:
131   friend class MaximizeModeControllerTest;
132   friend class MaximizeModeWindowManagerTest;
133   friend class test::MultiUserWindowManagerChromeOSTest;
134
135   // Set the TickClock. This is only to be used by tests that need to
136   // artificially and deterministically control the current time.
137   void SetTickClockForTest(scoped_ptr<base::TickClock> tick_clock);
138
139   // Detect hinge rotation from |base| and |lid| accelerometers and
140   // automatically start / stop maximize mode.
141   void HandleHingeRotation(const gfx::Vector3dF& base,
142                            const gfx::Vector3dF& lid);
143
144   // Detect screen rotation from |lid| accelerometer and automatically rotate
145   // screen.
146   void HandleScreenRotation(const gfx::Vector3dF& lid);
147
148   // Sets the display rotation and suppresses display notifications.
149   void SetDisplayRotation(DisplayManager* display_manager,
150                           gfx::Display::Rotation rotation);
151
152   // Returns true if the lid was recently opened.
153   bool WasLidOpenedRecently() const;
154
155   // Enables MaximizeModeWindowManager, and determines the current state of
156   // rotation lock.
157   void EnterMaximizeMode();
158
159   // Removes MaximizeModeWindowManager and resets the display rotation if there
160   // is no rotation lock.
161   void LeaveMaximizeMode();
162
163   // Record UMA stats tracking touchview usage.
164   void RecordTouchViewStateTransition();
165
166   // Checks DisplayManager for registered rotation lock, and rotation,
167   // preferences. These are then applied.
168   void LoadDisplayRotationProperties();
169
170   // The maximized window manager (if enabled).
171   scoped_ptr<MaximizeModeWindowManager> maximize_mode_window_manager_;
172
173   // A helper class which when instantiated will block native events from the
174   // internal keyboard and touchpad.
175   scoped_ptr<ScopedDisableInternalMouseAndKeyboard> event_blocker_;
176
177   // When true calls to OnAccelerometerUpdated will not rotate the display.
178   bool rotation_locked_;
179
180   // Whether we have ever seen accelerometer data.
181   bool have_seen_accelerometer_data_;
182
183   // True when changes being applied cause OnDisplayConfigurationChanged() to be
184   // called, and for which these changes should be ignored.
185   bool ignore_display_configuration_updates_;
186
187   // True when the hinge angle has been detected past 180 degrees.
188   bool lid_open_past_180_;
189
190   // True when Shutdown has been called. When shutting down the non maximize
191   // mode state should be restored, however user preferences should not be
192   // altered.
193   bool shutting_down_;
194
195   // The rotation of the display set by the user. This rotation will be
196   // restored upon exiting maximize mode.
197   gfx::Display::Rotation user_rotation_;
198
199   // The current rotation set by MaximizeModeController for the internal
200   // display. Compared in OnDisplayConfigurationChanged to determine user
201   // display setting changes.
202   gfx::Display::Rotation current_rotation_;
203
204   // Rotation Lock observers.
205   ObserverList<Observer> observers_;
206
207   // Tracks time spent in (and out of) touchview mode.
208   base::Time last_touchview_transition_time_;
209   base::TimeDelta total_touchview_time_;
210   base::TimeDelta total_non_touchview_time_;
211
212   // Tracks the last time we received a lid open event. This is used to suppress
213   // erroneous accelerometer readings as the lid is opened but the accelerometer
214   // reports readings that make the lid to appear near fully open.
215   base::TimeTicks last_lid_open_time_;
216
217   // Source for the current time in base::TimeTicks.
218   scoped_ptr<base::TickClock> tick_clock_;
219
220   // Tracks when the lid is closed. Used to prevent entering maximize mode.
221   bool lid_is_closed_;
222
223   DISALLOW_COPY_AND_ASSIGN(MaximizeModeController);
224 };
225
226 }  // namespace ash
227
228 #endif  // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_