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