crypt32: NULL ptr could leak into function (Coverity).
[wine] / dlls / mshtml / nsservice.c
1 /*
2  * Copyright 2005 Jacek Caban
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 "winreg.h"
29 #include "ole2.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 #include "mshtml_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 #define NS_PROMPTSERVICE_CONTRACTID "@mozilla.org/embedcomp/prompt-service;1"
39 #define NS_WINDOWWATCHER_CONTRACTID "@mozilla.org/embedcomp/window-watcher;1"
40 #define NS_TOOLTIPTEXTPROVIDER_CONTRACTID "@mozilla.org/embedcomp/tooltiptextprovider;1"
41
42 #define NS_TOOLTIPTEXTPROVIDER_CLASSNAME "nsTooltipTextProvider"
43
44 static const nsIID NS_PROMPTSERVICE_CID =
45     {0xa2112d6a,0x0e28,0x421f,{0xb4,0x6a,0x25,0xc0,0xb3,0x8,0xcb,0xd0}};
46 static const nsIID NS_TOOLTIPTEXTPROVIDER_CID =
47     {0x0b666e3e,0x569a,0x462c,{0xa7,0xf0,0xb1,0x6b,0xb1,0x5d,0x42,0xff}};
48
49 static nsresult NSAPI nsWindowCreator_QueryInterface(nsIWindowCreator2 *iface, nsIIDRef riid,
50         void **result)
51 {
52     *result = NULL;
53
54     if(IsEqualGUID(&IID_nsISupports, riid)) {
55         TRACE("(IID_nsISupports %p)\n", result);
56         *result = iface;
57     }else if(IsEqualGUID(&IID_nsIWindowCreator, riid)) {
58         TRACE("(IID_nsIWindowCreator %p)\n", result);
59         *result = iface;
60     }else if(IsEqualGUID(&IID_nsIWindowCreator2, riid)) {
61         TRACE("(IID_nsIWindowCreator2 %p)\n", result);
62         *result = iface;
63     }
64
65     if(*result) {
66         nsIWindowCreator_AddRef(iface);
67         return NS_OK;
68     }
69
70     WARN("(%s %p)\n", debugstr_guid(riid), result);
71     return NS_NOINTERFACE;
72 }
73
74 static nsrefcnt NSAPI nsWindowCreator_AddRef(nsIWindowCreator2 *iface)
75 {
76     return 2;
77 }
78
79 static nsrefcnt NSAPI nsWindowCreator_Release(nsIWindowCreator2 *iface)
80 {
81     return 1;
82 }
83
84 static nsresult NSAPI nsWindowCreator_CreateChromeWindow(nsIWindowCreator2 *iface,
85         nsIWebBrowserChrome *parent, PRUint32 chromeFlags, nsIWebBrowserChrome **_retval)
86 {
87     TRACE("(%p %08x %p)\n", parent, chromeFlags, _retval);
88     return nsIWindowCreator2_CreateChromeWindow2(iface, parent, chromeFlags, 0, NULL,
89                                                  NULL, _retval);
90 }
91
92 static nsresult NSAPI nsWindowCreator_CreateChromeWindow2(nsIWindowCreator2 *iface,
93         nsIWebBrowserChrome *parent, PRUint32 chromeFlags, PRUint32 contextFlags,
94         nsIURI *uri, PRBool *cancel, nsIWebBrowserChrome **_retval)
95 {
96     TRACE("(%p %08x %08x %p %p %p)\n", parent, chromeFlags, contextFlags, uri,
97           cancel, _retval);
98
99     if(cancel)
100         *cancel = FALSE;
101
102     return create_chrome_window(parent, _retval);
103 }
104
105 static const nsIWindowCreator2Vtbl nsWindowCreatorVtbl = {
106     nsWindowCreator_QueryInterface,
107     nsWindowCreator_AddRef,
108     nsWindowCreator_Release,
109     nsWindowCreator_CreateChromeWindow,
110     nsWindowCreator_CreateChromeWindow2
111 };
112
113 static nsIWindowCreator2 nsWindowCreator = { &nsWindowCreatorVtbl };
114
115 static nsresult NSAPI nsPromptService_QueryInterface(nsIPromptService *iface,
116         nsIIDRef riid, void **result)
117 {
118     *result = NULL;
119
120     if(IsEqualGUID(&IID_nsISupports, riid)) {
121         TRACE("(IID_nsISupports %p)\n", result);
122         *result = iface;
123     }else if(IsEqualGUID(&IID_nsIPromptService, riid)) {
124         TRACE("(IID_nsIPromptService %p)\n", result);
125         *result = iface;
126     }
127
128     if(*result)
129         return NS_OK;
130
131     TRACE("(%s %p)\n", debugstr_guid(riid), result);
132     return NS_NOINTERFACE;
133 }
134
135 static nsrefcnt NSAPI nsPromptService_AddRef(nsIPromptService *iface)
136 {
137     return 2;
138 }
139
140 static nsrefcnt NSAPI nsPromptService_Release(nsIPromptService *iface)
141 {
142     return 1;
143 }
144
145 static nsresult NSAPI nsPromptService_Alert(nsIPromptService *iface, nsIDOMWindow *aParent,
146         const PRUnichar *aDialogTitle, const PRUnichar *aText)
147 {
148     HTMLWindow *window;
149     BSTR text;
150
151     TRACE("(%p %s %s)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText));
152
153     window = nswindow_to_window(aParent);
154     if(!window) {
155         WARN("Could not find HTMLWindow for nsIDOMWindow %p\n", aParent);
156         return NS_ERROR_UNEXPECTED;
157     }
158
159     text = SysAllocString(aText);
160     IHTMLWindow2_alert(&window->IHTMLWindow2_iface, text);
161     SysFreeString(text);
162
163     return NS_OK;
164 }
165
166 static nsresult NSAPI nsPromptService_AlertCheck(nsIPromptService *iface,
167         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
168         const PRUnichar *aText, const PRUnichar *aCheckMsg, PRBool *aCheckState)
169 {
170     FIXME("(%p %s %s %s %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
171           debugstr_w(aCheckMsg), aCheckState);
172     return NS_ERROR_NOT_IMPLEMENTED;
173 }
174
175 static nsresult NSAPI nsPromptService_Confirm(nsIPromptService *iface,
176         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle, const PRUnichar *aText,
177         PRBool *_retval)
178 {
179     FIXME("(%p %s %s %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText), _retval);
180     return NS_ERROR_NOT_IMPLEMENTED;
181 }
182
183 static nsresult NSAPI nsPromptService_ConfirmCheck(nsIPromptService *iface,
184         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
185         const PRUnichar *aText, const PRUnichar *aCheckMsg, PRBool *aCheckState,
186         PRBool *_retval)
187 {
188     FIXME("(%p %s %s %s %p %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
189         debugstr_w(aCheckMsg), aCheckState, _retval);
190     return NS_ERROR_NOT_IMPLEMENTED;
191 }
192
193 static nsresult NSAPI nsPromptService_ConfirmEx(nsIPromptService *iface,
194         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
195         const PRUnichar *aText, PRUint32 aButtonFlags, const PRUnichar *aButton0Title,
196         const PRUnichar *aButton1Title, const PRUnichar *aButton2Title,
197         const PRUnichar *aCheckMsg, PRBool *aCheckState, PRInt32 *_retval)
198 {
199     static const PRUnichar wszContinue[] = {'C','o','n','t','i','n','u','e',0};
200
201     FIXME("(%p %s %s %08x %s %s %s %s %p %p) hack!\n", aParent, debugstr_w(aDialogTitle),
202           debugstr_w(aText), aButtonFlags, debugstr_w(aButton0Title),
203           debugstr_w(aButton1Title), debugstr_w(aButton2Title), debugstr_w(aCheckMsg),
204           aCheckState, _retval);
205
206     /*
207      * FIXME:
208      * This is really very very ugly hack!!!
209      */
210
211     if(aButton0Title && !memcmp(aButton0Title, wszContinue, sizeof(wszContinue)))
212         *_retval = 0;
213     else if(aButton1Title && !memcmp(aButton1Title, wszContinue, sizeof(wszContinue)))
214         *_retval = 1;
215     else if(aButton2Title && !memcmp(aButton2Title, wszContinue, sizeof(wszContinue)))
216         *_retval = 2;
217     /* else
218      *     let's hope that _retval is set to the default value */
219
220     return NS_OK;
221 }
222
223 static nsresult NSAPI nsPromptService_Prompt(nsIPromptService *iface,
224         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
225         const PRUnichar *aText, PRUnichar **aValue, const PRUnichar *aCheckMsg,
226         PRBool *aCheckState, PRBool *_retval)
227 {
228     FIXME("(%p %s %s %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
229           aValue, debugstr_w(aCheckMsg), aCheckState, _retval);
230     return NS_ERROR_NOT_IMPLEMENTED;
231 }
232
233 static nsresult NSAPI nsPromptService_PromptUsernameAndPassword(nsIPromptService *iface,
234         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
235         const PRUnichar *aText, PRUnichar **aUsername, PRUnichar **aPassword,
236         const PRUnichar *aCheckMsg, PRBool *aCheckState, PRBool *_retval)
237 {
238     FIXME("(%p %s %s %p %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle),
239         debugstr_w(aText), aUsername, aPassword, debugstr_w(aCheckMsg), aCheckState,
240         _retval);
241     return NS_ERROR_NOT_IMPLEMENTED;
242 }
243
244 static nsresult NSAPI nsPromptService_PromptPassword(nsIPromptService *iface,
245         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
246         const PRUnichar *aText, PRUnichar **aPassword, const PRUnichar *aCheckMsg,
247         PRBool *aCheckState, PRBool *_retval)
248 {
249     FIXME("(%p %s %s %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle),
250           debugstr_w(aText), aPassword, debugstr_w(aCheckMsg), aCheckState, _retval);
251     return NS_ERROR_NOT_IMPLEMENTED;
252 }
253
254 static nsresult NSAPI nsPromptService_Select(nsIPromptService *iface,
255         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
256         const PRUnichar *aText, PRUint32 aCount, const PRUnichar **aSelectList,
257         PRInt32 *aOutSelection, PRBool *_retval)
258 {
259     FIXME("(%p %s %s %d %p %p %p)\n", aParent, debugstr_w(aDialogTitle),
260         debugstr_w(aText), aCount, aSelectList, aOutSelection, _retval);
261     return NS_ERROR_NOT_IMPLEMENTED;
262 }
263
264 static const nsIPromptServiceVtbl PromptServiceVtbl = {
265     nsPromptService_QueryInterface,
266     nsPromptService_AddRef,
267     nsPromptService_Release,
268     nsPromptService_Alert,
269     nsPromptService_AlertCheck,
270     nsPromptService_Confirm,
271     nsPromptService_ConfirmCheck,
272     nsPromptService_ConfirmEx,
273     nsPromptService_Prompt,
274     nsPromptService_PromptUsernameAndPassword,
275     nsPromptService_PromptPassword,
276     nsPromptService_Select
277 };
278
279 static nsIPromptService nsPromptService = { &PromptServiceVtbl };
280
281 static nsresult NSAPI nsTooltipTextProvider_QueryInterface(nsITooltipTextProvider *iface,
282         nsIIDRef riid, void **result)
283 {
284     *result = NULL;
285
286     if(IsEqualGUID(&IID_nsISupports, riid)) {
287         TRACE("(IID_nsISupports %p)\n", result);
288         *result = iface;
289     }else if(IsEqualGUID(&IID_nsITooltipTextProvider, riid)) {
290         TRACE("(IID_nsITooltipTextProvider %p)\n", result);
291         *result = iface;
292     }
293
294     if(*result) {
295         nsITooltipTextProvider_AddRef(iface);
296         return NS_OK;
297     }
298
299     WARN("(%s %p)\n", debugstr_guid(riid), result);
300     return NS_NOINTERFACE;
301 }
302
303 static nsrefcnt NSAPI nsTooltipTextProvider_AddRef(nsITooltipTextProvider *iface)
304 {
305     return 2;
306 }
307
308 static nsrefcnt NSAPI nsTooltipTextProvider_Release(nsITooltipTextProvider *iface)
309 {
310     return 1;
311 }
312
313 static nsresult NSAPI nsTooltipTextProvider_GetNodeText(nsITooltipTextProvider *iface,
314         nsIDOMNode *aNode, PRUnichar **aText, PRBool *_retval)
315 {
316     nsIDOMHTMLElement *nselem;
317     nsIDOMNode *node = aNode, *parent;
318     nsAString title_str;
319     const PRUnichar *title = NULL;
320     nsresult nsres;
321
322     TRACE("(%p %p %p)\n", aNode, aText, _retval);
323
324     *aText = NULL;
325
326     nsAString_Init(&title_str, NULL);
327
328     do {
329         nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMHTMLElement, (void**)&nselem);
330         if(NS_SUCCEEDED(nsres)) {
331             title = NULL;
332
333             nsIDOMHTMLElement_GetTitle(nselem, &title_str);
334             nsIDOMHTMLElement_Release(nselem);
335
336             nsAString_GetData(&title_str, &title);
337             if(title && *title) {
338                 if(node != aNode)
339                     nsIDOMNode_Release(node);
340                 break;
341             }
342         }
343
344         nsres = nsIDOMNode_GetParentNode(node, &parent);
345         if(NS_FAILED(nsres))
346             parent = NULL;
347
348         if(node != aNode)
349             nsIDOMNode_Release(node);
350         node = parent;
351     } while(node);
352
353     if(title && *title) {
354         int size = (strlenW(title)+1)*sizeof(PRUnichar);
355
356         *aText = nsalloc(size);
357         memcpy(*aText, title, size);
358         TRACE("aText = %s\n", debugstr_w(*aText));
359
360         *_retval = TRUE;
361     }else {
362         *_retval = FALSE;
363     }
364
365     nsAString_Finish(&title_str);
366
367     return NS_OK;
368 }
369
370 static const nsITooltipTextProviderVtbl nsTooltipTextProviderVtbl = {
371     nsTooltipTextProvider_QueryInterface,
372     nsTooltipTextProvider_AddRef,
373     nsTooltipTextProvider_Release,
374     nsTooltipTextProvider_GetNodeText
375 };
376
377 static nsITooltipTextProvider nsTooltipTextProvider = { &nsTooltipTextProviderVtbl };
378
379 typedef struct {
380     const nsIFactoryVtbl *lpFactoryVtbl;
381     nsISupports *service;
382 } nsServiceFactory;
383
384 #define NSFACTORY(x)  ((nsIFactory*)  &(x)->lpFactoryVtbl)
385
386 #define NSFACTORY_THIS(iface) DEFINE_THIS(nsServiceFactory, Factory, iface)
387
388 static nsresult NSAPI nsServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
389         void **result)
390 {
391     nsServiceFactory *This = NSFACTORY_THIS(iface);
392
393     *result = NULL;
394
395     if(IsEqualGUID(&IID_nsISupports, riid)) {
396         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
397         *result = NSFACTORY(This);
398     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
399         TRACE("(%p)->(IID_nsIFactory %p)\n", This, result);
400         *result = NSFACTORY(This);
401     }
402
403     if(*result)
404         return NS_OK;
405
406     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
407     return NS_NOINTERFACE;
408 }
409
410 static nsrefcnt NSAPI nsServiceFactory_AddRef(nsIFactory *iface)
411 {
412     return 2;
413 }
414
415 static nsrefcnt NSAPI nsServiceFactory_Release(nsIFactory *iface)
416 {
417     return 1;
418 }
419
420 static nsresult NSAPI nsServiceFactory_CreateInstance(nsIFactory *iface,
421         nsISupports *aOuter, const nsIID *iid, void **result)
422 {
423     nsServiceFactory *This = NSFACTORY_THIS(iface);
424
425     TRACE("(%p)->(%p %s %p)\n", This, aOuter, debugstr_guid(iid), result);
426
427     return nsISupports_QueryInterface(This->service, iid, result);
428 }
429
430 static nsresult NSAPI nsServiceFactory_LockFactory(nsIFactory *iface, PRBool lock)
431 {
432     nsServiceFactory *This = NSFACTORY_THIS(iface);
433     WARN("(%p)->(%x)\n", This, lock);
434     return NS_OK;
435 }
436
437 #undef NSFACTORY_THIS
438
439 static const nsIFactoryVtbl nsServiceFactoryVtbl = {
440     nsServiceFactory_QueryInterface,
441     nsServiceFactory_AddRef,
442     nsServiceFactory_Release,
443     nsServiceFactory_CreateInstance,
444     nsServiceFactory_LockFactory
445 };
446
447 static nsServiceFactory nsPromptServiceFactory = {
448     &nsServiceFactoryVtbl,
449     (nsISupports*)&nsPromptService
450 };
451
452 static nsServiceFactory nsTooltipTextFactory = {
453     &nsServiceFactoryVtbl,
454     (nsISupports*)&nsTooltipTextProvider
455 };
456
457 void register_nsservice(nsIComponentRegistrar *registrar, nsIServiceManager *service_manager)
458 {
459     nsIWindowWatcher *window_watcher;
460     nsresult nsres;
461
462     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_PROMPTSERVICE_CID,
463             "Prompt Service", NS_PROMPTSERVICE_CONTRACTID, NSFACTORY(&nsPromptServiceFactory));
464     if(NS_FAILED(nsres))
465         ERR("RegisterFactory failed: %08x\n", nsres);
466
467     nsres = nsIServiceManager_GetServiceByContractID(service_manager, NS_WINDOWWATCHER_CONTRACTID,
468             &IID_nsIWindowWatcher, (void**)&window_watcher);
469     if(NS_SUCCEEDED(nsres)) {
470         nsres = nsIWindowWatcher_SetWindowCreator(window_watcher,
471                                                   (nsIWindowCreator*)&nsWindowCreator);
472         if(NS_FAILED(nsres))
473             ERR("SetWindowCreator failed: %08x\n", nsres);
474         nsIWindowWatcher_Release(window_watcher);
475     }else {
476         ERR("Could not get WindowWatcher object: %08x\n", nsres);
477     }
478
479     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_TOOLTIPTEXTPROVIDER_CID,
480             NS_TOOLTIPTEXTPROVIDER_CLASSNAME, NS_TOOLTIPTEXTPROVIDER_CONTRACTID,
481             NSFACTORY(&nsTooltipTextFactory));
482     if(NS_FAILED(nsres))
483         ERR("RegisterFactory failed: %08x\n", nsres);
484 }