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/graphics_2d.h"
13 #include "ppapi/cpp/graphics_3d.h"
14 #include "ppapi/cpp/image_data.h"
15 #include "ppapi/cpp/instance_handle.h"
16 #include "ppapi/cpp/logging.h"
17 #include "ppapi/cpp/module.h"
18 #include "ppapi/cpp/module_impl.h"
19 #include "ppapi/cpp/point.h"
20 #include "ppapi/cpp/resource.h"
21 #include "ppapi/cpp/var.h"
22 #include "ppapi/cpp/view.h"
28 template <> const char* interface_name
<PPB_Console_1_0
>() {
29 return PPB_CONSOLE_INTERFACE_1_0
;
32 template <> const char* interface_name
<PPB_InputEvent_1_0
>() {
33 return PPB_INPUT_EVENT_INTERFACE_1_0
;
36 template <> const char* interface_name
<PPB_Instance_1_0
>() {
37 return PPB_INSTANCE_INTERFACE_1_0
;
40 template <> const char* interface_name
<PPB_Messaging_1_0
>() {
41 return PPB_MESSAGING_INTERFACE_1_0
;
46 Instance::Instance(PP_Instance instance
) : pp_instance_(instance
) {
49 Instance::~Instance() {
52 bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[],
53 const char* /*argv*/[]) {
57 void Instance::DidChangeView(const View
& view
) {
58 // Call the deprecated version for source backwards-compat.
59 DidChangeView(view
.GetRect(), view
.GetClipRect());
62 void Instance::DidChangeView(const pp::Rect
& /*position*/,
63 const pp::Rect
& /*clip*/) {
66 void Instance::DidChangeFocus(bool /*has_focus*/) {
70 bool Instance::HandleDocumentLoad(const URLLoader
& /*url_loader*/) {
74 bool Instance::HandleInputEvent(const InputEvent
& /*event*/) {
78 void Instance::HandleMessage(const Var
& /*message*/) {
82 bool Instance::BindGraphics(const Graphics2D
& graphics
) {
83 if (!has_interface
<PPB_Instance_1_0
>())
85 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->BindGraphics(
86 pp_instance(), graphics
.pp_resource()));
89 bool Instance::BindGraphics(const Graphics3D
& graphics
) {
90 if (!has_interface
<PPB_Instance_1_0
>())
92 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->BindGraphics(
93 pp_instance(), graphics
.pp_resource()));
96 bool Instance::IsFullFrame() {
97 if (!has_interface
<PPB_Instance_1_0
>())
99 return PP_ToBool(get_interface
<PPB_Instance_1_0
>()->IsFullFrame(
103 int32_t Instance::RequestInputEvents(uint32_t event_classes
) {
104 if (!has_interface
<PPB_InputEvent_1_0
>())
105 return PP_ERROR_NOINTERFACE
;
106 return get_interface
<PPB_InputEvent_1_0
>()->RequestInputEvents(pp_instance(),
110 int32_t Instance::RequestFilteringInputEvents(uint32_t event_classes
) {
111 if (!has_interface
<PPB_InputEvent_1_0
>())
112 return PP_ERROR_NOINTERFACE
;
113 return get_interface
<PPB_InputEvent_1_0
>()->RequestFilteringInputEvents(
114 pp_instance(), event_classes
);
117 void Instance::ClearInputEventRequest(uint32_t event_classes
) {
118 if (!has_interface
<PPB_InputEvent_1_0
>())
120 get_interface
<PPB_InputEvent_1_0
>()->ClearInputEventRequest(pp_instance(),
124 void Instance::PostMessage(const Var
& message
) {
125 if (!has_interface
<PPB_Messaging_1_0
>())
127 get_interface
<PPB_Messaging_1_0
>()->PostMessage(pp_instance(),
131 void Instance::LogToConsole(PP_LogLevel level
, const Var
& value
) {
132 if (!has_interface
<PPB_Console_1_0
>())
134 get_interface
<PPB_Console_1_0
>()->Log(
135 pp_instance(), level
, value
.pp_var());
138 void Instance::LogToConsoleWithSource(PP_LogLevel level
,
141 if (!has_interface
<PPB_Console_1_0
>())
143 get_interface
<PPB_Console_1_0
>()->LogWithSource(
144 pp_instance(), level
, source
.pp_var(), value
.pp_var());
147 void Instance::AddPerInstanceObject(const std::string
& interface_name
,
149 // Ensure we're not trying to register more than one object per interface
150 // type. Otherwise, we'll get confused in GetPerInstanceObject.
151 PP_DCHECK(interface_name_to_objects_
.find(interface_name
) ==
152 interface_name_to_objects_
.end());
153 interface_name_to_objects_
[interface_name
] = object
;
156 void Instance::RemovePerInstanceObject(const std::string
& interface_name
,
158 InterfaceNameToObjectMap::iterator found
= interface_name_to_objects_
.find(
160 if (found
== interface_name_to_objects_
.end()) {
161 // Attempting to unregister an object that doesn't exist or was already
167 // Validate that we're removing the object we thing we are.
168 PP_DCHECK(found
->second
== object
);
169 (void)object
; // Prevent warning in release mode.
171 interface_name_to_objects_
.erase(found
);
175 void Instance::RemovePerInstanceObject(const InstanceHandle
& instance
,
176 const std::string
& interface_name
,
178 // TODO(brettw) assert we're on the main thread.
179 Instance
* that
= Module::Get()->InstanceForPPInstance(instance
.pp_instance());
182 that
->RemovePerInstanceObject(interface_name
, object
);
186 void* Instance::GetPerInstanceObject(PP_Instance instance
,
187 const std::string
& interface_name
) {
188 Instance
* that
= Module::Get()->InstanceForPPInstance(instance
);
191 InterfaceNameToObjectMap::iterator found
=
192 that
->interface_name_to_objects_
.find(interface_name
);
193 if (found
== that
->interface_name_to_objects_
.end())
195 return found
->second
;