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