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/window_tree_host_manager.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/modules/screen_orientation/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 WindowTreeHostManager::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(
80 aura::client::ActivationChangeObserver::ActivationReason reason
,
81 aura::Window
* gained_active
,
82 aura::Window
* lost_active
) override
;
84 // aura::WindowObserver:
85 void OnWindowVisibilityChanged(aura::Window
* window
, bool visible
) override
;
86 void OnWindowDestroying(aura::Window
* window
) override
;
88 // chromeos::AccelerometerReader::Observer:
89 void OnAccelerometerUpdated(
90 scoped_refptr
<const chromeos::AccelerometerUpdate
> update
) override
;
92 // content::ScreenOrientationDelegate:
93 bool FullScreenRequired(content::WebContents
* web_contents
) override
;
94 void Lock(content::WebContents
* web_contents
,
95 blink::WebScreenOrientationLockType lock_orientation
) override
;
96 bool ScreenOrientationProviderSupported() override
;
97 void Unlock(content::WebContents
* web_contents
) override
;
99 // WindowTreeHostManager::Observer:
100 void OnDisplayConfigurationChanged() override
;
103 void OnMaximizeModeStarted() override
;
104 void OnMaximizeModeEnded() override
;
107 // Sets the display rotation to |rotation|. Future accelerometer updates
108 // should not be used to change the rotation. SetRotationLocked(false) removes
109 // the rotation lock.
110 void LockRotation(gfx::Display::Rotation rotation
,
111 gfx::Display::RotationSource source
);
113 // Sets the display rotation based on |lock_orientation|. Future accelerometer
114 // updates should not be used to change the rotation. SetRotationLocked(false)
115 // removes the rotation lock.
116 void LockRotationToOrientation(
117 blink::WebScreenOrientationLockType lock_orientation
);
119 // Locks rotation to the angle matching the primary orientation for
120 // |lock_orientation|.
121 void LockRotationToPrimaryOrientation(
122 blink::WebScreenOrientationLockType lock_orientation
);
124 // Locks rotation to the angle matching the secondary orientation for
125 // |lock_orientation|.
126 void LockRotationToSecondaryOrientation(
127 blink::WebScreenOrientationLockType lock_orientation
);
129 // For orientations that do not specify primary or secondary, locks to the
130 // current rotation if it matches |lock_orientation|. Otherwise locks to a
131 // matching rotation.
132 void LockToRotationMatchingOrientation(
133 blink::WebScreenOrientationLockType lock_orientation
);
135 // Detect screen rotation from |lid| accelerometer and automatically rotate
137 void HandleScreenRotation(const chromeos::AccelerometerReading
& lid
);
139 // Checks DisplayManager for registered rotation lock, and rotation,
140 // preferences. These are then applied.
141 void LoadDisplayRotationProperties();
143 // Determines the rotation lock, and orientation, for the currently active
144 // window, and applies it. If there is none, rotation lock will be removed.
145 void ApplyLockForActiveWindow();
147 // Removes a window and its locking preference.
148 void RemoveLockingWindow(aura::Window
* window
);
150 // Both |blink::WebScreenOrientationLockLandscape| and
151 // |blink::WebScreenOrientationLockPortrait| allow for rotation between the
152 // two angles of the same screen orientation
153 // (http://www.w3.org/TR/screen-orientation/). Returns true if |rotation| is
154 // supported for the current |rotation_locked_orientation_|.
155 bool IsRotationAllowedInLockedState(gfx::Display::Rotation rotation
);
157 // Certain orientation locks allow for rotation between the two angles of the
158 // same screen orientation. Returns true if |rotation_locked_orientation_|
160 bool CanRotateInLockedState();
162 // The orientation of the display when at a rotation of 0.
163 blink::WebScreenOrientationLockType natural_orientation_
;
165 // True when changes being applied cause OnDisplayConfigurationChanged() to be
166 // called, and for which these changes should be ignored.
167 bool ignore_display_configuration_updates_
;
169 // When true then accelerometer updates should not rotate the display.
170 bool rotation_locked_
;
172 // The orientation to which the current |rotation_locked_| was applied.
173 blink::WebScreenOrientationLockType rotation_locked_orientation_
;
175 // The rotation of the display set by the user. This rotation will be
176 // restored upon exiting maximize mode.
177 gfx::Display::Rotation user_rotation_
;
179 // The current rotation set by ScreenOrientationController for the internal
181 gfx::Display::Rotation current_rotation_
;
183 // Rotation Lock observers.
184 base::ObserverList
<Observer
> observers_
;
186 // Tracks all windows that have requested a lock, as well as the requested
188 std::map
<aura::Window
*, blink::WebScreenOrientationLockType
> locking_windows_
;
190 DISALLOW_COPY_AND_ASSIGN(ScreenOrientationController
);
195 #endif // ASH_CONTENT_DISPLAY_SCREEN_ORIENTATION_CONTROLLER_CHROMEOS_H_