| OLD | NEW |
| (Empty) |
| 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 #include "ash/content/display/screen_orientation_delegate_chromeos.h" | |
| 6 | |
| 7 #include "ash/ash_switches.h" | |
| 8 #include "ash/display/display_info.h" | |
| 9 #include "ash/display/display_manager.h" | |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/wm/maximize_mode/maximize_mode_controller.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "content/public/browser/screen_orientation_provider.h" | |
| 14 #include "content/public/browser/web_contents.h" | |
| 15 #include "ui/aura/window.h" | |
| 16 #include "ui/gfx/display.h" | |
| 17 #include "ui/gfx/geometry/size.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { | |
| 22 ash::DisplayManager* display_manager = | |
| 23 ash::Shell::GetInstance()->display_manager(); | |
| 24 if (!display_manager->HasInternalDisplay()) | |
| 25 return blink::WebScreenOrientationLockLandscape; | |
| 26 | |
| 27 ash::DisplayInfo info = | |
| 28 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); | |
| 29 gfx::Size size = info.size_in_pixel(); | |
| 30 switch (info.rotation()) { | |
| 31 case gfx::Display::ROTATE_0: | |
| 32 case gfx::Display::ROTATE_180: | |
| 33 return size.height() >= size.width() | |
| 34 ? blink::WebScreenOrientationLockPortrait | |
| 35 : blink::WebScreenOrientationLockLandscape; | |
| 36 case gfx::Display::ROTATE_90: | |
| 37 case gfx::Display::ROTATE_270: | |
| 38 return size.height() < size.width() | |
| 39 ? blink::WebScreenOrientationLockPortrait | |
| 40 : blink::WebScreenOrientationLockLandscape; | |
| 41 } | |
| 42 NOTREACHED(); | |
| 43 return blink::WebScreenOrientationLockLandscape; | |
| 44 } | |
| 45 | |
| 46 } // namespace | |
| 47 | |
| 48 namespace ash { | |
| 49 | |
| 50 ScreenOrientationDelegate::ScreenOrientationDelegate() | |
| 51 : locking_window_(NULL), | |
| 52 natural_orientation_(GetDisplayNaturalOrientation()) { | |
| 53 content::ScreenOrientationProvider::SetDelegate(this); | |
| 54 } | |
| 55 | |
| 56 ScreenOrientationDelegate::~ScreenOrientationDelegate() { | |
| 57 content::ScreenOrientationProvider::SetDelegate(NULL); | |
| 58 } | |
| 59 | |
| 60 bool ScreenOrientationDelegate::FullScreenRequired( | |
| 61 content::WebContents* web_contents) { | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 void ScreenOrientationDelegate::Lock( | |
| 66 content::WebContents* web_contents, | |
| 67 blink::WebScreenOrientationLockType lock_orientation) { | |
| 68 aura::Window* requesting_window = web_contents->GetNativeView(); | |
| 69 | |
| 70 // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation | |
| 71 // lock. Have MaximizeModeController, and TrayRotationLock both use it | |
| 72 // instead. | |
| 73 MaximizeModeController* controller = | |
| 74 Shell::GetInstance()->maximize_mode_controller(); | |
| 75 | |
| 76 // TODO(jonross): Track one rotation lock per window. When the active window | |
| 77 // changes apply any corresponding rotation lock. | |
| 78 if (!locking_window_) | |
| 79 locking_window_ = requesting_window; | |
| 80 else if (requesting_window != locking_window_) | |
| 81 return; | |
| 82 | |
| 83 switch (lock_orientation) { | |
| 84 case blink::WebScreenOrientationLockAny: | |
| 85 controller->SetRotationLocked(false); | |
| 86 locking_window_ = NULL; | |
| 87 break; | |
| 88 case blink::WebScreenOrientationLockDefault: | |
| 89 NOTREACHED(); | |
| 90 break; | |
| 91 case blink::WebScreenOrientationLockPortraitPrimary: | |
| 92 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); | |
| 93 break; | |
| 94 case blink::WebScreenOrientationLockLandscape: | |
| 95 case blink::WebScreenOrientationLockPortrait: | |
| 96 LockToRotationMatchingOrientation(lock_orientation); | |
| 97 break; | |
| 98 case blink::WebScreenOrientationLockPortraitSecondary: | |
| 99 LockRotationToSecondaryOrientation( | |
| 100 blink::WebScreenOrientationLockPortrait); | |
| 101 break; | |
| 102 case blink::WebScreenOrientationLockLandscapeSecondary: | |
| 103 LockRotationToSecondaryOrientation( | |
| 104 blink::WebScreenOrientationLockLandscape); | |
| 105 break; | |
| 106 case blink::WebScreenOrientationLockLandscapePrimary: | |
| 107 LockRotationToPrimaryOrientation( | |
| 108 blink::WebScreenOrientationLockLandscape); | |
| 109 break; | |
| 110 case blink::WebScreenOrientationLockNatural: | |
| 111 controller->LockRotation(gfx::Display::ROTATE_0); | |
| 112 break; | |
| 113 default: | |
| 114 NOTREACHED(); | |
| 115 break; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { | |
| 120 return Shell::GetInstance() | |
| 121 ->maximize_mode_controller() | |
| 122 ->IsMaximizeModeWindowManagerEnabled() && | |
| 123 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 124 switches::kAshEnableTouchViewTesting); | |
| 125 } | |
| 126 | |
| 127 void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { | |
| 128 aura::Window* requesting_window = web_contents->GetNativeView(); | |
| 129 if (requesting_window != locking_window_) | |
| 130 return; | |
| 131 locking_window_ = NULL; | |
| 132 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); | |
| 133 } | |
| 134 | |
| 135 void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( | |
| 136 blink::WebScreenOrientationLockType lock_orientation) { | |
| 137 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | |
| 138 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 | |
| 139 : gfx::Display::ROTATE_90); | |
| 140 } | |
| 141 | |
| 142 void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( | |
| 143 blink::WebScreenOrientationLockType lock_orientation) { | |
| 144 Shell::GetInstance()->maximize_mode_controller()->LockRotation( | |
| 145 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 | |
| 146 : gfx::Display::ROTATE_270); | |
| 147 } | |
| 148 | |
| 149 void ScreenOrientationDelegate::LockToRotationMatchingOrientation( | |
| 150 blink::WebScreenOrientationLockType lock_orientation) { | |
| 151 // TODO(jonross): Update MaximizeModeController to allow rotation between | |
| 152 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from | |
| 153 // ROTATE_90 to ROTATE_270) | |
| 154 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
| 155 if (!display_manager->HasInternalDisplay()) | |
| 156 return; | |
| 157 | |
| 158 gfx::Display::Rotation rotation = | |
| 159 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) | |
| 160 .rotation(); | |
| 161 MaximizeModeController* controller = | |
| 162 Shell::GetInstance()->maximize_mode_controller(); | |
| 163 if (natural_orientation_ == lock_orientation) { | |
| 164 if (rotation == gfx::Display::ROTATE_0 || | |
| 165 rotation == gfx::Display::ROTATE_180) { | |
| 166 controller->SetRotationLocked(true); | |
| 167 } else { | |
| 168 controller->LockRotation(gfx::Display::ROTATE_0); | |
| 169 } | |
| 170 } else { | |
| 171 if (rotation == gfx::Display::ROTATE_90 || | |
| 172 rotation == gfx::Display::ROTATE_270) { | |
| 173 controller->SetRotationLocked(true); | |
| 174 } else { | |
| 175 controller->LockRotation(gfx::Display::ROTATE_90); | |
| 176 } | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 } // namespace ash | |
| OLD | NEW |