[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 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 | |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 7 | #include "ppapi/c/pp_errors.h" |
| 8 | #include "ppapi/c/ppb_input_event.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 9 | #include "ppapi/c/ppb_instance.h" |
[email protected] | 5f2cb1c9 | 2011-04-08 21:36:23 | [diff] [blame] | 10 | #include "ppapi/c/ppb_messaging.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 11 | #include "ppapi/cpp/graphics_2d.h" |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 12 | #include "ppapi/cpp/graphics_3d.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 13 | #include "ppapi/cpp/image_data.h" |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 14 | #include "ppapi/cpp/instance_handle.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 15 | #include "ppapi/cpp/logging.h" |
| 16 | #include "ppapi/cpp/module.h" |
| 17 | #include "ppapi/cpp/module_impl.h" |
| 18 | #include "ppapi/cpp/point.h" |
| 19 | #include "ppapi/cpp/resource.h" |
| 20 | #include "ppapi/cpp/var.h" |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 21 | #include "ppapi/cpp/view.h" |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 22 | |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 23 | namespace pp { |
| 24 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 25 | namespace { |
| 26 | |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 27 | template <> const char* interface_name<PPB_InputEvent_1_0>() { |
| 28 | return PPB_INPUT_EVENT_INTERFACE_1_0; |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 29 | } |
| 30 | |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 31 | template <> const char* interface_name<PPB_Instance_1_0>() { |
| 32 | return PPB_INSTANCE_INTERFACE_1_0; |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 33 | } |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 34 | |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 35 | template <> const char* interface_name<PPB_Messaging_1_0>() { |
| 36 | return PPB_MESSAGING_INTERFACE_1_0; |
[email protected] | 9888f13 | 2011-03-23 21:07:15 | [diff] [blame] | 37 | } |
| 38 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 39 | } // namespace |
| 40 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 41 | Instance::Instance(PP_Instance instance) : pp_instance_(instance) { |
| 42 | } |
| 43 | |
| 44 | Instance::~Instance() { |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | bool Instance::Init(uint32_t /*argc*/, const char* /*argn*/[], |
| 48 | const char* /*argv*/[]) { |
| 49 | return true; |
| 50 | } |
| 51 | |
[email protected] | e8f07ac | 2012-01-03 17:43:36 | [diff] [blame] | 52 | void Instance::DidChangeView(const View& view) { |
| 53 | // Call the deprecated version for source backwards-compat. |
| 54 | DidChangeView(view.GetRect(), view.GetClipRect()); |
| 55 | } |
| 56 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 57 | void Instance::DidChangeView(const pp::Rect& /*position*/, |
| 58 | const pp::Rect& /*clip*/) { |
| 59 | } |
| 60 | |
| 61 | void Instance::DidChangeFocus(bool /*has_focus*/) { |
| 62 | } |
| 63 | |
| 64 | |
[email protected] | 5a3f6285 | 2010-11-10 21:43:01 | [diff] [blame] | 65 | bool Instance::HandleDocumentLoad(const URLLoader& /*url_loader*/) { |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 69 | bool Instance::HandleInputEvent(const InputEvent& /*event*/) { |
| 70 | return false; |
| 71 | } |
| 72 | |
[email protected] | df50bde | 2011-03-28 15:18:32 | [diff] [blame] | 73 | void Instance::HandleMessage(const Var& /*message*/) { |
[email protected] | 9888f13 | 2011-03-23 21:07:15 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 77 | bool Instance::BindGraphics(const Graphics2D& graphics) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 78 | if (!has_interface<PPB_Instance_1_0>()) |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 79 | return false; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 80 | return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics( |
[email protected] | 6b7550a | 2010-12-20 19:03:07 | [diff] [blame] | 81 | pp_instance(), graphics.pp_resource())); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | fae0e94 | 2011-09-07 17:10:46 | [diff] [blame] | 84 | bool Instance::BindGraphics(const Graphics3D& graphics) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 85 | if (!has_interface<PPB_Instance_1_0>()) |
[email protected] | bd78a64 | 2011-07-19 20:31:44 | [diff] [blame] | 86 | return false; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 87 | return PP_ToBool(get_interface<PPB_Instance_1_0>()->BindGraphics( |
[email protected] | bd78a64 | 2011-07-19 20:31:44 | [diff] [blame] | 88 | pp_instance(), graphics.pp_resource())); |
| 89 | } |
| 90 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 91 | bool Instance::IsFullFrame() { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 92 | if (!has_interface<PPB_Instance_1_0>()) |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 93 | return false; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 94 | return PP_ToBool(get_interface<PPB_Instance_1_0>()->IsFullFrame( |
| 95 | pp_instance())); |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 96 | } |
| 97 | |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 98 | int32_t Instance::RequestInputEvents(uint32_t event_classes) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 99 | if (!has_interface<PPB_InputEvent_1_0>()) |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 100 | return PP_ERROR_NOINTERFACE; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 101 | return get_interface<PPB_InputEvent_1_0>()->RequestInputEvents(pp_instance(), |
| 102 | event_classes); |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | int32_t Instance::RequestFilteringInputEvents(uint32_t event_classes) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 106 | if (!has_interface<PPB_InputEvent_1_0>()) |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 107 | return PP_ERROR_NOINTERFACE; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 108 | return get_interface<PPB_InputEvent_1_0>()->RequestFilteringInputEvents( |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 109 | pp_instance(), event_classes); |
| 110 | } |
| 111 | |
| 112 | void Instance::ClearInputEventRequest(uint32_t event_classes) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 113 | if (!has_interface<PPB_InputEvent_1_0>()) |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 114 | return; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 115 | get_interface<PPB_InputEvent_1_0>()->ClearInputEventRequest(pp_instance(), |
[email protected] | 493d1421 | 2011-07-07 15:38:48 | [diff] [blame] | 116 | event_classes); |
| 117 | } |
| 118 | |
[email protected] | 9888f13 | 2011-03-23 21:07:15 | [diff] [blame] | 119 | void Instance::PostMessage(const Var& message) { |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 120 | if (!has_interface<PPB_Messaging_1_0>()) |
[email protected] | 9888f13 | 2011-03-23 21:07:15 | [diff] [blame] | 121 | return; |
[email protected] | 1c7e5c9 | 2012-03-22 00:58:28 | [diff] [blame] | 122 | get_interface<PPB_Messaging_1_0>()->PostMessage(pp_instance(), |
[email protected] | 5f2cb1c9 | 2011-04-08 21:36:23 | [diff] [blame] | 123 | message.pp_var()); |
[email protected] | 9888f13 | 2011-03-23 21:07:15 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 126 | void Instance::AddPerInstanceObject(const std::string& interface_name, |
| 127 | void* object) { |
| 128 | // Ensure we're not trying to register more than one object per interface |
| 129 | // type. Otherwise, we'll get confused in GetPerInstanceObject. |
| 130 | PP_DCHECK(interface_name_to_objects_.find(interface_name) == |
| 131 | interface_name_to_objects_.end()); |
| 132 | interface_name_to_objects_[interface_name] = object; |
| 133 | } |
| 134 | |
| 135 | void Instance::RemovePerInstanceObject(const std::string& interface_name, |
| 136 | void* object) { |
| 137 | InterfaceNameToObjectMap::iterator found = interface_name_to_objects_.find( |
| 138 | interface_name); |
| 139 | if (found == interface_name_to_objects_.end()) { |
| 140 | // Attempting to unregister an object that doesn't exist or was already |
| 141 | // unregistered. |
| 142 | PP_DCHECK(false); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Validate that we're removing the object we thing we are. |
| 147 | PP_DCHECK(found->second == object); |
| 148 | (void)object; // Prevent warning in release mode. |
| 149 | |
| 150 | interface_name_to_objects_.erase(found); |
| 151 | } |
| 152 | |
| 153 | // static |
[email protected] | 09af0f7 | 2012-02-27 20:23:19 | [diff] [blame] | 154 | void Instance::RemovePerInstanceObject(const InstanceHandle& instance, |
| 155 | const std::string& interface_name, |
| 156 | void* object) { |
| 157 | // TODO(brettw) assert we're on the main thread. |
| 158 | Instance* that = Module::Get()->InstanceForPPInstance(instance.pp_instance()); |
| 159 | if (!that) |
| 160 | return; |
| 161 | that->RemovePerInstanceObject(interface_name, object); |
| 162 | } |
| 163 | |
| 164 | // static |
[email protected] | 1758e88 | 2010-11-01 16:16:50 | [diff] [blame] | 165 | void* Instance::GetPerInstanceObject(PP_Instance instance, |
| 166 | const std::string& interface_name) { |
| 167 | Instance* that = Module::Get()->InstanceForPPInstance(instance); |
| 168 | if (!that) |
| 169 | return NULL; |
| 170 | InterfaceNameToObjectMap::iterator found = |
| 171 | that->interface_name_to_objects_.find(interface_name); |
| 172 | if (found == that->interface_name_to_objects_.end()) |
| 173 | return NULL; |
| 174 | return found->second; |
| 175 | } |
| 176 | |
| 177 | } // namespace pp |