Remove trailing backslash.
[wine.git] / dlls / mshtml / nsembed.c
blob641e89abbe196e73d3654fa71639e562f0a74d3f
1 /*
2 * Copyright 2005 Jacek Caban
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.
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.
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
19 #include "config.h"
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "ole2.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
34 #include "mshtml_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
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);
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"
45 #define APPSTARTUP_TOPIC "app-startup"
47 #define PR_UINT32_MAX 0xffffffff
49 typedef struct nsACString {
50 void *d1;
51 PRUint32 d2;
52 void *d3;
53 } nsString;
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**);
65 static HINSTANCE hXPCOM = NULL;
67 static nsIServiceManager *pServMgr = NULL;
68 static nsIComponentManager *pCompMgr = NULL;
69 static nsIIOService *pIOService = NULL;
71 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
73 static ATOM nscontainer_class;
75 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
77 HTMLDocument *This;
78 nsresult nsres;
80 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
82 if(msg == WM_CREATE) {
83 This = *(HTMLDocument**)lParam;
84 SetPropW(hwnd, wszTHIS, This);
85 }else {
86 This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
89 switch(msg) {
90 case WM_SIZE:
91 TRACE("(%p)->(WM_SIZE)\n", This);
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);
99 return DefWindowProcW(hwnd, msg, wParam, lParam);
103 static void register_nscontainer_class(void)
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,
113 wndclass.hInstance = hInst;
114 nscontainer_class = RegisterClassExW(&wndclass);
117 static BOOL get_mozilla_path(PRUnichar *gre_path)
119 DWORD res, type, i, size = MAX_PATH;
120 HKEY mozilla_key, hkey;
121 WCHAR key_name[100];
122 BOOL ret = FALSE;
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};
129 static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
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;
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);
145 RegCloseKey(mozilla_key);
146 return ret;
149 static BOOL get_mozctl_path(PRUnichar *gre_path)
151 HKEY hkey;
152 DWORD res, type, size = MAX_PATH;
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};
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;
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;
171 return TRUE;
174 static BOOL load_gecko()
176 nsresult nsres;
177 nsIObserver *pStartNotif;
178 nsString path;
179 nsIFile *gre_dir;
180 PRUnichar gre_path[MAX_PATH];
182 static BOOL tried_load = FALSE;
183 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
185 TRACE("()\n");
187 if(tried_load)
188 return pCompMgr != NULL;
189 tried_load = TRUE;
191 if(!get_mozctl_path(gre_path) && !get_mozilla_path(gre_path)) {
192 MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
193 return FALSE;
196 TRACE("found path %s\n", debugstr_w(gre_path));
198 hXPCOM = LoadLibraryW(strXPCOM);
199 if(!hXPCOM) {
200 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
201 WCHAR path_env[MAX_PATH];
202 static WCHAR wszPATH[] = {'P','A','T','H',0};
203 int len;
205 GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
206 len = strlenW(path_env);
207 path_env[len++] = ';';
208 strcpyW(path_env+len, gre_path);
209 SetEnvironmentVariableW(wszPATH, path_env);
211 hXPCOM = LoadLibraryW(strXPCOM);
212 if(!hXPCOM) {
213 ERR("Could not load XPCOM: %ld\n", GetLastError());
214 return FALSE;
218 #define NS_DLSYM(func) \
219 func = (typeof(func))GetProcAddress(hXPCOM, #func); \
220 if(!func) \
221 ERR("Could not GetProcAddress(" #func ") failed\n")
223 NS_DLSYM(NS_InitXPCOM2);
224 NS_DLSYM(NS_ShutdownXPCOM);
225 NS_DLSYM(NS_StringContainerInit);
226 NS_DLSYM(NS_CStringContainerInit);
227 NS_DLSYM(NS_StringContainerFinish);
228 NS_DLSYM(NS_CStringContainerFinish);
229 NS_DLSYM(NS_StringSetData);
230 NS_DLSYM(NS_CStringSetData);
231 NS_DLSYM(NS_NewLocalFile);
233 #undef NS_DLSYM
235 NS_StringContainerInit(&path);
236 NS_StringSetData(&path, gre_path, PR_UINT32_MAX);
237 nsres = NS_NewLocalFile(&path, FALSE, &gre_dir);
238 NS_StringContainerFinish(&path);
239 if(NS_FAILED(nsres)) {
240 ERR("NS_NewLocalFile failed: %08lx\n", nsres);
241 FreeLibrary(hXPCOM);
242 return FALSE;
245 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
246 if(NS_FAILED(nsres)) {
247 ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
248 FreeLibrary(hXPCOM);
249 return FALSE;
252 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
253 if(NS_FAILED(nsres))
254 ERR("Could not get nsIComponentManager: %08lx\n", nsres);
256 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_APPSTARTUPNOTIFIER_CONTRACTID,
257 NULL, &IID_nsIObserver, (void**)&pStartNotif);
258 if(NS_SUCCEEDED(nsres)) {
259 nsres = nsIObserver_Observe(pStartNotif, NULL, APPSTARTUP_TOPIC, NULL);
260 if(NS_FAILED(nsres))
261 ERR("Observe failed: %08lx\n", nsres);
263 nsIObserver_Release(pStartNotif);
264 }else {
265 ERR("could not get appstartup-notifier: %08lx\n", nsres);
268 return TRUE;
271 nsACString *nsACString_Create(void)
273 nsACString *ret;
274 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(nsACString));
275 NS_CStringContainerInit(ret);
276 return ret;
279 void nsACString_SetData(nsACString *str, const char *data)
281 NS_CStringSetData(str, data, PR_UINT32_MAX);
284 void nsACString_Destroy(nsACString *str)
286 NS_CStringContainerFinish(str);
287 HeapFree(GetProcessHeap(), 0, str);
290 void close_gecko()
292 TRACE("()\n");
294 if(pCompMgr)
295 nsIComponentManager_Release(pCompMgr);
297 if(pServMgr)
298 nsIServiceManager_Release(pServMgr);
300 if(hXPCOM)
301 FreeLibrary(hXPCOM);
304 nsIURI *get_nsIURI(LPCWSTR url)
306 nsIURI *ret;
307 nsACString *acstr;
308 nsresult nsres;
309 char *urla;
310 int len;
312 if(!pIOService) {
313 nsres = nsIServiceManager_GetServiceByContactID(pServMgr, NS_IOSERVICE_CONTRACTID,
314 &IID_nsIIOService, (void**)&pIOService);
315 if(NS_FAILED(nsres))
316 ERR("Failed to create nsIOService: %08lx\n", nsres);
319 len = WideCharToMultiByte(CP_ACP, 0, url, -1, NULL, -1, NULL, NULL);
320 urla = HeapAlloc(GetProcessHeap(), 0, len);
321 WideCharToMultiByte(CP_ACP, 0, url, -1, urla, -1, NULL, NULL);
323 acstr = nsACString_Create();
324 nsACString_SetData(acstr, urla);
326 nsres = nsIIOService_NewURI(pIOService, acstr, NULL, NULL, &ret);
327 if(NS_FAILED(nsres))
328 FIXME("NewURI failed: %08lx\n", nsres);
330 nsACString_Destroy(acstr);
331 HeapFree(GetProcessHeap(), 0, urla);
333 return ret;
336 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
338 nsIWebBrowserSetup *wbsetup;
339 nsresult nsres;
341 This->nscontainer = NULL;
343 if(!load_gecko())
344 return;
346 This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
348 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
349 NULL, &IID_nsIWebBrowser, (void**)&This->nscontainer->webbrowser);
350 if(NS_FAILED(nsres))
351 ERR("Creating WebBrowser failed: %08lx\n", nsres);
353 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIBaseWindow,
354 (void**)&This->nscontainer->window);
355 if(NS_FAILED(nsres))
356 ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
358 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser,
359 &IID_nsIWebBrowserSetup, (void**)&wbsetup);
360 if(NS_SUCCEEDED(nsres)) {
361 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
362 if(NS_FAILED(nsres))
363 ERR("SetProperty failed: %08lx\n", nsres);
364 nsIWebBrowserSetup_Release(wbsetup);
365 }else {
366 ERR("Could not get nsIWebBrowserSetup interface\n");
369 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebNavigation,
370 (void**)&This->nscontainer->navigation);
371 if(NS_FAILED(nsres))
372 ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
374 nsres = nsIWebBrowserStream_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebBrowserStream,
375 (void**)&This->nscontainer->stream);
376 if(NS_FAILED(nsres))
377 ERR("Could not get nsIWebBrowserStream interface: %08lx\n", nsres);
379 if(!nscontainer_class)
380 register_nscontainer_class();
382 This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
383 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
384 GetDesktopWindow(), NULL, hInst, This);
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);
393 nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
394 nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
395 }else {
396 ERR("InitWindow failed: %08lx\n", nsres);
400 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
402 TRACE("(%p)\n", This);
404 nsIWebBrowser_Release(This->nscontainer->webbrowser);
405 nsIWebNavigation_Release(This->nscontainer->navigation);
406 nsIBaseWindow_Release(This->nscontainer->window);
408 if(This->nscontainer->stream)
409 nsIWebBrowserStream_Release(This->nscontainer->stream);
411 HeapFree(GetProcessHeap(), 0, This->nscontainer);