| 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, | 19 const ScreenOrientationLockCallback& on_result_callback, |
| 24 WebContents* web_contents) | 20 WebContents* web_contents) |
| 25 : ScreenOrientationProvider(), | 21 : ScreenOrientationProvider(), |
| 26 WebContentsObserver(web_contents), | 22 WebContentsObserver(web_contents), |
| 27 dispatcher_(dispatcher), | 23 on_result_callback_(on_result_callback), |
| 28 lock_applied_(false), | 24 lock_applied_(false), |
| 29 pending_lock_(NULL) { | 25 pending_lock_orientation_(blink::WebScreenOrientationLockDefault) { |
| 30 } | 26 } |
| 31 | 27 |
| 32 ScreenOrientationProviderAndroid::~ScreenOrientationProviderAndroid() { | 28 ScreenOrientationProviderAndroid::~ScreenOrientationProviderAndroid() { |
| 33 if (pending_lock_) | |
| 34 delete pending_lock_; | |
| 35 } | 29 } |
| 36 | 30 |
| 37 WebContentsImpl* ScreenOrientationProviderAndroid::web_contents_impl() { | 31 WebContentsImpl* ScreenOrientationProviderAndroid::web_contents_impl() { |
| 38 return static_cast<WebContentsImpl*>(web_contents()); | 32 return static_cast<WebContentsImpl*>(web_contents()); |
| 39 } | 33 } |
| 40 | 34 |
| 41 // static | 35 // static |
| 42 bool ScreenOrientationProviderAndroid::Register(JNIEnv* env) { | 36 bool ScreenOrientationProviderAndroid::Register(JNIEnv* env) { |
| 43 return RegisterNativesImpl(env); | 37 return RegisterNativesImpl(env); |
| 44 } | 38 } |
| 45 | 39 |
| 46 void ScreenOrientationProviderAndroid::LockOrientation( | 40 void ScreenOrientationProviderAndroid::LockOrientation( |
| 47 int request_id, | 41 ScreenOrientationLockType lock_type) { |
| 48 blink::WebScreenOrientationLockType lock_orientation) { | 42 blink::WebScreenOrientationLockType lock_orientation = |
| 43 ScreenOrientationLockTypeToWebScreenOrientationLockType(lock_type); |
| 49 ContentViewCoreImpl* cvc = | 44 ContentViewCoreImpl* cvc = |
| 50 ContentViewCoreImpl::FromWebContents(web_contents()); | 45 ContentViewCoreImpl::FromWebContents(web_contents()); |
| 51 bool fullscreen_required = cvc ? cvc->IsFullscreenRequiredForOrientationLock() | 46 bool fullscreen_required = cvc ? cvc->IsFullscreenRequiredForOrientationLock() |
| 52 : true; | 47 : true; |
| 53 | 48 |
| 54 if (fullscreen_required && | 49 if (fullscreen_required && |
| 55 !web_contents_impl()->IsFullscreenForCurrentTab()) { | 50 !web_contents_impl()->IsFullscreenForCurrentTab()) { |
| 56 dispatcher_->NotifyLockError( | 51 on_result_callback_.Run( |
| 57 request_id, | 52 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED); |
| 58 blink::WebLockOrientationErrorFullScreenRequired); | |
| 59 return; | 53 return; |
| 60 } | 54 } |
| 61 | 55 |
| 62 if (lock_orientation == blink::WebScreenOrientationLockNatural) { | 56 if (lock_orientation == blink::WebScreenOrientationLockNatural) { |
| 63 lock_orientation = GetNaturalLockType(); | 57 lock_orientation = GetNaturalLockType(); |
| 64 if (lock_orientation == blink::WebScreenOrientationLockDefault) { | 58 if (lock_orientation == blink::WebScreenOrientationLockDefault) { |
| 65 // We are in a broken state, let's pretend we got canceled. | 59 // We are in a broken state, let's pretend we got canceled. |
| 66 dispatcher_->NotifyLockError(request_id, | 60 on_result_callback_.Run(SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED); |
| 67 blink::WebLockOrientationErrorCanceled); | |
| 68 return; | 61 return; |
| 69 } | 62 } |
| 70 } | 63 } |
| 71 | 64 |
| 72 lock_applied_ = true; | 65 lock_applied_ = true; |
| 73 Java_ScreenOrientationProvider_lockOrientation( | 66 Java_ScreenOrientationProvider_lockOrientation( |
| 74 base::android::AttachCurrentThread(), lock_orientation); | 67 base::android::AttachCurrentThread(), lock_orientation); |
| 75 | 68 |
| 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 | 69 // If the orientation we are locking to matches the current orientation, we |
| 83 // should succeed immediately. | 70 // should succeed immediately. |
| 84 if (LockMatchesCurrentOrientation(lock_orientation)) { | 71 if (LockMatchesCurrentOrientation(lock_orientation)) { |
| 85 dispatcher_->NotifyLockSuccess(request_id); | 72 on_result_callback_.Run(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 86 return; | 73 return; |
| 87 } | 74 } |
| 88 | 75 |
| 89 pending_lock_ = new LockInformation(request_id, lock_orientation); | 76 pending_lock_orientation_ = lock_orientation; |
| 90 } | 77 } |
| 91 | 78 |
| 92 void ScreenOrientationProviderAndroid::UnlockOrientation() { | 79 void ScreenOrientationProviderAndroid::UnlockOrientation() { |
| 93 if (!lock_applied_) | 80 if (!lock_applied_) |
| 94 return; | 81 return; |
| 95 | 82 |
| 96 Java_ScreenOrientationProvider_unlockOrientation( | 83 Java_ScreenOrientationProvider_unlockOrientation( |
| 97 base::android::AttachCurrentThread()); | 84 base::android::AttachCurrentThread()); |
| 98 lock_applied_ = false; | 85 lock_applied_ = false; |
| 99 } | 86 } |
| 100 | 87 |
| 101 void ScreenOrientationProviderAndroid::OnOrientationChange() { | 88 void ScreenOrientationProviderAndroid::OnOrientationChange() { |
| 102 if (!pending_lock_) | 89 if (pending_lock_orientation_ == blink::WebScreenOrientationLockDefault) |
| 103 return; | 90 return; |
| 104 | 91 |
| 105 if (LockMatchesCurrentOrientation(pending_lock_->lock)) { | 92 if (LockMatchesCurrentOrientation(pending_lock_orientation_)) { |
| 106 dispatcher_->NotifyLockSuccess(pending_lock_->request_id); | 93 on_result_callback_.Run(SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 107 delete pending_lock_; | 94 pending_lock_orientation_ = blink::WebScreenOrientationLockDefault; |
| 108 pending_lock_ = NULL; | |
| 109 } | 95 } |
| 110 } | 96 } |
| 111 | 97 |
| 112 void ScreenOrientationProviderAndroid::DidToggleFullscreenModeForTab( | 98 void ScreenOrientationProviderAndroid::DidToggleFullscreenModeForTab( |
| 113 bool entered_fullscreen) { | 99 bool entered_fullscreen) { |
| 114 if (!lock_applied_) | 100 if (!lock_applied_) |
| 115 return; | 101 return; |
| 116 | 102 |
| 117 // If fullscreen is not required in order to lock orientation, don't unlock | 103 // If fullscreen is not required in order to lock orientation, don't unlock |
| 118 // when fullscreen state changes. | 104 // when fullscreen state changes. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 } | 195 } |
| 210 | 196 |
| 211 // static | 197 // static |
| 212 void ScreenOrientationProviderAndroid::StopAccurateListening() { | 198 void ScreenOrientationProviderAndroid::StopAccurateListening() { |
| 213 Java_ScreenOrientationProvider_stopAccurateListening( | 199 Java_ScreenOrientationProvider_stopAccurateListening( |
| 214 base::android::AttachCurrentThread()); | 200 base::android::AttachCurrentThread()); |
| 215 } | 201 } |
| 216 | 202 |
| 217 // static | 203 // static |
| 218 ScreenOrientationProvider* ScreenOrientationProvider::Create( | 204 ScreenOrientationProvider* ScreenOrientationProvider::Create( |
| 219 ScreenOrientationDispatcherHost* dispatcher, | 205 const ScreenOrientationLockCallback& on_result_callback, |
| 220 WebContents* web_contents) { | 206 WebContents* web_contents) { |
| 221 return new ScreenOrientationProviderAndroid(dispatcher, web_contents); | 207 return new ScreenOrientationProviderAndroid(on_result_callback, web_contents); |
| 222 } | 208 } |
| 223 | 209 |
| 224 } // namespace content | 210 } // namespace content |
| OLD | NEW |