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 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL *bind_complete)
118 BSCallback *bscallback;
125 IUnknown *unk = NULL;
129 * "__PrecreatedObject"
130 * "BIND_CONTEXT_PARAM"
131 * "__HTMLLOADOPTIONS"
135 * "_ITransData_Object_"
139 IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
141 IOleClientSite *client = NULL;
143 hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
144 if(SUCCEEDED(hres)) {
145 TRACE("Got client site %p\n", client);
146 IOleObject_SetClientSite(OLEOBJ(This), client);
147 IOleClientSite_Release(client);
150 IUnknown_Release(unk);
154 This->readystate = READYSTATE_LOADING;
155 call_property_onchanged(This->cp_propnotif, DISPID_READYSTATE);
157 HTMLDocument_LockContainer(This, TRUE);
159 hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
161 WARN("GetDiaplayName failed: %08x\n", hres);
165 TRACE("got url: %s\n", debugstr_w(url));
168 IOleCommandTarget *cmdtrg = NULL;
170 hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
172 if(SUCCEEDED(hres)) {
177 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
179 IOleCommandTarget_Release(cmdtrg);
184 VARIANT silent, offline;
186 hres = get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
187 if(SUCCEEDED(hres)) {
188 if(V_VT(&silent) != VT_BOOL)
189 WARN("V_VT(silent) = %d\n", V_VT(&silent));
190 else if(V_BOOL(&silent))
191 FIXME("silent == true\n");
194 hres = get_client_disp_property(This->client,
195 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
196 if(SUCCEEDED(hres)) {
197 if(V_VT(&silent) != VT_BOOL)
198 WARN("V_VT(offline) = %d\n", V_VT(&silent));
199 else if(V_BOOL(&silent))
200 FIXME("offline == true\n");
204 bscallback = create_bscallback(mon);
207 task = mshtml_alloc(sizeof(task_t));
210 task->task_id = TASK_SETPROGRESS;
216 task = mshtml_alloc(sizeof(task_t));
219 task->task_id = TASK_SETDOWNLOADSTATE;
224 if(This->nscontainer) {
225 nsIInputStream *post_data_stream = get_post_data_stream(pibc);
227 This->nscontainer->bscallback = bscallback;
228 nsres = nsIWebNavigation_LoadURI(This->nscontainer->navigation, url,
229 LOAD_FLAGS_NONE, NULL, post_data_stream, NULL);
230 This->nscontainer->bscallback = NULL;
233 nsIInputStream_Release(post_data_stream);
235 if(NS_SUCCEEDED(nsres)) {
236 /* FIXME: don't return here (URL Moniker needs to be good enough) */
238 IBindStatusCallback_Release(STATUSCLB(bscallback));
242 *bind_complete = TRUE;
244 }else if(nsres != WINE_NS_LOAD_FROM_MONIKER) {
245 WARN("LoadURI failed: %08x\n", nsres);
249 set_document_bscallback(This, bscallback);
250 IBindStatusCallback_Release(STATUSCLB(bscallback));
254 *bind_complete = FALSE;
258 static HRESULT get_doc_string(HTMLDocument *This, char **str, DWORD *len)
260 nsIDOMDocument *nsdoc;
266 if(!This->nscontainer) {
267 WARN("no nscontainer, returning NULL\n");
271 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
272 if(NS_FAILED(nsres)) {
273 ERR("GetDocument failed: %08x\n", nsres);
277 nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
278 nsIDOMDocument_Release(nsdoc);
279 if(NS_FAILED(nsres)) {
280 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
284 nsAString_Init(&nsstr, NULL);
285 nsnode_to_nsstring(nsnode, &nsstr);
286 nsIDOMNode_Release(nsnode);
288 nsAString_GetData(&nsstr, &strw, NULL);
289 TRACE("%s\n", debugstr_w(strw));
291 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
292 *str = mshtml_alloc(*len);
293 WideCharToMultiByte(CP_ACP, 0, strw, -1, *str, *len, NULL, NULL);
295 nsAString_Finish(&nsstr);
301 /**********************************************************
302 * IPersistMoniker implementation
305 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
307 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
310 HTMLDocument *This = PERSISTMON_THIS(iface);
311 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
314 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
316 HTMLDocument *This = PERSISTMON_THIS(iface);
317 return IHTMLDocument2_AddRef(HTMLDOC(This));
320 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
322 HTMLDocument *This = PERSISTMON_THIS(iface);
323 return IHTMLDocument2_Release(HTMLDOC(This));
326 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
328 HTMLDocument *This = PERSISTMON_THIS(iface);
329 return IPersist_GetClassID(PERSIST(This), pClassID);
332 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
334 HTMLDocument *This = PERSISTMON_THIS(iface);
335 FIXME("(%p)\n", This);
339 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
340 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
342 HTMLDocument *This = PERSISTMON_THIS(iface);
343 BOOL bind_complete = FALSE;
346 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
348 hres = set_moniker(This, pimkName, pibc, &bind_complete);
353 return start_binding(This->bscallback);
358 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
359 LPBC pbc, BOOL fRemember)
361 HTMLDocument *This = PERSISTMON_THIS(iface);
362 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
366 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
368 HTMLDocument *This = PERSISTMON_THIS(iface);
369 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
373 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
375 HTMLDocument *This = PERSISTMON_THIS(iface);
376 FIXME("(%p)->(%p)\n", This, ppimkName);
380 static const IPersistMonikerVtbl PersistMonikerVtbl = {
381 PersistMoniker_QueryInterface,
382 PersistMoniker_AddRef,
383 PersistMoniker_Release,
384 PersistMoniker_GetClassID,
385 PersistMoniker_IsDirty,
388 PersistMoniker_SaveCompleted,
389 PersistMoniker_GetCurMoniker
392 /**********************************************************
393 * IMonikerProp implementation
396 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
398 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
400 HTMLDocument *This = MONPROP_THIS(iface);
401 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
404 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
406 HTMLDocument *This = MONPROP_THIS(iface);
407 return IHTMLDocument2_AddRef(HTMLDOC(This));
410 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
412 HTMLDocument *This = MONPROP_THIS(iface);
413 return IHTMLDocument_Release(HTMLDOC(This));
416 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
418 HTMLDocument *This = MONPROP_THIS(iface);
419 FIXME("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
423 static const IMonikerPropVtbl MonikerPropVtbl = {
424 MonikerProp_QueryInterface,
427 MonikerProp_PutProperty
430 /**********************************************************
431 * IPersistFile implementation
434 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
436 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
438 HTMLDocument *This = PERSISTFILE_THIS(iface);
439 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
442 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
444 HTMLDocument *This = PERSISTFILE_THIS(iface);
445 return IHTMLDocument2_AddRef(HTMLDOC(This));
448 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
450 HTMLDocument *This = PERSISTFILE_THIS(iface);
451 return IHTMLDocument2_Release(HTMLDOC(This));
454 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
456 HTMLDocument *This = PERSISTFILE_THIS(iface);
458 TRACE("(%p)->(%p)\n", This, pClassID);
463 memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
467 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
469 HTMLDocument *This = PERSISTFILE_THIS(iface);
470 FIXME("(%p)\n", This);
474 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
476 HTMLDocument *This = PERSISTFILE_THIS(iface);
477 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
481 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
483 HTMLDocument *This = PERSISTFILE_THIS(iface);
485 DWORD len, written=0;
489 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
491 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
492 FILE_ATTRIBUTE_NORMAL, NULL);
493 if(file == INVALID_HANDLE_VALUE) {
494 WARN("Could not create file: %u\n", GetLastError());
498 hres = get_doc_string(This, &str, &len);
502 WriteFile(file, str, len, &written, NULL);
507 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
509 HTMLDocument *This = PERSISTFILE_THIS(iface);
510 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
514 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
516 HTMLDocument *This = PERSISTFILE_THIS(iface);
517 FIXME("(%p)->(%p)\n", This, pszFileName);
521 static const IPersistFileVtbl PersistFileVtbl = {
522 PersistFile_QueryInterface,
525 PersistFile_GetClassID,
529 PersistFile_SaveCompleted,
530 PersistFile_GetCurFile
533 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
535 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
536 REFIID riid, void **ppv)
538 HTMLDocument *This = PERSTRINIT_THIS(iface);
539 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
542 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
544 HTMLDocument *This = PERSTRINIT_THIS(iface);
545 return IHTMLDocument2_AddRef(HTMLDOC(This));
548 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
550 HTMLDocument *This = PERSTRINIT_THIS(iface);
551 return IHTMLDocument2_Release(HTMLDOC(This));
554 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
556 HTMLDocument *This = PERSTRINIT_THIS(iface);
557 return IPersist_GetClassID(PERSIST(This), pClassID);
560 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
562 HTMLDocument *This = PERSTRINIT_THIS(iface);
563 FIXME("(%p)\n", This);
567 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
569 HTMLDocument *This = PERSTRINIT_THIS(iface);
573 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
575 TRACE("(%p)->(%p)\n", This, pStm);
577 hres = CreateURLMoniker(NULL, about_blankW, &mon);
579 WARN("CreateURLMoniker failed: %08x\n", hres);
583 hres = set_moniker(This, mon, NULL, NULL);
584 IMoniker_Release(mon);
588 return load_stream(This->bscallback, pStm);
591 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
594 HTMLDocument *This = PERSTRINIT_THIS(iface);
596 DWORD len, written=0;
599 WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
601 hres = get_doc_string(This, &str, &len);
606 hres = IStream_Write(pStm, str, len, &written);
608 FIXME("Write failed: %08x\n", hres);
614 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
615 ULARGE_INTEGER *pcbSize)
617 HTMLDocument *This = PERSTRINIT_THIS(iface);
618 FIXME("(%p)->(%p)\n", This, pcbSize);
622 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
624 HTMLDocument *This = PERSTRINIT_THIS(iface);
625 FIXME("(%p)\n", This);
629 #undef PERSTRINIT_THIS
631 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
632 PersistStreamInit_QueryInterface,
633 PersistStreamInit_AddRef,
634 PersistStreamInit_Release,
635 PersistStreamInit_GetClassID,
636 PersistStreamInit_IsDirty,
637 PersistStreamInit_Load,
638 PersistStreamInit_Save,
639 PersistStreamInit_GetSizeMax,
640 PersistStreamInit_InitNew
643 void HTMLDocument_Persist_Init(HTMLDocument *This)
645 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
646 This->lpPersistFileVtbl = &PersistFileVtbl;
647 This->lpMonikerPropVtbl = &MonikerPropVtbl;
648 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
650 This->bscallback = NULL;