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 = mshtml_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 = mshtml_alloc(sizeof(task_t));
287 task->task_id = TASK_SETPROGRESS;
293 task = mshtml_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, NULL);
366 TRACE("%s\n", debugstr_w(strw));
368 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
369 *str = mshtml_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);
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);
505 FIXME("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
509 static const IMonikerPropVtbl MonikerPropVtbl = {
510 MonikerProp_QueryInterface,
513 MonikerProp_PutProperty
516 /**********************************************************
517 * IPersistFile implementation
520 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
522 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
524 HTMLDocument *This = PERSISTFILE_THIS(iface);
525 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
528 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
530 HTMLDocument *This = PERSISTFILE_THIS(iface);
531 return IHTMLDocument2_AddRef(HTMLDOC(This));
534 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
536 HTMLDocument *This = PERSISTFILE_THIS(iface);
537 return IHTMLDocument2_Release(HTMLDOC(This));
540 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
542 HTMLDocument *This = PERSISTFILE_THIS(iface);
544 TRACE("(%p)->(%p)\n", This, pClassID);
549 memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
553 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
555 HTMLDocument *This = PERSISTFILE_THIS(iface);
557 TRACE("(%p)\n", This);
559 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
562 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
564 HTMLDocument *This = PERSISTFILE_THIS(iface);
565 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
569 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
571 HTMLDocument *This = PERSISTFILE_THIS(iface);
573 DWORD len, written=0;
577 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
579 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
580 FILE_ATTRIBUTE_NORMAL, NULL);
581 if(file == INVALID_HANDLE_VALUE) {
582 WARN("Could not create file: %u\n", GetLastError());
586 hres = get_doc_string(This, &str, &len);
590 WriteFile(file, str, len, &written, NULL);
595 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
597 HTMLDocument *This = PERSISTFILE_THIS(iface);
598 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
602 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
604 HTMLDocument *This = PERSISTFILE_THIS(iface);
605 FIXME("(%p)->(%p)\n", This, pszFileName);
609 static const IPersistFileVtbl PersistFileVtbl = {
610 PersistFile_QueryInterface,
613 PersistFile_GetClassID,
617 PersistFile_SaveCompleted,
618 PersistFile_GetCurFile
621 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
623 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
624 REFIID riid, void **ppv)
626 HTMLDocument *This = PERSTRINIT_THIS(iface);
627 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
630 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
632 HTMLDocument *This = PERSTRINIT_THIS(iface);
633 return IHTMLDocument2_AddRef(HTMLDOC(This));
636 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
638 HTMLDocument *This = PERSTRINIT_THIS(iface);
639 return IHTMLDocument2_Release(HTMLDOC(This));
642 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
644 HTMLDocument *This = PERSTRINIT_THIS(iface);
645 return IPersist_GetClassID(PERSIST(This), pClassID);
648 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
650 HTMLDocument *This = PERSTRINIT_THIS(iface);
652 TRACE("(%p)\n", This);
654 if(This->usermode == EDITMODE)
655 return editor_is_dirty(This);
660 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
662 HTMLDocument *This = PERSTRINIT_THIS(iface);
666 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
668 TRACE("(%p)->(%p)\n", This, pStm);
670 hres = CreateURLMoniker(NULL, about_blankW, &mon);
672 WARN("CreateURLMoniker failed: %08x\n", hres);
676 hres = set_moniker(This, mon, NULL, NULL);
677 IMoniker_Release(mon);
681 return load_stream(This->bscallback, pStm);
684 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
687 HTMLDocument *This = PERSTRINIT_THIS(iface);
689 DWORD len, written=0;
692 WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
694 hres = get_doc_string(This, &str, &len);
699 hres = IStream_Write(pStm, str, len, &written);
701 FIXME("Write failed: %08x\n", hres);
707 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
708 ULARGE_INTEGER *pcbSize)
710 HTMLDocument *This = PERSTRINIT_THIS(iface);
711 FIXME("(%p)->(%p)\n", This, pcbSize);
715 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
717 HTMLDocument *This = PERSTRINIT_THIS(iface);
718 FIXME("(%p)\n", This);
722 #undef PERSTRINIT_THIS
724 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
725 PersistStreamInit_QueryInterface,
726 PersistStreamInit_AddRef,
727 PersistStreamInit_Release,
728 PersistStreamInit_GetClassID,
729 PersistStreamInit_IsDirty,
730 PersistStreamInit_Load,
731 PersistStreamInit_Save,
732 PersistStreamInit_GetSizeMax,
733 PersistStreamInit_InitNew
736 void HTMLDocument_Persist_Init(HTMLDocument *This)
738 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
739 This->lpPersistFileVtbl = &PersistFileVtbl;
740 This->lpMonikerPropVtbl = &MonikerPropVtbl;
741 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
743 This->bscallback = NULL;