1 // Copyright 2014 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.
6 * Copyright (C) 2007 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "content/shell/tools/plugin/test_object.h"
32 #include "PluginObject.h"
37 static bool testEnumerate(NPObject
* npobj
,
40 static bool testHasMethod(NPObject
*, NPIdentifier name
);
41 static bool testInvoke(NPObject
*,
43 const NPVariant
* args
,
46 static bool testHasProperty(NPObject
*, NPIdentifier name
);
47 static bool testGetProperty(NPObject
*, NPIdentifier name
, NPVariant
*);
48 static NPObject
* testAllocate(NPP npp
, NPClass
* theClass
);
49 static void testDeallocate(NPObject
* obj
);
50 static bool testConstruct(NPObject
* obj
,
51 const NPVariant
* args
,
55 static NPClass g_test_class
= {
56 NP_CLASS_STRUCT_VERSION
, testAllocate
, testDeallocate
, 0,
57 testHasMethod
, testInvoke
, 0, testHasProperty
,
58 testGetProperty
, 0, 0, testEnumerate
,
62 static int g_test_object_count
= 0;
69 static bool identifiersInitialized
= false;
71 #define NUM_ENUMERATABLE_TEST_IDENTIFIERS 2
76 ID_PROPERTY_OBJECT_POINTER
,
77 ID_PROPERTY_TEST_OBJECT
,
78 ID_PROPERTY_REF_COUNT
,
82 static NPIdentifier testIdentifiers
[NUM_TEST_IDENTIFIERS
];
83 static const NPUTF8
* testIdentifierNames
[NUM_TEST_IDENTIFIERS
] = {
84 "foo", "bar", "objectPointer", "testObject", "refCount", };
86 #define ID_THROW_EXCEPTION_METHOD 0
87 #define NUM_METHOD_IDENTIFIERS 1
89 static NPIdentifier testMethodIdentifiers
[NUM_METHOD_IDENTIFIERS
];
90 static const NPUTF8
* testMethodIdentifierNames
[NUM_METHOD_IDENTIFIERS
] = {
93 static void initializeIdentifiers(void) {
94 browser
->getstringidentifiers(
95 testIdentifierNames
, NUM_TEST_IDENTIFIERS
, testIdentifiers
);
96 browser
->getstringidentifiers(
97 testMethodIdentifierNames
, NUM_METHOD_IDENTIFIERS
, testMethodIdentifiers
);
100 static NPObject
* testAllocate(NPP npp
, NPClass
* /*theClass*/) {
101 TestObject
* newInstance
=
102 static_cast<TestObject
*>(malloc(sizeof(TestObject
)));
103 newInstance
->testObject
= 0;
104 ++g_test_object_count
;
106 if (!identifiersInitialized
) {
107 identifiersInitialized
= true;
108 initializeIdentifiers();
111 return reinterpret_cast<NPObject
*>(newInstance
);
114 static void testDeallocate(NPObject
* obj
) {
115 TestObject
* testObject
= reinterpret_cast<TestObject
*>(obj
);
116 if (testObject
->testObject
)
117 browser
->releaseobject(testObject
->testObject
);
119 --g_test_object_count
;
123 static bool testHasMethod(NPObject
*, NPIdentifier name
) {
124 for (unsigned i
= 0; i
< NUM_METHOD_IDENTIFIERS
; i
++) {
125 if (testMethodIdentifiers
[i
] == name
)
131 static bool testInvoke(NPObject
* header
,
133 const NPVariant
* /*args*/,
134 uint32_t /*argCount*/,
135 NPVariant
* /*result*/) {
136 if (name
== testMethodIdentifiers
[ID_THROW_EXCEPTION_METHOD
]) {
137 browser
->setexception(header
, "test object throwException SUCCESS");
143 static bool testHasProperty(NPObject
*, NPIdentifier name
) {
144 for (unsigned i
= 0; i
< NUM_TEST_IDENTIFIERS
; i
++) {
145 if (testIdentifiers
[i
] == name
)
152 static bool testGetProperty(NPObject
* npobj
,
155 if (name
== testIdentifiers
[ID_PROPERTY_FOO
]) {
156 char* mem
= static_cast<char*>(browser
->memalloc(4));
158 STRINGZ_TO_NPVARIANT(mem
, *result
);
161 if (name
== testIdentifiers
[ID_PROPERTY_BAR
]) {
162 char* mem
= static_cast<char*>(browser
->memalloc(4));
164 STRINGZ_TO_NPVARIANT(mem
, *result
);
167 if (name
== testIdentifiers
[ID_PROPERTY_OBJECT_POINTER
]) {
168 int32_t objectPointer
=
169 static_cast<int32_t>(reinterpret_cast<long long>(npobj
));
171 INT32_TO_NPVARIANT(objectPointer
, *result
);
174 if (name
== testIdentifiers
[ID_PROPERTY_TEST_OBJECT
]) {
175 TestObject
* testObject
= reinterpret_cast<TestObject
*>(npobj
);
176 if (!testObject
->testObject
)
177 testObject
->testObject
= browser
->createobject(0, &g_test_class
);
178 browser
->retainobject(testObject
->testObject
);
179 OBJECT_TO_NPVARIANT(testObject
->testObject
, *result
);
182 if (name
== testIdentifiers
[ID_PROPERTY_REF_COUNT
]) {
183 INT32_TO_NPVARIANT(npobj
->referenceCount
, *result
);
190 static bool testEnumerate(NPObject
* /*npobj*/,
191 NPIdentifier
** value
,
193 *count
= NUM_ENUMERATABLE_TEST_IDENTIFIERS
;
195 *value
= (NPIdentifier
*)browser
->memalloc(NUM_ENUMERATABLE_TEST_IDENTIFIERS
*
196 sizeof(NPIdentifier
));
199 sizeof(NPIdentifier
) * NUM_ENUMERATABLE_TEST_IDENTIFIERS
);
204 static bool testConstruct(NPObject
* npobj
,
205 const NPVariant
* /*args*/,
206 uint32_t /*argCount*/,
208 browser
->retainobject(npobj
);
210 // Just return the same object.
211 OBJECT_TO_NPVARIANT(npobj
, *result
);
217 NPClass
* GetTestClass() { return &g_test_class
; }
219 int GetTestObjectCount() { return g_test_object_count
; }
221 } // namespace content