1 // Copyright (c) 2012 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.
5 #include "ppapi/cpp/instance.h"
7 #include "ppapi/c/pp_errors.h"
8 #include "ppapi/c/ppb_console.h"
9 #include "ppapi/c/ppb_input_event.h"
10 #include "ppapi/c/ppb_instance.h"
11 #include "ppapi/c/ppb_messaging.h"
12 #include "ppapi/c/ppp_message_handler.h"
13 #include "ppapi/cpp/compositor.h"
14 #include "ppapi/cpp/graphics_2d.h"
15 #include "ppapi/cpp/graphics_3d.h"
16 #include "ppapi/cpp/image_data.h"
17 #include "ppapi/cpp/instance_handle.h"
18 #include "ppapi/cpp/logging.h"
19 #include "ppapi/cpp/message_handler.h"
20 #include "ppapi/cpp/message_loop.h"
21 #include "ppapi/cpp/module.h"
22 #include "ppapi/cpp/module_impl.h"
23 #include "ppapi/cpp/point.h"
24 #include "ppapi/cpp/resource.h"
25 #include "ppapi/cpp/var.h"
26 #include "ppapi/cpp/view.h"
32 template <> const char* interface_name<PPB_Console_1_0>() {
33 return PPB_CONSOLE_INTERFACE_1_0;
36 template <> const char* interface_name<PPB_InputEvent_1_0>() {
37 return PPB_INPUT_EVENT_INTERFACE_1_0;
40 template <> const char* interface_name<PPB_Instance_1_0>() {
41 return PPB_INSTANCE_INTERFACE_1_0;
44 template <> const char* interface_name<PPB_Messaging_1_0>() {
45 return PPB_MESSAGING_INTERFACE_1_0;
48 template <> const char* interface_name<PPB_Messaging_1_2>() {
49 return PPB_MESSAGING_INTERFACE_1_2;
52 // PPP_MessageHandler implementation -------------------------------------------
53 void HandleMessage(PP_Instance pp_instance,
56 MessageHandler* message_handler = static_cast<MessageHandler*>(user_data);
57 message_handler->HandleMessage(InstanceHandle(pp_instance), Var(*var));
60 void HandleBlockingMessage(PP_Instance pp_instance,
64 MessageHandler* message_handler = static_cast<MessageHandler*>(user_data);
66 message_handler->HandleBlockingMessage(InstanceHandle(pp_instance),
68 *result = result_var.Detach();
71 void Destroy(PP_Instance pp_instance, void* user_data) {
72 MessageHandler* message_handler = static_cast<MessageHandler*>(user_data);
73 message_handler->WasUnregistered(InstanceHandle(pp_instance));
76 static PPP_MessageHandler_0_2 message_handler_if = {
77 &HandleMessage, &HandleBlockingMessage, &Destroy
82 Instance::Instance(PP_Instance instance) : pp_instance_(instance) {
85 Instance::~Instance() {
88 bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[],
89 const char* /*argv*/[]) {
93 void Instance::DidChangeView(const View& view) {
94 // Call the deprecated version for source backwards-compat.
95 DidChangeView(view.GetRect(), view.GetClipRect());
98 void Instance::DidChangeView(const pp::Rect& /*position*/,
99 const pp::Rect& /*clip*/) {
102 void Instance::DidChangeFocus(bool /*has_focus*/) {
106 bool Instance::HandleDocumentLoad(const URLLoader& /*url_loader*/) {
110 bool Instance::HandleInputEvent(const InputEvent& /*event*/) {
114 void Instance::HandleMessage(const Var& /*message*/) {
118 bool Instance::BindGraphics(const Graphics2D& graphics) {
119 if (!has_interface<PPB_Instance_1_0>())
121 return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
122 pp_instance(), graphics.pp_resource()));
125 bool Instance::BindGraphics(const Graphics3D& graphics) {
126 if (!has_interface<PPB_Instance_1_0>())
128 return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
129 pp_instance(), graphics.pp_resource()));
132 bool Instance::BindGraphics(const Compositor& compositor) {
133 if (!has_interface<PPB_Instance_1_0>())
135 return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
136 pp_instance(), compositor.pp_resource()));
139 bool Instance::IsFullFrame() {
140 if (!has_interface<PPB_Instance_1_0>())
142 return PP_ToBool(get_interface<PPB_Instance_1_0>()->IsFullFrame(
146 int32_t Instance::RequestInputEvents(uint32_t event_classes) {
147 if (!has_interface<PPB_InputEvent_1_0>())
148 return PP_ERROR_NOINTERFACE;
149 return get_interface<PPB_InputEvent_1_0>()->RequestInputEvents(pp_instance(),
153 int32_t Instance::RequestFilteringInputEvents(uint32_t event_classes) {
154 if (!has_interface<PPB_InputEvent_1_0>())
155 return PP_ERROR_NOINTERFACE;
156 return get_interface<PPB_InputEvent_1_0>()->RequestFilteringInputEvents(
157 pp_instance(), event_classes);
160 void Instance::ClearInputEventRequest(uint32_t event_classes) {
161 if (!has_interface<PPB_InputEvent_1_0>())
163 get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(),
167 void Instance::PostMessage(const Var& message) {
168 if (has_interface<PPB_Messaging_1_2>()) {
169 get_interface<PPB_Messaging_1_2>()->PostMessage(pp_instance(),
171 } else if (has_interface<PPB_Messaging_1_0>()) {
172 get_interface<PPB_Messaging_1_0>()->PostMessage(pp_instance(),
177 int32_t Instance::RegisterMessageHandler(MessageHandler* message_handler,
178 const MessageLoop& message_loop) {
179 if (!has_interface<PPB_Messaging_1_2>())
180 return PP_ERROR_NOTSUPPORTED;
181 return get_interface<PPB_Messaging_1_2>()->RegisterMessageHandler(
185 message_loop.pp_resource());
188 void Instance::UnregisterMessageHandler() {
189 if (!has_interface<PPB_Messaging_1_2>())
191 get_interface<PPB_Messaging_1_2>()->UnregisterMessageHandler(pp_instance());
194 void Instance::LogToConsole(PP_LogLevel level, const Var& value) {
195 if (!has_interface<PPB_Console_1_0>())
197 get_interface<PPB_Console_1_0>()->Log(
198 pp_instance(), level, value.pp_var());
201 void Instance::LogToConsoleWithSource(PP_LogLevel level,
204 if (!has_interface<PPB_Console_1_0>())
206 get_interface<PPB_Console_1_0>()->LogWithSource(
207 pp_instance(), level, source.pp_var(), value.pp_var());
210 void Instance::AddPerInstanceObject(const std::string& interface_name,
212 // Ensure we're not trying to register more than one object per interface
213 // type. Otherwise, we'll get confused in GetPerInstanceObject.
214 PP_DCHECK(interface_name_to_objects_.find(interface_name) ==
215 interface_name_to_objects_.end());
216 interface_name_to_objects_[interface_name] = object;
219 void Instance::RemovePerInstanceObject(const std::string& interface_name,
221 InterfaceNameToObjectMap::iterator found = interface_name_to_objects_.find(
223 if (found == interface_name_to_objects_.end()) {
224 // Attempting to unregister an object that doesn't exist or was already
230 // Validate that we're removing the object we thing we are.
231 PP_DCHECK(found->second == object);
232 (void)object; // Prevent warning in release mode.
234 interface_name_to_objects_.erase(found);
238 void Instance::RemovePerInstanceObject(const InstanceHandle& instance,
239 const std::string& interface_name,
241 // TODO(brettw) assert we're on the main thread.
242 Instance* that = Module::Get()->InstanceForPPInstance(instance.pp_instance());
245 that->RemovePerInstanceObject(interface_name, object);
249 void* Instance::GetPerInstanceObject(PP_Instance instance,
250 const std::string& interface_name) {
251 Instance* that = Module::Get()->InstanceForPPInstance(instance);
254 InterfaceNameToObjectMap::iterator found =
255 that->interface_name_to_objects_.find(interface_name);
256 if (found == that->interface_name_to_objects_.end())
258 return found->second;