mshtml: Added noscript tag handling tests.
[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
33 #include "mshtml_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36
37 #define NS_PROMPTSERVICE_CONTRACTID "@mozilla.org/embedcomp/prompt-service;1"
38 #define NS_TOOLTIPTEXTPROVIDER_CONTRACTID "@mozilla.org/embedcomp/tooltiptextprovider;1"
39
40 #define NS_TOOLTIPTEXTPROVIDER_CLASSNAME "nsTooltipTextProvider"
41
42 static const nsIID NS_PROMPTSERVICE_CID =
43     {0xa2112d6a,0x0e28,0x421f,{0xb4,0x6a,0x25,0xc0,0xb3,0x8,0xcb,0xd0}};
44 static const nsIID NS_TOOLTIPTEXTPROVIDER_CID =
45     {0x0b666e3e,0x569a,0x462c,{0xa7,0xf0,0xb1,0x6b,0xb1,0x5d,0x42,0xff}};
46
47 static nsresult NSAPI nsPromptService_QueryInterface(nsIPromptService *iface,
48         nsIIDRef riid, void **result)
49 {
50     *result = NULL;
51
52     if(IsEqualGUID(&IID_nsISupports, riid)) {
53         TRACE("(IID_nsISupports %p)\n", result);
54         *result = iface;
55     }else if(IsEqualGUID(&IID_nsIPromptService, riid)) {
56         TRACE("(IID_nsIPromptService %p)\n", result);
57         *result = iface;
58     }
59
60     if(*result)
61         return NS_OK;
62
63     TRACE("(%s %p)\n", debugstr_guid(riid), result);
64     return NS_NOINTERFACE;
65 }
66
67 static nsrefcnt NSAPI nsPromptService_AddRef(nsIPromptService *iface)
68 {
69     return 2;
70 }
71
72 static nsrefcnt NSAPI nsPromptService_Release(nsIPromptService *iface)
73 {
74     return 1;
75 }
76
77 static nsresult NSAPI nsPromptService_Alert(nsIPromptService *iface, nsIDOMWindow *aParent,
78         const PRUnichar *aDialogTitle, const PRUnichar *aText)
79 {
80     HTMLOuterWindow *window;
81     BSTR text;
82
83     TRACE("(%p %s %s)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText));
84
85     window = nswindow_to_window(aParent);
86     if(!window) {
87         WARN("Could not find HTMLWindow for nsIDOMWindow %p\n", aParent);
88         return NS_ERROR_UNEXPECTED;
89     }
90
91     text = SysAllocString(aText);
92     IHTMLWindow2_alert(&window->base.IHTMLWindow2_iface, text);
93     SysFreeString(text);
94
95     return NS_OK;
96 }
97
98 static nsresult NSAPI nsPromptService_AlertCheck(nsIPromptService *iface,
99         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
100         const PRUnichar *aText, const PRUnichar *aCheckMsg, cpp_bool *aCheckState)
101 {
102     FIXME("(%p %s %s %s %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
103           debugstr_w(aCheckMsg), aCheckState);
104     return NS_ERROR_NOT_IMPLEMENTED;
105 }
106
107 static nsresult NSAPI nsPromptService_Confirm(nsIPromptService *iface,
108         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle, const PRUnichar *aText,
109         cpp_bool *_retval)
110 {
111     FIXME("(%p %s %s %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText), _retval);
112     return NS_ERROR_NOT_IMPLEMENTED;
113 }
114
115 static nsresult NSAPI nsPromptService_ConfirmCheck(nsIPromptService *iface,
116         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
117         const PRUnichar *aText, const PRUnichar *aCheckMsg, cpp_bool *aCheckState,
118         cpp_bool *_retval)
119 {
120     FIXME("(%p %s %s %s %p %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
121         debugstr_w(aCheckMsg), aCheckState, _retval);
122     return NS_ERROR_NOT_IMPLEMENTED;
123 }
124
125 static nsresult NSAPI nsPromptService_ConfirmEx(nsIPromptService *iface,
126         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
127         const PRUnichar *aText, PRUint32 aButtonFlags, const PRUnichar *aButton0Title,
128         const PRUnichar *aButton1Title, const PRUnichar *aButton2Title,
129         const PRUnichar *aCheckMsg, cpp_bool *aCheckState, PRInt32 *_retval)
130 {
131     static const PRUnichar wszContinue[] = {'C','o','n','t','i','n','u','e',0};
132
133     FIXME("(%p %s %s %08x %s %s %s %s %p %p) hack!\n", aParent, debugstr_w(aDialogTitle),
134           debugstr_w(aText), aButtonFlags, debugstr_w(aButton0Title),
135           debugstr_w(aButton1Title), debugstr_w(aButton2Title), debugstr_w(aCheckMsg),
136           aCheckState, _retval);
137
138     /*
139      * FIXME:
140      * This is really very very ugly hack!!!
141      */
142
143     if(aButton0Title && !memcmp(aButton0Title, wszContinue, sizeof(wszContinue)))
144         *_retval = 0;
145     else if(aButton1Title && !memcmp(aButton1Title, wszContinue, sizeof(wszContinue)))
146         *_retval = 1;
147     else if(aButton2Title && !memcmp(aButton2Title, wszContinue, sizeof(wszContinue)))
148         *_retval = 2;
149     /* else
150      *     let's hope that _retval is set to the default value */
151
152     return NS_OK;
153 }
154
155 static nsresult NSAPI nsPromptService_Prompt(nsIPromptService *iface,
156         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
157         const PRUnichar *aText, PRUnichar **aValue, const PRUnichar *aCheckMsg,
158         cpp_bool *aCheckState, cpp_bool *_retval)
159 {
160     FIXME("(%p %s %s %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle), debugstr_w(aText),
161           aValue, debugstr_w(aCheckMsg), aCheckState, _retval);
162     return NS_ERROR_NOT_IMPLEMENTED;
163 }
164
165 static nsresult NSAPI nsPromptService_PromptUsernameAndPassword(nsIPromptService *iface,
166         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
167         const PRUnichar *aText, PRUnichar **aUsername, PRUnichar **aPassword,
168         const PRUnichar *aCheckMsg, cpp_bool *aCheckState, cpp_bool *_retval)
169 {
170     FIXME("(%p %s %s %p %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle),
171         debugstr_w(aText), aUsername, aPassword, debugstr_w(aCheckMsg), aCheckState,
172         _retval);
173     return NS_ERROR_NOT_IMPLEMENTED;
174 }
175
176 static nsresult NSAPI nsPromptService_PromptPassword(nsIPromptService *iface,
177         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
178         const PRUnichar *aText, PRUnichar **aPassword, const PRUnichar *aCheckMsg,
179         cpp_bool *aCheckState, cpp_bool *_retval)
180 {
181     FIXME("(%p %s %s %p %s %p %p)\n", aParent, debugstr_w(aDialogTitle),
182           debugstr_w(aText), aPassword, debugstr_w(aCheckMsg), aCheckState, _retval);
183     return NS_ERROR_NOT_IMPLEMENTED;
184 }
185
186 static nsresult NSAPI nsPromptService_Select(nsIPromptService *iface,
187         nsIDOMWindow *aParent, const PRUnichar *aDialogTitle,
188         const PRUnichar *aText, PRUint32 aCount, const PRUnichar **aSelectList,
189         PRInt32 *aOutSelection, cpp_bool *_retval)
190 {
191     FIXME("(%p %s %s %d %p %p %p)\n", aParent, debugstr_w(aDialogTitle),
192         debugstr_w(aText), aCount, aSelectList, aOutSelection, _retval);
193     return NS_ERROR_NOT_IMPLEMENTED;
194 }
195
196 static const nsIPromptServiceVtbl PromptServiceVtbl = {
197     nsPromptService_QueryInterface,
198     nsPromptService_AddRef,
199     nsPromptService_Release,
200     nsPromptService_Alert,
201     nsPromptService_AlertCheck,
202     nsPromptService_Confirm,
203     nsPromptService_ConfirmCheck,
204     nsPromptService_ConfirmEx,
205     nsPromptService_Prompt,
206     nsPromptService_PromptUsernameAndPassword,
207     nsPromptService_PromptPassword,
208     nsPromptService_Select
209 };
210
211 static nsIPromptService nsPromptService = { &PromptServiceVtbl };
212
213 static nsresult NSAPI nsTooltipTextProvider_QueryInterface(nsITooltipTextProvider *iface,
214         nsIIDRef riid, void **result)
215 {
216     *result = NULL;
217
218     if(IsEqualGUID(&IID_nsISupports, riid)) {
219         TRACE("(IID_nsISupports %p)\n", result);
220         *result = iface;
221     }else if(IsEqualGUID(&IID_nsITooltipTextProvider, riid)) {
222         TRACE("(IID_nsITooltipTextProvider %p)\n", result);
223         *result = iface;
224     }
225
226     if(*result) {
227         nsITooltipTextProvider_AddRef(iface);
228         return NS_OK;
229     }
230
231     WARN("(%s %p)\n", debugstr_guid(riid), result);
232     return NS_NOINTERFACE;
233 }
234
235 static nsrefcnt NSAPI nsTooltipTextProvider_AddRef(nsITooltipTextProvider *iface)
236 {
237     return 2;
238 }
239
240 static nsrefcnt NSAPI nsTooltipTextProvider_Release(nsITooltipTextProvider *iface)
241 {
242     return 1;
243 }
244
245 static nsresult NSAPI nsTooltipTextProvider_GetNodeText(nsITooltipTextProvider *iface,
246         nsIDOMNode *aNode, PRUnichar **aText, cpp_bool *_retval)
247 {
248     nsIDOMHTMLElement *nselem;
249     nsIDOMNode *node = aNode, *parent;
250     nsAString title_str;
251     const PRUnichar *title = NULL;
252     nsresult nsres;
253
254     TRACE("(%p %p %p)\n", aNode, aText, _retval);
255
256     *aText = NULL;
257
258     nsAString_Init(&title_str, NULL);
259
260     do {
261         nsres = nsIDOMNode_QueryInterface(node, &IID_nsIDOMHTMLElement, (void**)&nselem);
262         if(NS_SUCCEEDED(nsres)) {
263             title = NULL;
264
265             nsIDOMHTMLElement_GetTitle(nselem, &title_str);
266             nsIDOMHTMLElement_Release(nselem);
267
268             nsAString_GetData(&title_str, &title);
269             if(title && *title) {
270                 if(node != aNode)
271                     nsIDOMNode_Release(node);
272                 break;
273             }
274         }
275
276         nsres = nsIDOMNode_GetParentNode(node, &parent);
277         if(NS_FAILED(nsres))
278             parent = NULL;
279
280         if(node != aNode)
281             nsIDOMNode_Release(node);
282         node = parent;
283     } while(node);
284
285     if(title && *title) {
286         int size = (strlenW(title)+1)*sizeof(PRUnichar);
287
288         *aText = nsalloc(size);
289         memcpy(*aText, title, size);
290         TRACE("aText = %s\n", debugstr_w(*aText));
291
292         *_retval = TRUE;
293     }else {
294         *_retval = FALSE;
295     }
296
297     nsAString_Finish(&title_str);
298
299     return NS_OK;
300 }
301
302 static const nsITooltipTextProviderVtbl nsTooltipTextProviderVtbl = {
303     nsTooltipTextProvider_QueryInterface,
304     nsTooltipTextProvider_AddRef,
305     nsTooltipTextProvider_Release,
306     nsTooltipTextProvider_GetNodeText
307 };
308
309 static nsITooltipTextProvider nsTooltipTextProvider = { &nsTooltipTextProviderVtbl };
310
311 typedef struct {
312     nsIFactory nsIFactory_iface;
313     nsISupports *service;
314 } nsServiceFactory;
315
316 static inline nsServiceFactory *impl_from_nsIFactory(nsIFactory *iface)
317 {
318     return CONTAINING_RECORD(iface, nsServiceFactory, nsIFactory_iface);
319 }
320
321 static nsresult NSAPI nsServiceFactory_QueryInterface(nsIFactory *iface, nsIIDRef riid,
322         void **result)
323 {
324     nsServiceFactory *This = impl_from_nsIFactory(iface);
325
326     *result = NULL;
327
328     if(IsEqualGUID(&IID_nsISupports, riid)) {
329         TRACE("(%p)->(IID_nsISupports %p)\n", This, result);
330         *result = &This->nsIFactory_iface;
331     }else if(IsEqualGUID(&IID_nsIFactory, riid)) {
332         TRACE("(%p)->(IID_nsIFactory %p)\n", This, result);
333         *result = &This->nsIFactory_iface;
334     }
335
336     if(*result)
337         return NS_OK;
338
339     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), result);
340     return NS_NOINTERFACE;
341 }
342
343 static nsrefcnt NSAPI nsServiceFactory_AddRef(nsIFactory *iface)
344 {
345     return 2;
346 }
347
348 static nsrefcnt NSAPI nsServiceFactory_Release(nsIFactory *iface)
349 {
350     return 1;
351 }
352
353 static nsresult NSAPI nsServiceFactory_CreateInstance(nsIFactory *iface,
354         nsISupports *aOuter, const nsIID *iid, void **result)
355 {
356     nsServiceFactory *This = impl_from_nsIFactory(iface);
357
358     TRACE("(%p)->(%p %s %p)\n", This, aOuter, debugstr_guid(iid), result);
359
360     return nsISupports_QueryInterface(This->service, iid, result);
361 }
362
363 static nsresult NSAPI nsServiceFactory_LockFactory(nsIFactory *iface, cpp_bool lock)
364 {
365     nsServiceFactory *This = impl_from_nsIFactory(iface);
366     WARN("(%p)->(%x)\n", This, lock);
367     return NS_OK;
368 }
369
370 static const nsIFactoryVtbl nsServiceFactoryVtbl = {
371     nsServiceFactory_QueryInterface,
372     nsServiceFactory_AddRef,
373     nsServiceFactory_Release,
374     nsServiceFactory_CreateInstance,
375     nsServiceFactory_LockFactory
376 };
377
378 static nsServiceFactory nsPromptServiceFactory = {
379     { &nsServiceFactoryVtbl },
380     (nsISupports*)&nsPromptService
381 };
382
383 static nsServiceFactory nsTooltipTextFactory = {
384     { &nsServiceFactoryVtbl },
385     (nsISupports*)&nsTooltipTextProvider
386 };
387
388 void register_nsservice(nsIComponentRegistrar *registrar, nsIServiceManager *service_manager)
389 {
390     nsresult nsres;
391
392     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_PROMPTSERVICE_CID,
393             "Prompt Service", NS_PROMPTSERVICE_CONTRACTID, &nsPromptServiceFactory.nsIFactory_iface);
394     if(NS_FAILED(nsres))
395         ERR("RegisterFactory failed: %08x\n", nsres);
396
397     nsres = nsIComponentRegistrar_RegisterFactory(registrar, &NS_TOOLTIPTEXTPROVIDER_CID,
398             NS_TOOLTIPTEXTPROVIDER_CLASSNAME, NS_TOOLTIPTEXTPROVIDER_CONTRACTID,
399             &nsTooltipTextFactory.nsIFactory_iface);
400     if(NS_FAILED(nsres))
401         ERR("RegisterFactory failed: %08x\n", nsres);
402 }