blob: e000f9ccf53d7419788f4558482ba3d43a3c49a2 [file] [log] [blame]
[email protected]1c7e5c92012-03-22 00:58:281// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]1758e882010-11-01 16:16:502// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/cpp/instance.h"
6
[email protected]493d14212011-07-07 15:38:487#include "ppapi/c/pp_errors.h"
8#include "ppapi/c/ppb_input_event.h"
[email protected]1758e882010-11-01 16:16:509#include "ppapi/c/ppb_instance.h"
[email protected]5f2cb1c92011-04-08 21:36:2310#include "ppapi/c/ppb_messaging.h"
[email protected]1758e882010-11-01 16:16:5011#include "ppapi/cpp/graphics_2d.h"
[email protected]fae0e942011-09-07 17:10:4612#include "ppapi/cpp/graphics_3d.h"
[email protected]1758e882010-11-01 16:16:5013#include "ppapi/cpp/image_data.h"
[email protected]09af0f72012-02-27 20:23:1914#include "ppapi/cpp/instance_handle.h"
[email protected]1758e882010-11-01 16:16:5015#include "ppapi/cpp/logging.h"
16#include "ppapi/cpp/module.h"
17#include "ppapi/cpp/module_impl.h"
18#include "ppapi/cpp/point.h"
19#include "ppapi/cpp/resource.h"
20#include "ppapi/cpp/var.h"
[email protected]e8f07ac2012-01-03 17:43:3621#include "ppapi/cpp/view.h"
[email protected]1758e882010-11-01 16:16:5022
[email protected]6b7550a2010-12-20 19:03:0723namespace pp {
24
[email protected]1758e882010-11-01 16:16:5025namespace {
26
[email protected]1c7e5c92012-03-22 00:58:2827template <> const char* interface_name<PPB_InputEvent_1_0>() {
28 return PPB_INPUT_EVENT_INTERFACE_1_0;
[email protected]493d14212011-07-07 15:38:4829}
30
[email protected]1c7e5c92012-03-22 00:58:2831template <> const char* interface_name<PPB_Instance_1_0>() {
32 return PPB_INSTANCE_INTERFACE_1_0;
[email protected]6b7550a2010-12-20 19:03:0733}
[email protected]1758e882010-11-01 16:16:5034
[email protected]1c7e5c92012-03-22 00:58:2835template <> const char* interface_name<PPB_Messaging_1_0>() {
36 return PPB_MESSAGING_INTERFACE_1_0;
[email protected]9888f132011-03-23 21:07:1537}
38
[email protected]1758e882010-11-01 16:16:5039} // namespace
40
[email protected]1758e882010-11-01 16:16:5041Instance::Instance(PP_Instance instance) : pp_instance_(instance) {
42}
43
44Instance::~Instance() {
[email protected]1758e882010-11-01 16:16:5045}
46
47bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[],
48 const char* /*argv*/[]) {
49 return true;
50}
51
[email protected]e8f07ac2012-01-03 17:43:3652void Instance::DidChangeView(const View& view) {
53 // Call the deprecated version for source backwards-compat.
54 DidChangeView(view.GetRect(), view.GetClipRect());
55}
56
[email protected]1758e882010-11-01 16:16:5057void Instance::DidChangeView(const pp::Rect& /*position*/,
58 const pp::Rect& /*clip*/) {
59}
60
61void Instance::DidChangeFocus(bool /*has_focus*/) {
62}
63
64
[email protected]5a3f62852010-11-10 21:43:0165bool Instance::HandleDocumentLoad(const URLLoader& /*url_loader*/) {
[email protected]1758e882010-11-01 16:16:5066 return false;
67}
68
[email protected]493d14212011-07-07 15:38:4869bool Instance::HandleInputEvent(const InputEvent& /*event*/) {
70 return false;
71}
72
[email protected]df50bde2011-03-28 15:18:3273void Instance::HandleMessage(const Var& /*message*/) {
[email protected]9888f132011-03-23 21:07:1574 return;
75}
76
[email protected]1758e882010-11-01 16:16:5077bool Instance::BindGraphics(const Graphics2D& graphics) {
[email protected]1c7e5c92012-03-22 00:58:2878 if (!has_interface<PPB_Instance_1_0>())
[email protected]1758e882010-11-01 16:16:5079 return false;
[email protected]1c7e5c92012-03-22 00:58:2880 return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
[email protected]6b7550a2010-12-20 19:03:0781 pp_instance(), graphics.pp_resource()));
[email protected]1758e882010-11-01 16:16:5082}
83
[email protected]fae0e942011-09-07 17:10:4684bool Instance::BindGraphics(const Graphics3D& graphics) {
[email protected]1c7e5c92012-03-22 00:58:2885 if (!has_interface<PPB_Instance_1_0>())
[email protected]bd78a642011-07-19 20:31:4486 return false;
[email protected]1c7e5c92012-03-22 00:58:2887 return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
[email protected]bd78a642011-07-19 20:31:4488 pp_instance(), graphics.pp_resource()));
89}
90
[email protected]1758e882010-11-01 16:16:5091bool Instance::IsFullFrame() {
[email protected]1c7e5c92012-03-22 00:58:2892 if (!has_interface<PPB_Instance_1_0>())
[email protected]1758e882010-11-01 16:16:5093 return false;
[email protected]1c7e5c92012-03-22 00:58:2894 return PP_ToBool(get_interface<PPB_Instance_1_0>()->IsFullFrame(
95 pp_instance()));
[email protected]1758e882010-11-01 16:16:5096}
97
[email protected]493d14212011-07-07 15:38:4898int32_t Instance::RequestInputEvents(uint32_t event_classes) {
[email protected]1c7e5c92012-03-22 00:58:2899 if (!has_interface<PPB_InputEvent_1_0>())
[email protected]493d14212011-07-07 15:38:48100 return PP_ERROR_NOINTERFACE;
[email protected]1c7e5c92012-03-22 00:58:28101 return get_interface<PPB_InputEvent_1_0>()->RequestInputEvents(pp_instance(),
102 event_classes);
[email protected]493d14212011-07-07 15:38:48103}
104
105int32_t Instance::RequestFilteringInputEvents(uint32_t event_classes) {
[email protected]1c7e5c92012-03-22 00:58:28106 if (!has_interface<PPB_InputEvent_1_0>())
[email protected]493d14212011-07-07 15:38:48107 return PP_ERROR_NOINTERFACE;
[email protected]1c7e5c92012-03-22 00:58:28108 return get_interface<PPB_InputEvent_1_0>()->RequestFilteringInputEvents(
[email protected]493d14212011-07-07 15:38:48109 pp_instance(), event_classes);
110}
111
112void Instance::ClearInputEventRequest(uint32_t event_classes) {
[email protected]1c7e5c92012-03-22 00:58:28113 if (!has_interface<PPB_InputEvent_1_0>())
[email protected]493d14212011-07-07 15:38:48114 return;
[email protected]1c7e5c92012-03-22 00:58:28115 get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(),
[email protected]493d14212011-07-07 15:38:48116 event_classes);
117}
118
[email protected]9888f132011-03-23 21:07:15119void Instance::PostMessage(const Var& message) {
[email protected]1c7e5c92012-03-22 00:58:28120 if (!has_interface<PPB_Messaging_1_0>())
[email protected]9888f132011-03-23 21:07:15121 return;
[email protected]1c7e5c92012-03-22 00:58:28122 get_interface<PPB_Messaging_1_0>()->PostMessage(pp_instance(),
[email protected]5f2cb1c92011-04-08 21:36:23123 message.pp_var());
[email protected]9888f132011-03-23 21:07:15124}
125
[email protected]1758e882010-11-01 16:16:50126void Instance::AddPerInstanceObject(const std::string& interface_name,
127 void* object) {
128 // Ensure we're not trying to register more than one object per interface
129 // type. Otherwise, we'll get confused in GetPerInstanceObject.
130 PP_DCHECK(interface_name_to_objects_.find(interface_name) ==
131 interface_name_to_objects_.end());
132 interface_name_to_objects_[interface_name] = object;
133}
134
135void Instance::RemovePerInstanceObject(const std::string& interface_name,
136 void* object) {
137 InterfaceNameToObjectMap::iterator found = interface_name_to_objects_.find(
138 interface_name);
139 if (found == interface_name_to_objects_.end()) {
140 // Attempting to unregister an object that doesn't exist or was already
141 // unregistered.
142 PP_DCHECK(false);
143 return;
144 }
145
146 // Validate that we're removing the object we thing we are.
147 PP_DCHECK(found->second == object);
148 (void)object; // Prevent warning in release mode.
149
150 interface_name_to_objects_.erase(found);
151}
152
153// static
[email protected]09af0f72012-02-27 20:23:19154void Instance::RemovePerInstanceObject(const InstanceHandle& instance,
155 const std::string& interface_name,
156 void* object) {
157 // TODO(brettw) assert we're on the main thread.
158 Instance* that = Module::Get()->InstanceForPPInstance(instance.pp_instance());
159 if (!that)
160 return;
161 that->RemovePerInstanceObject(interface_name, object);
162}
163
164// static
[email protected]1758e882010-11-01 16:16:50165void* Instance::GetPerInstanceObject(PP_Instance instance,
166 const std::string& interface_name) {
167 Instance* that = Module::Get()->InstanceForPPInstance(instance);
168 if (!that)
169 return NULL;
170 InterfaceNameToObjectMap::iterator found =
171 that->interface_name_to_objects_.find(interface_name);
172 if (found == that->interface_name_to_objects_.end())
173 return NULL;
174 return found->second;
175}
176
177} // namespace pp