1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "PluginTest.h"
27
28#include <string.h>
29
30using namespace std;
31
32class NPRuntimeRemoveProperty : public PluginTest {
33public:
34    NPRuntimeRemoveProperty(NPP npp, const string& identifier)
35        : PluginTest(npp, identifier)
36    {
37    }
38
39private:
40    struct TestObject : Object<TestObject> {
41    public:
42        TestObject()
43            : m_lastRemovedProperty(0)
44        {
45        }
46
47        bool hasProperty(NPIdentifier propertyName)
48        {
49            if (identifierIs(propertyName, "lastRemovedProperty"))
50                return true;
51
52            return false;
53        }
54
55        bool getProperty(NPIdentifier propertyName, NPVariant* result)
56        {
57            assert(identifierIs(propertyName, "lastRemovedProperty"));
58
59            if (!m_lastRemovedProperty)
60                return false;
61
62            if (pluginTest()->NPN_IdentifierIsString(m_lastRemovedProperty)) {
63                char* lastRemovedPropertyName = pluginTest()->NPN_UTF8FromIdentifier(m_lastRemovedProperty);
64
65                STRINGZ_TO_NPVARIANT(lastRemovedPropertyName, *result);
66                return true;
67            }
68
69            int intIdentifier = pluginTest()->NPN_IntFromIdentifier(m_lastRemovedProperty);
70            DOUBLE_TO_NPVARIANT(intIdentifier, *result);
71            return true;
72        }
73
74        bool removeProperty(NPIdentifier propertyName)
75        {
76            m_lastRemovedProperty = propertyName;
77            return true;
78        }
79
80    private:
81        NPIdentifier m_lastRemovedProperty;
82    };
83
84    struct PluginObject : Object<PluginObject> {
85    public:
86        PluginObject()
87            : m_testObject(0)
88        {
89        }
90
91        ~PluginObject()
92        {
93            if (m_testObject)
94                pluginTest()->NPN_ReleaseObject(m_testObject);
95        }
96
97        bool hasMethod(NPIdentifier methodName)
98        {
99            if (identifierIs(methodName, "testRemoveProperty"))
100                return true;
101
102            return false;
103        }
104
105        bool invoke(NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result)
106        {
107            assert(identifierIs(methodName, "testRemoveProperty"));
108
109            if (argumentCount != 2)
110                return false;
111
112            if (!NPVARIANT_IS_OBJECT(arguments[0]))
113                return false;
114
115            if (!NPVARIANT_IS_STRING(arguments[1]) && !NPVARIANT_IS_DOUBLE(arguments[1]))
116                return false;
117
118            NPIdentifier propertyName;
119            if (NPVARIANT_IS_STRING(arguments[1])) {
120                string propertyNameString(arguments[1].value.stringValue.UTF8Characters,
121                                          arguments[1].value.stringValue.UTF8Length);
122
123                propertyName = pluginTest()->NPN_GetStringIdentifier(propertyNameString.c_str());
124            } else {
125                int32_t number = static_cast<int32_t>(arguments[1].value.doubleValue);
126                propertyName = pluginTest()->NPN_GetIntIdentifier(number);
127            }
128
129            pluginTest()->NPN_RemoveProperty(NPVARIANT_TO_OBJECT(arguments[0]), propertyName);
130
131            VOID_TO_NPVARIANT(*result);
132            return true;
133        }
134
135        bool hasProperty(NPIdentifier propertyName)
136        {
137            if (identifierIs(propertyName, "testObject"))
138                return true;
139
140            return false;
141        }
142
143        bool getProperty(NPIdentifier propertyName, NPVariant* result)
144        {
145            assert(identifierIs(propertyName, "testObject"));
146
147            if (!m_testObject)
148                m_testObject = TestObject::create(pluginTest());
149
150            OBJECT_TO_NPVARIANT(pluginTest()->NPN_RetainObject(m_testObject), *result);
151            return true;
152        }
153
154    private:
155        NPObject* m_testObject;
156    };
157
158    virtual NPError NPP_GetValue(NPPVariable variable, void *value)
159    {
160        if (variable != NPPVpluginScriptableNPObject)
161            return NPERR_GENERIC_ERROR;
162
163        *(NPObject**)value = PluginObject::create(this);
164
165        return NPERR_NO_ERROR;
166    }
167
168};
169
170static PluginTest::Register<NPRuntimeRemoveProperty> npRuntimeRemoveProperty("npruntime-remove-property");
171