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/browser/screen_orientation/screen_orientation_provider_android .h" | 5 #include "content/browser/screen_orientation/screen_orientation_provider_android .h" |
| 6 | 6 |
| 7 #include "content/browser/android/content_view_core_impl.h" | 7 #include "content/browser/android/content_view_core_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/common/screen_orientation_utils.h" | |
| 9 #include "content/public/browser/render_view_host.h" | 10 #include "content/public/browser/render_view_host.h" |
| 10 #include "content/public/browser/render_widget_host.h" | 11 #include "content/public/browser/render_widget_host.h" |
| 11 #include "content/public/browser/screen_orientation_dispatcher_host.h" | |
| 12 #include "jni/ScreenOrientationProvider_jni.h" | 12 #include "jni/ScreenOrientationProvider_jni.h" |
| 13 #include "third_party/WebKit/public/platform/WebLockOrientationError.h" | 13 #include "third_party/WebKit/public/platform/WebLockOrientationError.h" |
| 14 #include "third_party/WebKit/public/platform/WebScreenInfo.h" | 14 #include "third_party/WebKit/public/platform/WebScreenInfo.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 ScreenOrientationProviderAndroid::LockInformation::LockInformation( | |
| 19 int request_id, blink::WebScreenOrientationLockType lock) | |
| 20 : request_id(request_id), lock(lock) {} | |
| 21 | |
| 22 ScreenOrientationProviderAndroid::ScreenOrientationProviderAndroid( | 18 ScreenOrientationProviderAndroid::ScreenOrientationProviderAndroid( |
| 23 ScreenOrientationDispatcherHost* dispatcher, | |
| 24 WebContents* web_contents) | 19 WebContents* web_contents) |
| 25 : ScreenOrientationProvider(), | 20 : ScreenOrientationProvider(), |
| 26 WebContentsObserver(web_contents), | 21 WebContentsObserver(web_contents), |
| 27 dispatcher_(dispatcher), | |
| 28 lock_applied_(false), | 22 lock_applied_(false), |
| 29 pending_lock_(NULL) { | 23 pending_lock_orientation_(blink::WebScreenOrientationLockDefault) { |
| 30 } | 24 } |
| 31 | 25 |
| 32 ScreenOrientationProviderAndroid::~ScreenOrientationProviderAndroid() { | 26 ScreenOrientationProviderAndroid::~ScreenOrientationProviderAndroid() { |
| 33 if (pending_lock_) | |
| 34 delete pending_lock_; | |
| 35 } | 27 } |
| 36 | 28 |
| 37 WebContentsImpl* ScreenOrientationProviderAndroid::web_contents_impl() { | 29 WebContentsImpl* ScreenOrientationProviderAndroid::web_contents_impl() { |
| 38 return static_cast<WebContentsImpl*>(web_contents()); | 30 return static_cast<WebContentsImpl*>(web_contents()); |
| 39 } | 31 } |
| 40 | 32 |
| 41 // static | 33 // static |
| 42 bool ScreenOrientationProviderAndroid::Register(JNIEnv* env) { | 34 bool ScreenOrientationProviderAndroid::Register(JNIEnv* env) { |
| 43 return RegisterNativesImpl(env); | 35 return RegisterNativesImpl(env); |
| 44 } | 36 } |
| 45 | 37 |
| 38 void ScreenOrientationProviderAndroid::NotifyLockResult( | |
| 39 ScreenOrientationLockResult result) { | |
| 40 if (on_result_callback_.is_null()) | |
| 41 return; | |
| 42 on_result_callback_.Run(result); | |
| 43 pending_lock_orientation_ = blink::WebScreenOrientationLockDefault; | |
| 44 on_result_callback_ = ScreenOrientationLockCallback(); | |
| 45 } | |
| 46 | |
| 46 void ScreenOrientationProviderAndroid::LockOrientation( | 47 void ScreenOrientationProviderAndroid::LockOrientation( |
| 47 int request_id, | 48 ScreenOrientationLockType lock_type, |
| 48 blink::WebScreenOrientationLockType lock_orientation) { | 49 const ScreenOrientationLockCallback& callback) { |
| 50 // Cancel any pending request. | |
| 51 NotifyLockResult(SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); | |
| 52 on_result_callback_ = callback; | |
| 53 | |
| 54 blink::WebScreenOrientationLockType lock_orientation = | |
| 55 ScreenOrientationLockTypeToWebScreenOrientationLockType(lock_type); | |
| 49 ContentViewCoreImpl* cvc = | 56 ContentViewCoreImpl* cvc = |
| 50 ContentViewCoreImpl::FromWebContents(web_contents()); | 57 ContentViewCoreImpl::FromWebContents(web_contents()); |
| 51 bool fullscreen_required = cvc ? cvc->IsFullscreenRequiredForOrientationLock() | 58 bool fullscreen_required = cvc ? cvc->IsFullscreenRequiredForOrientationLock() |
| 52 : true; | 59 : true; |
| 53 | 60 |
| 54 if (fullscreen_required && | 61 if (fullscreen_required && |
| 55 !web_contents_impl()->IsFullscreenForCurrentTab()) { | 62 !web_contents_impl()->IsFullscreenForCurrentTab()) { |
| 56 dispatcher_->NotifyLockError( | 63 NotifyLockResult(SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); |
| 57 request_id, | |
| 58 blink::WebLockOrientationErrorFullScreenRequired); | |
| 59 return; | 64 return; |
| 60 } | 65 } |
| 61 | 66 |
| 62 if (lock_orientation == blink::WebScreenOrientationLockNatural) { | 67 if (lock_orientation == blink::WebScreenOrientationLockNatural) { |
| 63 lock_orientation = GetNaturalLockType(); | 68 lock_orientation = GetNaturalLockType(); |
| 64 if (lock_orientation == blink::WebScreenOrientationLockDefault) { | 69 if (lock_orientation == blink::WebScreenOrientationLockDefault) { |
| 65 // We are in a broken state, let's pretend we got canceled. | 70 // We are in a broken state, let's pretend we got canceled. |
| 66 dispatcher_->NotifyLockError(request_id, | 71 NotifyLockResult(SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 67 blink::WebLockOrientationErrorCanceled); | |
| 68 return; | 72 return; |
| 69 } | 73 } |
| 70 } | 74 } |
| 71 | 75 |
| 72 lock_applied_ = true; | 76 lock_applied_ = true; |
| 73 Java_ScreenOrientationProvider_lockOrientation( | 77 Java_ScreenOrientationProvider_lockOrientation( |
| 74 base::android::AttachCurrentThread(), lock_orientation); | 78 base::android::AttachCurrentThread(), lock_orientation); |
| 75 | 79 |
| 76 // If two calls happen close to each other, Android will ignore the first. | |
| 77 if (pending_lock_) { | |
| 78 delete pending_lock_; | |
| 79 pending_lock_ = NULL; | |
| 80 } | |
| 81 | |
| 82 // If the orientation we are locking to matches the current orientation, we | 80 // If the orientation we are locking to matches the current orientation, we |
| 83 // should succeed immediately. | 81 // should succeed immediately. |
| 84 if (LockMatchesCurrentOrientation(lock_orientation)) { | 82 if (LockMatchesCurrentOrientation(lock_orientation)) { |
| 85 dispatcher_->NotifyLockSuccess(request_id); | 83 NotifyLockResult(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 86 return; | 84 return; |
| 87 } | 85 } |
| 88 | 86 |
| 89 pending_lock_ = new LockInformation(request_id, lock_orientation); | 87 pending_lock_orientation_ = lock_orientation; |
| 90 } | 88 } |
| 91 | 89 |
| 92 void ScreenOrientationProviderAndroid::UnlockOrientation() { | 90 void ScreenOrientationProviderAndroid::UnlockOrientation() { |
| 93 if (!lock_applied_) | 91 if (!lock_applied_) |
| 94 return; | 92 return; |
| 95 | 93 |
| 96 Java_ScreenOrientationProvider_unlockOrientation( | 94 Java_ScreenOrientationProvider_unlockOrientation( |
| 97 base::android::AttachCurrentThread()); | 95 base::android::AttachCurrentThread()); |
| 98 lock_applied_ = false; | 96 lock_applied_ = false; |
| 99 } | 97 } |
| 100 | 98 |
| 101 void ScreenOrientationProviderAndroid::OnOrientationChange() { | 99 void ScreenOrientationProviderAndroid::OnOrientationChange() { |
| 102 if (!pending_lock_) | 100 if (pending_lock_orientation_ == blink::WebScreenOrientationLockDefault) |
| 103 return; | 101 return; |
| 104 | 102 |
| 105 if (LockMatchesCurrentOrientation(pending_lock_->lock)) { | 103 if (LockMatchesCurrentOrientation(pending_lock_orientation_)) { |
|
qsr
2014/09/23 08:22:59
I don't understand exactly why we need this. If we
| |
| 106 dispatcher_->NotifyLockSuccess(pending_lock_->request_id); | 104 DCHECK(!on_result_callback_.is_null()); |
| 107 delete pending_lock_; | 105 NotifyLockResult(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 108 pending_lock_ = NULL; | |
| 109 } | 106 } |
| 110 } | 107 } |
| 111 | 108 |
| 112 void ScreenOrientationProviderAndroid::DidToggleFullscreenModeForTab( | 109 void ScreenOrientationProviderAndroid::DidToggleFullscreenModeForTab( |
| 113 bool entered_fullscreen) { | 110 bool entered_fullscreen) { |
| 114 if (!lock_applied_) | 111 if (!lock_applied_) |
| 115 return; | 112 return; |
| 116 | 113 |
| 117 // If fullscreen is not required in order to lock orientation, don't unlock | 114 // If fullscreen is not required in order to lock orientation, don't unlock |
| 118 // when fullscreen state changes. | 115 // when fullscreen state changes. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 } | 206 } |
| 210 | 207 |
| 211 // static | 208 // static |
| 212 void ScreenOrientationProviderAndroid::StopAccurateListening() { | 209 void ScreenOrientationProviderAndroid::StopAccurateListening() { |
| 213 Java_ScreenOrientationProvider_stopAccurateListening( | 210 Java_ScreenOrientationProvider_stopAccurateListening( |
| 214 base::android::AttachCurrentThread()); | 211 base::android::AttachCurrentThread()); |
| 215 } | 212 } |
| 216 | 213 |
| 217 // static | 214 // static |
| 218 ScreenOrientationProvider* ScreenOrientationProvider::Create( | 215 ScreenOrientationProvider* ScreenOrientationProvider::Create( |
| 219 ScreenOrientationDispatcherHost* dispatcher, | |
| 220 WebContents* web_contents) { | 216 WebContents* web_contents) { |
| 221 return new ScreenOrientationProviderAndroid(dispatcher, web_contents); | 217 return new ScreenOrientationProviderAndroid(web_contents); |
| 222 } | 218 } |
| 223 | 219 |
| 224 } // namespace content | 220 } // namespace content |
| OLD | NEW |