Index: third_party/WebKit/Source/modules/screen_orientation/ScreenOrientationControllerImpl.cpp |
diff --git a/third_party/WebKit/Source/modules/screen_orientation/ScreenOrientationControllerImpl.cpp b/third_party/WebKit/Source/modules/screen_orientation/ScreenOrientationControllerImpl.cpp |
index 6ce6ca399484c608e202bbc331d00eef55846963..109cd304bc77568ff2f934155375e72c3998606c 100644 |
--- a/third_party/WebKit/Source/modules/screen_orientation/ScreenOrientationControllerImpl.cpp |
+++ b/third_party/WebKit/Source/modules/screen_orientation/ScreenOrientationControllerImpl.cpp |
@@ -4,6 +4,8 @@ |
#include "modules/screen_orientation/ScreenOrientationControllerImpl.h" |
+#include <memory> |
+#include <utility> |
#include "core/dom/Document.h" |
#include "core/dom/TaskRunnerHelper.h" |
#include "core/events/Event.h" |
@@ -15,20 +17,19 @@ |
#include "modules/screen_orientation/ScreenOrientation.h" |
#include "platform/LayoutTestSupport.h" |
#include "platform/ScopedOrientationChangeIndicator.h" |
+#include "public/platform/InterfaceProvider.h" |
#include "public/platform/WebScreenInfo.h" |
-#include "public/platform/modules/screen_orientation/WebScreenOrientationClient.h" |
-#include <memory> |
-#include <utility> |
+#include "wtf/Functional.h" |
namespace blink { |
+using device::mojom::blink::ScreenOrientationLockResult; |
+ |
ScreenOrientationControllerImpl::~ScreenOrientationControllerImpl() = default; |
-void ScreenOrientationControllerImpl::provideTo( |
- LocalFrame& frame, |
- WebScreenOrientationClient* client) { |
+void ScreenOrientationControllerImpl::provideTo(LocalFrame& frame) { |
ScreenOrientationController::provideTo( |
- frame, new ScreenOrientationControllerImpl(frame, client)); |
+ frame, new ScreenOrientationControllerImpl(frame)); |
} |
ScreenOrientationControllerImpl* ScreenOrientationControllerImpl::from( |
@@ -38,16 +39,17 @@ ScreenOrientationControllerImpl* ScreenOrientationControllerImpl::from( |
} |
ScreenOrientationControllerImpl::ScreenOrientationControllerImpl( |
- LocalFrame& frame, |
- WebScreenOrientationClient* client) |
+ LocalFrame& frame) |
: ScreenOrientationController(frame), |
ContextLifecycleObserver(frame.document()), |
PageVisibilityObserver(frame.page()), |
- m_client(client), |
m_dispatchEventTimer( |
TaskRunnerHelper::get(TaskType::MiscPlatformAPI, &frame), |
this, |
- &ScreenOrientationControllerImpl::dispatchEventTimerFired) {} |
+ &ScreenOrientationControllerImpl::dispatchEventTimerFired) { |
+ frame.interfaceProvider()->getInterface( |
+ mojo::MakeRequest(&m_remoteScreenOrientation)); |
+} |
// Compute the screen orientation using the orientation angle and the screen |
// width / height. |
@@ -98,10 +100,19 @@ void ScreenOrientationControllerImpl::updateOrientation() { |
m_orientation->setType(orientationType); |
m_orientation->setAngle(screenInfo.orientationAngle); |
+ |
+ for (auto iter = m_waitCallbacks.begin(); iter != m_waitCallbacks.end();) { |
+ if (lockMatchesCurrentOrientation(iter->second)) { |
+ iter->first->onSuccess(); |
+ m_waitCallbacks.erase(iter); |
+ } else { |
+ ++iter; |
+ } |
+ } |
} |
bool ScreenOrientationControllerImpl::isActive() const { |
- return m_orientation && m_client; |
+ return m_orientation && m_remoteScreenOrientation; |
} |
bool ScreenOrientationControllerImpl::isVisible() const { |
@@ -174,26 +185,110 @@ void ScreenOrientationControllerImpl::setOrientation( |
notifyAccurateListen(); |
} |
+void ScreenOrientationControllerImpl::onLockOrientationResult( |
+ WebLockOrientationCallback* callback, |
+ ScreenOrientationLockResult result, |
+ WebScreenOrientationLockType lock) { |
+ // Lock type natual has been converted to other concrete values in browser |
+ // side. |
+ DCHECK(lock != WebScreenOrientationLockNatural); |
+ // This lock request has already been cancelled by another new lock or unlock |
+ // request. |
+ if (!m_pendingLockCallback || m_pendingLockCallback.get() != callback) |
+ return; |
+ |
+ switch (result) { |
+ case ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS: |
+ if (lockMatchesCurrentOrientation(lock)) { |
+ m_pendingLockCallback->onSuccess(); |
+ } else { |
+ // Wait until next updateOrientation(). |
+ m_waitCallbacks.insert( |
+ std::make_pair(std::move(m_pendingLockCallback), lock)); |
+ } |
+ break; |
+ case ScreenOrientationLockResult:: |
+ SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE: |
+ m_pendingLockCallback->onError( |
+ blink::WebLockOrientationErrorNotAvailable); |
+ break; |
+ case ScreenOrientationLockResult:: |
+ SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED: |
+ m_pendingLockCallback->onError( |
+ blink::WebLockOrientationErrorFullscreenRequired); |
+ break; |
+ case ScreenOrientationLockResult:: |
+ SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED: |
+ m_pendingLockCallback->onError(blink::WebLockOrientationErrorCanceled); |
+ break; |
+ default: |
+ NOTREACHED(); |
+ break; |
+ } |
+ m_pendingLockCallback = nullptr; |
+} |
+ |
+void ScreenOrientationControllerImpl::cancelPendingLock() { |
+ if (!m_pendingLockCallback) |
+ return; |
+ m_pendingLockCallback->onError(blink::WebLockOrientationErrorCanceled); |
+ m_pendingLockCallback = nullptr; |
+} |
+ |
+bool ScreenOrientationControllerImpl::lockMatchesCurrentOrientation( |
+ WebScreenOrientationLockType lock) { |
+ switch (lock) { |
+ case WebScreenOrientationLockPortraitPrimary: |
+ return m_orientation->getType() == WebScreenOrientationPortraitPrimary; |
+ case WebScreenOrientationLockPortraitSecondary: |
+ return m_orientation->getType() == WebScreenOrientationPortraitSecondary; |
+ case WebScreenOrientationLockLandscapePrimary: |
+ return m_orientation->getType() == WebScreenOrientationLandscapePrimary; |
+ case WebScreenOrientationLockLandscapeSecondary: |
+ return m_orientation->getType() == WebScreenOrientationLandscapeSecondary; |
+ case WebScreenOrientationLockLandscape: |
+ return m_orientation->getType() == WebScreenOrientationLandscapePrimary || |
+ m_orientation->getType() == WebScreenOrientationLandscapeSecondary; |
+ case WebScreenOrientationLockPortrait: |
+ return m_orientation->getType() == WebScreenOrientationPortraitPrimary || |
+ m_orientation->getType() == WebScreenOrientationPortraitSecondary; |
+ case WebScreenOrientationLockAny: |
+ return true; |
+ case WebScreenOrientationLockNatural: |
+ case WebScreenOrientationLockDefault: |
+ NOTREACHED(); |
+ return false; |
+ } |
+ |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
void ScreenOrientationControllerImpl::lock( |
WebScreenOrientationLockType orientation, |
std::unique_ptr<WebLockOrientationCallback> callback) { |
- // When detached, the client is no longer valid. |
- if (!m_client) |
+ cancelPendingLock(); |
+ // When detached, the remote screen orientation is no longer valid. |
+ if (!m_remoteScreenOrientation) |
return; |
- m_client->lockOrientation(orientation, std::move(callback)); |
- m_activeLock = true; |
+ m_remoteScreenOrientation->LockOrientation( |
+ orientation, |
+ convertToBaseCallback( |
+ WTF::bind(&ScreenOrientationControllerImpl::onLockOrientationResult, |
+ wrapPersistent(this), WTF::unretained(callback.get())))); |
+ m_pendingLockCallback = std::move(callback); |
} |
void ScreenOrientationControllerImpl::unlock() { |
- // When detached, the client is no longer valid. |
- if (!m_client) |
+ cancelPendingLock(); |
+ // When detached, the remote screen orientation is no longer valid. |
+ if (!m_remoteScreenOrientation) |
return; |
- m_client->unlockOrientation(); |
- m_activeLock = false; |
+ m_remoteScreenOrientation->UnlockOrientation(); |
} |
bool ScreenOrientationControllerImpl::maybeHasActiveLock() const { |
- return m_activeLock; |
+ return !!m_pendingLockCallback; |
} |
void ScreenOrientationControllerImpl::dispatchEventTimerFired(TimerBase*) { |
@@ -206,24 +301,24 @@ void ScreenOrientationControllerImpl::dispatchEventTimerFired(TimerBase*) { |
void ScreenOrientationControllerImpl::contextDestroyed(ExecutionContext*) { |
if (m_startedAccurateListen) { |
- m_client->stopAccurateListen(); |
+ m_remoteScreenOrientation->StopAccurateListen(); |
m_startedAccurateListen = false; |
} |
- m_client = nullptr; |
- m_activeLock = false; |
+ m_remoteScreenOrientation = nullptr; |
+ m_pendingLockCallback = nullptr; |
} |
void ScreenOrientationControllerImpl::notifyAccurateListen() { |
if (m_orientation && page()->isPageVisible()) { |
// Timing to start accurate listening. |
if (!m_startedAccurateListen) { |
- m_client->startAccurateListen(); |
+ m_remoteScreenOrientation->StartAccurateListen(); |
m_startedAccurateListen = true; |
} |
} else { |
// Timing to stop accurate listening. |
if (m_startedAccurateListen) { |
- m_client->stopAccurateListen(); |
+ m_remoteScreenOrientation->StopAccurateListen(); |
m_startedAccurateListen = false; |
} |
} |