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)
49 if(!strncasecmp(USER_AGENT, buf, sizeof(USER_AGENT)-1)) {
52 ptr = strstr(buf, "\r\n" USER_AGENT);
60 FIXME("Ignoring User-Agent header\n");
62 ptr2 = strstr(ptr, "\r\n");
64 memmove(ptr, ptr2, strlen(ptr2)+1);
68 if(!strncasecmp(CONTENT_TYPE, buf, sizeof(CONTENT_TYPE)-1)) {
71 ptr = strstr(buf, "\r\n" CONTENT_TYPE);
79 TRACE("Ignoring Content-Type header\n");
81 ptr2 = strstr(ptr, "\r\n");
83 memmove(ptr, ptr2, strlen(ptr2)+1);
90 static nsIInputStream *get_post_data_stream(IBindCtx *bctx)
92 nsIInputStream *ret = NULL;
93 IBindStatusCallback *callback;
94 IHttpNegotiate *http_negotiate;
97 DWORD post_len = 0, headers_len = 0;
98 LPWSTR headers = NULL;
99 WCHAR emptystr[] = {0};
103 static WCHAR _BSCB_Holder_[] =
104 {'_','B','S','C','B','_','H','o','l','d','e','r','_',0};
107 /* FIXME: This should be done in URLMoniker */
111 hres = IBindCtx_GetObjectParam(bctx, _BSCB_Holder_, (IUnknown**)&callback);
115 hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
116 (void**)&http_negotiate);
117 if(SUCCEEDED(hres)) {
118 hres = IHttpNegotiate_BeginningTransaction(http_negotiate, emptystr,
119 emptystr, 0, &headers);
120 IHttpNegotiate_Release(http_negotiate);
122 if(SUCCEEDED(hres) && headers)
123 headers_len = WideCharToMultiByte(CP_ACP, 0, headers, -1, NULL, 0, NULL, NULL);
126 memset(&bindinfo, 0, sizeof(bindinfo));
127 bindinfo.cbSize = sizeof(bindinfo);
129 hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
131 if(SUCCEEDED(hres) && bindinfo.dwBindVerb == BINDVERB_POST)
132 post_len = bindinfo.cbstgmedData;
134 if(headers_len || post_len) {
137 static const char content_length[] = "Content-Length: %u\r\n\r\n";
139 data = heap_alloc(headers_len+post_len+sizeof(content_length)+8);
142 WideCharToMultiByte(CP_ACP, 0, headers, -1, data, -1, NULL, NULL);
143 CoTaskMemFree(headers);
144 len = fix_headers(data, post_len);
148 if(len >= 4 && !strcmp(data+len-4, "\r\n\r\n"))
151 sprintf(data+len, content_length, post_len);
154 memcpy(data+len, bindinfo.stgmedData.u.hGlobal, post_len);
157 TRACE("data = %s\n", debugstr_an(data, len+post_len));
159 ret = create_nsstream(data, len+post_len);
162 ReleaseBindInfo(&bindinfo);
163 IBindStatusCallback_Release(callback);
168 void set_current_mon(HTMLDocument *This, IMoniker *mon)
173 IMoniker_Release(This->mon);
178 CoTaskMemFree(This->url);
185 IMoniker_AddRef(mon);
188 hres = IMoniker_GetDisplayName(mon, NULL, NULL, &This->url);
190 WARN("GetDisplayName failed: %08x\n", hres);
193 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL *bind_complete)
195 BSCallback *bscallback;
202 IUnknown *unk = NULL;
206 * "__PrecreatedObject"
207 * "BIND_CONTEXT_PARAM"
208 * "__HTMLLOADOPTIONS"
212 * "_ITransData_Object_"
216 IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
218 IOleClientSite *client = NULL;
220 hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
221 if(SUCCEEDED(hres)) {
222 TRACE("Got client site %p\n", client);
223 IOleObject_SetClientSite(OLEOBJ(This), client);
224 IOleClientSite_Release(client);
227 IUnknown_Release(unk);
231 This->readystate = READYSTATE_LOADING;
232 call_property_onchanged(&This->cp_propnotif, DISPID_READYSTATE);
233 update_doc(This, UPDATE_TITLE);
235 HTMLDocument_LockContainer(This, TRUE);
237 hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
239 WARN("GetDiaplayName failed: %08x\n", hres);
243 TRACE("got url: %s\n", debugstr_w(url));
245 set_current_mon(This, mon);
248 VARIANT silent, offline;
249 IOleCommandTarget *cmdtrg = NULL;
251 hres = get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
252 if(SUCCEEDED(hres)) {
253 if(V_VT(&silent) != VT_BOOL)
254 WARN("V_VT(silent) = %d\n", V_VT(&silent));
255 else if(V_BOOL(&silent))
256 FIXME("silent == true\n");
259 hres = get_client_disp_property(This->client,
260 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
261 if(SUCCEEDED(hres)) {
262 if(V_VT(&silent) != VT_BOOL)
263 WARN("V_VT(offline) = %d\n", V_VT(&silent));
264 else if(V_BOOL(&silent))
265 FIXME("offline == true\n");
268 hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
270 if(SUCCEEDED(hres)) {
275 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
277 IOleCommandTarget_Release(cmdtrg);
281 bscallback = create_bscallback(mon);
284 task = heap_alloc(sizeof(task_t));
287 task->task_id = TASK_SETPROGRESS;
293 task = heap_alloc(sizeof(task_t));
296 task->task_id = TASK_SETDOWNLOADSTATE;
301 if(This->nscontainer) {
302 nsIInputStream *post_data_stream = get_post_data_stream(pibc);
304 This->nscontainer->bscallback = bscallback;
305 nsres = nsIWebNavigation_LoadURI(This->nscontainer->navigation, url,
306 LOAD_FLAGS_NONE, NULL, post_data_stream, NULL);
307 This->nscontainer->bscallback = NULL;
310 nsIInputStream_Release(post_data_stream);
312 if(NS_SUCCEEDED(nsres)) {
313 /* FIXME: don't return here (URL Moniker needs to be good enough) */
315 IBindStatusCallback_Release(STATUSCLB(bscallback));
319 *bind_complete = TRUE;
321 }else if(nsres != WINE_NS_LOAD_FROM_MONIKER) {
322 WARN("LoadURI failed: %08x\n", nsres);
326 set_document_bscallback(This, bscallback);
327 IBindStatusCallback_Release(STATUSCLB(bscallback));
331 *bind_complete = FALSE;
335 static HRESULT get_doc_string(HTMLDocument *This, char **str, DWORD *len)
337 nsIDOMDocument *nsdoc;
343 if(!This->nscontainer) {
344 WARN("no nscontainer, returning NULL\n");
348 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
349 if(NS_FAILED(nsres)) {
350 ERR("GetDocument failed: %08x\n", nsres);
354 nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
355 nsIDOMDocument_Release(nsdoc);
356 if(NS_FAILED(nsres)) {
357 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
361 nsAString_Init(&nsstr, NULL);
362 nsnode_to_nsstring(nsnode, &nsstr);
363 nsIDOMNode_Release(nsnode);
365 nsAString_GetData(&nsstr, &strw);
366 TRACE("%s\n", debugstr_w(strw));
368 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
369 *str = heap_alloc(*len);
370 WideCharToMultiByte(CP_ACP, 0, strw, -1, *str, *len, NULL, NULL);
372 nsAString_Finish(&nsstr);
378 /**********************************************************
379 * IPersistMoniker implementation
382 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
384 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
387 HTMLDocument *This = PERSISTMON_THIS(iface);
388 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
391 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
393 HTMLDocument *This = PERSISTMON_THIS(iface);
394 return IHTMLDocument2_AddRef(HTMLDOC(This));
397 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
399 HTMLDocument *This = PERSISTMON_THIS(iface);
400 return IHTMLDocument2_Release(HTMLDOC(This));
403 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
405 HTMLDocument *This = PERSISTMON_THIS(iface);
406 return IPersist_GetClassID(PERSIST(This), pClassID);
409 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
411 HTMLDocument *This = PERSISTMON_THIS(iface);
413 TRACE("(%p)\n", This);
415 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
418 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
419 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
421 HTMLDocument *This = PERSISTMON_THIS(iface);
422 BOOL bind_complete = FALSE;
425 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
427 hres = set_moniker(This, pimkName, pibc, &bind_complete);
432 return start_binding(This, This->bscallback, pibc);
437 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
438 LPBC pbc, BOOL fRemember)
440 HTMLDocument *This = PERSISTMON_THIS(iface);
441 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
445 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
447 HTMLDocument *This = PERSISTMON_THIS(iface);
448 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
452 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
454 HTMLDocument *This = PERSISTMON_THIS(iface);
456 TRACE("(%p)->(%p)\n", This, ppimkName);
461 IMoniker_AddRef(This->mon);
462 *ppimkName = This->mon;
466 static const IPersistMonikerVtbl PersistMonikerVtbl = {
467 PersistMoniker_QueryInterface,
468 PersistMoniker_AddRef,
469 PersistMoniker_Release,
470 PersistMoniker_GetClassID,
471 PersistMoniker_IsDirty,
474 PersistMoniker_SaveCompleted,
475 PersistMoniker_GetCurMoniker
478 /**********************************************************
479 * IMonikerProp implementation
482 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
484 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
486 HTMLDocument *This = MONPROP_THIS(iface);
487 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
490 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
492 HTMLDocument *This = MONPROP_THIS(iface);
493 return IHTMLDocument2_AddRef(HTMLDOC(This));
496 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
498 HTMLDocument *This = MONPROP_THIS(iface);
499 return IHTMLDocument_Release(HTMLDOC(This));
502 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
504 HTMLDocument *This = MONPROP_THIS(iface);
506 TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
510 heap_free(This->mime);
511 This->mime = heap_strdupW(val);
518 FIXME("mkp %d\n", mkp);
525 static const IMonikerPropVtbl MonikerPropVtbl = {
526 MonikerProp_QueryInterface,
529 MonikerProp_PutProperty
532 /**********************************************************
533 * IPersistFile implementation
536 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
538 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
540 HTMLDocument *This = PERSISTFILE_THIS(iface);
541 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
544 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
546 HTMLDocument *This = PERSISTFILE_THIS(iface);
547 return IHTMLDocument2_AddRef(HTMLDOC(This));
550 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
552 HTMLDocument *This = PERSISTFILE_THIS(iface);
553 return IHTMLDocument2_Release(HTMLDOC(This));
556 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
558 HTMLDocument *This = PERSISTFILE_THIS(iface);
560 TRACE("(%p)->(%p)\n", This, pClassID);
565 memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
569 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
571 HTMLDocument *This = PERSISTFILE_THIS(iface);
573 TRACE("(%p)\n", This);
575 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
578 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
580 HTMLDocument *This = PERSISTFILE_THIS(iface);
581 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
585 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
587 HTMLDocument *This = PERSISTFILE_THIS(iface);
589 DWORD len, written=0;
593 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
595 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
596 FILE_ATTRIBUTE_NORMAL, NULL);
597 if(file == INVALID_HANDLE_VALUE) {
598 WARN("Could not create file: %u\n", GetLastError());
602 hres = get_doc_string(This, &str, &len);
606 WriteFile(file, str, len, &written, NULL);
611 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
613 HTMLDocument *This = PERSISTFILE_THIS(iface);
614 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
618 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
620 HTMLDocument *This = PERSISTFILE_THIS(iface);
621 FIXME("(%p)->(%p)\n", This, pszFileName);
625 static const IPersistFileVtbl PersistFileVtbl = {
626 PersistFile_QueryInterface,
629 PersistFile_GetClassID,
633 PersistFile_SaveCompleted,
634 PersistFile_GetCurFile
637 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
639 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
640 REFIID riid, void **ppv)
642 HTMLDocument *This = PERSTRINIT_THIS(iface);
643 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
646 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
648 HTMLDocument *This = PERSTRINIT_THIS(iface);
649 return IHTMLDocument2_AddRef(HTMLDOC(This));
652 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
654 HTMLDocument *This = PERSTRINIT_THIS(iface);
655 return IHTMLDocument2_Release(HTMLDOC(This));
658 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
660 HTMLDocument *This = PERSTRINIT_THIS(iface);
661 return IPersist_GetClassID(PERSIST(This), pClassID);
664 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
666 HTMLDocument *This = PERSTRINIT_THIS(iface);
668 TRACE("(%p)\n", This);
670 if(This->usermode == EDITMODE)
671 return editor_is_dirty(This);
676 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
678 HTMLDocument *This = PERSTRINIT_THIS(iface);
682 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
684 TRACE("(%p)->(%p)\n", This, pStm);
686 hres = CreateURLMoniker(NULL, about_blankW, &mon);
688 WARN("CreateURLMoniker failed: %08x\n", hres);
692 hres = set_moniker(This, mon, NULL, NULL);
693 IMoniker_Release(mon);
697 return load_stream(This->bscallback, pStm);
700 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
703 HTMLDocument *This = PERSTRINIT_THIS(iface);
705 DWORD len, written=0;
708 WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
710 hres = get_doc_string(This, &str, &len);
715 hres = IStream_Write(pStm, str, len, &written);
717 FIXME("Write failed: %08x\n", hres);
723 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
724 ULARGE_INTEGER *pcbSize)
726 HTMLDocument *This = PERSTRINIT_THIS(iface);
727 FIXME("(%p)->(%p)\n", This, pcbSize);
731 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
733 HTMLDocument *This = PERSTRINIT_THIS(iface);
734 FIXME("(%p)\n", This);
738 #undef PERSTRINIT_THIS
740 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
741 PersistStreamInit_QueryInterface,
742 PersistStreamInit_AddRef,
743 PersistStreamInit_Release,
744 PersistStreamInit_GetClassID,
745 PersistStreamInit_IsDirty,
746 PersistStreamInit_Load,
747 PersistStreamInit_Save,
748 PersistStreamInit_GetSizeMax,
749 PersistStreamInit_InitNew
752 void HTMLDocument_Persist_Init(HTMLDocument *This)
754 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
755 This->lpPersistFileVtbl = &PersistFileVtbl;
756 This->lpMonikerPropVtbl = &MonikerPropVtbl;
757 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
759 This->bscallback = NULL;