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 "config.h"
27#include "JSNPMethod.h"
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#include "JSNPObject.h"
32#include <JavaScriptCore/Error.h>
33#include <JavaScriptCore/FunctionPrototype.h>
34#include <JavaScriptCore/JSGlobalObject.h>
35#include <JavaScriptCore/JSObject.h>
36#include <WebCore/JSHTMLElement.h>
37#include <WebCore/JSPluginElementFunctions.h>
38
39using namespace JSC;
40using namespace WebCore;
41
42namespace WebKit {
43
44STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(JSNPMethod);
45
46const ClassInfo JSNPMethod::s_info = { "NPMethod", &InternalFunction::s_info, 0, 0, CREATE_METHOD_TABLE(JSNPMethod) };
47
48JSNPMethod::JSNPMethod(JSGlobalObject* globalObject, Structure* structure, NPIdentifier npIdentifier)
49    : InternalFunction(globalObject->vm(), structure)
50    , m_npIdentifier(npIdentifier)
51{
52}
53
54void JSNPMethod::finishCreation(VM& vm, const String& name)
55{
56    Base::finishCreation(vm, name);
57    ASSERT(inherits(info()));
58}
59
60static EncodedJSValue JSC_HOST_CALL callMethod(ExecState* exec)
61{
62    JSNPMethod* jsNPMethod = jsCast<JSNPMethod*>(exec->callee());
63
64    JSValue thisValue = exec->thisValue();
65
66    // Check if we're calling a method on the plug-in script object.
67    if (thisValue.inherits(JSHTMLElement::info())) {
68        JSHTMLElement* element = jsCast<JSHTMLElement*>(asObject(thisValue));
69
70        // Try to get the script object from the element
71        if (JSObject* scriptObject = pluginScriptObject(exec, element))
72            thisValue = scriptObject;
73    }
74
75    if (thisValue.inherits(JSNPObject::info())) {
76        JSNPObject* jsNPObject = jsCast<JSNPObject*>(asObject(thisValue));
77
78        return JSValue::encode(jsNPObject->callMethod(exec, jsNPMethod->npIdentifier()));
79    }
80
81    return throwVMTypeError(exec);
82}
83
84CallType JSNPMethod::getCallData(JSCell*, CallData& callData)
85{
86    callData.native.function = callMethod;
87    return CallTypeHost;
88}
89
90} // namespace WebKit
91
92#endif // ENABLE(NETSCAPE_PLUGIN_API)
93