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
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #include "mshtml_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 DEFINE_GUID(CLSID_StartupNotifier, 0x1f59b001,0x02c9,0x11d5,0xae,0x76,0xcc,0x92,0xf7,0xdb,0x9e,0x03);
43 DEFINE_GUID(CLSID_nsWebBrowser, 0xf1eac761,0x87e9,0x11d3,0xaf,0x80,0x00,0xa0,0x24,0xff,0xc0,0x8c);
45 #define NS_APPSTARTUPNOTIFIER_CONTRACTID "@mozilla.org/embedcomp/appstartup-notifier;1"
46 #define NS_WEBBROWSER_CONTRACTID "@mozilla.org/embedding/browser/nsWebBrowser;1"
48 #define APPSTARTUP_TOPIC "app-startup"
50 #define PR_UINT32_MAX 0xffffffff
58 static nsresult (*NS_InitXPCOM2)(nsIServiceManager**,void*,void*);
59 static nsresult (*NS_ShutdownXPCOM)(nsIServiceManager*);
60 static nsresult (*NS_StringContainerInit)(nsString*);
61 static nsresult (*NS_StringContainerFinish)(nsString*);
62 static nsresult (*NS_StringSetData)(nsString*,const PRUnichar*,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;
70 static const WCHAR wszNsContainer[] = {'N','s','C','o','n','t','a','i','n','e','r',0};
72 static ATOM nscontainer_class;
74 static LRESULT WINAPI nsembed_proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
79 static const WCHAR wszTHIS[] = {'T','H','I','S',0};
81 if(msg == WM_CREATE) {
82 This = *(HTMLDocument**)lParam;
83 SetPropW(hwnd, wszTHIS, This);
85 This = (HTMLDocument*)GetPropW(hwnd, wszTHIS);
90 TRACE("(%p)->(WM_SIZE)\n", This);
92 nsres = nsIBaseWindow_SetSize(This->nscontainer->window,
93 LOWORD(lParam), HIWORD(lParam), TRUE);
95 WARN("SetSize failed: %08lx\n", nsres);
98 return DefWindowProcW(hwnd, msg, wParam, lParam);
102 static void register_nscontainer_class(void)
104 static WNDCLASSEXW wndclass = {
108 0, 0, NULL, NULL, NULL, NULL, NULL,
112 wndclass.hInstance = hInst;
113 nscontainer_class = RegisterClassExW(&wndclass);
116 static BOOL get_mozilla_path(PRUnichar *gre_path)
118 DWORD res, type, i, size = MAX_PATH;
119 HKEY mozilla_key, hkey;
123 static const WCHAR wszGreKey[] =
124 {'S','o','f','t','w','a','r','e','\\',
125 'm','o','z','i','l','l','a','.','o','r','g','\\',
128 static const WCHAR wszGreHome[] = {'G','r','e','H','o','m','e',0};
130 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszGreKey, &mozilla_key);
131 if(res != ERROR_SUCCESS) {
132 TRACE("Could not open key %s\n", debugstr_w(wszGreKey));
136 for(i=0; !ret && RegEnumKeyW(mozilla_key, i, key_name, sizeof(key_name)/sizeof(WCHAR)) == ERROR_SUCCESS; i++) {
137 RegOpenKeyW(mozilla_key, key_name, &hkey);
138 res = RegQueryValueExW(hkey, wszGreHome, NULL, &type, (LPBYTE)gre_path, &size);
139 if(res == ERROR_SUCCESS)
144 RegCloseKey(mozilla_key);
148 static BOOL get_mozctl_path(PRUnichar *gre_path)
151 DWORD res, type, size = MAX_PATH;
153 static const WCHAR wszMozCtlKey[] =
154 {'S','o','f','t','w','a','r','e','\\','M','o','z','i','l','l','a',0};
155 static const WCHAR wszBinDirectoryPath[] =
156 {'B','i','n','D','i','r','e','c','t','o','r','y','P','a','t','h',0};
158 res = RegOpenKeyW(HKEY_LOCAL_MACHINE, wszMozCtlKey, &hkey);
159 if(res != ERROR_SUCCESS) {
160 TRACE("Could not open key %s\n", debugstr_w(wszMozCtlKey));
164 res = RegQueryValueExW(hkey, wszBinDirectoryPath, NULL, &type, (LPBYTE)gre_path, &size);
165 if(res != ERROR_SUCCESS) {
166 ERR("Could not get value %s\n", debugstr_w(wszBinDirectoryPath));
173 static BOOL load_gecko()
176 nsIObserver *pStartNotif;
179 PRUnichar gre_path[MAX_PATH];
181 static BOOL tried_load = FALSE;
182 static const WCHAR strXPCOM[] = {'x','p','c','o','m','.','d','l','l',0};
187 return pCompMgr != NULL;
190 if(!get_mozctl_path(gre_path) && !get_mozilla_path(gre_path)) {
191 MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
195 TRACE("found path %s\n", debugstr_w(gre_path));
197 hXPCOM = LoadLibraryW(strXPCOM);
199 /* We have to modify PATH as XPCOM loads other DLLs from this directory. */
200 WCHAR path_env[MAX_PATH];
201 static WCHAR wszPATH[] = {'P','A','T','H',0};
204 GetEnvironmentVariableW(wszPATH, path_env, sizeof(path_env)/sizeof(WCHAR));
205 len = strlenW(path_env);
206 path_env[len++] = ';';
207 strcpyW(path_env+len, gre_path);
208 SetEnvironmentVariableW(wszPATH, path_env);
210 hXPCOM = LoadLibraryW(strXPCOM);
212 ERR("Could not load XPCOM: %ld\n", GetLastError());
217 #define NS_DLSYM(func) \
218 func = (typeof(func))GetProcAddress(hXPCOM, #func); \
220 ERR("Could not GetProcAddress(" #func ") failed\n");
222 NS_DLSYM(NS_InitXPCOM2)
223 NS_DLSYM(NS_ShutdownXPCOM)
224 NS_DLSYM(NS_StringContainerInit)
225 NS_DLSYM(NS_StringContainerFinish)
226 NS_DLSYM(NS_StringSetData)
227 NS_DLSYM(NS_NewLocalFile)
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);
241 nsres = NS_InitXPCOM2(&pServMgr, gre_dir, NULL);
242 if(NS_FAILED(nsres)) {
243 ERR("NS_InitXPCOM2 failed: %08lx\n", nsres);
248 nsres = nsIServiceManager_QueryInterface(pServMgr, &IID_nsIComponentManager, (void**)&pCompMgr);
250 ERR("Could not get nsIComponentManager: %08lx\n", nsres);
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);
257 ERR("Observe failed: %08lx\n", nsres);
259 nsIObserver_Release(pStartNotif);
261 ERR("could not get appstartup-notifier: %08lx\n", nsres);
272 nsIComponentManager_Release(pCompMgr);
275 nsIServiceManager_Release(pServMgr);
281 void HTMLDocument_NSContainer_Init(HTMLDocument *This)
283 nsIWebBrowserSetup *wbsetup;
286 This->nscontainer = NULL;
291 This->nscontainer = HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer));
293 nsres = nsIComponentManager_CreateInstanceByContractID(pCompMgr, NS_WEBBROWSER_CONTRACTID,
294 NULL, &IID_nsIWebBrowser, (void**)&This->nscontainer->webbrowser);
296 ERR("Creating WebBrowser failed: %08lx\n", nsres);
298 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIBaseWindow,
299 (void**)&This->nscontainer->window);
301 ERR("Could not get nsIBaseWindow interface: %08lx\n", nsres);
303 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser,
304 &IID_nsIWebBrowserSetup, (void**)&wbsetup);
305 if(NS_SUCCEEDED(nsres)) {
306 nsres = nsIWebBrowserSetup_SetProperty(wbsetup, SETUP_IS_CHROME_WRAPPER, TRUE);
308 ERR("SetProperty failed: %08lx\n", nsres);
309 nsIWebBrowserSetup_Release(wbsetup);
311 ERR("Could not get nsIWebBrowserSetup interface\n");
314 nsres = nsIWebBrowser_QueryInterface(This->nscontainer->webbrowser, &IID_nsIWebNavigation,
315 (void**)&This->nscontainer->navigation);
317 ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres);
319 if(!nscontainer_class)
320 register_nscontainer_class();
322 This->nscontainer->hwnd = CreateWindowExW(0, wszNsContainer, NULL,
323 WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, 100, 100,
324 GetDesktopWindow(), NULL, hInst, This);
326 nsres = nsIBaseWindow_InitWindow(This->nscontainer->window, This->nscontainer->hwnd, NULL,
328 if(NS_SUCCEEDED(nsres)) {
329 nsres = nsIBaseWindow_Create(This->nscontainer->window);
331 WARN("Creating window failed: %08lx\n", nsres);
333 nsIBaseWindow_SetVisibility(This->nscontainer->window, FALSE);
334 nsIBaseWindow_SetEnabled(This->nscontainer->window, FALSE);
336 ERR("InitWindow failed: %08lx\n", nsres);
340 void HTMLDocument_NSContainer_Destroy(HTMLDocument *This)
342 TRACE("(%p)\n", This);
344 nsIWebBrowser_Release(This->nscontainer->webbrowser);
345 nsIWebNavigation_Release(This->nscontainer->navigation);
346 nsIBaseWindow_Release(This->nscontainer->window);
348 HeapFree(GetProcessHeap(), 0, This->nscontainer);