summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/mouse_lock.cc
blob: 2ece8fac26308a6455620d4e9e71e14e22689036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/cpp/mouse_lock.h"

#include "ppapi/c/ppb_mouse_lock.h"
#include "ppapi/c/ppp_mouse_lock.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/cpp/instance.h"
#include "ppapi/cpp/instance_handle.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/module_impl.h"

namespace pp {

namespace {

static const char kPPPMouseLockInterface[] = PPP_MOUSELOCK_INTERFACE;

void MouseLockLost(PP_Instance instance) {
  void* object =
      Instance::GetPerInstanceObject(instance, kPPPMouseLockInterface);
  if (!object)
    return;
  static_cast<MouseLock*>(object)->MouseLockLost();
}

const PPP_MouseLock ppp_mouse_lock = {
  &MouseLockLost
};

template <> const char* interface_name<PPB_MouseLock_1_0>() {
  return PPB_MOUSELOCK_INTERFACE_1_0;
}

}  // namespace

MouseLock::MouseLock(Instance* instance)
    : associated_instance_(instance) {
  Module::Get()->AddPluginInterface(kPPPMouseLockInterface, &ppp_mouse_lock);
  instance->AddPerInstanceObject(kPPPMouseLockInterface, this);
}

MouseLock::~MouseLock() {
  Instance::RemovePerInstanceObject(associated_instance_,
                                    kPPPMouseLockInterface, this);
}

int32_t MouseLock::LockMouse(const CompletionCallback& cc) {
  if (!has_interface<PPB_MouseLock_1_0>())
    return cc.MayForce(PP_ERROR_NOINTERFACE);
  return get_interface<PPB_MouseLock_1_0>()->LockMouse(
      associated_instance_.pp_instance(), cc.pp_completion_callback());
}

void MouseLock::UnlockMouse() {
  if (has_interface<PPB_MouseLock_1_0>()) {
    get_interface<PPB_MouseLock_1_0>()->UnlockMouse(
        associated_instance_.pp_instance());
  }
}

}  // namespace pp