Fix DisownOpener and related tests.
[chromium-blink-merge.git] / ppapi / cpp / instance.cc
bloba5be2b9761358af70c48fb7628a7cfb08375af1b
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"
25 namespace pp {
27 namespace {
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;
45 } // namespace
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*/[]) {
55 return true;
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*/) {
72 return false;
75 bool Instance::HandleInputEvent(const InputEvent& /*event*/) {
76 return false;
79 void Instance::HandleMessage(const Var& /*message*/) {
80 return;
83 bool Instance::BindGraphics(const Graphics2D& graphics) {
84 if (!has_interface<PPB_Instance_1_0>())
85 return false;
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>())
92 return false;
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>())
99 return false;
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>())
106 return false;
107 return PP_ToBool(get_interface<PPB_Instance_1_0>()->IsFullFrame(
108 pp_instance()));
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(),
115 event_classes);
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>())
127 return;
128 get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(),
129 event_classes);
132 void Instance::PostMessage(const Var& message) {
133 if (!has_interface<PPB_Messaging_1_0>())
134 return;
135 get_interface<PPB_Messaging_1_0>()->PostMessage(pp_instance(),
136 message.pp_var());
139 void Instance::LogToConsole(PP_LogLevel level, const Var& value) {
140 if (!has_interface<PPB_Console_1_0>())
141 return;
142 get_interface<PPB_Console_1_0>()->Log(
143 pp_instance(), level, value.pp_var());
146 void Instance::LogToConsoleWithSource(PP_LogLevel level,
147 const Var& source,
148 const Var& value) {
149 if (!has_interface<PPB_Console_1_0>())
150 return;
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,
156 void* object) {
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,
165 void* object) {
166 InterfaceNameToObjectMap::iterator found = interface_name_to_objects_.find(
167 interface_name);
168 if (found == interface_name_to_objects_.end()) {
169 // Attempting to unregister an object that doesn't exist or was already
170 // unregistered.
171 PP_DCHECK(false);
172 return;
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);
182 // static
183 void Instance::RemovePerInstanceObject(const InstanceHandle& instance,
184 const std::string& interface_name,
185 void* object) {
186 // TODO(brettw) assert we're on the main thread.
187 Instance* that = Module::Get()->InstanceForPPInstance(instance.pp_instance());
188 if (!that)
189 return;
190 that->RemovePerInstanceObject(interface_name, object);
193 // static
194 void* Instance::GetPerInstanceObject(PP_Instance instance,
195 const std::string& interface_name) {
196 Instance* that = Module::Get()->InstanceForPPInstance(instance);
197 if (!that)
198 return NULL;
199 InterfaceNameToObjectMap::iterator found =
200 that->interface_name_to_objects_.find(interface_name);
201 if (found == that->interface_name_to_objects_.end())
202 return NULL;
203 return found->second;
206 } // namespace pp