1/*
2 * Copyright (C) 2005 Apple Computer, 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30#import <WebKit/npapi.h>
31
32#import "WebNetscapePluginView.h"
33#import "WebKitLogging.h"
34#import <WebCore/PluginMainThreadScheduler.h>
35
36using namespace WebCore;
37
38WebNetscapePluginView *pluginViewForInstance(NPP instance);
39
40// general plug-in to browser functions
41
42void* NPN_MemAlloc(uint32_t size)
43{
44    return malloc(size);
45}
46
47void NPN_MemFree(void* ptr)
48{
49    free(ptr);
50}
51
52uint32_t NPN_MemFlush(uint32_t size)
53{
54    LOG(Plugins, "NPN_MemFlush");
55    return size;
56}
57
58void NPN_ReloadPlugins(NPBool reloadPages)
59{
60    LOG(Plugins, "NPN_ReloadPlugins");
61}
62
63NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
64{
65    LOG(Plugins, "NPN_RequestRead");
66    return NPERR_GENERIC_ERROR;
67}
68
69// instance-specific functions
70// The plugin view is always the ndata of the instance. Sometimes, plug-ins will call an instance-specific function
71// with a NULL instance. To workaround this, call the last plug-in view that made a call to a plug-in.
72// Currently, the current plug-in view is only set before NPP_New in [WebNetscapePluginView start].
73// This specifically works around Flash and Shockwave. When we call NPP_New, they call NPN_UserAgent with a NULL instance.
74WebNetscapePluginView *pluginViewForInstance(NPP instance)
75{
76    if (instance && instance->ndata)
77        return (WebNetscapePluginView *)instance->ndata;
78    else
79        return [WebNetscapePluginView currentPluginView];
80}
81
82NPError NPN_GetURLNotify(NPP instance, const char* URL, const char* target, void* notifyData)
83{
84    return [pluginViewForInstance(instance) getURLNotify:URL target:target notifyData:notifyData];
85}
86
87NPError NPN_GetURL(NPP instance, const char* URL, const char* target)
88{
89    return [pluginViewForInstance(instance) getURL:URL target:target];
90}
91
92NPError NPN_PostURLNotify(NPP instance, const char* URL, const char* target, uint32_t len, const char* buf, NPBool file, void* notifyData)
93{
94    return [pluginViewForInstance(instance) postURLNotify:URL target:target len:len buf:buf file:file notifyData:notifyData];
95}
96
97NPError NPN_PostURL(NPP instance, const char* URL, const char* target, uint32_t len, const char* buf, NPBool file)
98{
99    return [pluginViewForInstance(instance) postURL:URL target:target len:len buf:buf file:file];
100}
101
102NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream)
103{
104    return [pluginViewForInstance(instance) newStream:type target:target stream:stream];
105}
106
107int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
108{
109    return [pluginViewForInstance(instance) write:stream len:len buffer:buffer];
110}
111
112NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
113{
114    return [pluginViewForInstance(instance) destroyStream:stream reason:reason];
115}
116
117const char* NPN_UserAgent(NPP instance)
118{
119    return [pluginViewForInstance(instance) userAgent];
120}
121
122void NPN_Status(NPP instance, const char* message)
123{
124    [pluginViewForInstance(instance) status:message];
125}
126
127void NPN_InvalidateRect(NPP instance, NPRect *invalidRect)
128{
129    [pluginViewForInstance(instance) invalidateRect:invalidRect];
130}
131
132void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
133{
134    [pluginViewForInstance(instance) invalidateRegion:invalidRegion];
135}
136
137void NPN_ForceRedraw(NPP instance)
138{
139    [pluginViewForInstance(instance) forceRedraw];
140}
141
142NPError NPN_GetValue(NPP instance, NPNVariable variable, void *value)
143{
144    return [pluginViewForInstance(instance) getVariable:variable value:value];
145}
146
147NPError NPN_SetValue(NPP instance, NPPVariable variable, void *value)
148{
149    return [pluginViewForInstance(instance) setVariable:variable value:value];
150}
151
152// Unsupported functions
153
154void* NPN_GetJavaEnv(void)
155{
156    LOG(Plugins, "NPN_GetJavaEnv");
157    return NULL;
158}
159
160void* NPN_GetJavaPeer(NPP instance)
161{
162    LOG(Plugins, "NPN_GetJavaPeer");
163    return NULL;
164}
165
166void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
167{
168}
169
170void NPN_PopPopupsEnabledState(NPP instance)
171{
172}
173
174void NPN_PluginThreadAsyncCall(NPP instance, void (*func) (void *), void *userData)
175{
176    PluginMainThreadScheduler::scheduler().scheduleCall(instance, func, userData);
177}
178
179uint32_t NPN_ScheduleTimer(NPP instance, uint32_t interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32_t timerID))
180{
181    return [pluginViewForInstance(instance) scheduleTimerWithInterval:interval repeat:repeat timerFunc:timerFunc];
182}
183
184void NPN_UnscheduleTimer(NPP instance, uint32_t timerID)
185{
186    [pluginViewForInstance(instance) unscheduleTimer:timerID];
187}
188
189NPError NPN_PopUpContextMenu(NPP instance, NPMenu *menu)
190{
191    return [pluginViewForInstance(instance) popUpContextMenu:menu];
192}
193
194NPError NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char* url, char** value, uint32_t* len)
195{
196    return [pluginViewForInstance(instance) getVariable:variable forURL:url value:value length:len];
197}
198
199NPError NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char* url, const char* value, uint32_t len)
200{
201    return [pluginViewForInstance(instance) setVariable:variable forURL:url value:value length:len];
202}
203
204NPError 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)
205{
206    return [pluginViewForInstance(instance) getAuthenticationInfoWithProtocol:protocol
207                                                                         host:host
208                                                                         port:port
209                                                                       scheme:scheme
210                                                                        realm:realm
211                                                                     username:username usernameLength:ulen
212                                                                     password:password passwordLength:plen];
213}
214
215NPBool NPN_ConvertPoint(NPP instance, double sourceX, double sourceY, NPCoordinateSpace sourceSpace, double *destX, double *destY, NPCoordinateSpace destSpace)
216{
217    return [pluginViewForInstance(instance) convertFromX:sourceX andY:sourceY space:sourceSpace toX:destX andY:destY space:destSpace];
218}
219
220uint32_t WKN_CheckIfAllowedToLoadURL(NPP instance, const char* url, const char* frame, void (*callbackFunc)(NPP npp, uint32_t, NPBool, void*), void* context)
221{
222    return [pluginViewForInstance(instance) checkIfAllowedToLoadURL:url frame:frame callbackFunc:callbackFunc context:context];
223}
224
225void WKN_CancelCheckIfAllowedToLoadURL(NPP instance, uint32_t checkID)
226{
227    [pluginViewForInstance(instance) cancelCheckIfAllowedToLoadURL:checkID];
228}
229
230char* WKN_ResolveURL(NPP instance, const char* url, const char* target)
231{
232    return [pluginViewForInstance(instance) resolveURL:url forTarget:target];
233}
234
235#endif
236