mshtml: Added get_URL implementation.
[wine.git] / dlls / mshtml / persist.c
blob58ca42229f08d4191088d076d9f5120aeccb536b
1 /*
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
19 #include "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "ole2.h"
32 #include "shlguid.h"
33 #include "idispids.h"
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;
47 BINDINFO bindinfo;
48 DWORD bindf = 0;
49 DWORD post_len = 0, headers_len = 0;
50 LPWSTR headers = NULL;
51 WCHAR emptystr[] = {0};
52 char *data;
53 HRESULT hres;
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 */
60 if(!bctx)
61 return NULL;
63 hres = IBindCtx_GetObjectParam(bctx, _BSCB_Holder_, (IUnknown**)&callback);
64 if(FAILED(hres))
65 return NULL;
67 hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
68 (void**)&http_negotiate);
69 if(SUCCEEDED(hres)) {
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);
93 if(headers_len) {
94 WideCharToMultiByte(CP_ACP, 0, headers, -1, data, -1, NULL, NULL);
95 CoTaskMemFree(headers);
98 if(post_len) {
99 sprintf(data+len, content_length, post_len);
100 len = strlen(data);
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);
113 return ret;
116 void set_current_mon(HTMLDocument *This, IMoniker *mon)
118 HRESULT hres;
120 if(This->mon) {
121 IMoniker_Release(This->mon);
122 This->mon = NULL;
125 if(This->url) {
126 SysFreeString(This->url);
127 This->url = NULL;
130 if(!mon)
131 return;
133 IMoniker_AddRef(mon);
134 This->mon = mon;
136 hres = IMoniker_GetDisplayName(mon, NULL, NULL, &This->url);
137 if(FAILED(hres))
138 WARN("GetDisplayName failed: %08x\n", hres);
141 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL *bind_complete)
143 BSCallback *bscallback;
144 LPOLESTR url = NULL;
145 task_t *task;
146 HRESULT hres;
147 nsresult nsres;
149 if(pibc) {
150 IUnknown *unk = NULL;
152 /* FIXME:
153 * Use params:
154 * "__PrecreatedObject"
155 * "BIND_CONTEXT_PARAM"
156 * "__HTMLLOADOPTIONS"
157 * "__DWNBINDINFO"
158 * "URL Context"
159 * "CBinding Context"
160 * "_ITransData_Object_"
161 * "_EnumFORMATETC_"
164 IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
165 if(unk) {
166 IOleClientSite *client = NULL;
168 hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
169 if(SUCCEEDED(hres)) {
170 TRACE("Got client site %p\n", client);
171 IOleObject_SetClientSite(OLEOBJ(This), client);
172 IOleClientSite_Release(client);
175 IUnknown_Release(unk);
179 This->readystate = READYSTATE_LOADING;
180 call_property_onchanged(This->cp_propnotif, DISPID_READYSTATE);
181 update_doc(This, UPDATE_TITLE);
183 HTMLDocument_LockContainer(This, TRUE);
185 hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
186 if(FAILED(hres)) {
187 WARN("GetDiaplayName failed: %08x\n", hres);
188 return hres;
191 TRACE("got url: %s\n", debugstr_w(url));
193 set_current_mon(This, mon);
195 if(This->client) {
196 VARIANT silent, offline;
197 IOleCommandTarget *cmdtrg = NULL;
199 hres = get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
200 if(SUCCEEDED(hres)) {
201 if(V_VT(&silent) != VT_BOOL)
202 WARN("V_VT(silent) = %d\n", V_VT(&silent));
203 else if(V_BOOL(&silent))
204 FIXME("silent == true\n");
207 hres = get_client_disp_property(This->client,
208 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
209 if(SUCCEEDED(hres)) {
210 if(V_VT(&silent) != VT_BOOL)
211 WARN("V_VT(offline) = %d\n", V_VT(&silent));
212 else if(V_BOOL(&silent))
213 FIXME("offline == true\n");
216 hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
217 (void**)&cmdtrg);
218 if(SUCCEEDED(hres)) {
219 VARIANT var;
221 V_VT(&var) = VT_I4;
222 V_I4(&var) = 0;
223 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
225 IOleCommandTarget_Release(cmdtrg);
229 bscallback = create_bscallback(mon);
231 if(This->frame) {
232 task = mshtml_alloc(sizeof(task_t));
234 task->doc = This;
235 task->task_id = TASK_SETPROGRESS;
236 task->next = NULL;
238 push_task(task);
241 task = mshtml_alloc(sizeof(task_t));
243 task->doc = This;
244 task->task_id = TASK_SETDOWNLOADSTATE;
245 task->next = NULL;
247 push_task(task);
249 if(This->nscontainer) {
250 nsIInputStream *post_data_stream = get_post_data_stream(pibc);
252 This->nscontainer->bscallback = bscallback;
253 nsres = nsIWebNavigation_LoadURI(This->nscontainer->navigation, url,
254 LOAD_FLAGS_NONE, NULL, post_data_stream, NULL);
255 This->nscontainer->bscallback = NULL;
257 if(post_data_stream)
258 nsIInputStream_Release(post_data_stream);
260 if(NS_SUCCEEDED(nsres)) {
261 /* FIXME: don't return here (URL Moniker needs to be good enough) */
263 IBindStatusCallback_Release(STATUSCLB(bscallback));
264 CoTaskMemFree(url);
266 if(bind_complete)
267 *bind_complete = TRUE;
268 return S_OK;
269 }else if(nsres != WINE_NS_LOAD_FROM_MONIKER) {
270 WARN("LoadURI failed: %08x\n", nsres);
274 set_document_bscallback(This, bscallback);
275 IBindStatusCallback_Release(STATUSCLB(bscallback));
276 CoTaskMemFree(url);
278 if(bind_complete)
279 *bind_complete = FALSE;
280 return S_OK;
283 static HRESULT get_doc_string(HTMLDocument *This, char **str, DWORD *len)
285 nsIDOMDocument *nsdoc;
286 nsIDOMNode *nsnode;
287 LPCWSTR strw;
288 nsAString nsstr;
289 nsresult nsres;
291 if(!This->nscontainer) {
292 WARN("no nscontainer, returning NULL\n");
293 return S_OK;
296 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
297 if(NS_FAILED(nsres)) {
298 ERR("GetDocument failed: %08x\n", nsres);
299 return E_FAIL;
302 nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
303 nsIDOMDocument_Release(nsdoc);
304 if(NS_FAILED(nsres)) {
305 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
306 return E_FAIL;
309 nsAString_Init(&nsstr, NULL);
310 nsnode_to_nsstring(nsnode, &nsstr);
311 nsIDOMNode_Release(nsnode);
313 nsAString_GetData(&nsstr, &strw, NULL);
314 TRACE("%s\n", debugstr_w(strw));
316 *len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
317 *str = mshtml_alloc(*len);
318 WideCharToMultiByte(CP_ACP, 0, strw, -1, *str, *len, NULL, NULL);
320 nsAString_Finish(&nsstr);
322 return S_OK;
326 /**********************************************************
327 * IPersistMoniker implementation
330 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
332 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
333 void **ppvObject)
335 HTMLDocument *This = PERSISTMON_THIS(iface);
336 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
339 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
341 HTMLDocument *This = PERSISTMON_THIS(iface);
342 return IHTMLDocument2_AddRef(HTMLDOC(This));
345 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
347 HTMLDocument *This = PERSISTMON_THIS(iface);
348 return IHTMLDocument2_Release(HTMLDOC(This));
351 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
353 HTMLDocument *This = PERSISTMON_THIS(iface);
354 return IPersist_GetClassID(PERSIST(This), pClassID);
357 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
359 HTMLDocument *This = PERSISTMON_THIS(iface);
361 TRACE("(%p)\n", This);
363 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
366 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
367 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
369 HTMLDocument *This = PERSISTMON_THIS(iface);
370 BOOL bind_complete = FALSE;
371 HRESULT hres;
373 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
375 hres = set_moniker(This, pimkName, pibc, &bind_complete);
376 if(FAILED(hres))
377 return hres;
379 if(!bind_complete)
380 return start_binding(This->bscallback);
382 return S_OK;
385 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
386 LPBC pbc, BOOL fRemember)
388 HTMLDocument *This = PERSISTMON_THIS(iface);
389 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
390 return E_NOTIMPL;
393 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
395 HTMLDocument *This = PERSISTMON_THIS(iface);
396 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
397 return E_NOTIMPL;
400 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
402 HTMLDocument *This = PERSISTMON_THIS(iface);
404 TRACE("(%p)->(%p)\n", This, ppimkName);
406 if(!This->mon)
407 return E_UNEXPECTED;
409 IMoniker_AddRef(This->mon);
410 *ppimkName = This->mon;
411 return S_OK;
414 static const IPersistMonikerVtbl PersistMonikerVtbl = {
415 PersistMoniker_QueryInterface,
416 PersistMoniker_AddRef,
417 PersistMoniker_Release,
418 PersistMoniker_GetClassID,
419 PersistMoniker_IsDirty,
420 PersistMoniker_Load,
421 PersistMoniker_Save,
422 PersistMoniker_SaveCompleted,
423 PersistMoniker_GetCurMoniker
426 /**********************************************************
427 * IMonikerProp implementation
430 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
432 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
434 HTMLDocument *This = MONPROP_THIS(iface);
435 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
438 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
440 HTMLDocument *This = MONPROP_THIS(iface);
441 return IHTMLDocument2_AddRef(HTMLDOC(This));
444 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
446 HTMLDocument *This = MONPROP_THIS(iface);
447 return IHTMLDocument_Release(HTMLDOC(This));
450 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
452 HTMLDocument *This = MONPROP_THIS(iface);
453 FIXME("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
454 return E_NOTIMPL;
457 static const IMonikerPropVtbl MonikerPropVtbl = {
458 MonikerProp_QueryInterface,
459 MonikerProp_AddRef,
460 MonikerProp_Release,
461 MonikerProp_PutProperty
464 /**********************************************************
465 * IPersistFile implementation
468 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
470 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
472 HTMLDocument *This = PERSISTFILE_THIS(iface);
473 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
476 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
478 HTMLDocument *This = PERSISTFILE_THIS(iface);
479 return IHTMLDocument2_AddRef(HTMLDOC(This));
482 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
484 HTMLDocument *This = PERSISTFILE_THIS(iface);
485 return IHTMLDocument2_Release(HTMLDOC(This));
488 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
490 HTMLDocument *This = PERSISTFILE_THIS(iface);
492 TRACE("(%p)->(%p)\n", This, pClassID);
494 if(!pClassID)
495 return E_INVALIDARG;
497 memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
498 return S_OK;
501 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
503 HTMLDocument *This = PERSISTFILE_THIS(iface);
505 TRACE("(%p)\n", This);
507 return IPersistStreamInit_IsDirty(PERSTRINIT(This));
510 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
512 HTMLDocument *This = PERSISTFILE_THIS(iface);
513 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
514 return E_NOTIMPL;
517 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
519 HTMLDocument *This = PERSISTFILE_THIS(iface);
520 char *str;
521 DWORD len, written=0;
522 HANDLE file;
523 HRESULT hres;
525 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
527 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
528 FILE_ATTRIBUTE_NORMAL, NULL);
529 if(file == INVALID_HANDLE_VALUE) {
530 WARN("Could not create file: %u\n", GetLastError());
531 return E_FAIL;
534 hres = get_doc_string(This, &str, &len);
535 if(FAILED(hres))
536 return hres;
538 WriteFile(file, str, len, &written, NULL);
539 CloseHandle(file);
540 return S_OK;
543 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
545 HTMLDocument *This = PERSISTFILE_THIS(iface);
546 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
547 return E_NOTIMPL;
550 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
552 HTMLDocument *This = PERSISTFILE_THIS(iface);
553 FIXME("(%p)->(%p)\n", This, pszFileName);
554 return E_NOTIMPL;
557 static const IPersistFileVtbl PersistFileVtbl = {
558 PersistFile_QueryInterface,
559 PersistFile_AddRef,
560 PersistFile_Release,
561 PersistFile_GetClassID,
562 PersistFile_IsDirty,
563 PersistFile_Load,
564 PersistFile_Save,
565 PersistFile_SaveCompleted,
566 PersistFile_GetCurFile
569 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
571 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
572 REFIID riid, void **ppv)
574 HTMLDocument *This = PERSTRINIT_THIS(iface);
575 return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
578 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
580 HTMLDocument *This = PERSTRINIT_THIS(iface);
581 return IHTMLDocument2_AddRef(HTMLDOC(This));
584 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
586 HTMLDocument *This = PERSTRINIT_THIS(iface);
587 return IHTMLDocument2_Release(HTMLDOC(This));
590 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
592 HTMLDocument *This = PERSTRINIT_THIS(iface);
593 return IPersist_GetClassID(PERSIST(This), pClassID);
596 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
598 HTMLDocument *This = PERSTRINIT_THIS(iface);
600 TRACE("(%p)\n", This);
602 if(This->usermode == EDITMODE)
603 FIXME("Unimplemented in edit mode\n");
605 return S_FALSE;
608 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
610 HTMLDocument *This = PERSTRINIT_THIS(iface);
611 IMoniker *mon;
612 HRESULT hres;
614 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
616 TRACE("(%p)->(%p)\n", This, pStm);
618 hres = CreateURLMoniker(NULL, about_blankW, &mon);
619 if(FAILED(hres)) {
620 WARN("CreateURLMoniker failed: %08x\n", hres);
621 return hres;
624 hres = set_moniker(This, mon, NULL, NULL);
625 IMoniker_Release(mon);
626 if(FAILED(hres))
627 return hres;
629 return load_stream(This->bscallback, pStm);
632 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
633 BOOL fClearDirty)
635 HTMLDocument *This = PERSTRINIT_THIS(iface);
636 char *str;
637 DWORD len, written=0;
638 HRESULT hres;
640 WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
642 hres = get_doc_string(This, &str, &len);
643 if(FAILED(hres))
644 return hres;
647 hres = IStream_Write(pStm, str, len, &written);
648 if(FAILED(hres))
649 FIXME("Write failed: %08x\n", hres);
651 mshtml_free(str);
652 return S_OK;
655 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
656 ULARGE_INTEGER *pcbSize)
658 HTMLDocument *This = PERSTRINIT_THIS(iface);
659 FIXME("(%p)->(%p)\n", This, pcbSize);
660 return E_NOTIMPL;
663 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
665 HTMLDocument *This = PERSTRINIT_THIS(iface);
666 FIXME("(%p)\n", This);
667 return E_NOTIMPL;
670 #undef PERSTRINIT_THIS
672 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
673 PersistStreamInit_QueryInterface,
674 PersistStreamInit_AddRef,
675 PersistStreamInit_Release,
676 PersistStreamInit_GetClassID,
677 PersistStreamInit_IsDirty,
678 PersistStreamInit_Load,
679 PersistStreamInit_Save,
680 PersistStreamInit_GetSizeMax,
681 PersistStreamInit_InitNew
684 void HTMLDocument_Persist_Init(HTMLDocument *This)
686 This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
687 This->lpPersistFileVtbl = &PersistFileVtbl;
688 This->lpMonikerPropVtbl = &MonikerPropVtbl;
689 This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
691 This->bscallback = NULL;
692 This->mon = NULL;
693 This->url = NULL;