Moved mciavi32 to the top-level dlls directory.
[wine] / dlls / mshtml / nsembed.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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_GUID(CLSID_StartupNotifier, 0x1f59b001,0x02c9,0x11d5,0xae,0x76,0xcc,0x92,0xf7,0xdb,0x9e,0x03);
39 DEFINE_GUID(CLSID_nsWebBrowser, 0xf1eac761,0x87e9,0x11d3,0xaf,0x80,0x00,0xa0,0x24,0xff,0xc0,0x8c);
40
41 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
42 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
43 #define NS_IOSERVICE_CONTRACTID "@mozilla.org/network/io-service;1"
44
45 #define APPSTARTUP_TOPIC "app-startup"
46
47 #define PR_UINT32_MAX 0xffffffff
48
49 typedef struct nsACString {
50     void *d1;
51     PRUint32 d2;
52     void *d3;
53 } nsString;
54
55 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
56 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
57 static nsresult (*NS_StringContainerInit)(nsString*);
58 static nsresult (*NS_CStringContainerInit)(nsACString*);
59 static nsresult (*NS_StringContainerFinish)(nsString*);
60 static nsresult (*NS_CStringContainerFinish)(nsACString*);
61 static nsresult (*NS_StringSetData)(nsString*,const PRUnichar*,PRUint32);
62 static nsresult (*NS_CStringSetData)(nsString*,const char*,PRUint32);
63 static nsresult (*NS_NewLocalFile)(const nsString*,PRBool,nsIFile**);
64
65 static HINSTANCE hXPCOM = NULL;
66
67 static nsIServiceManager *pServMgr = NULL;
68 static nsIComponentManager *pCompMgr = NULL;
69 static nsIIOService *pIOService = NULL;
70
71 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
72
73 static ATOM nscontainer_class;
74
75 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
76 {
77     HTMLDocument *This;
78     nsresult nsres;
79
80     static const WCHAR wszTHIS[] = {'T','H','I','S',0};
81
82     if(msg == WM_CREATE) {
83         This = *(HTMLDocument**)lParam;
84         SetPropW(hwnd, wszTHIS, This);
85     }else {
86         This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
87     }
88
89     switch(msg) {
90         case WM_SIZE:
91             TRACE("(%p)->(WM_SIZE)\n", This);
92
93             nsres = nsIBaseWindow_SetSize(This->nscontainer->window,
94                     LOWORD(lParam), HIWORD(lParam), TRUE);
95             if(NS_FAILED(nsres))
96                 WARN("SetSize failed: %08lx\n", nsres);
97     }
98
99     return DefWindowProcW(hwnd, msg, wParam, lParam);
100 }
101
102
103 static void register_nscontainer_class(void)
104 {
105     static WNDCLASSEXW wndclass = {
106         sizeof(WNDCLASSEXW),
107         CS_DBLCLKS,
108         nsembed_proc,
109         0, 0, NULL, NULL, NULL, NULL, NULL,
110         wszNsContainer,
111         NULL,
112     };
113     wndclass.hInstance = hInst;
114     nscontainer_class = RegisterClassExW(&wndclass);
115 }
116
117 static BOOL get_mozilla_path(PRUnichar *gre_path)
118 {
119     DWORD res, type, i, size = MAX_PATH;
120     HKEY mozilla_key, hkey;
121     WCHAR key_name[100];
122     BOOL ret = FALSE;
123
124     static const WCHAR wszGreKey[] =
125         {'S','o','f','t','w','a','r','e','\\',
126             'm','o','z','i','l','l','a','.','o','r','g','\\',
127                 'G','R','E',0};
128
129     static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
130
131     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
132     if(res != ERROR_SUCCESS) {
133         TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
134         return FALSE;
135     }
136
137     for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
138         RegOpenKeyW(mozilla_key, key_name, &hkey);
139         res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
140         if(res == ERROR_SUCCESS)
141             ret = TRUE;
142         RegCloseKey(hkey);
143     }
144
145     RegCloseKey(mozilla_key);
146     return ret;
147 }
148
149 static BOOL get_mozctl_path(PRUnichar *gre_path)
150 {
151     HKEY hkey;
152     DWORD res, type, size = MAX_PATH;
153
154     static const WCHAR wszMozCtlKey[] =
155         {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
156     static const WCHAR wszBinDirectoryPath[] =
157         {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
158
159     res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
160     if(res != ERROR_SUCCESS) {
161         TRACE("Could not open key %s\n", debugstr_w(wszMozCtlKey));
162         return FALSE;
163     }
164
165     res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
166     if(res != ERROR_SUCCESS) {
167         ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
168         return FALSE;
169     }
170
171     return TRUE;
172 }
173
174 static BOOL load_gecko()
175 {
176     nsresult nsres;
177     nsIObserver *pStartNotif;
178     nsString path;
179     nsIFile *gre_dir;
180     PRUnichar gre_path[MAX_PATH];
181     WCHAR path_env[MAX_PATH];
182     int len;
183
184     static BOOL tried_load = FALSE;
185     static const WCHAR wszPATH[] = {'P','A','T','H',0};
186     static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
187
188     TRACE("()\n");
189
190     if(tried_load)
191         return pCompMgr != NULL;
192     tried_load = TRUE;
193
194     if(!get_mozctl_path(gre_path) && !get_mozilla_path(gre_path)) {
195         MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
196         return FALSE;
197     }
198
199     TRACE("found path %s\n", debugstr_w(gre_path));
200
201     /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
202     GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
203     len = strlenW(path_env);
204     path_env[len++] = ';';
205     strcpyW(path_env+len, gre_path);
206     SetEnvironmentVariableW(wszPATH, path_env);
207
208     hXPCOM = LoadLibraryW(strXPCOM);
209     if(!hXPCOM) {
210         ERR("Could not load XPCOM: %ld\n", GetLastError());
211         return FALSE;
212     }
213
214 #define NS_DLSYM(func) \
215     func = (typeof(func))GetProcAddress(hXPCOM, #func); \
216     if(!func) \
217         ERR("Could not GetProcAddress(" #func ") failed\n")
218
219     NS_DLSYM(NS_InitXPCOM2);
220     NS_DLSYM(NS_ShutdownXPCOM);
221     NS_DLSYM(NS_StringContainerInit);
222     NS_DLSYM(NS_CStringContainerInit);
223     NS_DLSYM(NS_StringContainerFinish);
224     NS_DLSYM(NS_CStringContainerFinish);
225     NS_DLSYM(NS_StringSetData);
226     NS_DLSYM(NS_CStringSetData);
227     NS_DLSYM(NS_NewLocalFile);
228
229 #undef NS_DLSYM
230
231     NS_StringContainerInit(&path);
232     NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
233     nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
234     NS_StringContainerFinish(&path);
235     if(NS_FAILED(nsres)) {
236         ERR("NS_NewLocalFile failed: %08lx\n", nsres);
237         FreeLibrary(hXPCOM);
238         return FALSE;
239     }
240
241     nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
242     if(NS_FAILED(nsres)) {
243         ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
244         FreeLibrary(hXPCOM);
245         return FALSE;
246     }
247
248     nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
249     if(NS_FAILED(nsres))
250         ERR("Could not get nsIComponentManager: %08lx\n", nsres);
251
252     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
253             NULL, &IID_nsIObserver, (void**)&pStartNotif);
254     if(NS_SUCCEEDED(nsres)) {
255         nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
256         if(NS_FAILED(nsres))
257             ERR("Observe failed: %08lx\n", nsres);
258
259         nsIObserver_Release(pStartNotif);
260     }else {
261         ERR("could not get appstartup-notifier: %08lx\n", nsres);
262     }
263
264     return TRUE;
265 }
266
267 nsACString *nsACString_Create(void)
268 {
269     nsACString *ret;
270     ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsACString));
271     NS_CStringContainerInit(ret);
272     return ret;
273 }
274
275 void nsACString_SetData(nsACString *str, const char *data)
276 {
277     NS_CStringSetData(str, data, PR_UINT32_MAX);
278 }
279
280 void nsACString_Destroy(nsACString *str)
281 {
282     NS_CStringContainerFinish(str);
283     HeapFree(GetProcessHeap(), 0, str);
284 }
285
286 void close_gecko()
287 {
288     TRACE("()\n");
289
290     if(pCompMgr)
291         nsIComponentManager_Release(pCompMgr);
292
293     if(pServMgr)
294         nsIServiceManager_Release(pServMgr);
295
296     if(hXPCOM)
297         FreeLibrary(hXPCOM);
298 }
299
300 nsIURI *get_nsIURI(LPCWSTR url)
301 {
302     nsIURI *ret;
303     nsACString *acstr;
304     nsresult nsres;
305     char *urla;
306     int len;
307
308     if(!pIOService) {
309         nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_IOSERVICE_CONTRACTID,
310                 &IID_nsIIOService, (void**)&pIOService);
311         if(NS_FAILED(nsres))
312             ERR("Failed to create nsIOService: %08lx\n", nsres);
313     }
314
315     len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
316     urla = HeapAlloc(GetProcessHeap(), 0, len);
317     WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
318
319     acstr = nsACString_Create();
320     nsACString_SetData(acstr, urla);
321
322     nsres = nsIIOService_NewURI(pIOService, acstr, NULL, NULL, &ret);
323     if(NS_FAILED(nsres))
324         FIXME("NewURI failed: %08lx\n", nsres);
325
326     nsACString_Destroy(acstr);
327     HeapFree(GetProcessHeap(), 0, urla);
328
329     return ret;
330 }
331
332 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
333 {
334     nsIWebBrowserSetup *wbsetup;
335     nsresult nsres;
336
337     This->nscontainer = NULL;
338
339     if(!load_gecko())
340         return;
341
342     This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
343
344     nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
345             NULL, &IID_nsIWebBrowser, (void**)&This->nscontainer->webbrowser);
346     if(NS_FAILED(nsres))
347         ERR("Creating WebBrowser failed: %08lx\n", nsres);
348
349     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIBaseWindow,
350             (void**)&This->nscontainer->window);
351     if(NS_FAILED(nsres))
352         ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
353
354     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser,
355             &IID_nsIWebBrowserSetup, (void**)&wbsetup);
356     if(NS_SUCCEEDED(nsres)) {
357         nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
358         if(NS_FAILED(nsres))
359             ERR("SetProperty failed: %08lx\n", nsres);
360         nsIWebBrowserSetup_Release(wbsetup);
361     }else {
362         ERR("Could not get nsIWebBrowserSetup interface\n");
363     }
364
365     nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebNavigation,
366             (void**)&This->nscontainer->navigation);
367     if(NS_FAILED(nsres))
368         ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
369
370 #if 0
371     nsres = nsIWebBrowserStream_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebBrowserStream,
372             (void**)&This->nscontainer->stream);
373     if(NS_FAILED(nsres))
374         ERR("Could not get nsIWebBrowserStream interface: %08lx\n", nsres);
375 #else
376     This->nscontainer->stream = NULL;
377 #endif
378
379     if(!nscontainer_class)
380         register_nscontainer_class();
381
382     This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
383             WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
384             GetDesktopWindow(), NULL, hInst, This);
385
386     nsres = nsIBaseWindow_InitWindow(This->nscontainer->window, This->nscontainer->hwnd, NULL,
387             0, 0, 100, 100);
388     if(NS_SUCCEEDED(nsres)) {
389         nsres = nsIBaseWindow_Create(This->nscontainer->window);
390         if(NS_FAILED(nsres))
391             WARN("Creating window failed: %08lx\n", nsres);
392
393         nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
394         nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
395     }else {
396         ERR("InitWindow failed: %08lx\n", nsres);
397     }
398 }
399
400 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
401 {
402     TRACE("(%p)\n", This);
403
404     nsIWebBrowser_Release(This->nscontainer->webbrowser);
405     nsIWebNavigation_Release(This->nscontainer->navigation);
406     nsIBaseWindow_Release(This->nscontainer->window);
407
408     if(This->nscontainer->stream)
409         nsIWebBrowserStream_Release(This->nscontainer->stream);
410
411     HeapFree(GetProcessHeap(), 0, This->nscontainer);
412 }