8445bfa50acc4819604dcf9163136fe09a4fd3e8
[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 "base/power_monitor/power_observer.h"
17 #include "ui/gfx/display.h"
18
19 namespace ui {
20 class EventHandler;
21 }
22
23 namespace ash {
24
25 class MaximizeModeControllerTest;
26 class MaximizeModeEventBlocker;
27 class MaximizeModeWindowManager;
28 class MaximizeModeWindowManagerTest;
29
30 // MaximizeModeController listens to accelerometer events and automatically
31 // enters and exits maximize mode when the lid is opened beyond the triggering
32 // angle and rotates the display to match the device when in maximize mode.
33 class ASH_EXPORT MaximizeModeController : public AccelerometerObserver,
34     public base::PowerObserver,
35     public ShellObserver,
36     public DisplayController::Observer {
37  public:
38   // Observer that reports changes to the state of MaximizeModeController's
39   // rotation lock.
40   class Observer {
41    public:
42     // Invoked whenever |rotation_locked_| is changed.
43     virtual void OnRotationLockChanged(bool rotation_locked) {}
44
45    protected:
46     virtual ~Observer() {}
47   };
48
49   MaximizeModeController();
50   virtual ~MaximizeModeController();
51
52   bool in_set_screen_rotation() const {
53     return in_set_screen_rotation_;
54   }
55
56   // True if |rotation_lock_| has been set, and OnAccelerometerUpdated will not
57   // change the display rotation.
58   bool rotation_locked() {
59     return rotation_locked_;
60   }
61
62   // If |rotation_locked| future calls to OnAccelerometerUpdated will not
63   // change the display rotation.
64   void SetRotationLocked(bool rotation_locked);
65
66   // Add/Remove observers.
67   void AddObserver(Observer* observer);
68   void RemoveObserver(Observer* observer);
69
70   // True if it is possible to enter maximize mode in the current
71   // configuration. If this returns false, it should never be the case that
72   // maximize mode becomes enabled.
73   bool CanEnterMaximizeMode();
74
75   // TODO(jonross): Merge this with EnterMaximizeMode. Currently these are
76   // separate for several reasons: there is no internal display when running
77   // unittests; the event blocker prevents keyboard input when running ChromeOS
78   // on linux. http://crbug.com/362881
79   // Turn the always maximize mode window manager on or off.
80   void EnableMaximizeModeWindowManager(bool enable);
81
82   // Test if the MaximizeModeWindowManager is enabled or not.
83   bool IsMaximizeModeWindowManagerEnabled() const;
84
85   // TODO(jonross): move this into the destructor. Currently separated as
86   // ShellOberver notifies of maximize mode ending, and the observers end up
87   // attempting to access MaximizeModeController via the Shell. If done in
88   // destructor the controller is null, and the observers segfault.
89   // Shuts down down the MaximizeModeWindowManager and notifies all observers.
90   void Shutdown();
91
92   // AccelerometerObserver:
93   virtual void OnAccelerometerUpdated(const gfx::Vector3dF& base,
94                                       const gfx::Vector3dF& lid) OVERRIDE;
95
96   // ShellObserver:
97   virtual void OnAppTerminating() OVERRIDE;
98   virtual void OnMaximizeModeStarted() OVERRIDE;
99   virtual void OnMaximizeModeEnded() OVERRIDE;
100
101   // base::PowerObserver:
102   virtual void OnSuspend() OVERRIDE;
103   virtual void OnResume() OVERRIDE;
104
105   // DisplayController::Observer:
106   virtual void OnDisplayConfigurationChanged() OVERRIDE;
107
108  private:
109   friend class MaximizeModeControllerTest;
110   friend class MaximizeModeWindowManagerTest;
111
112   // Detect hinge rotation from |base| and |lid| accelerometers and
113   // automatically start / stop maximize mode.
114   void HandleHingeRotation(const gfx::Vector3dF& base,
115                            const gfx::Vector3dF& lid);
116
117   // Detect screen rotation from |lid| accelerometer and automatically rotate
118   // screen.
119   void HandleScreenRotation(const gfx::Vector3dF& lid);
120
121   // Sets the display rotation and suppresses display notifications.
122   void SetDisplayRotation(DisplayManager* display_manager,
123                           gfx::Display::Rotation rotation);
124
125   // Enables MaximizeModeWindowManager, and determines the current state of
126   // rotation lock.
127   void EnterMaximizeMode();
128
129   // Removes MaximizeModeWindowManager and resets the display rotation if there
130   // is no rotation lock.
131   void LeaveMaximizeMode();
132
133   // Record UMA stats tracking touchview usage.
134   void RecordTouchViewStateTransition();
135
136   // The maximized window manager (if enabled).
137   scoped_ptr<MaximizeModeWindowManager> maximize_mode_window_manager_;
138
139   // An event targeter controller which traps mouse and keyboard events while
140   // maximize mode is engaged.
141   scoped_ptr<MaximizeModeEventBlocker> event_blocker_;
142
143   // An event handler used to detect screenshot actions while in maximize mode.
144   scoped_ptr<ui::EventHandler> event_handler_;
145
146   // When true calls to OnAccelerometerUpdated will not rotate the display.
147   bool rotation_locked_;
148
149   // Whether we have ever seen accelerometer data.
150   bool have_seen_accelerometer_data_;
151
152   // True when the screen's orientation is being changed.
153   bool in_set_screen_rotation_;
154
155   // The rotation of the display set by the user. This rotation will be
156   // restored upon exiting maximize mode.
157   gfx::Display::Rotation user_rotation_;
158
159   // The current rotation set by MaximizeModeController for the internal
160   // display. Compared in OnDisplayConfigurationChanged to determine user
161   // display setting changes.
162   gfx::Display::Rotation current_rotation_;
163
164   // Rotation Lock observers.
165   ObserverList<Observer> observers_;
166
167   // Tracks time spent in (and out of) touchview mode.
168   base::Time last_touchview_transition_time_;
169   base::TimeDelta total_touchview_time_;
170   base::TimeDelta total_non_touchview_time_;
171
172   DISALLOW_COPY_AND_ASSIGN(MaximizeModeController);
173 };
174
175 }  // namespace ash
176
177 #endif  // ASH_WM_MAXIMIZE_MODE_MAXIMIZE_MODE_CONTROLLER_H_