Load 57 into trunk.
[nativeclient.git] / tools / npapi_runtime / npobject_handle.cc
blob82cf15ae06e6272b64b9079b1e3da97ee321e543
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "native_client/tools/npapi_runtime/npobject_handle.h"
35 #include "native_client/tools/npapi_runtime/npbridge.h"
37 namespace {
39 NPObject* Alloc(NPP npp, NPClass* aClass) {
40 return nacl::NPHandleObject::GetLastAllocated();
43 void Deallocate(NPObject* object) {
44 delete static_cast<nacl::NPHandleObject*>(object);
47 void Invalidate(NPObject* object) {
48 return static_cast<nacl::NPHandleObject*>(object)->Invalidate();
51 bool HasMethod(NPObject* object, NPIdentifier name) {
52 return static_cast<nacl::NPHandleObject*>(object)->HasMethod(name);
55 bool Invoke(NPObject* object, NPIdentifier name,
56 const NPVariant* args, uint32_t arg_count,
57 NPVariant* result) {
58 return static_cast<nacl::NPHandleObject*>(object)->Invoke(
59 name, args, arg_count, result);
62 bool InvokeDefault(NPObject* object, const NPVariant* args, uint32_t arg_count,
63 NPVariant* result) {
64 return static_cast<nacl::NPHandleObject*>(object)->InvokeDefault(
65 args, arg_count, result);
68 bool HasProperty(NPObject* object, NPIdentifier name) {
69 return static_cast<nacl::NPHandleObject*>(object)->HasProperty(name);
72 bool GetProperty(NPObject* object, NPIdentifier name, NPVariant* result) {
73 return static_cast<nacl::NPHandleObject*>(object)->GetProperty(name, result);
76 bool SetProperty(NPObject* object, NPIdentifier name, const NPVariant* value) {
77 return static_cast<nacl::NPHandleObject*>(object)->SetProperty(name, value);
80 bool RemoveProperty(NPObject* object, NPIdentifier name) {
81 return static_cast<nacl::NPHandleObject*>(object)->RemoveProperty(name);
84 NPIdentifier id_handle = NULL;
86 } // namespace
88 namespace nacl {
90 NPClass NPHandleObject::np_class = {
91 NP_CLASS_STRUCT_VERSION,
92 ::Alloc,
93 ::Deallocate,
94 ::Invalidate,
95 ::HasMethod,
96 ::Invoke,
97 ::InvokeDefault,
98 ::HasProperty,
99 ::GetProperty,
100 ::SetProperty,
101 ::RemoveProperty
104 NPObject* NPHandleObject::last_allocated;
106 NPHandleObject::NPHandleObject(NPBridge* bridge, HtpHandle handle) {
107 if (id_handle == NULL) {
108 id_handle = NPN_GetStringIdentifier("handle");
110 handle_ = handle;
111 last_allocated = this;
112 NPN_CreateObject(bridge->npp(), &np_class);
115 NPHandleObject::~NPHandleObject() {
116 Close(handle_);
119 void NPHandleObject::Invalidate() {
122 bool NPHandleObject::HasMethod(NPIdentifier name) {
123 return false;
126 bool NPHandleObject::Invoke(NPIdentifier name,
127 const NPVariant* args, uint32_t arg_count,
128 NPVariant* variant) {
129 return false;
132 bool NPHandleObject::InvokeDefault(const NPVariant* args, uint32_t arg_count,
133 NPVariant* variant) {
134 return true;
137 bool NPHandleObject::HasProperty(NPIdentifier name) {
138 return name == id_handle;
141 bool NPHandleObject::GetProperty(NPIdentifier name, NPVariant* variant) {
142 static const int kHandleStringLength = 19;
144 if (name == id_handle && variant) {
145 if (char* hex = static_cast<char*>(NPN_MemAlloc(kHandleStringLength))) {
146 #if NACL_WINDOWS
147 _snprintf_s(hex, kHandleStringLength, kHandleStringLength,
148 "0x%p", (void*) handle_);
149 #else
150 snprintf(hex, kHandleStringLength, "%p", (void*) handle_);
151 #endif
152 STRINGZ_TO_NPVARIANT(hex, *variant);
153 return true;
156 return false;
159 bool NPHandleObject::SetProperty(NPIdentifier name, const NPVariant* value) {
160 return false;
163 bool NPHandleObject::RemoveProperty(NPIdentifier name) {
164 return false;
167 } // namespace nacl