Add to sammc@chromium.org to chrome/browser/extensions/api/file_system/OWNERS.
[chromium-blink-merge.git] / ppapi / cpp / instance.cc
blobd285fd4936671ad61bc3198a273ad4510cdafc2f
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"
24 namespace pp {
26 namespace {
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;
44 } // namespace
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*/[]) {
54 return true;
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*/) {
71 return false;
74 bool Instance::HandleInputEvent(const InputEvent& /*event*/) {
75 return false;
78 void Instance::HandleMessage(const Var& /*message*/) {
79 return;
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()));
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()));
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()));
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);
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>())
119 return;
120 get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(),
121 event_classes);
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());
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());
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());
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;
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;
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);
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);
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;
198 } // namespace pp