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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
38 #include "mshtml_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
42 #define USER_AGENT "User-Agent:"
43 #define CONTENT_TYPE "Content-Type:"
45 static int fix_headers(char *buf, DWORD post_len)
47 char *ptr = buf, *ptr2;
49 while(*ptr && (ptr[0] != '\r' || ptr[1] != '\n')) {
50 for(ptr2=ptr+1; *ptr2 && (ptr2[0] != '\r' || ptr2[1] != '\n'); ptr2++);
55 if(!strncasecmp(ptr, USER_AGENT, sizeof(USER_AGENT)-1)) {
56 FIXME("Ignoring User-Agent header\n");
57 memmove(ptr, ptr2, strlen(ptr2)+1);
58 }else if(!post_len && !strncasecmp(ptr, CONTENT_TYPE, sizeof(CONTENT_TYPE)-1)) {
59 TRACE("Ignoring Content-Type header\n");
60 memmove(ptr, ptr2, strlen(ptr2)+1);
70 static nsIInputStream *get_post_data_stream(IBindCtx *bctx)
72 nsIInputStream *ret = NULL;
74 IBindStatusCallback *callback;
75 IServiceProvider *service_provider;
78 DWORD post_len = 0, headers_len = 0;
79 LPWSTR headers = NULL;
80 WCHAR emptystr[] = {0};
84 static WCHAR _BSCB_Holder_[] =
85 {'_','B','S','C','B','_','H','o','l','d','e','r','_',0};
88 /* FIXME: This should be done in URLMoniker */
92 hres = IBindCtx_GetObjectParam(bctx, _BSCB_Holder_, &unk);
96 hres = IUnknown_QueryInterface(unk, &IID_IBindStatusCallback, (void**)&callback);
98 IUnknown_Release(unk);
102 hres = IUnknown_QueryInterface(unk, &IID_IServiceProvider, (void**)&service_provider);
103 IUnknown_Release(unk);
104 if(SUCCEEDED(hres)) {
105 IHttpNegotiate *http_negotiate;
107 hres = IServiceProvider_QueryService(service_provider, &IID_IHttpNegotiate, &IID_IHttpNegotiate,
108 (void**)&http_negotiate);
109 if(SUCCEEDED(hres)) {
110 hres = IHttpNegotiate_BeginningTransaction(http_negotiate, emptystr,
111 emptystr, 0, &headers);
112 IHttpNegotiate_Release(http_negotiate);
114 if(SUCCEEDED(hres) && headers)
115 headers_len = WideCharToMultiByte(CP_ACP, 0, headers, -1, NULL, 0, NULL, NULL);
118 IServiceProvider_Release(service_provider);
121 memset(&bindinfo, 0, sizeof(bindinfo));
122 bindinfo.cbSize = sizeof(bindinfo);
124 hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
126 if(SUCCEEDED(hres) && bindinfo.dwBindVerb == BINDVERB_POST)
127 post_len = bindinfo.cbstgmedData;
129 if(headers_len || post_len) {
132 static const char content_length[] = "Content-Length: %u\r\n\r\n";
134 data = heap_alloc(headers_len+post_len+sizeof(content_length)+8);
137 WideCharToMultiByte(CP_ACP, 0, headers, -1, data, headers_len, NULL, NULL);
138 len = fix_headers(data, post_len);
142 sprintf(data+len, content_length, post_len);
143 len += strlen(data+len);
145 memcpy(data+len, bindinfo.stgmedData.u.hGlobal, post_len);
148 TRACE("data = %s\n", debugstr_an(data, len+post_len));
151 ret = create_nsstream(data, len+post_len);
154 CoTaskMemFree(headers);
155 ReleaseBindInfo(&bindinfo);
156 IBindStatusCallback_Release(callback);
161 static BOOL use_gecko_script(LPCWSTR url)
163 static const WCHAR fileW[] = {'f','i','l','e',':'};
164 return strncmpiW(fileW, url, sizeof(fileW)/sizeof(WCHAR));
167 void set_current_mon(HTMLDocument *This, IMoniker *mon)
172 IMoniker_Release(This->mon);
177 CoTaskMemFree(This->url);
184 IMoniker_AddRef(mon);
187 hres = IMoniker_GetDisplayName(mon, NULL, NULL, &This->url);
189 WARN("GetDisplayName failed: %08x\n", hres);
191 set_script_mode(This, use_gecko_script(This->url) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT);
194 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL *bind_complete)
196 nsChannelBSC *bscallback;
203 IUnknown *unk = NULL;
207 * "__PrecreatedObject"
208 * "BIND_CONTEXT_PARAM"
209 * "__HTMLLOADOPTIONS"
213 * "_ITransData_Object_"
217 IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
219 IOleClientSite *client = NULL;
221 hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
222 if(SUCCEEDED(hres)) {
223 TRACE("Got client site %p\n", client);
224 IOleObject_SetClientSite(OLEOBJ(This), client);
225 IOleClientSite_Release(client);
228 IUnknown_Release(unk);
232 This->readystate = READYSTATE_LOADING;
233 call_property_onchanged(&This->cp_propnotif, DISPID_READYSTATE);
234 update_doc(This, UPDATE_TITLE);
236 HTMLDocument_LockContainer(This, TRUE);
238 hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
240 WARN("GetDiaplayName failed: %08x\n", hres);
244 TRACE("got url: %s\n", debugstr_w(url));
246 set_current_mon(This, mon);
249 VARIANT silent, offline;
250 IOleCommandTarget *cmdtrg = NULL;
252 hres = get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
253 if(SUCCEEDED(hres)) {
254 if(V_VT(&silent) != VT_BOOL)
255 WARN("V_VT(silent) = %d\n", V_VT(&silent));
256 else if(V_BOOL(&silent))
257 FIXME("silent == true\n");
260 hres = get_client_disp_property(This->client,
261 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
262 if(SUCCEEDED(hres)) {
263 if(V_VT(&silent) != VT_BOOL)
264 WARN("V_VT(offline) = %d\n", V_VT(&silent));
265 else if(V_BOOL(&silent))
266 FIXME("offline == true\n");
269 hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
271 if(SUCCEEDED(hres)) {
276 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
278 IOleCommandTarget_Release(cmdtrg);
282 bscallback = create_channelbsc(mon);
285 task = heap_alloc(sizeof(task_t));
288 task->task_id = TASK_SETPROGRESS;
294 task = heap_alloc(sizeof(task_t));
297 task->task_id = TASK_SETDOWNLOADSTATE;
302 if(This->nscontainer) {
303 nsIInputStream *post_data_stream = get_post_data_stream(pibc);
305 This->nscontainer->bscallback = bscallback;
306 nsres = nsIWebNavigation_LoadURI(This->nscontainer->navigation, url,
307 LOAD_FLAGS_NONE, NULL, post_data_stream, NULL);
308 This->nscontainer->bscallback = NULL;
311 nsIInputStream_Release(post_data_stream);
313 if(NS_SUCCEEDED(nsres)) {
314 /* FIXME: don't return here (URL Moniker needs to be good enough) */
316 IUnknown_Release((IUnknown*)bscallback);
320 *bind_complete = TRUE;
322 }else if(nsres != WINE_NS_LOAD_FROM_MONIKER) {
323 WARN("LoadURI failed: %08x\n", nsres);
327 set_document_bscallback(This, bscallback);
328 IUnknown_Release((IUnknown*)bscallback);
332 *bind_complete = FALSE;
336 static HRESULT get_doc_string(HTMLDocument *This, char **str, DWORD *len)
338 nsIDOMDocument *nsdoc;
344 if(!This->nscontainer) {
345 WARN("no nscontainer, returning NULL\n");
349 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
350 if(NS_FAILED(nsres)) {
351 ERR("GetDocument failed: %08x\n", nsres);
355 nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
356 nsIDOMDocument_Release(nsdoc);
357 if(NS_FAILED(nsres)) {
358 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
362 nsAString_Init(&nsstr, NULL);
363 nsnode_to_nsstring(nsnode, &nsstr);
364 nsIDOMNode_Release(nsnode);
366 nsAString_GetData(&nsstr, &strw);
367 TRACE("%s\n", debugstr_w(strw));
369 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
370 *str = heap_alloc(*len);
371 WideCharToMultiByte(CP_ACP, 0, strw, -1, *str, *len, NULL, NULL);
373 nsAString_Finish(&nsstr);
379 /**********************************************************
380 * IPersistMoniker implementation
383 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
385 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
388 HTMLDocument *This = PERSISTMON_THIS(iface);
389 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
392 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
394 HTMLDocument *This = PERSISTMON_THIS(iface);
395 return IHTMLDocument2_AddRef(HTMLDOC(This));
398 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
400 HTMLDocument *This = PERSISTMON_THIS(iface);
401 return IHTMLDocument2_Release(HTMLDOC(This));
404 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
406 HTMLDocument *This = PERSISTMON_THIS(iface);
407 return IPersist_GetClassID(PERSIST(This), pClassID);
410 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
412 HTMLDocument *This = PERSISTMON_THIS(iface);
414 TRACE("(%p)\n", This);
416 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
419 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
420 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
422 HTMLDocument *This = PERSISTMON_THIS(iface);
423 BOOL bind_complete = FALSE;
426 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
428 hres = set_moniker(This, pimkName, pibc, &bind_complete);
433 return start_binding(This, (BSCallback*)This->bscallback, pibc);
438 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
439 LPBC pbc, BOOL fRemember)
441 HTMLDocument *This = PERSISTMON_THIS(iface);
442 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
446 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
448 HTMLDocument *This = PERSISTMON_THIS(iface);
449 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
453 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
455 HTMLDocument *This = PERSISTMON_THIS(iface);
457 TRACE("(%p)->(%p)\n", This, ppimkName);
462 IMoniker_AddRef(This->mon);
463 *ppimkName = This->mon;
467 static const IPersistMonikerVtbl PersistMonikerVtbl = {
468 PersistMoniker_QueryInterface,
469 PersistMoniker_AddRef,
470 PersistMoniker_Release,
471 PersistMoniker_GetClassID,
472 PersistMoniker_IsDirty,
475 PersistMoniker_SaveCompleted,
476 PersistMoniker_GetCurMoniker
479 /**********************************************************
480 * IMonikerProp implementation
483 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
485 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
487 HTMLDocument *This = MONPROP_THIS(iface);
488 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
491 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
493 HTMLDocument *This = MONPROP_THIS(iface);
494 return IHTMLDocument2_AddRef(HTMLDOC(This));
497 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
499 HTMLDocument *This = MONPROP_THIS(iface);
500 return IHTMLDocument_Release(HTMLDOC(This));
503 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
505 HTMLDocument *This = MONPROP_THIS(iface);
507 TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
511 heap_free(This->mime);
512 This->mime = heap_strdupW(val);
519 FIXME("mkp %d\n", mkp);
526 static const IMonikerPropVtbl MonikerPropVtbl = {
527 MonikerProp_QueryInterface,
530 MonikerProp_PutProperty
533 /**********************************************************
534 * IPersistFile implementation
537 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
539 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
541 HTMLDocument *This = PERSISTFILE_THIS(iface);
542 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
545 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
547 HTMLDocument *This = PERSISTFILE_THIS(iface);
548 return IHTMLDocument2_AddRef(HTMLDOC(This));
551 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
553 HTMLDocument *This = PERSISTFILE_THIS(iface);
554 return IHTMLDocument2_Release(HTMLDOC(This));
557 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
559 HTMLDocument *This = PERSISTFILE_THIS(iface);
561 TRACE("(%p)->(%p)\n", This, pClassID);
566 *pClassID = CLSID_HTMLDocument;
570 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
572 HTMLDocument *This = PERSISTFILE_THIS(iface);
574 TRACE("(%p)\n", This);
576 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
579 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
581 HTMLDocument *This = PERSISTFILE_THIS(iface);
582 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
586 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
588 HTMLDocument *This = PERSISTFILE_THIS(iface);
590 DWORD len, written=0;
594 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
596 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
597 FILE_ATTRIBUTE_NORMAL, NULL);
598 if(file == INVALID_HANDLE_VALUE) {
599 WARN("Could not create file: %u\n", GetLastError());
603 hres = get_doc_string(This, &str, &len);
607 WriteFile(file, str, len, &written, NULL);
612 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
614 HTMLDocument *This = PERSISTFILE_THIS(iface);
615 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
619 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
621 HTMLDocument *This = PERSISTFILE_THIS(iface);
622 FIXME("(%p)->(%p)\n", This, pszFileName);
626 static const IPersistFileVtbl PersistFileVtbl = {
627 PersistFile_QueryInterface,
630 PersistFile_GetClassID,
634 PersistFile_SaveCompleted,
635 PersistFile_GetCurFile
638 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
640 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
641 REFIID riid, void **ppv)
643 HTMLDocument *This = PERSTRINIT_THIS(iface);
644 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
647 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
649 HTMLDocument *This = PERSTRINIT_THIS(iface);
650 return IHTMLDocument2_AddRef(HTMLDOC(This));
653 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
655 HTMLDocument *This = PERSTRINIT_THIS(iface);
656 return IHTMLDocument2_Release(HTMLDOC(This));
659 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
661 HTMLDocument *This = PERSTRINIT_THIS(iface);
662 return IPersist_GetClassID(PERSIST(This), pClassID);
665 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
667 HTMLDocument *This = PERSTRINIT_THIS(iface);
669 TRACE("(%p)\n", This);
671 if(This->usermode == EDITMODE)
672 return editor_is_dirty(This);
677 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
679 HTMLDocument *This = PERSTRINIT_THIS(iface);
683 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
685 TRACE("(%p)->(%p)\n", This, pStm);
687 hres = CreateURLMoniker(NULL, about_blankW, &mon);
689 WARN("CreateURLMoniker failed: %08x\n", hres);
693 hres = set_moniker(This, mon, NULL, NULL);
694 IMoniker_Release(mon);
698 return channelbsc_load_stream(This->bscallback, pStm);
701 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
704 HTMLDocument *This = PERSTRINIT_THIS(iface);
706 DWORD len, written=0;
709 TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
711 hres = get_doc_string(This, &str, &len);
715 hres = IStream_Write(pStm, str, len, &written);
717 FIXME("Write failed: %08x\n", hres);
722 set_dirty(This, VARIANT_FALSE);
727 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
728 ULARGE_INTEGER *pcbSize)
730 HTMLDocument *This = PERSTRINIT_THIS(iface);
731 FIXME("(%p)->(%p)\n", This, pcbSize);
735 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
737 HTMLDocument *This = PERSTRINIT_THIS(iface);
738 FIXME("(%p)\n", This);
742 #undef PERSTRINIT_THIS
744 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
745 PersistStreamInit_QueryInterface,
746 PersistStreamInit_AddRef,
747 PersistStreamInit_Release,
748 PersistStreamInit_GetClassID,
749 PersistStreamInit_IsDirty,
750 PersistStreamInit_Load,
751 PersistStreamInit_Save,
752 PersistStreamInit_GetSizeMax,
753 PersistStreamInit_InitNew
756 void HTMLDocument_Persist_Init(HTMLDocument *This)
758 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
759 This->lpPersistFileVtbl = &PersistFileVtbl;
760 This->lpMonikerPropVtbl = &MonikerPropVtbl;
761 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
763 This->bscallback = NULL;