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 static nsIInputStream
*get_post_data_stream(IBindCtx
*bctx
)
44 nsIInputStream
*ret
= NULL
;
45 IBindStatusCallback
*callback
;
46 IHttpNegotiate
*http_negotiate
;
49 DWORD post_len
= 0, headers_len
= 0;
50 LPWSTR headers
= NULL
;
51 WCHAR emptystr
[] = {0};
55 static WCHAR _BSCB_Holder_
[] =
56 {'_','B','S','C','B','_','H','o','l','d','e','r','_',0};
59 /* FIXME: This should be done in URLMoniker */
63 hres
= IBindCtx_GetObjectParam(bctx
, _BSCB_Holder_
, (IUnknown
**)&callback
);
67 hres
= IBindStatusCallback_QueryInterface(callback
, &IID_IHttpNegotiate
,
68 (void**)&http_negotiate
);
70 hres
= IHttpNegotiate_BeginningTransaction(http_negotiate
, emptystr
,
71 emptystr
, 0, &headers
);
72 IHttpNegotiate_Release(http_negotiate
);
74 if(SUCCEEDED(hres
) && headers
)
75 headers_len
= WideCharToMultiByte(CP_ACP
, 0, headers
, -1, NULL
, 0, NULL
, NULL
);
78 memset(&bindinfo
, 0, sizeof(bindinfo
));
79 bindinfo
.cbSize
= sizeof(bindinfo
);
81 hres
= IBindStatusCallback_GetBindInfo(callback
, &bindf
, &bindinfo
);
83 if(SUCCEEDED(hres
) && bindinfo
.dwBindVerb
== BINDVERB_POST
)
84 post_len
= bindinfo
.cbstgmedData
;
86 if(headers_len
|| post_len
) {
87 int len
= headers_len
? headers_len
-1 : 0;
89 static const char content_length
[] = "Content-Length: %u\r\n\r\n";
91 data
= mshtml_alloc(headers_len
+post_len
+sizeof(content_length
)+8);
94 WideCharToMultiByte(CP_ACP
, 0, headers
, -1, data
, -1, NULL
, NULL
);
95 CoTaskMemFree(headers
);
99 sprintf(data
+len
, content_length
, post_len
);
102 memcpy(data
+len
, bindinfo
.stgmedData
.u
.hGlobal
, post_len
);
105 TRACE("data = %s\n", debugstr_an(data
, len
+post_len
));
107 ret
= create_nsstream(data
, len
+post_len
);
110 ReleaseBindInfo(&bindinfo
);
111 IBindStatusCallback_Release(callback
);
116 void set_current_mon(HTMLDocument
*This
, IMoniker
*mon
)
119 IMoniker_Release(This
->mon
);
121 IMoniker_AddRef(mon
);
125 static HRESULT
set_moniker(HTMLDocument
*This
, IMoniker
*mon
, IBindCtx
*pibc
, BOOL
*bind_complete
)
127 BSCallback
*bscallback
;
134 IUnknown
*unk
= NULL
;
138 * "__PrecreatedObject"
139 * "BIND_CONTEXT_PARAM"
140 * "__HTMLLOADOPTIONS"
144 * "_ITransData_Object_"
148 IBindCtx_GetObjectParam(pibc
, (LPOLESTR
)SZ_HTML_CLIENTSITE_OBJECTPARAM
, &unk
);
150 IOleClientSite
*client
= NULL
;
152 hres
= IUnknown_QueryInterface(unk
, &IID_IOleClientSite
, (void**)&client
);
153 if(SUCCEEDED(hres
)) {
154 TRACE("Got client site %p\n", client
);
155 IOleObject_SetClientSite(OLEOBJ(This
), client
);
156 IOleClientSite_Release(client
);
159 IUnknown_Release(unk
);
163 This
->readystate
= READYSTATE_LOADING
;
164 call_property_onchanged(This
->cp_propnotif
, DISPID_READYSTATE
);
165 update_doc(This
, UPDATE_TITLE
);
167 HTMLDocument_LockContainer(This
, TRUE
);
169 hres
= IMoniker_GetDisplayName(mon
, pibc
, NULL
, &url
);
171 WARN("GetDiaplayName failed: %08x\n", hres
);
175 TRACE("got url: %s\n", debugstr_w(url
));
177 set_current_mon(This
, mon
);
180 VARIANT silent
, offline
;
181 IOleCommandTarget
*cmdtrg
= NULL
;
183 hres
= get_client_disp_property(This
->client
, DISPID_AMBIENT_SILENT
, &silent
);
184 if(SUCCEEDED(hres
)) {
185 if(V_VT(&silent
) != VT_BOOL
)
186 WARN("V_VT(silent) = %d\n", V_VT(&silent
));
187 else if(V_BOOL(&silent
))
188 FIXME("silent == true\n");
191 hres
= get_client_disp_property(This
->client
,
192 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED
, &offline
);
193 if(SUCCEEDED(hres
)) {
194 if(V_VT(&silent
) != VT_BOOL
)
195 WARN("V_VT(offline) = %d\n", V_VT(&silent
));
196 else if(V_BOOL(&silent
))
197 FIXME("offline == true\n");
200 hres
= IOleClientSite_QueryInterface(This
->client
, &IID_IOleCommandTarget
,
202 if(SUCCEEDED(hres
)) {
207 IOleCommandTarget_Exec(cmdtrg
, &CGID_ShellDocView
, 37, 0, &var
, NULL
);
209 IOleCommandTarget_Release(cmdtrg
);
213 bscallback
= create_bscallback(mon
);
216 task
= mshtml_alloc(sizeof(task_t
));
219 task
->task_id
= TASK_SETPROGRESS
;
225 task
= mshtml_alloc(sizeof(task_t
));
228 task
->task_id
= TASK_SETDOWNLOADSTATE
;
233 if(This
->nscontainer
) {
234 nsIInputStream
*post_data_stream
= get_post_data_stream(pibc
);
236 This
->nscontainer
->bscallback
= bscallback
;
237 nsres
= nsIWebNavigation_LoadURI(This
->nscontainer
->navigation
, url
,
238 LOAD_FLAGS_NONE
, NULL
, post_data_stream
, NULL
);
239 This
->nscontainer
->bscallback
= NULL
;
242 nsIInputStream_Release(post_data_stream
);
244 if(NS_SUCCEEDED(nsres
)) {
245 /* FIXME: don't return here (URL Moniker needs to be good enough) */
247 IBindStatusCallback_Release(STATUSCLB(bscallback
));
251 *bind_complete
= TRUE
;
253 }else if(nsres
!= WINE_NS_LOAD_FROM_MONIKER
) {
254 WARN("LoadURI failed: %08x\n", nsres
);
258 set_document_bscallback(This
, bscallback
);
259 IBindStatusCallback_Release(STATUSCLB(bscallback
));
263 *bind_complete
= FALSE
;
267 static HRESULT
get_doc_string(HTMLDocument
*This
, char **str
, DWORD
*len
)
269 nsIDOMDocument
*nsdoc
;
275 if(!This
->nscontainer
) {
276 WARN("no nscontainer, returning NULL\n");
280 nsres
= nsIWebNavigation_GetDocument(This
->nscontainer
->navigation
, &nsdoc
);
281 if(NS_FAILED(nsres
)) {
282 ERR("GetDocument failed: %08x\n", nsres
);
286 nsres
= nsIDOMDocument_QueryInterface(nsdoc
, &IID_nsIDOMNode
, (void**)&nsnode
);
287 nsIDOMDocument_Release(nsdoc
);
288 if(NS_FAILED(nsres
)) {
289 ERR("Could not get nsIDOMNode failed: %08x\n", nsres
);
293 nsAString_Init(&nsstr
, NULL
);
294 nsnode_to_nsstring(nsnode
, &nsstr
);
295 nsIDOMNode_Release(nsnode
);
297 nsAString_GetData(&nsstr
, &strw
, NULL
);
298 TRACE("%s\n", debugstr_w(strw
));
300 *len
= WideCharToMultiByte(CP_ACP
, 0, strw
, -1, NULL
, 0, NULL
, NULL
);
301 *str
= mshtml_alloc(*len
);
302 WideCharToMultiByte(CP_ACP
, 0, strw
, -1, *str
, *len
, NULL
, NULL
);
304 nsAString_Finish(&nsstr
);
310 /**********************************************************
311 * IPersistMoniker implementation
314 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
316 static HRESULT WINAPI
PersistMoniker_QueryInterface(IPersistMoniker
*iface
, REFIID riid
,
319 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
320 return IHTMLDocument2_QueryInterface(HTMLDOC(This
), riid
, ppvObject
);
323 static ULONG WINAPI
PersistMoniker_AddRef(IPersistMoniker
*iface
)
325 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
326 return IHTMLDocument2_AddRef(HTMLDOC(This
));
329 static ULONG WINAPI
PersistMoniker_Release(IPersistMoniker
*iface
)
331 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
332 return IHTMLDocument2_Release(HTMLDOC(This
));
335 static HRESULT WINAPI
PersistMoniker_GetClassID(IPersistMoniker
*iface
, CLSID
*pClassID
)
337 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
338 return IPersist_GetClassID(PERSIST(This
), pClassID
);
341 static HRESULT WINAPI
PersistMoniker_IsDirty(IPersistMoniker
*iface
)
343 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
345 TRACE("(%p)\n", This
);
347 return IPersistStreamInit_IsDirty(PERSTRINIT(This
));
350 static HRESULT WINAPI
PersistMoniker_Load(IPersistMoniker
*iface
, BOOL fFullyAvailable
,
351 IMoniker
*pimkName
, LPBC pibc
, DWORD grfMode
)
353 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
354 BOOL bind_complete
= FALSE
;
357 TRACE("(%p)->(%x %p %p %08x)\n", This
, fFullyAvailable
, pimkName
, pibc
, grfMode
);
359 hres
= set_moniker(This
, pimkName
, pibc
, &bind_complete
);
364 return start_binding(This
->bscallback
);
369 static HRESULT WINAPI
PersistMoniker_Save(IPersistMoniker
*iface
, IMoniker
*pimkName
,
370 LPBC pbc
, BOOL fRemember
)
372 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
373 FIXME("(%p)->(%p %p %x)\n", This
, pimkName
, pbc
, fRemember
);
377 static HRESULT WINAPI
PersistMoniker_SaveCompleted(IPersistMoniker
*iface
, IMoniker
*pimkName
, LPBC pibc
)
379 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
380 FIXME("(%p)->(%p %p)\n", This
, pimkName
, pibc
);
384 static HRESULT WINAPI
PersistMoniker_GetCurMoniker(IPersistMoniker
*iface
, IMoniker
**ppimkName
)
386 HTMLDocument
*This
= PERSISTMON_THIS(iface
);
388 TRACE("(%p)->(%p)\n", This
, ppimkName
);
393 IMoniker_AddRef(This
->mon
);
394 *ppimkName
= This
->mon
;
398 static const IPersistMonikerVtbl PersistMonikerVtbl
= {
399 PersistMoniker_QueryInterface
,
400 PersistMoniker_AddRef
,
401 PersistMoniker_Release
,
402 PersistMoniker_GetClassID
,
403 PersistMoniker_IsDirty
,
406 PersistMoniker_SaveCompleted
,
407 PersistMoniker_GetCurMoniker
410 /**********************************************************
411 * IMonikerProp implementation
414 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
416 static HRESULT WINAPI
MonikerProp_QueryInterface(IMonikerProp
*iface
, REFIID riid
, void **ppvObject
)
418 HTMLDocument
*This
= MONPROP_THIS(iface
);
419 return IHTMLDocument2_QueryInterface(HTMLDOC(This
), riid
, ppvObject
);
422 static ULONG WINAPI
MonikerProp_AddRef(IMonikerProp
*iface
)
424 HTMLDocument
*This
= MONPROP_THIS(iface
);
425 return IHTMLDocument2_AddRef(HTMLDOC(This
));
428 static ULONG WINAPI
MonikerProp_Release(IMonikerProp
*iface
)
430 HTMLDocument
*This
= MONPROP_THIS(iface
);
431 return IHTMLDocument_Release(HTMLDOC(This
));
434 static HRESULT WINAPI
MonikerProp_PutProperty(IMonikerProp
*iface
, MONIKERPROPERTY mkp
, LPCWSTR val
)
436 HTMLDocument
*This
= MONPROP_THIS(iface
);
437 FIXME("(%p)->(%d %s)\n", This
, mkp
, debugstr_w(val
));
441 static const IMonikerPropVtbl MonikerPropVtbl
= {
442 MonikerProp_QueryInterface
,
445 MonikerProp_PutProperty
448 /**********************************************************
449 * IPersistFile implementation
452 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
454 static HRESULT WINAPI
PersistFile_QueryInterface(IPersistFile
*iface
, REFIID riid
, void **ppvObject
)
456 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
457 return IHTMLDocument2_QueryInterface(HTMLDOC(This
), riid
, ppvObject
);
460 static ULONG WINAPI
PersistFile_AddRef(IPersistFile
*iface
)
462 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
463 return IHTMLDocument2_AddRef(HTMLDOC(This
));
466 static ULONG WINAPI
PersistFile_Release(IPersistFile
*iface
)
468 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
469 return IHTMLDocument2_Release(HTMLDOC(This
));
472 static HRESULT WINAPI
PersistFile_GetClassID(IPersistFile
*iface
, CLSID
*pClassID
)
474 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
476 TRACE("(%p)->(%p)\n", This
, pClassID
);
481 memcpy(pClassID
, &CLSID_HTMLDocument
, sizeof(CLSID
));
485 static HRESULT WINAPI
PersistFile_IsDirty(IPersistFile
*iface
)
487 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
489 TRACE("(%p)\n", This
);
491 return IPersistStreamInit_IsDirty(PERSTRINIT(This
));
494 static HRESULT WINAPI
PersistFile_Load(IPersistFile
*iface
, LPCOLESTR pszFileName
, DWORD dwMode
)
496 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
497 FIXME("(%p)->(%s %08x)\n", This
, debugstr_w(pszFileName
), dwMode
);
501 static HRESULT WINAPI
PersistFile_Save(IPersistFile
*iface
, LPCOLESTR pszFileName
, BOOL fRemember
)
503 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
505 DWORD len
, written
=0;
509 TRACE("(%p)->(%s %x)\n", This
, debugstr_w(pszFileName
), fRemember
);
511 file
= CreateFileW(pszFileName
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
512 FILE_ATTRIBUTE_NORMAL
, NULL
);
513 if(file
== INVALID_HANDLE_VALUE
) {
514 WARN("Could not create file: %u\n", GetLastError());
518 hres
= get_doc_string(This
, &str
, &len
);
522 WriteFile(file
, str
, len
, &written
, NULL
);
527 static HRESULT WINAPI
PersistFile_SaveCompleted(IPersistFile
*iface
, LPCOLESTR pszFileName
)
529 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
530 FIXME("(%p)->(%s)\n", This
, debugstr_w(pszFileName
));
534 static HRESULT WINAPI
PersistFile_GetCurFile(IPersistFile
*iface
, LPOLESTR
*pszFileName
)
536 HTMLDocument
*This
= PERSISTFILE_THIS(iface
);
537 FIXME("(%p)->(%p)\n", This
, pszFileName
);
541 static const IPersistFileVtbl PersistFileVtbl
= {
542 PersistFile_QueryInterface
,
545 PersistFile_GetClassID
,
549 PersistFile_SaveCompleted
,
550 PersistFile_GetCurFile
553 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
555 static HRESULT WINAPI
PersistStreamInit_QueryInterface(IPersistStreamInit
*iface
,
556 REFIID riid
, void **ppv
)
558 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
559 return IHTMLDocument2_QueryInterface(HTMLDOC(This
), riid
, ppv
);
562 static ULONG WINAPI
PersistStreamInit_AddRef(IPersistStreamInit
*iface
)
564 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
565 return IHTMLDocument2_AddRef(HTMLDOC(This
));
568 static ULONG WINAPI
PersistStreamInit_Release(IPersistStreamInit
*iface
)
570 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
571 return IHTMLDocument2_Release(HTMLDOC(This
));
574 static HRESULT WINAPI
PersistStreamInit_GetClassID(IPersistStreamInit
*iface
, CLSID
*pClassID
)
576 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
577 return IPersist_GetClassID(PERSIST(This
), pClassID
);
580 static HRESULT WINAPI
PersistStreamInit_IsDirty(IPersistStreamInit
*iface
)
582 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
584 TRACE("(%p)\n", This
);
586 if(This
->usermode
== EDITMODE
)
587 FIXME("Unimplemented in edit mode\n");
592 static HRESULT WINAPI
PersistStreamInit_Load(IPersistStreamInit
*iface
, LPSTREAM pStm
)
594 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
598 static const WCHAR about_blankW
[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
600 TRACE("(%p)->(%p)\n", This
, pStm
);
602 hres
= CreateURLMoniker(NULL
, about_blankW
, &mon
);
604 WARN("CreateURLMoniker failed: %08x\n", hres
);
608 hres
= set_moniker(This
, mon
, NULL
, NULL
);
609 IMoniker_Release(mon
);
613 return load_stream(This
->bscallback
, pStm
);
616 static HRESULT WINAPI
PersistStreamInit_Save(IPersistStreamInit
*iface
, LPSTREAM pStm
,
619 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
621 DWORD len
, written
=0;
624 WARN("(%p)->(%p %x) needs more work\n", This
, pStm
, fClearDirty
);
626 hres
= get_doc_string(This
, &str
, &len
);
631 hres
= IStream_Write(pStm
, str
, len
, &written
);
633 FIXME("Write failed: %08x\n", hres
);
639 static HRESULT WINAPI
PersistStreamInit_GetSizeMax(IPersistStreamInit
*iface
,
640 ULARGE_INTEGER
*pcbSize
)
642 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
643 FIXME("(%p)->(%p)\n", This
, pcbSize
);
647 static HRESULT WINAPI
PersistStreamInit_InitNew(IPersistStreamInit
*iface
)
649 HTMLDocument
*This
= PERSTRINIT_THIS(iface
);
650 FIXME("(%p)\n", This
);
654 #undef PERSTRINIT_THIS
656 static const IPersistStreamInitVtbl PersistStreamInitVtbl
= {
657 PersistStreamInit_QueryInterface
,
658 PersistStreamInit_AddRef
,
659 PersistStreamInit_Release
,
660 PersistStreamInit_GetClassID
,
661 PersistStreamInit_IsDirty
,
662 PersistStreamInit_Load
,
663 PersistStreamInit_Save
,
664 PersistStreamInit_GetSizeMax
,
665 PersistStreamInit_InitNew
668 void HTMLDocument_Persist_Init(HTMLDocument
*This
)
670 This
->lpPersistMonikerVtbl
= &PersistMonikerVtbl
;
671 This
->lpPersistFileVtbl
= &PersistFileVtbl
;
672 This
->lpMonikerPropVtbl
= &MonikerPropVtbl
;
673 This
->lpPersistStreamInitVtbl
= &PersistStreamInitVtbl
;
675 This
->bscallback
= NULL
;