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 COMPUTER, 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 COMPUTER, 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 size)
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* stream, NPByteRange* rangeList)
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    pluginViewForInstance(instance)->invalidateRect(invalidRect);
125}
126
127void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
128{
129    pluginViewForInstance(instance)->invalidateRegion(invalidRegion);
130}
131
132void NPN_ForceRedraw(NPP instance)
133{
134    pluginViewForInstance(instance)->forceRedraw();
135}
136
137NPError NPN_GetValue(NPP instance, NPNVariable variable, void* value)
138{
139    PluginView* view = pluginViewForInstance(instance);
140
141     if (!view)
142         return PluginView::getValueStatic(variable, value);
143
144    return pluginViewForInstance(instance)->getValue(variable, value);
145}
146
147NPError NPN_SetValue(NPP instance, NPPVariable variable, void* value)
148{
149   return pluginViewForInstance(instance)->setValue(variable, value);
150}
151
152void* NPN_GetJavaEnv()
153{
154    // Unsupported
155    return 0;
156}
157
158void* NPN_GetJavaPeer(NPP instance)
159{
160    // Unsupported
161    return 0;
162}
163
164void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
165{
166    pluginViewForInstance(instance)->pushPopupsEnabledState(enabled);
167}
168
169void NPN_PopPopupsEnabledState(NPP instance)
170{
171    pluginViewForInstance(instance)->popPopupsEnabledState();
172}
173
174extern "C" typedef void PluginThreadAsyncCallFunction(void*);
175void NPN_PluginThreadAsyncCall(NPP instance, PluginThreadAsyncCallFunction func, void* userData)
176{
177    // Callback function type only differs from MainThreadFunction by being extern "C", which doesn't affect calling convention on any compilers we use.
178    PluginMainThreadScheduler::scheduler().scheduleCall(instance, reinterpret_cast<PluginMainThreadScheduler::MainThreadFunction*>(func), userData);
179}
180
181NPError NPN_GetValueForURL(NPP instance, NPNURLVariable variable, const char* url, char** value, uint32_t* len)
182{
183    return pluginViewForInstance(instance)->getValueForURL(variable, url, value, len);
184}
185
186NPError NPN_SetValueForURL(NPP instance, NPNURLVariable variable, const char* url, const char* value, uint32_t len)
187{
188    return pluginViewForInstance(instance)->setValueForURL(variable, url, value, len);
189}
190
191NPError 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)
192{
193    return pluginViewForInstance(instance)->getAuthenticationInfo(protocol, host, port, scheme, realm, username, ulen, password, plen);
194}
195
196#ifdef PLUGIN_SCHEDULE_TIMER
197uint32_t NPN_ScheduleTimer(NPP instance, uint32_t interval, NPBool repeat,
198                         void (*timerFunc)(NPP npp, uint32_t timerID))
199{
200    return pluginViewForInstance(instance)->scheduleTimer(instance, interval,
201                                                        repeat != 0, timerFunc);
202}
203
204void NPN_UnscheduleTimer(NPP instance, uint32_t timerID)
205{
206    pluginViewForInstance(instance)->unscheduleTimer(instance, timerID);
207}
208#endif
209