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