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 NPJSObjectWrapperMap_h
27#define NPJSObjectWrapperMap_h
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#include <heap/Weak.h>
32#include <heap/WeakInlines.h>
33#include <wtf/Forward.h>
34#include <wtf/HashMap.h>
35#include <wtf/RunLoop.h>
36
37struct NPObject;
38typedef struct _NPVariant NPVariant;
39
40namespace JSC {
41    class ExecState;
42    class VM;
43    class JSGlobalObject;
44    class JSObject;
45    class JSValue;
46}
47
48namespace WebKit {
49
50class JSNPObject;
51class NPJSObject;
52class PluginView;
53
54// A per plug-in map of NPObjects that wrap JavaScript objects.
55class NPRuntimeObjectMap : private JSC::WeakHandleOwner {
56public:
57    explicit NPRuntimeObjectMap(PluginView*);
58
59    class PluginProtector {
60    public:
61        explicit PluginProtector(NPRuntimeObjectMap* npRuntimeObjectMap);
62        ~PluginProtector();
63
64    private:
65        RefPtr<PluginView> m_pluginView;
66    };
67
68    // Returns an NPObject that wraps the given JSObject object. If there is already an NPObject that wraps this JSObject, it will
69    // retain it and return it.
70    NPObject* getOrCreateNPObject(JSC::VM&, JSC::JSObject*);
71    void npJSObjectDestroyed(NPJSObject*);
72
73    // Returns a JSObject object that wraps the given NPObject.
74    JSC::JSObject* getOrCreateJSObject(JSC::JSGlobalObject*, NPObject*);
75    void jsNPObjectDestroyed(JSNPObject*);
76
77    void convertJSValueToNPVariant(JSC::ExecState*, JSC::JSValue, NPVariant&);
78    JSC::JSValue convertNPVariantToJSValue(JSC::ExecState*, JSC::JSGlobalObject*, const NPVariant&);
79
80    bool evaluate(NPObject*, const String& scriptString, NPVariant* result);
81
82    // Called when the plug-in is destroyed. Will invalidate all the NPObjects.
83    void invalidate();
84
85    JSC::JSGlobalObject* globalObject() const;
86    JSC::ExecState* globalExec() const;
87
88    static void setGlobalException(const String& exceptionString);
89    static void moveGlobalExceptionToExecState(JSC::ExecState*);
90
91private:
92    // WeakHandleOwner
93    virtual void finalize(JSC::Handle<JSC::Unknown>, void* context);
94    void addToInvalidationQueue(NPObject*);
95    void invalidateQueuedObjects();
96
97    PluginView* m_pluginView;
98    HashMap<JSC::JSObject*, NPJSObject*> m_npJSObjects;
99    HashMap<NPObject*, JSC::Weak<JSNPObject>> m_jsNPObjects;
100    Vector<NPObject*> m_npObjectsToFinalize;
101    RunLoop::Timer<NPRuntimeObjectMap> m_finalizationTimer;
102};
103
104} // namespace WebKit
105
106#endif // ENABLE(NETSCAPE_PLUGIN_API)
107
108#endif // NPJSObjectWrapperMap_h
109