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
9namespace content {
10
11ScreenOrientationDispatcher::ScreenOrientationDispatcher(
12    RenderFrame* render_frame)
13    : RenderFrameObserver(render_frame) {
14}
15
16ScreenOrientationDispatcher::~ScreenOrientationDispatcher() {
17}
18
19bool 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
34void ScreenOrientationDispatcher::OnLockSuccess(int request_id) {
35  blink::WebLockOrientationCallback* callback =
36      pending_callbacks_.Lookup(request_id);
37  if (!callback)
38    return;
39  callback->onSuccess();
40  pending_callbacks_.Remove(request_id);
41}
42
43void 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
53void ScreenOrientationDispatcher::CancelPendingLocks() {
54  for (CallbackMap::Iterator<blink::WebLockOrientationCallback>
55       iterator(&pending_callbacks_); !iterator.IsAtEnd(); iterator.Advance()) {
56    iterator.GetCurrentValue()->onError(blink::WebLockOrientationErrorCanceled);
57    pending_callbacks_.Remove(iterator.GetCurrentKey());
58  }
59}
60
61void ScreenOrientationDispatcher::lockOrientation(
62    blink::WebScreenOrientationLockType orientation,
63    blink::WebLockOrientationCallback* callback) {
64  CancelPendingLocks();
65
66  int request_id = pending_callbacks_.Add(callback);
67  Send(new ScreenOrientationHostMsg_LockRequest(
68      routing_id(), orientation, request_id));
69}
70
71void ScreenOrientationDispatcher::unlockOrientation() {
72  CancelPendingLocks();
73  Send(new ScreenOrientationHostMsg_Unlock(routing_id()));
74}
75
76}  // namespace content
77