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 void set_current_mon(HTMLDocument
*This
, IMoniker
*mon
)
166 IMoniker_Release(This
->mon
);
171 CoTaskMemFree(This
->url
);
178 IMoniker_AddRef(mon
);
181 hres
= IMoniker_GetDisplayName(mon
, NULL
, NULL
, &This
->url
);
183 WARN("GetDisplayName failed: %08x\n", hres
);
186 static HRESULT
set_moniker(HTMLDocument
*This
, IMoniker
*mon
, IBindCtx
*pibc
, BOOL
*bind_complete
)
188 nsChannelBSC
*bscallback
;
195 IUnknown
*unk
= NULL
;
199 * "__PrecreatedObject"
200 * "BIND_CONTEXT_PARAM"
201 * "__HTMLLOADOPTIONS"
205 * "_ITransData_Object_"
209 IBindCtx_GetObjectParam(pibc
, (LPOLESTR
)SZ_HTML_CLIENTSITE_OBJECTPARAM
, &unk
);
211 IOleClientSite
*client
= NULL
;
213 hres
= IUnknown_QueryInterface(unk
, &IID_IOleClientSite
, (void**)&client
);
214 if(SUCCEEDED(hres
)) {
215 TRACE("Got client site %p\n", client
);
216 IOleObject_SetClientSite(OLEOBJ(This
), client
);
217 IOleClientSite_Release(client
);
220 IUnknown_Release(unk
);
224 This
->readystate
= READYSTATE_LOADING
;
225 call_property_onchanged(&This
->cp_propnotif
, DISPID_READYSTATE
);
226 update_doc(This
, UPDATE_TITLE
);
228 HTMLDocument_LockContainer(This
, TRUE
);
230 hres
= IMoniker_GetDisplayName(mon
, pibc
, NULL
, &url
);
232 WARN("GetDiaplayName failed: %08x\n", hres
);
236 TRACE("got url: %s\n", debugstr_w(url
));
238 set_current_mon(This
, mon
);
241 VARIANT silent
, offline
;
242 IOleCommandTarget
*cmdtrg
= NULL
;
244 hres
= get_client_disp_property(This
->client
, DISPID_AMBIENT_SILENT
, &silent
);
245 if(SUCCEEDED(hres
)) {
246 if(V_VT(&silent
) != VT_BOOL
)
247 WARN("V_VT(silent) = %d\n", V_VT(&silent
));
248 else if(V_BOOL(&silent
))
249 FIXME("silent == true\n");
252 hres
= get_client_disp_property(This
->client
,
253 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED
, &offline
);
254 if(SUCCEEDED(hres
)) {
255 if(V_VT(&silent
) != VT_BOOL
)
256 WARN("V_VT(offline) = %d\n", V_VT(&silent
));
257 else if(V_BOOL(&silent
))
258 FIXME("offline == true\n");
261 hres
= IOleClientSite_QueryInterface(This
->client
, &IID_IOleCommandTarget
,
263 if(SUCCEEDED(hres
)) {
268 IOleCommandTarget_Exec(cmdtrg
, &CGID_ShellDocView
, 37, 0, &var
, NULL
);
270 IOleCommandTarget_Release(cmdtrg
);
274 bscallback
= create_channelbsc(mon
);
277 task
= heap_alloc(sizeof(task_t
));
280 task
->task_id
= TASK_SETPROGRESS
;
286 task
= heap_alloc(sizeof(task_t
));
289 task
->task_id
= TASK_SETDOWNLOADSTATE
;
294 if(This
->nscontainer
) {
295 nsIInputStream
*post_data_stream
= get_post_data_stream(pibc
);
297 This
->nscontainer
->bscallback
= bscallback
;
298 nsres
= nsIWebNavigation_LoadURI(This
->nscontainer
->navigation
, url
,
299 LOAD_FLAGS_NONE
, NULL
, post_data_stream
, NULL
);
300 This
->nscontainer
->bscallback
= NULL
;
303 nsIInputStream_Release(post_data_stream
);
305 if(NS_SUCCEEDED(nsres
)) {
306 /* FIXME: don't return here (URL Moniker needs to be good enough) */
308 IUnknown_Release((IUnknown
*)bscallback
);
312 *bind_complete
= TRUE
;
314 }else if(nsres
!= WINE_NS_LOAD_FROM_MONIKER
) {
315 WARN("LoadURI failed: %08x\n", nsres
);
319 set_document_bscallback(This
, bscallback
);
320 IUnknown_Release((IUnknown
*)bscallback
);
324 *bind_complete
= FALSE
;
328 static HRESULT
get_doc_string(HTMLDocument
*This
, char **str
, DWORD
*len
)
330 nsIDOMDocument
*nsdoc
;
336 if(!This
->nscontainer
) {
337 WARN("no nscontainer, returning NULL\n");
341 nsres
= nsIWebNavigation_GetDocument(This
->nscontainer
->navigation
, &nsdoc
);
342 if(NS_FAILED(nsres
)) {
343 ERR("GetDocument failed: %08x\n", nsres
);
347 nsres
= nsIDOMDocument_QueryInterface(nsdoc
, &IID_nsIDOMNode
, (void**)&nsnode
);
348 nsIDOMDocument_Release(nsdoc
);
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
);
599 WriteFile(file
, str
, len
, &written
, NULL
);
604 static HRESULT WINAPI
PersistFile_SaveCompleted(IPersistFile
*iface
, LPCOLESTR pszFileName
)
606 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
607 FIXME("(%p)->(%s)\n", This
, debugstr_w(pszFileName
));
611 static HRESULT WINAPI
PersistFile_GetCurFile(IPersistFile
*iface
, LPOLESTR
*pszFileName
)
613 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
614 FIXME("(%p)->(%p)\n", This
, pszFileName
);
618 static const IPersistFileVtbl PersistFileVtbl
= {
619 PersistFile_QueryInterface
,
622 PersistFile_GetClassID
,
626 PersistFile_SaveCompleted
,
627 PersistFile_GetCurFile
630 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
632 static HRESULT WINAPI
PersistStreamInit_QueryInterface(IPersistStreamInit
*iface
,
633 REFIID riid
, void **ppv
)
635 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
636 return IHTMLDocument2_QueryInterface(HTMLDOC(This
), riid
, ppv
);
639 static ULONG WINAPI
PersistStreamInit_AddRef(IPersistStreamInit
*iface
)
641 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
642 return IHTMLDocument2_AddRef(HTMLDOC(This
));
645 static ULONG WINAPI
PersistStreamInit_Release(IPersistStreamInit
*iface
)
647 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
648 return IHTMLDocument2_Release(HTMLDOC(This
));
651 static HRESULT WINAPI
PersistStreamInit_GetClassID(IPersistStreamInit
*iface
, CLSID
*pClassID
)
653 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
654 return IPersist_GetClassID(PERSIST(This
), pClassID
);
657 static HRESULT WINAPI
PersistStreamInit_IsDirty(IPersistStreamInit
*iface
)
659 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
661 TRACE("(%p)\n", This
);
663 if(This
->usermode
== EDITMODE
)
664 return editor_is_dirty(This
);
669 static HRESULT WINAPI
PersistStreamInit_Load(IPersistStreamInit
*iface
, LPSTREAM pStm
)
671 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
675 static const WCHAR about_blankW
[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
677 TRACE("(%p)->(%p)\n", This
, pStm
);
679 hres
= CreateURLMoniker(NULL
, about_blankW
, &mon
);
681 WARN("CreateURLMoniker failed: %08x\n", hres
);
685 hres
= set_moniker(This
, mon
, NULL
, NULL
);
686 IMoniker_Release(mon
);
690 return channelbsc_load_stream(This
->bscallback
, pStm
);
693 static HRESULT WINAPI
PersistStreamInit_Save(IPersistStreamInit
*iface
, LPSTREAM pStm
,
696 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
698 DWORD len
, written
=0;
701 TRACE("(%p)->(%p %x)\n", This
, pStm
, fClearDirty
);
703 hres
= get_doc_string(This
, &str
, &len
);
707 hres
= IStream_Write(pStm
, str
, len
, &written
);
709 FIXME("Write failed: %08x\n", hres
);
714 set_dirty(This
, VARIANT_FALSE
);
719 static HRESULT WINAPI
PersistStreamInit_GetSizeMax(IPersistStreamInit
*iface
,
720 ULARGE_INTEGER
*pcbSize
)
722 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
723 FIXME("(%p)->(%p)\n", This
, pcbSize
);
727 static HRESULT WINAPI
PersistStreamInit_InitNew(IPersistStreamInit
*iface
)
729 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
730 FIXME("(%p)\n", This
);
734 #undef PERSTRINIT_THIS
736 static const IPersistStreamInitVtbl PersistStreamInitVtbl
= {
737 PersistStreamInit_QueryInterface
,
738 PersistStreamInit_AddRef
,
739 PersistStreamInit_Release
,
740 PersistStreamInit_GetClassID
,
741 PersistStreamInit_IsDirty
,
742 PersistStreamInit_Load
,
743 PersistStreamInit_Save
,
744 PersistStreamInit_GetSizeMax
,
745 PersistStreamInit_InitNew
748 void HTMLDocument_Persist_Init(HTMLDocument
*This
)
750 This
->lpPersistMonikerVtbl
= &PersistMonikerVtbl
;
751 This
->lpPersistFileVtbl
= &PersistFileVtbl
;
752 This
->lpMonikerPropVtbl
= &MonikerPropVtbl
;
753 This
->lpPersistStreamInitVtbl
= &PersistStreamInitVtbl
;
755 This
->bscallback
= NULL
;