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.
5 #ifndef ASH_CONTENT_DISPLAY_SCREEN_ORIENTATION_CONTROLLER_CHROMEOS_H_
6 #define ASH_CONTENT_DISPLAY_SCREEN_ORIENTATION_CONTROLLER_CHROMEOS_H_
10 #include "ash/ash_export.h"
11 #include "ash/display/display_controller.h"
12 #include "ash/shell_observer.h"
13 #include "base/macros.h"
14 #include "base/observer_list.h"
15 #include "chromeos/accelerometer/accelerometer_reader.h"
16 #include "chromeos/accelerometer/accelerometer_types.h"
17 #include "content/public/browser/screen_orientation_delegate.h"
18 #include "third_party/WebKit/public/platform/WebScreenOrientationLockType.h"
19 #include "ui/aura/window_observer.h"
20 #include "ui/gfx/display.h"
21 #include "ui/wm/public/activation_change_observer.h"
33 // Implements ChromeOS specific functionality for ScreenOrientationProvider.
34 class ASH_EXPORT ScreenOrientationController
35 : public aura::client::ActivationChangeObserver
,
36 public aura::WindowObserver
,
37 public chromeos::AccelerometerReader::Observer
,
38 public content::ScreenOrientationDelegate
,
39 public DisplayController::Observer
,
40 public ShellObserver
{
42 // Observer that reports changes to the state of ScreenOrientationProvider's
46 // Invoked when rotation is locked or unlocked.
47 virtual void OnRotationLockChanged(bool rotation_locked
) {}
50 virtual ~Observer() {}
53 ScreenOrientationController();
54 ~ScreenOrientationController() override
;
56 // Add/Remove observers.
57 void AddObserver(Observer
* observer
);
58 void RemoveObserver(Observer
* observer
);
60 bool ignore_display_configuration_updates() const {
61 return ignore_display_configuration_updates_
;
64 // True if |rotation_lock_| has been set and accelerometer updates should not
65 // rotate the display.
66 bool rotation_locked() const { return rotation_locked_
; }
68 // If |rotation_locked| future accelerometer updates should not change the
70 void SetRotationLocked(bool rotation_locked
);
72 // Sets the display rotation for the given |source|. The new |rotation| will
73 // also become active. Display changed notifications are surpressed for this
75 void SetDisplayRotation(gfx::Display::Rotation rotation
,
76 gfx::Display::RotationSource source
);
78 // aura::client::ActivationChangeObserver:
79 void OnWindowActivated(aura::Window
* gained_active
,
80 aura::Window
* lost_active
) override
;
82 // aura::WindowObserver:
83 void OnWindowVisibilityChanged(aura::Window
* window
, bool visible
) override
;
84 void OnWindowDestroying(aura::Window
* window
) override
;
86 // chromeos::AccelerometerReader::Observer:
87 void OnAccelerometerUpdated(
88 scoped_refptr
<const chromeos::AccelerometerUpdate
> update
) override
;
90 // content::ScreenOrientationDelegate:
91 bool FullScreenRequired(content::WebContents
* web_contents
) override
;
92 void Lock(content::WebContents
* web_contents
,
93 blink::WebScreenOrientationLockType lock_orientation
) override
;
94 bool ScreenOrientationProviderSupported() override
;
95 void Unlock(content::WebContents
* web_contents
) override
;
97 // DisplayController::Observer:
98 void OnDisplayConfigurationChanged() override
;
101 void OnMaximizeModeStarted() override
;
102 void OnMaximizeModeEnded() override
;
105 // Sets the display rotation to |rotation|. Future accelerometer updates
106 // should not be used to change the rotation. SetRotationLocked(false) removes
107 // the rotation lock.
108 void LockRotation(gfx::Display::Rotation rotation
,
109 gfx::Display::RotationSource source
);
111 // Sets the display rotation based on |lock_orientation|. Future accelerometer
112 // updates should not be used to change the rotation. SetRotationLocked(false)
113 // removes the rotation lock.
114 void LockRotationToOrientation(
115 blink::WebScreenOrientationLockType lock_orientation
);
117 // Locks rotation to the angle matching the primary orientation for
118 // |lock_orientation|.
119 void LockRotationToPrimaryOrientation(
120 blink::WebScreenOrientationLockType lock_orientation
);
122 // Locks rotation to the angle matching the secondary orientation for
123 // |lock_orientation|.
124 void LockRotationToSecondaryOrientation(
125 blink::WebScreenOrientationLockType lock_orientation
);
127 // For orientations that do not specify primary or secondary, locks to the
128 // current rotation if it matches |lock_orientation|. Otherwise locks to a
129 // matching rotation.
130 void LockToRotationMatchingOrientation(
131 blink::WebScreenOrientationLockType lock_orientation
);
133 // Detect screen rotation from |lid| accelerometer and automatically rotate
135 void HandleScreenRotation(const chromeos::AccelerometerReading
& lid
);
137 // Checks DisplayManager for registered rotation lock, and rotation,
138 // preferences. These are then applied.
139 void LoadDisplayRotationProperties();
141 // Determines the rotation lock, and orientation, for the currently active
142 // window, and applies it. If there is none, rotation lock will be removed.
143 void ApplyLockForActiveWindow();
145 // Removes a window and its locking preference.
146 void RemoveLockingWindow(aura::Window
* window
);
148 // Both |blink::WebScreenOrientationLockLandscape| and
149 // |blink::WebScreenOrientationLockPortrait| allow for rotation between the
150 // two angles of the same screen orientation
151 // (http://www.w3.org/TR/screen-orientation/). Returns true if |rotation| is
152 // supported for the current |rotation_locked_orientation_|.
153 bool IsRotationAllowedInLockedState(gfx::Display::Rotation rotation
);
155 // Certain orientation locks allow for rotation between the two angles of the
156 // same screen orientation. Returns true if |rotation_locked_orientation_|
158 bool CanRotateInLockedState();
160 // The orientation of the display when at a rotation of 0.
161 blink::WebScreenOrientationLockType natural_orientation_
;
163 // True when changes being applied cause OnDisplayConfigurationChanged() to be
164 // called, and for which these changes should be ignored.
165 bool ignore_display_configuration_updates_
;
167 // When true then accelerometer updates should not rotate the display.
168 bool rotation_locked_
;
170 // The orientation to which the current |rotation_locked_| was applied.
171 blink::WebScreenOrientationLockType rotation_locked_orientation_
;
173 // The rotation of the display set by the user. This rotation will be
174 // restored upon exiting maximize mode.
175 gfx::Display::Rotation user_rotation_
;
177 // The current rotation set by ScreenOrientationController for the internal
179 gfx::Display::Rotation current_rotation_
;
181 // Rotation Lock observers.
182 ObserverList
<Observer
> observers_
;
184 // Tracks all windows that have requested a lock, as well as the requested
186 std::map
<aura::Window
*, blink::WebScreenOrientationLockType
> locking_windows_
;
188 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationController
);
193 #endif // ASH_CONTENT_DISPLAY_SCREEN_ORIENTATION_CONTROLLER_CHROMEOS_H_