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
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
{
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
)
80 static const WCHAR wszTHIS
[] = {'T','H','I','S',0};
82 if(msg
== WM_CREATE
) {
83 This
= *(HTMLDocument
**)lParam
;
84 SetPropW(hwnd
, wszTHIS
, This
);
86 This
= (HTMLDocument
*)GetPropW(hwnd
, wszTHIS
);
91 TRACE("(%p)->(WM_SIZE)\n", This
);
93 nsres
= nsIBaseWindow_SetSize(This
->nscontainer
->window
,
94 LOWORD(lParam
), HIWORD(lParam
), TRUE
);
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
= {
109 0, 0, NULL
, NULL
, NULL
, NULL
, 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
;
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','\\',
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
));
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
)
145 RegCloseKey(mozilla_key
);
149 static BOOL
get_mozctl_path(PRUnichar
*gre_path
)
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
));
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
));
174 static BOOL
load_gecko()
177 nsIObserver
*pStartNotif
;
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};
188 return pCompMgr
!= NULL
;
191 if(!get_mozctl_path(gre_path
) && !get_mozilla_path(gre_path
)) {
192 MESSAGE("Could not load Mozilla. HTML rendering will be disabled.\n");
196 TRACE("found path %s\n", debugstr_w(gre_path
));
198 hXPCOM
= LoadLibraryW(strXPCOM
);
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};
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
);
213 ERR("Could not load XPCOM: %ld\n", GetLastError());
218 #define NS_DLSYM(func) \
219 func = (typeof(func))GetProcAddress(hXPCOM, #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
);
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
);
245 nsres
= NS_InitXPCOM2(&pServMgr
, gre_dir
, NULL
);
246 if(NS_FAILED(nsres
)) {
247 ERR("NS_InitXPCOM2 failed: %08lx\n", nsres
);
252 nsres
= nsIServiceManager_QueryInterface(pServMgr
, &IID_nsIComponentManager
, (void**)&pCompMgr
);
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
);
261 ERR("Observe failed: %08lx\n", nsres
);
263 nsIObserver_Release(pStartNotif
);
265 ERR("could not get appstartup-notifier: %08lx\n", nsres
);
271 nsACString
*nsACString_Create(void)
274 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(nsACString
));
275 NS_CStringContainerInit(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
);
295 nsIComponentManager_Release(pCompMgr
);
298 nsIServiceManager_Release(pServMgr
);
304 nsIURI
*get_nsIURI(LPCWSTR url
)
313 nsres
= nsIServiceManager_GetServiceByContactID(pServMgr
, NS_IOSERVICE_CONTRACTID
,
314 &IID_nsIIOService
, (void**)&pIOService
);
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
);
328 FIXME("NewURI failed: %08lx\n", nsres
);
330 nsACString_Destroy(acstr
);
331 HeapFree(GetProcessHeap(), 0, urla
);
336 void HTMLDocument_NSContainer_Init(HTMLDocument
*This
)
338 nsIWebBrowserSetup
*wbsetup
;
341 This
->nscontainer
= NULL
;
346 This
->nscontainer
= HeapAlloc(GetProcessHeap(), 0, sizeof(NSContainer
));
348 nsres
= nsIComponentManager_CreateInstanceByContractID(pCompMgr
, NS_WEBBROWSER_CONTRACTID
,
349 NULL
, &IID_nsIWebBrowser
, (void**)&This
->nscontainer
->webbrowser
);
351 ERR("Creating WebBrowser failed: %08lx\n", nsres
);
353 nsres
= nsIWebBrowser_QueryInterface(This
->nscontainer
->webbrowser
, &IID_nsIBaseWindow
,
354 (void**)&This
->nscontainer
->window
);
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
);
363 ERR("SetProperty failed: %08lx\n", nsres
);
364 nsIWebBrowserSetup_Release(wbsetup
);
366 ERR("Could not get nsIWebBrowserSetup interface\n");
369 nsres
= nsIWebBrowser_QueryInterface(This
->nscontainer
->webbrowser
, &IID_nsIWebNavigation
,
370 (void**)&This
->nscontainer
->navigation
);
372 ERR("Could not get nsIWebNavigation interface: %08lx\n", nsres
);
374 nsres
= nsIWebBrowserStream_QueryInterface(This
->nscontainer
->webbrowser
, &IID_nsIWebBrowserStream
,
375 (void**)&This
->nscontainer
->stream
);
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
,
388 if(NS_SUCCEEDED(nsres
)) {
389 nsres
= nsIBaseWindow_Create(This
->nscontainer
->window
);
391 WARN("Creating window failed: %08lx\n", nsres
);
393 nsIBaseWindow_SetVisibility(This
->nscontainer
->window
, FALSE
);
394 nsIBaseWindow_SetEnabled(This
->nscontainer
->window
, FALSE
);
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
);