- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / instance.cc
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.
4
5 #include "ppapi/cpp/instance.h"
6
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"
23
24 namespace pp {
25
26 namespace {
27
28 template <> const char* interface_name<PPB_Console_1_0>() {
29   return PPB_CONSOLE_INTERFACE_1_0;
30 }
31
32 template <> const char* interface_name<PPB_InputEvent_1_0>() {
33   return PPB_INPUT_EVENT_INTERFACE_1_0;
34 }
35
36 template <> const char* interface_name<PPB_Instance_1_0>() {
37   return PPB_INSTANCE_INTERFACE_1_0;
38 }
39
40 template <> const char* interface_name<PPB_Messaging_1_0>() {
41   return PPB_MESSAGING_INTERFACE_1_0;
42 }
43
44 }  // namespace
45
46 Instance::Instance(PP_Instance instance) : pp_instance_(instance) {
47 }
48
49 Instance::~Instance() {
50 }
51
52 bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[],
53                     const char* /*argv*/[]) {
54   return true;
55 }
56
57 void Instance::DidChangeView(const View& view) {
58   // Call the deprecated version for source backwards-compat.
59   DidChangeView(view.GetRect(), view.GetClipRect());
60 }
61
62 void Instance::DidChangeView(const pp::Rect& /*position*/,
63                              const pp::Rect& /*clip*/) {
64 }
65
66 void Instance::DidChangeFocus(bool /*has_focus*/) {
67 }
68
69
70 bool Instance::HandleDocumentLoad(const URLLoader& /*url_loader*/) {
71   return false;
72 }
73
74 bool Instance::HandleInputEvent(const InputEvent& /*event*/) {
75   return false;
76 }
77
78 void Instance::HandleMessage(const Var& /*message*/) {
79   return;
80 }
81
82 bool Instance::BindGraphics(const Graphics2D& graphics) {
83   if (!has_interface<PPB_Instance_1_0>())
84     return false;
85   return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
86       pp_instance(), graphics.pp_resource()));
87 }
88
89 bool Instance::BindGraphics(const Graphics3D& graphics) {
90   if (!has_interface<PPB_Instance_1_0>())
91     return false;
92   return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics(
93       pp_instance(), graphics.pp_resource()));
94 }
95
96 bool Instance::IsFullFrame() {
97   if (!has_interface<PPB_Instance_1_0>())
98     return false;
99   return PP_ToBool(get_interface<PPB_Instance_1_0>()->IsFullFrame(
100       pp_instance()));
101 }
102
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(),
107                                                                  event_classes);
108 }
109
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);
115 }
116
117 void Instance::ClearInputEventRequest(uint32_t event_classes) {
118   if (!has_interface<PPB_InputEvent_1_0>())
119     return;
120   get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(),
121                                                           event_classes);
122 }
123
124 void Instance::PostMessage(const Var& message) {
125   if (!has_interface<PPB_Messaging_1_0>())
126     return;
127   get_interface<PPB_Messaging_1_0>()->PostMessage(pp_instance(),
128                                               message.pp_var());
129 }
130
131 void Instance::LogToConsole(PP_LogLevel level, const Var& value) {
132   if (!has_interface<PPB_Console_1_0>())
133     return;
134   get_interface<PPB_Console_1_0>()->Log(
135       pp_instance(), level, value.pp_var());
136 }
137
138 void Instance::LogToConsoleWithSource(PP_LogLevel level,
139                                       const Var& source,
140                                       const Var& value) {
141   if (!has_interface<PPB_Console_1_0>())
142     return;
143   get_interface<PPB_Console_1_0>()->LogWithSource(
144       pp_instance(), level, source.pp_var(), value.pp_var());
145 }
146
147 void Instance::AddPerInstanceObject(const std::string& interface_name,
148                                     void* object) {
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;
154 }
155
156 void Instance::RemovePerInstanceObject(const std::string& interface_name,
157                                        void* object) {
158   InterfaceNameToObjectMap::iterator found = interface_name_to_objects_.find(
159       interface_name);
160   if (found == interface_name_to_objects_.end()) {
161     // Attempting to unregister an object that doesn't exist or was already
162     // unregistered.
163     PP_DCHECK(false);
164     return;
165   }
166
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.
170
171   interface_name_to_objects_.erase(found);
172 }
173
174 // static
175 void Instance::RemovePerInstanceObject(const InstanceHandle& instance,
176                                        const std::string& interface_name,
177                                        void* object) {
178   // TODO(brettw) assert we're on the main thread.
179   Instance* that = Module::Get()->InstanceForPPInstance(instance.pp_instance());
180   if (!that)
181     return;
182   that->RemovePerInstanceObject(interface_name, object);
183 }
184
185 // static
186 void* Instance::GetPerInstanceObject(PP_Instance instance,
187                                      const std::string& interface_name) {
188   Instance* that = Module::Get()->InstanceForPPInstance(instance);
189   if (!that)
190     return NULL;
191   InterfaceNameToObjectMap::iterator found =
192       that->interface_name_to_objects_.find(interface_name);
193   if (found == that->interface_name_to_objects_.end())
194     return NULL;
195   return found->second;
196 }
197
198 }  // namespace pp