mshtml: Populate dynamic properties table in get_dynamic_data.
[wine] / dlls / mshtml / npplugin.c
1 /*
2  * Copyright 2010 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "shlobj.h"
30
31 #include "mshtml_private.h"
32 #include "pluginhost.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 /* Parts of npapi.h */
39
40 #define NPERR_BASE                         0
41 #define NPERR_NO_ERROR                    (NPERR_BASE + 0)
42 #define NPERR_GENERIC_ERROR               (NPERR_BASE + 1)
43 #define NPERR_INVALID_INSTANCE_ERROR      (NPERR_BASE + 2)
44 #define NPERR_INVALID_FUNCTABLE_ERROR     (NPERR_BASE + 3)
45 #define NPERR_MODULE_LOAD_FAILED_ERROR    (NPERR_BASE + 4)
46 #define NPERR_OUT_OF_MEMORY_ERROR         (NPERR_BASE + 5)
47 #define NPERR_INVALID_PLUGIN_ERROR        (NPERR_BASE + 6)
48 #define NPERR_INVALID_PLUGIN_DIR_ERROR    (NPERR_BASE + 7)
49 #define NPERR_INCOMPATIBLE_VERSION_ERROR  (NPERR_BASE + 8)
50 #define NPERR_INVALID_PARAM               (NPERR_BASE + 9)
51 #define NPERR_INVALID_URL                 (NPERR_BASE + 10)
52 #define NPERR_FILE_NOT_FOUND              (NPERR_BASE + 11)
53 #define NPERR_NO_DATA                     (NPERR_BASE + 12)
54 #define NPERR_STREAM_NOT_SEEKABLE         (NPERR_BASE + 13)
55
56 /* Parts of npfunctions.h */
57
58 typedef NPError (CDECL *NPP_NewProcPtr)(NPMIMEType,NPP,UINT16,INT16,char**,char**,NPSavedData*);
59 typedef NPError (CDECL *NPP_DestroyProcPtr)(NPP,NPSavedData**);
60 typedef NPError (CDECL *NPP_SetWindowProcPtr)(NPP,NPWindow*);
61 typedef NPError (CDECL *NPP_NewStreamProcPtr)(NPP,NPMIMEType,NPStream*,NPBool,UINT16*);
62 typedef NPError (CDECL *NPP_DestroyStreamProcPtr)(NPP,NPStream*,NPReason);
63 typedef INT32 (CDECL *NPP_WriteReadyProcPtr)(NPP,NPStream*);
64 typedef INT32 (CDECL *NPP_WriteProcPtr)(NPP,NPStream*,INT32,INT32,void*);
65 typedef void (CDECL *NPP_StreamAsFileProcPtr)(NPP,NPStream*,const char*);
66 typedef void (CDECL *NPP_PrintProcPtr)(NPP,NPPrint*);
67 typedef INT16 (CDECL *NPP_HandleEventProcPtr)(NPP,void*);
68 typedef void (CDECL *NPP_URLNotifyProcPtr)(NPP,const char*,NPReason,void*);
69 typedef NPError (CDECL *NPP_GetValueProcPtr)(NPP,NPPVariable,void*);
70 typedef NPError (CDECL *NPP_SetValueProcPtr)(NPP,NPNVariable,void*);
71 typedef NPBool (CDECL *NPP_GotFocusPtr)(NPP,NPFocusDirection);
72 typedef void (CDECL *NPP_LostFocusPtr)(NPP);
73
74 typedef struct _NPPluginFuncs {
75     UINT16 size;
76     UINT16 version;
77     NPP_NewProcPtr newp;
78     NPP_DestroyProcPtr destroy;
79     NPP_SetWindowProcPtr setwindow;
80     NPP_NewStreamProcPtr newstream;
81     NPP_DestroyStreamProcPtr destroystream;
82     NPP_StreamAsFileProcPtr asfile;
83     NPP_WriteReadyProcPtr writeready;
84     NPP_WriteProcPtr write;
85     NPP_PrintProcPtr print;
86     NPP_HandleEventProcPtr event;
87     NPP_URLNotifyProcPtr urlnotify;
88     void *javaClass;
89     NPP_GetValueProcPtr getvalue;
90     NPP_SetValueProcPtr setvalue;
91     NPP_GotFocusPtr gotfocus;
92     NPP_LostFocusPtr lostfocus;
93 } NPPluginFuncs;
94
95 static nsIDOMElement *get_dom_element(NPP instance)
96 {
97     nsISupports *instance_unk = (nsISupports*)instance->ndata;
98     nsIPluginInstance *plugin_instance;
99     nsIPluginInstanceOwner *owner;
100     nsIPluginTagInfo *tag_info;
101     nsIDOMElement *elem;
102     nsresult nsres;
103
104     nsres = nsISupports_QueryInterface(instance_unk, &IID_nsIPluginInstance, (void**)&plugin_instance);
105     if(NS_FAILED(nsres))
106         return NULL;
107
108     nsres = nsIPluginInstance_GetOwner(plugin_instance, &owner);
109     nsIPluginInstance_Release(instance_unk);
110     if(NS_FAILED(nsres) || !owner)
111         return NULL;
112
113     nsres = nsISupports_QueryInterface(owner, &IID_nsIPluginTagInfo, (void**)&tag_info);
114     nsISupports_Release(owner);
115     if(NS_FAILED(nsres))
116         return NULL;
117
118     nsres = nsIPluginTagInfo_GetDOMElement(tag_info, &elem);
119     nsIPluginTagInfo_Release(tag_info);
120     if(NS_FAILED(nsres))
121         return NULL;
122
123     return elem;
124 }
125
126 static HTMLWindow *get_elem_window(nsIDOMElement *elem)
127 {
128     nsIDOMWindow *nswindow;
129     nsIDOMDocument *nsdoc;
130     HTMLWindow *window;
131     nsresult nsres;
132
133     nsres = nsIDOMElement_GetOwnerDocument(elem, &nsdoc);
134     if(NS_FAILED(nsres))
135         return NULL;
136
137     nswindow = get_nsdoc_window(nsdoc);
138     nsIDOMDocument_Release(nsdoc);
139     if(!nswindow)
140         return NULL;
141
142     window = nswindow_to_window(nswindow);
143     nsIDOMWindow_Release(nswindow);
144
145     return window;
146 }
147
148 static BOOL parse_classid(const PRUnichar *classid, CLSID *clsid)
149 {
150     const WCHAR *ptr;
151     unsigned len;
152     HRESULT hres;
153
154     static const PRUnichar clsidW[] = {'c','l','s','i','d',':'};
155
156     if(strncmpiW(classid, clsidW, sizeof(clsidW)/sizeof(WCHAR)))
157         return FALSE;
158
159     ptr = classid + sizeof(clsidW)/sizeof(WCHAR);
160     len = strlenW(ptr);
161
162     if(len == 38) {
163         hres = CLSIDFromString(ptr, clsid);
164     }else if(len == 36) {
165         WCHAR buf[39];
166
167         buf[0] = '{';
168         memcpy(buf+1, ptr, len*sizeof(WCHAR));
169         buf[37] = '}';
170         buf[38] = 0;
171         hres = CLSIDFromString(buf, clsid);
172     }else {
173         return FALSE;
174     }
175
176     return SUCCEEDED(hres);
177 }
178
179 static BOOL get_elem_clsid(nsIDOMElement *elem, CLSID *clsid)
180 {
181     nsAString attr_str, val_str;
182     nsresult nsres;
183     BOOL ret = FALSE;
184
185     static const PRUnichar classidW[] = {'c','l','a','s','s','i','d',0};
186
187     nsAString_InitDepend(&attr_str, classidW);
188     nsAString_Init(&val_str, NULL);
189     nsres = nsIDOMElement_GetAttribute(elem, &attr_str, &val_str);
190     nsAString_Finish(&attr_str);
191     if(NS_SUCCEEDED(nsres)) {
192         const PRUnichar *val;
193
194         nsAString_GetData(&val_str, &val);
195         if(*val)
196             ret = parse_classid(val, clsid);
197     }else {
198         ERR("GetAttribute failed: %08x\n", nsres);
199     }
200
201     nsAString_Finish(&attr_str);
202     return ret;
203 }
204
205 static IUnknown *create_activex_object(HTMLWindow *window, nsIDOMElement *nselem, CLSID *clsid)
206 {
207     IClassFactoryEx *cfex;
208     IClassFactory *cf;
209     IUnknown *obj;
210     DWORD policy;
211     HRESULT hres;
212
213     if(!get_elem_clsid(nselem, clsid)) {
214         WARN("Could not determine element CLSID\n");
215         return NULL;
216     }
217
218     TRACE("clsid %s\n", debugstr_guid(clsid));
219
220     policy = 0;
221     hres = IInternetHostSecurityManager_ProcessUrlAction(&window->doc->IInternetHostSecurityManager_iface,
222             URLACTION_ACTIVEX_RUN, (BYTE*)&policy, sizeof(policy), (BYTE*)clsid, sizeof(GUID), 0, 0);
223     if(FAILED(hres) || policy != URLPOLICY_ALLOW) {
224         WARN("ProcessUrlAction returned %08x %x\n", hres, policy);
225         return NULL;
226     }
227
228     hres = CoGetClassObject(clsid, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER, NULL, &IID_IClassFactory, (void**)&cf);
229     if(FAILED(hres))
230         return NULL;
231
232     hres = IClassFactory_QueryInterface(cf, &IID_IClassFactoryEx, (void**)&cfex);
233     if(SUCCEEDED(hres)) {
234         FIXME("Use IClassFactoryEx\n");
235         IClassFactoryEx_Release(cfex);
236     }
237
238     hres = IClassFactory_CreateInstance(cf, NULL, &IID_IUnknown, (void**)&obj);
239     if(FAILED(hres))
240         return NULL;
241
242     return obj;
243 }
244
245 static NPError CDECL NPP_New(NPMIMEType pluginType, NPP instance, UINT16 mode, INT16 argc, char **argn,
246         char **argv, NPSavedData *saved)
247 {
248     nsIDOMElement *nselem;
249     HTMLWindow *window;
250     IUnknown *obj;
251     CLSID clsid;
252     NPError err = NPERR_NO_ERROR;
253
254     TRACE("(%s %p %x %d %p %p %p)\n", debugstr_a(pluginType), instance, mode, argc, argn, argv, saved);
255
256     nselem = get_dom_element(instance);
257     if(!nselem) {
258         ERR("Could not get DOM element\n");
259         return NPERR_GENERIC_ERROR;
260     }
261
262     window = get_elem_window(nselem);
263     if(!window) {
264         ERR("Could not get element's window object\n");
265         nsIDOMElement_Release(nselem);
266         return NPERR_GENERIC_ERROR;
267     }
268
269     obj = create_activex_object(window, nselem, &clsid);
270     if(obj) {
271         PluginHost *host;
272         HRESULT hres;
273
274         hres = create_plugin_host(window->doc, nselem, obj, &clsid, &host);
275         nsIDOMElement_Release(nselem);
276         IUnknown_Release(obj);
277         if(SUCCEEDED(hres))
278             instance->pdata = host;
279         else
280             err = NPERR_GENERIC_ERROR;
281     }else {
282         err = NPERR_GENERIC_ERROR;
283     }
284
285     nsIDOMElement_Release(nselem);
286     return err;
287 }
288
289 static NPError CDECL NPP_Destroy(NPP instance, NPSavedData **save)
290 {
291     PluginHost *host = instance->pdata;
292
293     TRACE("(%p %p)\n", instance, save);
294
295     if(!host)
296         return NPERR_GENERIC_ERROR;
297
298     detach_plugin_host(host);
299     IOleClientSite_Release(&host->IOleClientSite_iface);
300     instance->pdata = NULL;
301     return NPERR_NO_ERROR;
302 }
303
304 static NPError CDECL NPP_SetWindow(NPP instance, NPWindow *window)
305 {
306     PluginHost *host = instance->pdata;
307     RECT pos_rect = {0, 0, window->width, window->height};
308
309     TRACE("(%p %p)\n", instance, window);
310
311     if(!host)
312         return NPERR_GENERIC_ERROR;
313
314     update_plugin_window(host, window->window, &pos_rect);
315     return NPERR_NO_ERROR;
316 }
317
318 static NPError CDECL NPP_NewStream(NPP instance, NPMIMEType type, NPStream *stream, NPBool seekable, UINT16 *stype)
319 {
320     TRACE("\n");
321     return NPERR_GENERIC_ERROR;
322 }
323
324 static NPError CDECL NPP_DestroyStream(NPP instance, NPStream *stream, NPReason reason)
325 {
326     TRACE("\n");
327     return NPERR_GENERIC_ERROR;
328 }
329
330 static INT32 CDECL NPP_WriteReady(NPP instance, NPStream *stream)
331 {
332     TRACE("\n");
333     return NPERR_GENERIC_ERROR;
334 }
335
336 static INT32 CDECL NPP_Write(NPP instance, NPStream *stream, INT32 offset, INT32 len, void *buffer)
337 {
338     TRACE("\n");
339     return NPERR_GENERIC_ERROR;
340 }
341
342 static void CDECL NPP_StreamAsFile(NPP instance, NPStream *stream, const char *fname)
343 {
344     TRACE("\n");
345 }
346
347 static void CDECL NPP_Print(NPP instance, NPPrint *platformPrint)
348 {
349     FIXME("\n");
350 }
351
352 static INT16 CDECL NPP_HandleEvent(NPP instance, void *event)
353 {
354     TRACE("\n");
355     return NPERR_GENERIC_ERROR;
356 }
357
358 static void CDECL NPP_URLNotify(NPP instance, const char *url, NPReason reason, void *notifyData)
359 {
360     TRACE("\n");
361 }
362
363 static NPError CDECL NPP_GetValue(NPP instance, NPPVariable variable, void *ret_value)
364 {
365     TRACE("\n");
366     return NPERR_GENERIC_ERROR;
367 }
368
369 static NPError CDECL NPP_SetValue(NPP instance, NPNVariable variable, void *value)
370 {
371     TRACE("\n");
372     return NPERR_GENERIC_ERROR;
373 }
374
375 static NPBool CDECL NPP_GotFocus(NPP instance, NPFocusDirection direction)
376 {
377     FIXME("\n");
378     return NPERR_GENERIC_ERROR;
379 }
380
381 static void CDECL NPP_LostFocus(NPP instance)
382 {
383     FIXME("\n");
384 }
385
386 /***********************************************************************
387  *          NP_GetEntryPoints (mshtml.@)
388  */
389 NPError WINAPI NP_GetEntryPoints(NPPluginFuncs* funcs)
390 {
391     TRACE("(%p)\n", funcs);
392
393     funcs->version = (NP_VERSION_MAJOR << 8) | NP_VERSION_MINOR;
394     funcs->newp = NPP_New;
395     funcs->destroy = NPP_Destroy;
396     funcs->setwindow = NPP_SetWindow;
397     funcs->newstream = NPP_NewStream;
398     funcs->destroystream = NPP_DestroyStream;
399     funcs->asfile = NPP_StreamAsFile;
400     funcs->writeready = NPP_WriteReady;
401     funcs->write = NPP_Write;
402     funcs->print = NPP_Print;
403     funcs->event = NPP_HandleEvent;
404     funcs->urlnotify = NPP_URLNotify;
405     funcs->javaClass = NULL;
406     funcs->getvalue = NPP_GetValue;
407     funcs->setvalue = NPP_SetValue;
408     funcs->gotfocus = NPP_GotFocus;
409     funcs->lostfocus = NPP_LostFocus;
410
411     return NPERR_NO_ERROR;
412 }