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)
344 WARN("NULL nsdoc\n");
348 nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
349 if(NS_FAILED(nsres)) {
350 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
354 nsAString_Init(&nsstr, NULL);
355 nsnode_to_nsstring(nsnode, &nsstr);
356 nsIDOMNode_Release(nsnode);
358 nsAString_GetData(&nsstr, &strw);
359 TRACE("%s\n", debugstr_w(strw));
361 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
362 *str = heap_alloc(*len);
363 WideCharToMultiByte(CP_ACP, 0, strw, -1, *str, *len, NULL, NULL);
365 nsAString_Finish(&nsstr);
371 /**********************************************************
372 * IPersistMoniker implementation
375 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
377 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
380 HTMLDocument *This = PERSISTMON_THIS(iface);
381 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
384 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
386 HTMLDocument *This = PERSISTMON_THIS(iface);
387 return IHTMLDocument2_AddRef(HTMLDOC(This));
390 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
392 HTMLDocument *This = PERSISTMON_THIS(iface);
393 return IHTMLDocument2_Release(HTMLDOC(This));
396 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
398 HTMLDocument *This = PERSISTMON_THIS(iface);
399 return IPersist_GetClassID(PERSIST(This), pClassID);
402 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
404 HTMLDocument *This = PERSISTMON_THIS(iface);
406 TRACE("(%p)\n", This);
408 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
411 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
412 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
414 HTMLDocument *This = PERSISTMON_THIS(iface);
415 BOOL bind_complete = FALSE;
418 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
420 hres = set_moniker(This, pimkName, pibc, &bind_complete);
425 return start_binding(This, (BSCallback*)This->bscallback, pibc);
430 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
431 LPBC pbc, BOOL fRemember)
433 HTMLDocument *This = PERSISTMON_THIS(iface);
434 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
438 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
440 HTMLDocument *This = PERSISTMON_THIS(iface);
441 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
445 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
447 HTMLDocument *This = PERSISTMON_THIS(iface);
449 TRACE("(%p)->(%p)\n", This, ppimkName);
454 IMoniker_AddRef(This->mon);
455 *ppimkName = This->mon;
459 static const IPersistMonikerVtbl PersistMonikerVtbl = {
460 PersistMoniker_QueryInterface,
461 PersistMoniker_AddRef,
462 PersistMoniker_Release,
463 PersistMoniker_GetClassID,
464 PersistMoniker_IsDirty,
467 PersistMoniker_SaveCompleted,
468 PersistMoniker_GetCurMoniker
471 /**********************************************************
472 * IMonikerProp implementation
475 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
477 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
479 HTMLDocument *This = MONPROP_THIS(iface);
480 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
483 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
485 HTMLDocument *This = MONPROP_THIS(iface);
486 return IHTMLDocument2_AddRef(HTMLDOC(This));
489 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
491 HTMLDocument *This = MONPROP_THIS(iface);
492 return IHTMLDocument_Release(HTMLDOC(This));
495 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
497 HTMLDocument *This = MONPROP_THIS(iface);
499 TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
503 heap_free(This->mime);
504 This->mime = heap_strdupW(val);
511 FIXME("mkp %d\n", mkp);
518 static const IMonikerPropVtbl MonikerPropVtbl = {
519 MonikerProp_QueryInterface,
522 MonikerProp_PutProperty
525 /**********************************************************
526 * IPersistFile implementation
529 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
531 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
533 HTMLDocument *This = PERSISTFILE_THIS(iface);
534 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
537 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
539 HTMLDocument *This = PERSISTFILE_THIS(iface);
540 return IHTMLDocument2_AddRef(HTMLDOC(This));
543 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
545 HTMLDocument *This = PERSISTFILE_THIS(iface);
546 return IHTMLDocument2_Release(HTMLDOC(This));
549 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
551 HTMLDocument *This = PERSISTFILE_THIS(iface);
553 TRACE("(%p)->(%p)\n", This, pClassID);
558 *pClassID = CLSID_HTMLDocument;
562 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
564 HTMLDocument *This = PERSISTFILE_THIS(iface);
566 TRACE("(%p)\n", This);
568 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
571 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
573 HTMLDocument *This = PERSISTFILE_THIS(iface);
574 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
578 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
580 HTMLDocument *This = PERSISTFILE_THIS(iface);
582 DWORD len, written=0;
586 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
588 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
589 FILE_ATTRIBUTE_NORMAL, NULL);
590 if(file == INVALID_HANDLE_VALUE) {
591 WARN("Could not create file: %u\n", GetLastError());
595 hres = get_doc_string(This, &str, &len);
597 WriteFile(file, str, len, &written, NULL);
603 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
605 HTMLDocument *This = PERSISTFILE_THIS(iface);
606 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
610 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
612 HTMLDocument *This = PERSISTFILE_THIS(iface);
613 FIXME("(%p)->(%p)\n", This, pszFileName);
617 static const IPersistFileVtbl PersistFileVtbl = {
618 PersistFile_QueryInterface,
621 PersistFile_GetClassID,
625 PersistFile_SaveCompleted,
626 PersistFile_GetCurFile
629 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
631 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
632 REFIID riid, void **ppv)
634 HTMLDocument *This = PERSTRINIT_THIS(iface);
635 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
638 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
640 HTMLDocument *This = PERSTRINIT_THIS(iface);
641 return IHTMLDocument2_AddRef(HTMLDOC(This));
644 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
646 HTMLDocument *This = PERSTRINIT_THIS(iface);
647 return IHTMLDocument2_Release(HTMLDOC(This));
650 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
652 HTMLDocument *This = PERSTRINIT_THIS(iface);
653 return IPersist_GetClassID(PERSIST(This), pClassID);
656 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
658 HTMLDocument *This = PERSTRINIT_THIS(iface);
660 TRACE("(%p)\n", This);
662 if(This->usermode == EDITMODE)
663 return editor_is_dirty(This);
668 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
670 HTMLDocument *This = PERSTRINIT_THIS(iface);
674 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
676 TRACE("(%p)->(%p)\n", This, pStm);
678 hres = CreateURLMoniker(NULL, about_blankW, &mon);
680 WARN("CreateURLMoniker failed: %08x\n", hres);
684 hres = set_moniker(This, mon, NULL, NULL);
685 IMoniker_Release(mon);
689 return channelbsc_load_stream(This->bscallback, pStm);
692 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
695 HTMLDocument *This = PERSTRINIT_THIS(iface);
697 DWORD len, written=0;
700 TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
702 hres = get_doc_string(This, &str, &len);
706 hres = IStream_Write(pStm, str, len, &written);
708 FIXME("Write failed: %08x\n", hres);
713 set_dirty(This, VARIANT_FALSE);
718 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
719 ULARGE_INTEGER *pcbSize)
721 HTMLDocument *This = PERSTRINIT_THIS(iface);
722 FIXME("(%p)->(%p)\n", This, pcbSize);
726 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
728 HTMLDocument *This = PERSTRINIT_THIS(iface);
729 FIXME("(%p)\n", This);
733 #undef PERSTRINIT_THIS
735 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
736 PersistStreamInit_QueryInterface,
737 PersistStreamInit_AddRef,
738 PersistStreamInit_Release,
739 PersistStreamInit_GetClassID,
740 PersistStreamInit_IsDirty,
741 PersistStreamInit_Load,
742 PersistStreamInit_Save,
743 PersistStreamInit_GetSizeMax,
744 PersistStreamInit_InitNew
747 void HTMLDocument_Persist_Init(HTMLDocument *This)
749 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
750 This->lpPersistFileVtbl = &PersistFileVtbl;
751 This->lpMonikerPropVtbl = &MonikerPropVtbl;
752 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
754 This->bscallback = NULL;