| 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/common/associated_interface_provider.h" |
| 6 #include "content/public/renderer/render_frame.h" |
| 5 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" | 7 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" |
| 6 | 8 |
| 7 #include "content/common/screen_orientation_messages.h" | |
| 8 | |
| 9 namespace content { | 9 namespace content { |
| 10 | 10 |
| 11 using ::blink::mojom::ScreenOrientationLockResult; |
| 12 |
| 11 ScreenOrientationDispatcher::ScreenOrientationDispatcher( | 13 ScreenOrientationDispatcher::ScreenOrientationDispatcher( |
| 12 RenderFrame* render_frame) | 14 RenderFrame* render_frame) |
| 13 : RenderFrameObserver(render_frame) { | 15 : render_frame_(render_frame) {} |
| 14 } | |
| 15 | 16 |
| 16 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() { | 17 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() { |
| 17 } | 18 } |
| 18 | 19 |
| 19 bool ScreenOrientationDispatcher::OnMessageReceived( | 20 void ScreenOrientationDispatcher::OnLockOrientationResult( |
| 20 const IPC::Message& message) { | 21 int request_id, |
| 21 bool handled = true; | 22 ScreenOrientationLockResult result) { |
| 22 | |
| 23 IPC_BEGIN_MESSAGE_MAP(ScreenOrientationDispatcher, message) | |
| 24 IPC_MESSAGE_HANDLER(ScreenOrientationMsg_LockSuccess, | |
| 25 OnLockSuccess) | |
| 26 IPC_MESSAGE_HANDLER(ScreenOrientationMsg_LockError, | |
| 27 OnLockError) | |
| 28 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 29 IPC_END_MESSAGE_MAP() | |
| 30 | |
| 31 return handled; | |
| 32 } | |
| 33 | |
| 34 void ScreenOrientationDispatcher::OnDestruct() { | |
| 35 delete this; | |
| 36 } | |
| 37 | |
| 38 void ScreenOrientationDispatcher::OnLockSuccess(int request_id) { | |
| 39 blink::WebLockOrientationCallback* callback = | 23 blink::WebLockOrientationCallback* callback = |
| 40 pending_callbacks_.Lookup(request_id); | 24 pending_callbacks_.Lookup(request_id); |
| 41 if (!callback) | 25 if (!callback) |
| 42 return; | 26 return; |
| 43 callback->onSuccess(); | 27 |
| 28 switch (result) { |
| 29 case ScreenOrientationLockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS: |
| 30 callback->onSuccess(); |
| 31 break; |
| 32 case ScreenOrientationLockResult:: |
| 33 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE: |
| 34 callback->onError(blink::WebLockOrientationErrorNotAvailable); |
| 35 break; |
| 36 case ScreenOrientationLockResult:: |
| 37 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED: |
| 38 callback->onError(blink::WebLockOrientationErrorFullscreenRequired); |
| 39 break; |
| 40 case ScreenOrientationLockResult:: |
| 41 SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED: |
| 42 callback->onError(blink::WebLockOrientationErrorCanceled); |
| 43 break; |
| 44 default: |
| 45 NOTREACHED(); |
| 46 break; |
| 47 } |
| 48 |
| 44 pending_callbacks_.Remove(request_id); | 49 pending_callbacks_.Remove(request_id); |
| 45 } | 50 } |
| 46 | 51 |
| 47 void ScreenOrientationDispatcher::OnLockError( | |
| 48 int request_id, blink::WebLockOrientationError error) { | |
| 49 blink::WebLockOrientationCallback* callback = | |
| 50 pending_callbacks_.Lookup(request_id); | |
| 51 if (!callback) | |
| 52 return; | |
| 53 callback->onError(error); | |
| 54 pending_callbacks_.Remove(request_id); | |
| 55 } | |
| 56 | |
| 57 void ScreenOrientationDispatcher::CancelPendingLocks() { | 52 void ScreenOrientationDispatcher::CancelPendingLocks() { |
| 58 for (CallbackMap::Iterator<blink::WebLockOrientationCallback> | 53 for (CallbackMap::Iterator<blink::WebLockOrientationCallback> |
| 59 iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) { | 54 iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) { |
| 60 iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled); | 55 iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled); |
| 61 pending_callbacks_.Remove(iterator.GetCurrentKey()); | 56 pending_callbacks_.Remove(iterator.GetCurrentKey()); |
| 62 } | 57 } |
| 63 } | 58 } |
| 64 | 59 |
| 65 void ScreenOrientationDispatcher::lockOrientation( | 60 void ScreenOrientationDispatcher::lockOrientation( |
| 66 blink::WebScreenOrientationLockType orientation, | 61 blink::WebScreenOrientationLockType orientation, |
| 67 blink::WebLockOrientationCallback* callback) { | 62 blink::WebLockOrientationCallback* callback) { |
| 68 CancelPendingLocks(); | 63 CancelPendingLocks(); |
| 69 | 64 |
| 70 int request_id = pending_callbacks_.Add(callback); | 65 int request_id = pending_callbacks_.Add(callback); |
| 71 Send(new ScreenOrientationHostMsg_LockRequest( | 66 setScreenOrientationInterface(); |
| 72 routing_id(), orientation, request_id)); | 67 screen_orientation_->LockOrientation( |
| 68 orientation, |
| 69 base::Bind(&ScreenOrientationDispatcher::OnLockOrientationResult, |
| 70 base::Unretained(this), request_id)); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void ScreenOrientationDispatcher::unlockOrientation() { | 73 void ScreenOrientationDispatcher::unlockOrientation() { |
| 76 CancelPendingLocks(); | 74 CancelPendingLocks(); |
| 77 Send(new ScreenOrientationHostMsg_Unlock(routing_id())); | 75 setScreenOrientationInterface(); |
| 76 screen_orientation_->UnlockOrientation(); |
| 77 } |
| 78 |
| 79 void ScreenOrientationDispatcher::setScreenOrientationInterface() { |
| 80 if (!screen_orientation_) |
| 81 render_frame_->GetRemoteAssociatedInterfaces()->GetInterface( |
| 82 &screen_orientation_); |
| 83 } |
| 84 |
| 85 int ScreenOrientationDispatcher::getRequestIdForTests() { |
| 86 if (pending_callbacks_.IsEmpty()) |
| 87 return -1; |
| 88 CallbackMap::Iterator<blink::WebLockOrientationCallback> iterator( |
| 89 &pending_callbacks_); |
| 90 return iterator.GetCurrentKey(); |
| 78 } | 91 } |
| 79 | 92 |
| 80 } // namespace content | 93 } // namespace content |
| OLD | NEW |