| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" | |
| 6 | |
| 7 #include "content/public/common/associated_interface_provider.h" | |
| 8 #include "content/public/renderer/render_frame.h" | |
| 9 | |
| 10 namespace content { | |
| 11 | |
| 12 using device::mojom::ScreenOrientationLockResult; | |
| 13 | |
| 14 ScreenOrientationDispatcher::ScreenOrientationDispatcher( | |
| 15 RenderFrame* render_frame) | |
| 16 : RenderFrameObserver(render_frame) { | |
| 17 } | |
| 18 | |
| 19 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() { | |
| 20 } | |
| 21 | |
| 22 void ScreenOrientationDispatcher::OnDestruct() { | |
| 23 delete this; | |
| 24 } | |
| 25 | |
| 26 void ScreenOrientationDispatcher::OnLockOrientationResult( | |
| 27 int request_id, | |
| 28 ScreenOrientationLockResult result) { | |
| 29 blink::WebLockOrientationCallback* callback = | |
| 30 pending_callbacks_.Lookup(request_id); | |
| 31 if (!callback) | |
| 32 return; | |
| 33 | |
| 34 switch (result) { | |
| 35 case ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS: | |
| 36 callback->onSuccess(); | |
| 37 break; | |
| 38 case ScreenOrientationLockResult:: | |
| 39 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE: | |
| 40 callback->onError(blink::WebLockOrientationErrorNotAvailable); | |
| 41 break; | |
| 42 case ScreenOrientationLockResult:: | |
| 43 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED: | |
| 44 callback->onError(blink::WebLockOrientationErrorFullscreenRequired); | |
| 45 break; | |
| 46 case ScreenOrientationLockResult:: | |
| 47 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED: | |
| 48 callback->onError(blink::WebLockOrientationErrorCanceled); | |
| 49 break; | |
| 50 default: | |
| 51 NOTREACHED(); | |
| 52 break; | |
| 53 } | |
| 54 | |
| 55 pending_callbacks_.Remove(request_id); | |
| 56 } | |
| 57 | |
| 58 void ScreenOrientationDispatcher::CancelPendingLocks() { | |
| 59 for (CallbackMap::Iterator<blink::WebLockOrientationCallback> | |
| 60 iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) { | |
| 61 iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled); | |
| 62 pending_callbacks_.Remove(iterator.GetCurrentKey()); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void ScreenOrientationDispatcher::lockOrientation( | |
| 67 blink::WebScreenOrientationLockType orientation, | |
| 68 std::unique_ptr<blink::WebLockOrientationCallback> callback) { | |
| 69 CancelPendingLocks(); | |
| 70 | |
| 71 int request_id = pending_callbacks_.Add(std::move(callback)); | |
| 72 GetRemoteScreenOrientation()->LockOrientation( | |
| 73 orientation, | |
| 74 base::Bind(&ScreenOrientationDispatcher::OnLockOrientationResult, | |
| 75 base::Unretained(this), request_id)); | |
| 76 } | |
| 77 | |
| 78 void ScreenOrientationDispatcher::unlockOrientation() { | |
| 79 CancelPendingLocks(); | |
| 80 GetRemoteScreenOrientation()->UnlockOrientation(); | |
| 81 } | |
| 82 | |
| 83 void ScreenOrientationDispatcher::startAccurateListen() { | |
| 84 GetRemoteScreenOrientation()->StartAccurateListen(); | |
| 85 } | |
| 86 | |
| 87 void ScreenOrientationDispatcher::stopAccurateListen() { | |
| 88 GetRemoteScreenOrientation()->StopAccurateListen(); | |
| 89 } | |
| 90 | |
| 91 device::mojom::ScreenOrientation* | |
| 92 ScreenOrientationDispatcher::GetRemoteScreenOrientation() { | |
| 93 if (!screen_orientation_) { | |
| 94 render_frame()->GetRemoteAssociatedInterfaces()->GetInterface( | |
| 95 &screen_orientation_); | |
| 96 } | |
| 97 | |
| 98 return screen_orientation_.get(); | |
| 99 } | |
| 100 | |
| 101 int ScreenOrientationDispatcher::GetRequestIdForTests() { | |
| 102 if (pending_callbacks_.IsEmpty()) | |
| 103 return -1; | |
| 104 CallbackMap::Iterator<blink::WebLockOrientationCallback> iterator( | |
| 105 &pending_callbacks_); | |
| 106 return iterator.GetCurrentKey(); | |
| 107 } | |
| 108 | |
| 109 } // namespace content | |
| OLD | NEW |