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 
33 using 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.
pluginViewForInstance(NPP instance)39 static PluginView* pluginViewForInstance(NPP instance)
40 {
41     if (instance && instance->ndata)
42         return static_cast<PluginView*>(instance->ndata);
43     return PluginView::currentPluginView();
44 }
45 
NPN_MemAlloc(uint32_t size)46 void* NPN_MemAlloc(uint32_t size)
47 {
48     return malloc(size);
49 }
50 
NPN_MemFree(void * ptr)51 void NPN_MemFree(void* ptr)
52 {
53     free(ptr);
54 }
55 
NPN_MemFlush(uint32_t size)56 uint32_t NPN_MemFlush(uint32_t size)
57 {
58     // Do nothing
59     return 0;
60 }
61 
NPN_ReloadPlugins(NPBool reloadPages)62 void NPN_ReloadPlugins(NPBool reloadPages)
63 {
64     Page::refreshPlugins(reloadPages);
65 }
66 
NPN_RequestRead(NPStream * stream,NPByteRange * rangeList)67 NPError NPN_RequestRead(NPStream* stream, NPByteRange* rangeList)
68 {
69     return NPERR_STREAM_NOT_SEEKABLE;
70 }
71 
NPN_GetURLNotify(NPP instance,const char * url,const char * target,void * notifyData)72 NPError NPN_GetURLNotify(NPP instance, const char* url, const char* target, void* notifyData)
73 {
74     return pluginViewForInstance(instance)->getURLNotify(url, target, notifyData);
75 }
76 
NPN_GetURL(NPP instance,const char * url,const char * target)77 NPError NPN_GetURL(NPP instance, const char* url, const char* target)
78 {
79     return pluginViewForInstance(instance)->getURL(url, target);
80 }
81 
NPN_PostURLNotify(NPP instance,const char * url,const char * target,uint32_t len,const char * buf,NPBool file,void * notifyData)82 NPError 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 
NPN_PostURL(NPP instance,const char * url,const char * target,uint32_t len,const char * buf,NPBool file)87 NPError 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 
NPN_NewStream(NPP instance,NPMIMEType type,const char * target,NPStream ** stream)92 NPError NPN_NewStream(NPP instance, NPMIMEType type, const char* target, NPStream** stream)
93 {
94     return pluginViewForInstance(instance)->newStream(type, target, stream);
95 }
96 
NPN_Write(NPP instance,NPStream * stream,int32_t len,void * buffer)97 int32_t NPN_Write(NPP instance, NPStream* stream, int32_t len, void* buffer)
98 {
99     return pluginViewForInstance(instance)->write(stream, len, buffer);
100 }
101 
NPN_DestroyStream(NPP instance,NPStream * stream,NPReason reason)102 NPError NPN_DestroyStream(NPP instance, NPStream* stream, NPReason reason)
103 {
104     return pluginViewForInstance(instance)->destroyStream(stream, reason);
105 }
106 
NPN_UserAgent(NPP instance)107 const 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 
NPN_Status(NPP instance,const char * message)117 void NPN_Status(NPP instance, const char* message)
118 {
119     pluginViewForInstance(instance)->status(message);
120 }
121 
NPN_InvalidateRect(NPP instance,NPRect * invalidRect)122 void NPN_InvalidateRect(NPP instance, NPRect* invalidRect)
123 {
124     pluginViewForInstance(instance)->invalidateRect(invalidRect);
125 }
126 
NPN_InvalidateRegion(NPP instance,NPRegion invalidRegion)127 void NPN_InvalidateRegion(NPP instance, NPRegion invalidRegion)
128 {
129     pluginViewForInstance(instance)->invalidateRegion(invalidRegion);
130 }
131 
NPN_ForceRedraw(NPP instance)132 void NPN_ForceRedraw(NPP instance)
133 {
134     pluginViewForInstance(instance)->forceRedraw();
135 }
136 
NPN_GetValue(NPP instance,NPNVariable variable,void * value)137 NPError 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 
NPN_SetValue(NPP instance,NPPVariable variable,void * value)147 NPError NPN_SetValue(NPP instance, NPPVariable variable, void* value)
148 {
149    return pluginViewForInstance(instance)->setValue(variable, value);
150 }
151 
NPN_GetJavaEnv()152 void* NPN_GetJavaEnv()
153 {
154     // Unsupported
155     return 0;
156 }
157 
NPN_GetJavaPeer(NPP instance)158 void* NPN_GetJavaPeer(NPP instance)
159 {
160     // Unsupported
161     return 0;
162 }
163 
NPN_PushPopupsEnabledState(NPP instance,NPBool enabled)164 void NPN_PushPopupsEnabledState(NPP instance, NPBool enabled)
165 {
166     pluginViewForInstance(instance)->pushPopupsEnabledState(enabled);
167 }
168 
NPN_PopPopupsEnabledState(NPP instance)169 void NPN_PopPopupsEnabledState(NPP instance)
170 {
171     pluginViewForInstance(instance)->popPopupsEnabledState();
172 }
173 
174 extern "C" typedef void PluginThreadAsyncCallFunction(void*);
NPN_PluginThreadAsyncCall(NPP instance,PluginThreadAsyncCallFunction func,void * userData)175 void 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 
NPN_GetValueForURL(NPP instance,NPNURLVariable variable,const char * url,char ** value,uint32_t * len)181 NPError 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 
NPN_SetValueForURL(NPP instance,NPNURLVariable variable,const char * url,const char * value,uint32_t len)186 NPError 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 
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)191 NPError 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