1/*
2 * Copyright (C) 2006, 2007 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#include "Page.h"
29#include "PluginMainThreadScheduler.h"
30#include "PluginView.h"
31#include "npruntime_internal.h"
32
33using namespace WebCore;
34
35// The plugin view is always the ndata of the instance,. Sometimes, plug-ins will call an instance-specific function
36// with a NULL instance. To workaround this, call the last plug-in view that made a call to a plug-in.
37// Currently, the current plug-in view is only set before NPP_New in PluginView::start.
38// This specifically works around Flash and Shockwave. When we call NPP_New, they call NPN_Useragent with a NULL instance.
39static PluginView* pluginViewForInstance(NPP instance)
40{
41    if (instance && instance->ndata)
42        return static_cast<PluginView*>(instance->ndata);
43    return PluginView::currentPluginView();
44}
45
46void* NPN_MemAlloc(uint32_t size)
47{
48    return malloc(size);
49}
50
51void NPN_MemFree(void* ptr)
52{
53    free(ptr);
54}
55
56uint32_t NPN_MemFlush(uint32_t)
57{
58    // Do nothing
59    return 0;
60}
61
62void NPN_ReloadPlugins(NPBool reloadPages)
63{
64    Page::refreshPlugins(reloadPages);
65}
66
67NPError NPN_RequestRead(NPStream*, NPByteRange*)
68{
69    return NPERR_STREAM_NOT_SEEKABLE;
70}
71
72NPError NPN_GetURLNotify(NPP instance, const char* url, const char* target, void* notifyData)
73{
74    return pluginViewForInstance(instance)->getURLNotify(url, target, notifyData);
75}
76
77NPError NPN_GetURL(NPP instance, const char* url, const char* target)
78{
79    return pluginViewForInstance(instance)->getURL(url, target);
80}
81
82NPError NPN_PostURLNotify(NPP instance, const char* url, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
83{
84    return pluginViewForInstance(instance)->postURLNotify(url, target, len, buf, file, notifyData);
85}
86
87NPError NPN_PostURL(NPP instance, const char* url, const char* target, uint32_t len, const char* buf, NPBool file)
88{
89    return pluginViewForInstance(instance)->postURL(url, target, len, buf, file);
90}
91
92NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream)
93{
94    return pluginViewForInstance(instance)->newStream(type, target, stream);
95}
96
97int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
98{
99    return pluginViewForInstance(instance)->write(stream, len, buffer);
100}
101
102NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
103{
104    return pluginViewForInstance(instance)->destroyStream(stream, reason);
105}
106
107const char* NPN_UserAgent(NPP instance)
108{
109    PluginView* view = pluginViewForInstance(instance);
110
111     if (!view)
112         return PluginView::userAgentStatic();
113
114    return view->userAgent();
115}
116
117void NPN_Status(NPP instance, const char* message)
118{
119    pluginViewForInstance(instance)->status(message);
120}
121
122void NPN_InvalidateRect(NPP instance, NPRect* invalidRect)
123{
124    PluginView* view = pluginViewForInstance(instance);
125#if defined(XP_UNIX)
126    // NSPluginWrapper, a plugin wrapper binary that allows running 32-bit plugins
127    // on 64-bit architectures typically used in X11, will sometimes give us a null NPP here.
128    if (!view)
129        return;
130#endif
131    view->invalidateRect(invalidRect);
132}
133
134void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
135{
136    pluginViewForInstance(instance)->invalidateRegion(invalidRegion);
137}
138
139void NPN_ForceRedraw(NPP instance)
140{
141    pluginViewForInstance(instance)->forceRedraw();
142}
143
144NPError NPN_GetValue(NPP instance, NPNVariable variable, void* value)
145{
146    PluginView* view = pluginViewForInstance(instance);
147
148     if (!view)
149         return PluginView::getValueStatic(variable, value);
150
151    return pluginViewForInstance(instance)->getValue(variable, value);
152}
153
154NPError NPN_SetValue(NPP instance, NPPVariable variable, void* value)
155{
156   return pluginViewForInstance(instance)->setValue(variable, value);
157}
158
159void* NPN_GetJavaEnv()
160{
161    // Unsupported
162    return 0;
163}
164
165void* NPN_GetJavaPeer(NPP)
166{
167    // Unsupported
168    return 0;
169}
170
171void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
172{
173    pluginViewForInstance(instance)->pushPopupsEnabledState(enabled);
174}
175
176void NPN_PopPopupsEnabledState(NPP instance)
177{
178    pluginViewForInstance(instance)->popPopupsEnabledState();
179}
180
181extern "C" typedef void PluginThreadAsyncCallFunction(void*);
182void NPN_PluginThreadAsyncCall(NPP instance, PluginThreadAsyncCallFunction func, void* userData)
183{
184    // Callback function type only differs from MainThreadFunction by being extern "C", which doesn't affect calling convention on any compilers we use.
185    PluginMainThreadScheduler::scheduler().scheduleCall(instance, reinterpret_cast<PluginMainThreadScheduler::MainThreadFunction*>(func), userData);
186}
187
188NPError NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char* url, char** value, uint32_t* len)
189{
190    return pluginViewForInstance(instance)->getValueForURL(variable, url, value, len);
191}
192
193NPError NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char* url, const char* value, uint32_t len)
194{
195    return pluginViewForInstance(instance)->setValueForURL(variable, url, value, len);
196}
197
198NPError NPN_GetAuthenticationInfo(NPP instance, const char* protocol, const char* host, int32_t port, const char* scheme, const char* realm, char** username, uint32_t* ulen, char** password, uint32_t* plen)
199{
200    return pluginViewForInstance(instance)->getAuthenticationInfo(protocol, host, port, scheme, realm, username, ulen, password, plen);
201}
202
203NPError NPN_PopUpContextMenu(NPP instance, NPMenu* menu)
204{
205    UNUSED_PARAM(instance);
206    UNUSED_PARAM(menu);
207    return NPERR_NO_ERROR;
208}
209