Home | History | Annotate | Download | only in screen_orientation
      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/common/screen_orientation_messages.h"
      8 
      9 namespace content {
     10 
     11 ScreenOrientationDispatcher::ScreenOrientationDispatcher(
     12     RenderFrame* render_frame)
     13     : RenderFrameObserver(render_frame) {
     14 }
     15 
     16 ScreenOrientationDispatcher::~ScreenOrientationDispatcher() {
     17 }
     18 
     19 bool ScreenOrientationDispatcher::OnMessageReceived(
     20     const IPC::Message& message) {
     21   bool handled = true;
     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(
     35     int request_id,
     36     unsigned angle,
     37     blink::WebScreenOrientationType orientation) {
     38   blink::WebLockOrientationCallback* callback =
     39       pending_callbacks_.Lookup(request_id);
     40   if (!callback)
     41     return;
     42   callback->onSuccess(angle, orientation);
     43   pending_callbacks_.Remove(request_id);
     44 }
     45 
     46 void ScreenOrientationDispatcher::OnLockError(
     47     int request_id,
     48     blink::WebLockOrientationCallback::ErrorType 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() {
     58   for (CallbackMap::Iterator<blink::WebLockOrientationCallback>
     59        iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) {
     60     iterator.GetCurrentValue()->onError(
     61         blink::WebLockOrientationCallback::ErrorTypeCanceled);
     62     pending_callbacks_.Remove(iterator.GetCurrentKey());
     63   }
     64 }
     65 
     66 void ScreenOrientationDispatcher::lockOrientation(
     67     blink::WebScreenOrientationLockType orientation,
     68     blink::WebLockOrientationCallback* callback) {
     69   CancelPendingLocks();
     70 
     71   int request_id = pending_callbacks_.Add(callback);
     72   Send(new ScreenOrientationHostMsg_LockRequest(
     73       routing_id(), orientation, request_id));
     74 }
     75 
     76 void ScreenOrientationDispatcher::unlockOrientation() {
     77   CancelPendingLocks();
     78   Send(new ScreenOrientationHostMsg_Unlock(routing_id()));
     79 }
     80 
     81 }  // namespace content
     82