Chromium Code Reviews| 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 "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/optional.h" | |
| 9 #include "content/browser/renderer_host/render_view_host_impl.h" | 8 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 11 #include "content/public/browser/render_widget_host.h" | 10 #include "content/public/browser/render_widget_host.h" |
| 12 #include "content/public/browser/screen_orientation_delegate.h" | 11 #include "content/public/browser/screen_orientation_delegate.h" |
| 13 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 14 #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" |
| 15 | 14 |
| 16 namespace content { | 15 namespace content { |
| 17 | 16 |
| 18 using device::mojom::ScreenOrientationLockResult; | 17 using device::mojom::ScreenOrientationLockResult; |
| 19 | 18 |
| 20 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; | 19 ScreenOrientationDelegate* ScreenOrientationProvider::delegate_ = nullptr; |
| 21 | 20 |
| 22 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) | 21 ScreenOrientationProvider::ScreenOrientationProvider(WebContents* web_contents) |
| 23 : WebContentsObserver(web_contents), lock_applied_(false) {} | 22 : WebContentsObserver(web_contents), lock_applied_(false) {} |
| 24 | 23 |
| 25 ScreenOrientationProvider::~ScreenOrientationProvider() { | 24 ScreenOrientationProvider::~ScreenOrientationProvider() { |
| 26 } | 25 } |
| 27 | 26 |
| 28 void ScreenOrientationProvider::LockOrientation( | 27 void ScreenOrientationProvider::LockOrientation( |
| 29 blink::WebScreenOrientationLockType orientation, | 28 blink::WebScreenOrientationLockType orientation, |
| 30 const LockOrientationCallback& callback) { | 29 const LockOrientationCallback& callback) { |
| 31 // ScreenOrientation should have cancelled all pending request at this point. | 30 // Cancel any pending lock request. |
| 32 DCHECK(on_result_callback_.is_null()); | 31 NotifyLockResult(ScreenOrientationLockResult:: |
| 33 DCHECK(!pending_lock_orientation_.has_value()); | 32 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 34 on_result_callback_ = callback; | 33 // Record new pending lock request. |
| 34 pending_callback_ = callback; | |
| 35 | 35 |
| 36 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { | 36 if (!delegate_ || !delegate_->ScreenOrientationProviderSupported()) { |
| 37 NotifyLockResult(ScreenOrientationLockResult:: | 37 NotifyLockResult(ScreenOrientationLockResult:: |
| 38 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); | 38 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE); |
| 39 return; | 39 return; |
| 40 } | 40 } |
| 41 | 41 |
| 42 if (delegate_->FullScreenRequired(web_contents())) { | 42 if (delegate_->FullScreenRequired(web_contents())) { |
| 43 RenderViewHostImpl* rvhi = | 43 RenderViewHostImpl* rvhi = |
| 44 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); | 44 static_cast<RenderViewHostImpl*>(web_contents()->GetRenderViewHost()); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 74 if (LockMatchesCurrentOrientation(orientation)) { | 74 if (LockMatchesCurrentOrientation(orientation)) { |
| 75 NotifyLockResult( | 75 NotifyLockResult( |
| 76 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 76 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 77 return; | 77 return; |
| 78 } | 78 } |
| 79 | 79 |
| 80 pending_lock_orientation_ = orientation; | 80 pending_lock_orientation_ = orientation; |
| 81 } | 81 } |
| 82 | 82 |
| 83 void ScreenOrientationProvider::UnlockOrientation() { | 83 void ScreenOrientationProvider::UnlockOrientation() { |
| 84 // Cancel any pending lock request. | |
| 85 NotifyLockResult(ScreenOrientationLockResult:: | |
| 86 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 87 | |
| 84 if (!lock_applied_ || !delegate_) | 88 if (!lock_applied_ || !delegate_) |
| 85 return; | 89 return; |
| 86 | 90 |
| 87 delegate_->Unlock(web_contents()); | 91 delegate_->Unlock(web_contents()); |
| 88 | 92 |
| 89 lock_applied_ = false; | 93 lock_applied_ = false; |
| 90 pending_lock_orientation_.reset(); | |
| 91 } | 94 } |
| 92 | 95 |
| 93 void ScreenOrientationProvider::OnOrientationChange() { | 96 void ScreenOrientationProvider::OnOrientationChange() { |
| 94 if (!pending_lock_orientation_.has_value()) | 97 if (!pending_lock_orientation_.has_value()) |
| 95 return; | 98 return; |
| 96 | 99 |
| 97 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { | 100 if (LockMatchesCurrentOrientation(pending_lock_orientation_.value())) { |
| 98 DCHECK(!on_result_callback_.is_null()); | 101 DCHECK(!pending_callback_.is_null()); |
| 99 NotifyLockResult( | 102 NotifyLockResult( |
| 100 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 103 ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 101 } | 104 } |
| 102 } | 105 } |
| 103 | 106 |
| 104 void ScreenOrientationProvider::NotifyLockResult( | 107 void ScreenOrientationProvider::NotifyLockResult( |
| 105 ScreenOrientationLockResult result) { | 108 ScreenOrientationLockResult result) { |
| 106 if (on_result_callback_.is_null()) { | 109 if (!pending_callback_.is_null()) { |
| 107 pending_lock_orientation_.reset(); | 110 base::ResetAndReturn(&pending_callback_).Run(result); |
| 108 return; | |
| 109 } | 111 } |
|
kinuko
2017/01/23 02:39:50
nit: no need of block for one-line body
leonhsl(Using Gerrit)
2017/01/23 02:47:10
Got it, as I've sent this CL to CQ now so I will a
| |
| 110 base::ResetAndReturn(&on_result_callback_).Run(result); | |
| 111 pending_lock_orientation_.reset(); | 112 pending_lock_orientation_.reset(); |
| 112 } | 113 } |
| 113 | 114 |
| 114 void ScreenOrientationProvider::SetDelegate( | 115 void ScreenOrientationProvider::SetDelegate( |
| 115 ScreenOrientationDelegate* delegate) { | 116 ScreenOrientationDelegate* delegate) { |
| 116 delegate_ = delegate; | 117 delegate_ = delegate; |
| 117 } | 118 } |
| 118 | 119 |
| 119 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( | 120 void ScreenOrientationProvider::DidToggleFullscreenModeForTab( |
| 120 bool entered_fullscreen, bool will_cause_resize) { | 121 bool entered_fullscreen, bool will_cause_resize) { |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 case blink::WebScreenOrientationLockDefault: | 201 case blink::WebScreenOrientationLockDefault: |
| 201 NOTREACHED(); | 202 NOTREACHED(); |
| 202 return false; | 203 return false; |
| 203 } | 204 } |
| 204 | 205 |
| 205 NOTREACHED(); | 206 NOTREACHED(); |
| 206 return false; | 207 return false; |
| 207 } | 208 } |
| 208 | 209 |
| 209 } // namespace content | 210 } // namespace content |
| OLD | NEW |