| 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/renderer/screen_orientation/screen_orientation_dispatcher.h" | 5 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" | 
| 6 | 6 | 
| 7 #include "content/common/screen_orientation_messages.h" | 7 #include "content/common/screen_orientation_utils.h" | 
|  | 8 #include "content/public/common/service_registry.h" | 
| 8 | 9 | 
| 9 namespace content { | 10 namespace content { | 
| 10 | 11 | 
| 11 ScreenOrientationDispatcher::ScreenOrientationDispatcher( | 12 ScreenOrientationDispatcher::ScreenOrientationDispatcher( | 
| 12     RenderFrame* render_frame) | 13     ScreenOrientationService* screen_orientation_service) | 
| 13     : RenderFrameObserver(render_frame) { | 14     : screen_orientation_service_(screen_orientation_service) { | 
|  | 15   DCHECK(screen_orientation_service_); | 
| 14 } | 16 } | 
| 15 | 17 | 
| 16 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() { | 18 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() { | 
| 17 } | 19 } | 
| 18 | 20 | 
| 19 bool ScreenOrientationDispatcher::OnMessageReceived( | 21 void ScreenOrientationDispatcher::OnLockOrientationResult( | 
| 20     const IPC::Message& message) { | 22     int request_id, | 
| 21   bool handled = true; | 23     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::OnLockSuccess(int request_id) { |  | 
| 35   blink::WebLockOrientationCallback* callback = | 24   blink::WebLockOrientationCallback* callback = | 
| 36       pending_callbacks_.Lookup(request_id); | 25       pending_callbacks_.Lookup(request_id); | 
| 37   if (!callback) | 26   if (!callback) | 
| 38     return; | 27     return; | 
| 39   callback->onSuccess(); | 28 | 
|  | 29   switch (result) { | 
|  | 30     case SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS: | 
|  | 31       callback->onSuccess(); | 
|  | 32       break; | 
|  | 33     case SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE: | 
|  | 34       callback->onError(blink::WebLockOrientationErrorNotAvailable); | 
|  | 35       break; | 
|  | 36     case SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED: | 
|  | 37       callback->onError(blink::WebLockOrientationErrorFullScreenRequired); | 
|  | 38       break; | 
|  | 39     case SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED: | 
|  | 40       callback->onError(blink::WebLockOrientationErrorCanceled); | 
|  | 41       break; | 
|  | 42     default: | 
|  | 43       NOTREACHED(); | 
|  | 44       break; | 
|  | 45   } | 
|  | 46 | 
| 40   pending_callbacks_.Remove(request_id); | 47   pending_callbacks_.Remove(request_id); | 
| 41 } | 48 } | 
| 42 | 49 | 
| 43 void ScreenOrientationDispatcher::OnLockError( |  | 
| 44     int request_id, blink::WebLockOrientationError error) { |  | 
| 45   blink::WebLockOrientationCallback* callback = |  | 
| 46       pending_callbacks_.Lookup(request_id); |  | 
| 47   if (!callback) |  | 
| 48     return; |  | 
| 49   callback->onError(error); |  | 
| 50   pending_callbacks_.Remove(request_id); |  | 
| 51 } |  | 
| 52 |  | 
| 53 void ScreenOrientationDispatcher::CancelPendingLocks() { | 50 void ScreenOrientationDispatcher::CancelPendingLocks() { | 
| 54   for (CallbackMap::Iterator<blink::WebLockOrientationCallback> | 51   for (CallbackMap::Iterator<blink::WebLockOrientationCallback> | 
| 55        iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) { | 52        iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) { | 
| 56     iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled); | 53     iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled); | 
| 57     pending_callbacks_.Remove(iterator.GetCurrentKey()); | 54     pending_callbacks_.Remove(iterator.GetCurrentKey()); | 
| 58   } | 55   } | 
| 59 } | 56 } | 
| 60 | 57 | 
| 61 void ScreenOrientationDispatcher::lockOrientation( | 58 void ScreenOrientationDispatcher::lockOrientation( | 
| 62     blink::WebScreenOrientationLockType orientation, | 59     blink::WebScreenOrientationLockType orientation, | 
| 63     blink::WebLockOrientationCallback* callback) { | 60     blink::WebLockOrientationCallback* callback) { | 
| 64   CancelPendingLocks(); | 61   CancelPendingLocks(); | 
| 65 | 62 | 
| 66   int request_id = pending_callbacks_.Add(callback); | 63   int request_id = pending_callbacks_.Add(callback); | 
| 67   Send(new ScreenOrientationHostMsg_LockRequest( | 64   DCHECK(screen_orientation_service_); | 
| 68       routing_id(), orientation, request_id)); | 65   screen_orientation_service_->LockOrientation( | 
|  | 66       WebScreenOrientationLockTypeToScreenOrientationLockType(orientation), | 
|  | 67       base::Bind(&ScreenOrientationDispatcher::OnLockOrientationResult, | 
|  | 68                  base::Unretained(this), | 
|  | 69                  request_id)); | 
| 69 } | 70 } | 
| 70 | 71 | 
| 71 void ScreenOrientationDispatcher::unlockOrientation() { | 72 void ScreenOrientationDispatcher::unlockOrientation() { | 
| 72   CancelPendingLocks(); | 73   CancelPendingLocks(); | 
| 73   Send(new ScreenOrientationHostMsg_Unlock(routing_id())); | 74   screen_orientation_service_->UnlockOrientation(); | 
| 74 } | 75 } | 
| 75 | 76 | 
| 76 }  // namespace content | 77 }  // namespace content | 
| OLD | NEW | 
|---|