| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/browser/screen_orientation_provider.h" | 5 #include "content/public/browser/screen_orientation_provider.h" |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/render_view_host_impl.h" | 7 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_impl.h" | 8 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
| 10 #include "content/public/browser/screen_orientation_delegate.h" | 10 #include "content/public/browser/screen_orientation_delegate.h" |
| 11 #include "content/public/browser/screen_orientation_dispatcher_host.h" | 11 #include "content/public/browser/screen_orientation_dispatcher_host.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationError.h" | 13 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationError.h" |
| 14 | 14 |
| 15 namespace content { | 15 namespace content { |
| 16 | 16 |
| 17 using mojom::ScreenOrientationLockResult; |
| 18 |
| 17 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; | 19 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; |
| 18 | 20 |
| 19 ScreenOrientationProvider::LockInformation::LockInformation(int request_id, | 21 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) |
| 20 blink::WebScreenOrientationLockType lock) | |
| 21 : request_id(request_id), | |
| 22 lock(lock) { | |
| 23 } | |
| 24 | |
| 25 ScreenOrientationProvider::ScreenOrientationProvider( | |
| 26 ScreenOrientationDispatcherHost* dispatcher_host, | |
| 27 WebContents* web_contents) | |
| 28 : WebContentsObserver(web_contents), | 22 : WebContentsObserver(web_contents), |
| 29 dispatcher_(dispatcher_host), | 23 lock_applied_(false), |
| 30 lock_applied_(false) { | 24 pending_lock_orientation_(blink::WebScreenOrientationLockDefault) {} |
| 31 } | |
| 32 | 25 |
| 33 ScreenOrientationProvider::~ScreenOrientationProvider() { | 26 ScreenOrientationProvider::~ScreenOrientationProvider() { |
| 34 } | 27 } |
| 35 | 28 |
| 36 void ScreenOrientationProvider::LockOrientation(int request_id, | 29 void ScreenOrientationProvider::LockOrientation( |
| 37 blink::WebScreenOrientationLockType lock_orientation) { | 30 blink::WebScreenOrientationLockType orientation, |
| 31 const LockOrientationCallback& callback) { |
| 32 // Cancel any pending requests. |
| 33 NotifyLockResult(ScreenOrientationLockResult:: |
| 34 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 35 on_result_callback_ = callback; |
| 38 | 36 |
| 39 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { | 37 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { |
| 40 dispatcher_->NotifyLockError(request_id, | 38 NotifyLockResult(ScreenOrientationLockResult:: |
| 41 blink::WebLockOrientationErrorNotAvailable); | 39 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); |
| 42 return; | 40 return; |
| 43 } | 41 } |
| 44 | 42 |
| 45 if (delegate_->FullScreenRequired(web_contents())) { | 43 if (delegate_->FullScreenRequired(web_contents())) { |
| 46 RenderViewHostImpl* rvhi = | 44 RenderViewHostImpl* rvhi = |
| 47 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | 45 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); |
| 48 if (!rvhi) { | 46 if (!rvhi) { |
| 49 dispatcher_->NotifyLockError(request_id, | 47 NotifyLockResult(ScreenOrientationLockResult:: |
| 50 blink::WebLockOrientationErrorCanceled); | 48 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 51 return; | 49 return; |
| 52 } | 50 } |
| 53 if (!static_cast<WebContentsImpl*>(web_contents()) | 51 if (!static_cast<WebContentsImpl*>(web_contents()) |
| 54 ->IsFullscreenForCurrentTab()) { | 52 ->IsFullscreenForCurrentTab()) { |
| 55 dispatcher_->NotifyLockError(request_id, | 53 NotifyLockResult( |
| 56 blink::WebLockOrientationErrorFullscreenRequired); | 54 ScreenOrientationLockResult:: |
| 55 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); |
| 57 return; | 56 return; |
| 58 } | 57 } |
| 59 } | 58 } |
| 60 | 59 |
| 61 if (lock_orientation == blink::WebScreenOrientationLockNatural) { | 60 if (orientation == blink::WebScreenOrientationLockNatural) { |
| 62 lock_orientation = GetNaturalLockType(); | 61 orientation = GetNaturalLockType(); |
| 63 if (lock_orientation == blink::WebScreenOrientationLockDefault) { | 62 if (orientation == blink::WebScreenOrientationLockDefault) { |
| 64 // We are in a broken state, let's pretend we got canceled. | 63 // We are in a broken state, let's pretend we got canceled. |
| 65 dispatcher_->NotifyLockError(request_id, | 64 NotifyLockResult(ScreenOrientationLockResult:: |
| 66 blink::WebLockOrientationErrorCanceled); | 65 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 67 return; | 66 return; |
| 68 } | 67 } |
| 69 } | 68 } |
| 70 | 69 |
| 71 lock_applied_ = true; | 70 lock_applied_ = true; |
| 72 delegate_->Lock(web_contents(), lock_orientation); | 71 delegate_->Lock(web_contents(), orientation); |
| 73 | |
| 74 // If two calls happen close to each other some platforms will ignore the | |
| 75 // first. A successful lock will be once orientation matches the latest | |
| 76 // request. | |
| 77 pending_lock_.reset(); | |
| 78 | 72 |
| 79 // If the orientation we are locking to matches the current orientation, we | 73 // If the orientation we are locking to matches the current orientation, we |
| 80 // should succeed immediately. | 74 // should succeed immediately. |
| 81 if (LockMatchesCurrentOrientation(lock_orientation)) { | 75 if (LockMatchesCurrentOrientation(orientation)) { |
| 82 dispatcher_->NotifyLockSuccess(request_id); | 76 NotifyLockResult( |
| 77 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 83 return; | 78 return; |
| 84 } | 79 } |
| 85 | 80 |
| 86 pending_lock_.reset(new LockInformation(request_id, lock_orientation)); | 81 pending_lock_orientation_ = orientation; |
| 87 } | 82 } |
| 88 | 83 |
| 89 void ScreenOrientationProvider::UnlockOrientation() { | 84 void ScreenOrientationProvider::UnlockOrientation() { |
| 90 if (!lock_applied_ || !delegate_) | 85 if (!lock_applied_ || !delegate_) |
| 91 return; | 86 return; |
| 92 | 87 |
| 93 delegate_->Unlock(web_contents()); | 88 delegate_->Unlock(web_contents()); |
| 94 | 89 |
| 95 lock_applied_ = false; | 90 lock_applied_ = false; |
| 96 pending_lock_.reset(); | |
| 97 } | 91 } |
| 98 | 92 |
| 99 void ScreenOrientationProvider::OnOrientationChange() { | 93 void ScreenOrientationProvider::OnOrientationChange() { |
| 100 if (!pending_lock_.get()) | 94 if (pending_lock_orientation_ == blink::WebScreenOrientationLockDefault) |
| 101 return; | 95 return; |
| 102 | 96 |
| 103 if (LockMatchesCurrentOrientation(pending_lock_->lock)) { | 97 if (LockMatchesCurrentOrientation(pending_lock_orientation_)) { |
| 104 dispatcher_->NotifyLockSuccess(pending_lock_->request_id); | 98 DCHECK(!on_result_callback_.is_null()); |
| 105 pending_lock_.reset(); | 99 NotifyLockResult( |
| 100 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 106 } | 101 } |
| 107 } | 102 } |
| 108 | 103 |
| 104 void ScreenOrientationProvider::NotifyLockResult( |
| 105 ScreenOrientationLockResult result) { |
| 106 if (on_result_callback_.is_null()) |
| 107 return; |
| 108 on_result_callback_.Run(result); |
| 109 pending_lock_orientation_ = blink::WebScreenOrientationLockDefault; |
| 110 on_result_callback_ = LockOrientationCallback(); |
| 111 } |
| 112 |
| 109 void ScreenOrientationProvider::SetDelegate( | 113 void ScreenOrientationProvider::SetDelegate( |
| 110 ScreenOrientationDelegate* delegate) { | 114 ScreenOrientationDelegate* delegate) { |
| 111 delegate_ = delegate; | 115 delegate_ = delegate; |
| 112 } | 116 } |
| 113 | 117 |
| 114 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( | 118 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( |
| 115 bool entered_fullscreen, bool will_cause_resize) { | 119 bool entered_fullscreen, bool will_cause_resize) { |
| 116 if (!lock_applied_ || !delegate_) | 120 if (!lock_applied_ || !delegate_) |
| 117 return; | 121 return; |
| 118 | 122 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 case blink::WebScreenOrientationLockDefault: | 199 case blink::WebScreenOrientationLockDefault: |
| 196 NOTREACHED(); | 200 NOTREACHED(); |
| 197 return false; | 201 return false; |
| 198 } | 202 } |
| 199 | 203 |
| 200 NOTREACHED(); | 204 NOTREACHED(); |
| 201 return false; | 205 return false; |
| 202 } | 206 } |
| 203 | 207 |
| 204 } // namespace content | 208 } // namespace content |
| OLD | NEW |