diff --git a/ash/display/screen_orientation_controller.cc b/ash/display/screen_orientation_controller.cc index 041a579b3645ac..64ba5535371e9c 100644 --- a/ash/display/screen_orientation_controller.cc +++ b/ash/display/screen_orientation_controller.cc @@ -14,6 +14,7 @@ #include "ash/wm/window_state.h" #include "base/auto_reset.h" #include "base/command_line.h" +#include "base/stl_util.h" #include "ui/aura/client/aura_constants.h" #include "ui/display/display.h" #include "ui/display/manager/display_manager.h" @@ -339,9 +340,8 @@ void ScreenOrientationController::OnWindowDestroying(aura::Window* window) { void ScreenOrientationController::OnWindowVisibilityChanged( aura::Window* window, bool visible) { - if (lock_info_map_.find(window) == lock_info_map_.end()) - return; - ApplyLockForActiveWindow(); + if (base::ContainsKey(lock_info_map_, window)) + ApplyLockForActiveWindow(); } void ScreenOrientationController::OnAccelerometerUpdated( @@ -594,37 +594,47 @@ void ScreenOrientationController::ApplyLockForActiveWindow() { for (auto* window : mru_windows) { if (!window->TargetVisibility()) continue; - for (auto& pair : lock_info_map_) { - if (pair.first->TargetVisibility() && window->Contains(pair.first)) { - if (pair.second.orientation_lock == OrientationLockType::kCurrent) { - // If the app requested "current" without previously - // specifying an orientation, use the current rotation. - pair.second.orientation_lock = - RotationToOrientation(natural_orientation_, current_rotation_); - LockRotationToOrientation(pair.second.orientation_lock); - } else { - const auto orientation_lock = ResolveOrientationLock( - pair.second.orientation_lock, user_locked_orientation_); - LockRotationToOrientation(orientation_lock); - if (pair.second.lock_completion_behavior == - LockCompletionBehavior::DisableSensor) { - pair.second.lock_completion_behavior = LockCompletionBehavior::None; - pair.second.orientation_lock = orientation_lock; - } + if (ApplyLockForWindowIfPossible(window)) + return; + } + + LockRotationToOrientation(user_locked_orientation_); +} + +bool ScreenOrientationController::ApplyLockForWindowIfPossible( + const aura::Window* window) { + for (auto& pair : lock_info_map_) { + const aura::Window* lock_window = pair.first; + LockInfo& lock_info = pair.second; + if (lock_window->TargetVisibility() && window->Contains(lock_window)) { + if (lock_info.orientation_lock == OrientationLockType::kCurrent) { + // If the app requested "current" without previously + // specifying an orientation, use the current rotation. + lock_info.orientation_lock = + RotationToOrientation(natural_orientation_, current_rotation_); + LockRotationToOrientation(lock_info.orientation_lock); + } else { + const auto orientation_lock = ResolveOrientationLock( + lock_info.orientation_lock, user_locked_orientation_); + LockRotationToOrientation(orientation_lock); + if (lock_info.lock_completion_behavior == + LockCompletionBehavior::DisableSensor) { + lock_info.lock_completion_behavior = LockCompletionBehavior::None; + lock_info.orientation_lock = orientation_lock; } - return; } - } - // The default orientation for all chrome browser/apps windows is - // ANY, so use the user_locked_orientation_; - if (window->TargetVisibility() && - static_cast(window->GetProperty(aura::client::kAppType)) != - AppType::OTHERS) { - LockRotationToOrientation(user_locked_orientation_); - return; + return true; } } - LockRotationToOrientation(user_locked_orientation_); + + // The default orientation for all chrome browser/apps windows is + // ANY, so use the user_locked_orientation_; + if (static_cast(window->GetProperty(aura::client::kAppType)) != + AppType::OTHERS) { + LockRotationToOrientation(user_locked_orientation_); + return true; + } + return false; } bool ScreenOrientationController::IsRotationAllowedInLockedState( diff --git a/ash/display/screen_orientation_controller.h b/ash/display/screen_orientation_controller.h index a1b13e03d57cdf..1e7dabe7cce49f 100644 --- a/ash/display/screen_orientation_controller.h +++ b/ash/display/screen_orientation_controller.h @@ -68,13 +68,12 @@ class ASH_EXPORT ScreenOrientationController virtual ~Observer() {} }; - // Controls the behavior after lock is applied to the window (when - // the window becomes active window). |DisableSensor| disables - // the sensor based rotation and locks to the specific orientation. - // For example, PORTRAIT may rotate to PORTRAIT_PRIMARY or - // PORTRAIT_SECONDARY, and will allow rotate between these two. - // |DisableSensor| will lock the orientation to the one of them - // after locked to disalow the sensor basd rotation. + // Controls the behavior after lock is applied to the window (when the window + // becomes the active window). |DisableSensor| disables the sensor-based + // rotation and locks to the specific orientation. For example, PORTRAIT may + // rotate to PORTRAIT_PRIMARY or PORTRAIT_SECONDARY, and will allow rotation + // between these two. |DisableSensor| disallows the sensor-based rotation by + // locking the rotation to whichever specific orientation is applied. enum class LockCompletionBehavior { None, DisableSensor, @@ -206,6 +205,10 @@ class ASH_EXPORT ScreenOrientationController // window, and applies it. If there is none, rotation lock will be removed. void ApplyLockForActiveWindow(); + // If there is a rotation lock that can be applied to window, applies it and + // returns true. Otherwise returns false. + bool ApplyLockForWindowIfPossible(const aura::Window* window); + // Both |OrientationLockType::kLandscape| and // |OrientationLock::kPortrait| allow for rotation between the // two angles of the same screen orientation