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/cpp/compositor.h"
13 #include "ppapi/cpp/graphics_2d.h"
14 #include "ppapi/cpp/graphics_3d.h"
15 #include "ppapi/cpp/image_data.h"
16 #include "ppapi/cpp/instance_handle.h"
17 #include "ppapi/cpp/logging.h"
18 #include "ppapi/cpp/module.h"
19 #include "ppapi/cpp/module_impl.h"
20 #include "ppapi/cpp/point.h"
21 #include "ppapi/cpp/resource.h"
22 #include "ppapi/cpp/var.h"
23 #include "ppapi/cpp/view.h"
29 template <> const char* interface_name
<PPB_Console_1_0
>() {
30 return PPB_CONSOLE_INTERFACE_1_0
;
33 template <> const char* interface_name
<PPB_InputEvent_1_0
>() {
34 return PPB_INPUT_EVENT_INTERFACE_1_0
;
37 template <> const char* interface_name
<PPB_Instance_1_0
>() {
38 return PPB_INSTANCE_INTERFACE_1_0
;
41 template <> const char* interface_name
<PPB_Messaging_1_0
>() {
42 return PPB_MESSAGING_INTERFACE_1_0
;
47 Instance::Instance(PP_Instance instance
) : pp_instance_(instance
) {
50 Instance::~Instance() {
53 bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[],
54 const char* /*argv*/[]) {
58 void Instance::DidChangeView(const View
& view
) {
59 // Call the deprecated version for source backwards-compat.
60 DidChangeView(view
.GetRect(), view
.GetClipRect());
63 void Instance::DidChangeView(const pp::Rect
& /*position*/,
64 const pp::Rect
& /*clip*/) {
67 void Instance::DidChangeFocus(bool /*has_focus*/) {
71 bool Instance::HandleDocumentLoad(const URLLoader
& /*url_loader*/) {
75 bool Instance::HandleInputEvent(const InputEvent
& /*event*/) {
79 void Instance::HandleMessage(const Var
& /*message*/) {
83 bool Instance::BindGraphics(const Graphics2D
& graphics
) {
84 if (!has_interface
<PPB_Instance_1_0
>())
86 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->BindGraphics(
87 pp_instance(), graphics
.pp_resource()));
90 bool Instance::BindGraphics(const Graphics3D
& graphics
) {
91 if (!has_interface
<PPB_Instance_1_0
>())
93 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->BindGraphics(
94 pp_instance(), graphics
.pp_resource()));
97 bool Instance::BindGraphics(const Compositor
& compositor
) {
98 if (!has_interface
<PPB_Instance_1_0
>())
100 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->BindGraphics(
101 pp_instance(), compositor
.pp_resource()));
104 bool Instance::IsFullFrame() {
105 if (!has_interface
<PPB_Instance_1_0
>())
107 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->IsFullFrame(
111 int32_t Instance::RequestInputEvents(uint32_t event_classes
) {
112 if (!has_interface
<PPB_InputEvent_1_0
>())
113 return PP_ERROR_NOINTERFACE
;
114 return get_interface
<PPB_InputEvent_1_0
>()->RequestInputEvents(pp_instance(),
118 int32_t Instance::RequestFilteringInputEvents(uint32_t event_classes
) {
119 if (!has_interface
<PPB_InputEvent_1_0
>())
120 return PP_ERROR_NOINTERFACE
;
121 return get_interface
<PPB_InputEvent_1_0
>()->RequestFilteringInputEvents(
122 pp_instance(), event_classes
);
125 void Instance::ClearInputEventRequest(uint32_t event_classes
) {
126 if (!has_interface
<PPB_InputEvent_1_0
>())
128 get_interface
<PPB_InputEvent_1_0
>()->ClearInputEventRequest(pp_instance(),
132 void Instance::PostMessage(const Var
& message
) {
133 if (!has_interface
<PPB_Messaging_1_0
>())
135 get_interface
<PPB_Messaging_1_0
>()->PostMessage(pp_instance(),
139 void Instance::LogToConsole(PP_LogLevel level
, const Var
& value
) {
140 if (!has_interface
<PPB_Console_1_0
>())
142 get_interface
<PPB_Console_1_0
>()->Log(
143 pp_instance(), level
, value
.pp_var());
146 void Instance::LogToConsoleWithSource(PP_LogLevel level
,
149 if (!has_interface
<PPB_Console_1_0
>())
151 get_interface
<PPB_Console_1_0
>()->LogWithSource(
152 pp_instance(), level
, source
.pp_var(), value
.pp_var());
155 void Instance::AddPerInstanceObject(const std::string
& interface_name
,
157 // Ensure we're not trying to register more than one object per interface
158 // type. Otherwise, we'll get confused in GetPerInstanceObject.
159 PP_DCHECK(interface_name_to_objects_
.find(interface_name
) ==
160 interface_name_to_objects_
.end());
161 interface_name_to_objects_
[interface_name
] = object
;
164 void Instance::RemovePerInstanceObject(const std::string
& interface_name
,
166 InterfaceNameToObjectMap::iterator found
= interface_name_to_objects_
.find(
168 if (found
== interface_name_to_objects_
.end()) {
169 // Attempting to unregister an object that doesn't exist or was already
175 // Validate that we're removing the object we thing we are.
176 PP_DCHECK(found
->second
== object
);
177 (void)object
; // Prevent warning in release mode.
179 interface_name_to_objects_
.erase(found
);
183 void Instance::RemovePerInstanceObject(const InstanceHandle
& instance
,
184 const std::string
& interface_name
,
186 // TODO(brettw) assert we're on the main thread.
187 Instance
* that
= Module::Get()->InstanceForPPInstance(instance
.pp_instance());
190 that
->RemovePerInstanceObject(interface_name
, object
);
194 void* Instance::GetPerInstanceObject(PP_Instance instance
,
195 const std::string
& interface_name
) {
196 Instance
* that
= Module::Get()->InstanceForPPInstance(instance
);
199 InterfaceNameToObjectMap::iterator found
=
200 that
->interface_name_to_objects_
.find(interface_name
);
201 if (found
== that
->interface_name_to_objects_
.end())
203 return found
->second
;