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#ifndef NPJSObject_h
27#define NPJSObject_h
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#include <JavaScriptCore/Strong.h>
32#include <WebCore/npruntime_internal.h>
33#include <wtf/Noncopyable.h>
34
35namespace JSC {
36
37class VM;
38class JSGlobalObject;
39class JSObject;
40
41}
42
43namespace WebKit {
44
45class NPRuntimeObjectMap;
46
47// NPJSObject is an NPObject that wraps a JSObject.
48class NPJSObject : public NPObject {
49    WTF_MAKE_NONCOPYABLE(NPJSObject);
50public:
51    static NPJSObject* create(JSC::VM&, NPRuntimeObjectMap*, JSC::JSObject*);
52
53    JSC::JSObject* jsObject() const { return m_jsObject.get(); }
54
55    static bool isNPJSObject(NPObject*);
56
57    static NPJSObject* toNPJSObject(NPObject* npObject)
58    {
59        ASSERT_WITH_SECURITY_IMPLICATION(isNPJSObject(npObject));
60        return static_cast<NPJSObject*>(npObject);
61    }
62
63private:
64    NPJSObject();
65    ~NPJSObject();
66
67    void initialize(JSC::VM&, NPRuntimeObjectMap*, JSC::JSObject*);
68
69    bool hasMethod(NPIdentifier methodName);
70    bool invoke(NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
71    bool invokeDefault(const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
72    bool hasProperty(NPIdentifier propertyName);
73    bool getProperty(NPIdentifier propertyName, NPVariant* result);
74    bool setProperty(NPIdentifier propertyName, const NPVariant* value);
75    bool removeProperty(NPIdentifier propertyName);
76    bool enumerate(NPIdentifier** identifiers, uint32_t* identifierCount);
77    bool construct(const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
78
79    bool invoke(JSC::ExecState*, JSC::JSGlobalObject*, JSC::JSValue function, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
80
81    static NPClass* npClass();
82    static NPObject* NP_Allocate(NPP, NPClass*);
83    static void NP_Deallocate(NPObject*);
84    static bool NP_HasMethod(NPObject*, NPIdentifier methodName);
85    static bool NP_Invoke(NPObject*, NPIdentifier methodName, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
86    static bool NP_InvokeDefault(NPObject*, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
87    static bool NP_HasProperty(NPObject*, NPIdentifier propertyName);
88    static bool NP_GetProperty(NPObject*, NPIdentifier propertyName, NPVariant* result);
89    static bool NP_SetProperty(NPObject*, NPIdentifier propertyName, const NPVariant* value);
90    static bool NP_RemoveProperty(NPObject*, NPIdentifier propertyName);
91    static bool NP_Enumerate(NPObject*, NPIdentifier** identifiers, uint32_t* identifierCount);
92    static bool NP_Construct(NPObject*, const NPVariant* arguments, uint32_t argumentCount, NPVariant* result);
93
94    NPRuntimeObjectMap* m_objectMap;
95    JSC::Strong<JSC::JSObject> m_jsObject;
96};
97
98} // namespace WebKit
99
100#endif // ENABLE(NETSCAPE_PLUGIN_API)
101
102#endif // NPJSObject_h
103