| 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 "content/public/browser/screen_orientation_provider.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 9 #include "content/browser/web_contents/web_contents_impl.h" | |
| 10 #include "content/public/browser/render_widget_host.h" | |
| 11 #include "content/public/browser/screen_orientation_delegate.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationError.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 using device::mojom::ScreenOrientationLockResult; | |
| 18 | |
| 19 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; | |
| 20 | |
| 21 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) | |
| 22 : WebContentsObserver(web_contents), lock_applied_(false) {} | |
| 23 | |
| 24 ScreenOrientationProvider::~ScreenOrientationProvider() { | |
| 25 } | |
| 26 | |
| 27 void ScreenOrientationProvider::LockOrientation( | |
| 28 blink::WebScreenOrientationLockType orientation, | |
| 29 const LockOrientationCallback& callback) { | |
| 30 // Cancel any pending lock request. | |
| 31 NotifyLockResult(ScreenOrientationLockResult:: | |
| 32 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 33 // Record new pending lock request. | |
| 34 pending_callback_ = callback; | |
| 35 | |
| 36 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { | |
| 37 NotifyLockResult(ScreenOrientationLockResult:: | |
| 38 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 if (delegate_->FullScreenRequired(web_contents())) { | |
| 43 RenderViewHostImpl* rvhi = | |
| 44 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | |
| 45 if (!rvhi) { | |
| 46 NotifyLockResult(ScreenOrientationLockResult:: | |
| 47 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 48 return; | |
| 49 } | |
| 50 if (!static_cast<WebContentsImpl*>(web_contents()) | |
| 51 ->IsFullscreenForCurrentTab()) { | |
| 52 NotifyLockResult( | |
| 53 ScreenOrientationLockResult:: | |
| 54 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); | |
| 55 return; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 if (orientation == blink::WebScreenOrientationLockNatural) { | |
| 60 orientation = GetNaturalLockType(); | |
| 61 if (orientation == blink::WebScreenOrientationLockDefault) { | |
| 62 // We are in a broken state, let's pretend we got canceled. | |
| 63 NotifyLockResult(ScreenOrientationLockResult:: | |
| 64 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 65 return; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 lock_applied_ = true; | |
| 70 delegate_->Lock(web_contents(), orientation); | |
| 71 | |
| 72 // If the orientation we are locking to matches the current orientation, we | |
| 73 // should succeed immediately. | |
| 74 if (LockMatchesCurrentOrientation(orientation)) { | |
| 75 NotifyLockResult( | |
| 76 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 pending_lock_orientation_ = orientation; | |
| 81 } | |
| 82 | |
| 83 void ScreenOrientationProvider::UnlockOrientation() { | |
| 84 // Cancel any pending lock request. | |
| 85 NotifyLockResult(ScreenOrientationLockResult:: | |
| 86 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 87 | |
| 88 if (!lock_applied_ || !delegate_) | |
| 89 return; | |
| 90 | |
| 91 delegate_->Unlock(web_contents()); | |
| 92 | |
| 93 lock_applied_ = false; | |
| 94 } | |
| 95 | |
| 96 void ScreenOrientationProvider::OnOrientationChange() { | |
| 97 if (!pending_lock_orientation_.has_value()) | |
| 98 return; | |
| 99 | |
| 100 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { | |
| 101 DCHECK(!pending_callback_.is_null()); | |
| 102 NotifyLockResult( | |
| 103 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void ScreenOrientationProvider::NotifyLockResult( | |
| 108 ScreenOrientationLockResult result) { | |
| 109 if (!pending_callback_.is_null()) { | |
| 110 base::ResetAndReturn(&pending_callback_).Run(result); | |
| 111 } | |
| 112 pending_lock_orientation_.reset(); | |
| 113 } | |
| 114 | |
| 115 void ScreenOrientationProvider::SetDelegate( | |
| 116 ScreenOrientationDelegate* delegate) { | |
| 117 delegate_ = delegate; | |
| 118 } | |
| 119 | |
| 120 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( | |
| 121 bool entered_fullscreen, bool will_cause_resize) { | |
| 122 if (!lock_applied_ || !delegate_) | |
| 123 return; | |
| 124 | |
| 125 // If fullscreen is not required in order to lock orientation, don't unlock | |
| 126 // when fullscreen state changes. | |
| 127 if (!delegate_->FullScreenRequired(web_contents())) | |
| 128 return; | |
| 129 | |
| 130 DCHECK(!entered_fullscreen); | |
| 131 UnlockOrientation(); | |
| 132 } | |
| 133 | |
| 134 blink::WebScreenOrientationLockType | |
| 135 ScreenOrientationProvider::GetNaturalLockType() const { | |
| 136 RenderWidgetHost* rwh = web_contents()->GetRenderViewHost()->GetWidget(); | |
| 137 if (!rwh) | |
| 138 return blink::WebScreenOrientationLockDefault; | |
| 139 | |
| 140 ScreenInfo screen_info; | |
| 141 rwh->GetScreenInfo(&screen_info); | |
| 142 | |
| 143 switch (screen_info.orientation_type) { | |
| 144 case SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY: | |
| 145 case SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY: | |
| 146 if (screen_info.orientation_angle == 0 || | |
| 147 screen_info.orientation_angle == 180) { | |
| 148 return blink::WebScreenOrientationLockPortraitPrimary; | |
| 149 } | |
| 150 return blink::WebScreenOrientationLockLandscapePrimary; | |
| 151 case SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY: | |
| 152 case SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY: | |
| 153 if (screen_info.orientation_angle == 0 || | |
| 154 screen_info.orientation_angle == 180) { | |
| 155 return blink::WebScreenOrientationLockLandscapePrimary; | |
| 156 } | |
| 157 return blink::WebScreenOrientationLockPortraitPrimary; | |
| 158 default: | |
| 159 break; | |
| 160 } | |
| 161 | |
| 162 NOTREACHED(); | |
| 163 return blink::WebScreenOrientationLockDefault; | |
| 164 } | |
| 165 | |
| 166 bool ScreenOrientationProvider::LockMatchesCurrentOrientation( | |
| 167 blink::WebScreenOrientationLockType lock) { | |
| 168 RenderWidgetHost* rwh = web_contents()->GetRenderViewHost()->GetWidget(); | |
| 169 if (!rwh) | |
| 170 return false; | |
| 171 | |
| 172 ScreenInfo screen_info; | |
| 173 rwh->GetScreenInfo(&screen_info); | |
| 174 | |
| 175 switch (lock) { | |
| 176 case blink::WebScreenOrientationLockPortraitPrimary: | |
| 177 return screen_info.orientation_type == | |
| 178 SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY; | |
| 179 case blink::WebScreenOrientationLockPortraitSecondary: | |
| 180 return screen_info.orientation_type == | |
| 181 SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY; | |
| 182 case blink::WebScreenOrientationLockLandscapePrimary: | |
| 183 return screen_info.orientation_type == | |
| 184 SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY; | |
| 185 case blink::WebScreenOrientationLockLandscapeSecondary: | |
| 186 return screen_info.orientation_type == | |
| 187 SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY; | |
| 188 case blink::WebScreenOrientationLockLandscape: | |
| 189 return screen_info.orientation_type == | |
| 190 SCREEN_ORIENTATION_VALUES_LANDSCAPE_PRIMARY || | |
| 191 screen_info.orientation_type == | |
| 192 SCREEN_ORIENTATION_VALUES_LANDSCAPE_SECONDARY; | |
| 193 case blink::WebScreenOrientationLockPortrait: | |
| 194 return screen_info.orientation_type == | |
| 195 SCREEN_ORIENTATION_VALUES_PORTRAIT_PRIMARY || | |
| 196 screen_info.orientation_type == | |
| 197 SCREEN_ORIENTATION_VALUES_PORTRAIT_SECONDARY; | |
| 198 case blink::WebScreenOrientationLockAny: | |
| 199 return true; | |
| 200 case blink::WebScreenOrientationLockNatural: | |
| 201 case blink::WebScreenOrientationLockDefault: | |
| 202 NOTREACHED(); | |
| 203 return false; | |
| 204 } | |
| 205 | |
| 206 NOTREACHED(); | |
| 207 return false; | |
| 208 } | |
| 209 | |
| 210 } // namespace content | |
| OLD | NEW |