mshtml: Added GetCurMoniker implementation.
[wine.git] / dlls / mshtml / persist.c
blobf933b73d293fbcac5044c9eecdf9d61abfe21b03
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 if(This->mon)
119 IMoniker_Release(This->mon);
120 if(mon)
121 IMoniker_AddRef(mon);
122 This->mon = mon;
125 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL *bind_complete)
127 BSCallback *bscallback;
128 LPOLESTR url = NULL;
129 task_t *task;
130 HRESULT hres;
131 nsresult nsres;
133 if(pibc) {
134 IUnknown *unk = NULL;
136 /* FIXME:
137 * Use params:
138 * "__PrecreatedObject"
139 * "BIND_CONTEXT_PARAM"
140 * "__HTMLLOADOPTIONS"
141 * "__DWNBINDINFO"
142 * "URL Context"
143 * "CBinding Context"
144 * "_ITransData_Object_"
145 * "_EnumFORMATETC_"
148 IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
149 if(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);
170 if(FAILED(hres)) {
171 WARN("GetDiaplayName failed: %08x\n", hres);
172 return hres;
175 TRACE("got url: %s\n", debugstr_w(url));
177 set_current_mon(This, mon);
179 if(This->client) {
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,
201 (void**)&cmdtrg);
202 if(SUCCEEDED(hres)) {
203 VARIANT var;
205 V_VT(&var) = VT_I4;
206 V_I4(&var) = 0;
207 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
209 IOleCommandTarget_Release(cmdtrg);
213 bscallback = create_bscallback(mon);
215 if(This->frame) {
216 task = mshtml_alloc(sizeof(task_t));
218 task->doc = This;
219 task->task_id = TASK_SETPROGRESS;
220 task->next = NULL;
222 push_task(task);
225 task = mshtml_alloc(sizeof(task_t));
227 task->doc = This;
228 task->task_id = TASK_SETDOWNLOADSTATE;
229 task->next = NULL;
231 push_task(task);
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;
241 if(post_data_stream)
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));
248 CoTaskMemFree(url);
250 if(bind_complete)
251 *bind_complete = TRUE;
252 return S_OK;
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));
260 CoTaskMemFree(url);
262 if(bind_complete)
263 *bind_complete = FALSE;
264 return S_OK;
267 static HRESULT get_doc_string(HTMLDocument *This, char **str, DWORD *len)
269 nsIDOMDocument *nsdoc;
270 nsIDOMNode *nsnode;
271 LPCWSTR strw;
272 nsAString nsstr;
273 nsresult nsres;
275 if(!This->nscontainer) {
276 WARN("no nscontainer, returning NULL\n");
277 return S_OK;
280 nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
281 if(NS_FAILED(nsres)) {
282 ERR("GetDocument failed: %08x\n", nsres);
283 return E_FAIL;
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);
290 return E_FAIL;
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);
306 return S_OK;
310 /**********************************************************
311 * IPersistMoniker implementation
314 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
316 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
317 void **ppvObject)
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;
355 HRESULT hres;
357 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
359 hres = set_moniker(This, pimkName, pibc, &bind_complete);
360 if(FAILED(hres))
361 return hres;
363 if(!bind_complete)
364 return start_binding(This->bscallback);
366 return S_OK;
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);
374 return E_NOTIMPL;
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);
381 return E_NOTIMPL;
384 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
386 HTMLDocument *This = PERSISTMON_THIS(iface);
388 TRACE("(%p)->(%p)\n", This, ppimkName);
390 if(!This->mon)
391 return E_UNEXPECTED;
393 IMoniker_AddRef(This->mon);
394 *ppimkName = This->mon;
395 return S_OK;
398 static const IPersistMonikerVtbl PersistMonikerVtbl = {
399 PersistMoniker_QueryInterface,
400 PersistMoniker_AddRef,
401 PersistMoniker_Release,
402 PersistMoniker_GetClassID,
403 PersistMoniker_IsDirty,
404 PersistMoniker_Load,
405 PersistMoniker_Save,
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));
438 return E_NOTIMPL;
441 static const IMonikerPropVtbl MonikerPropVtbl = {
442 MonikerProp_QueryInterface,
443 MonikerProp_AddRef,
444 MonikerProp_Release,
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);
478 if(!pClassID)
479 return E_INVALIDARG;
481 memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
482 return S_OK;
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);
498 return E_NOTIMPL;
501 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
503 HTMLDocument *This = PERSISTFILE_THIS(iface);
504 char *str;
505 DWORD len, written=0;
506 HANDLE file;
507 HRESULT hres;
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());
515 return E_FAIL;
518 hres = get_doc_string(This, &str, &len);
519 if(FAILED(hres))
520 return hres;
522 WriteFile(file, str, len, &written, NULL);
523 CloseHandle(file);
524 return S_OK;
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));
531 return E_NOTIMPL;
534 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
536 HTMLDocument *This = PERSISTFILE_THIS(iface);
537 FIXME("(%p)->(%p)\n", This, pszFileName);
538 return E_NOTIMPL;
541 static const IPersistFileVtbl PersistFileVtbl = {
542 PersistFile_QueryInterface,
543 PersistFile_AddRef,
544 PersistFile_Release,
545 PersistFile_GetClassID,
546 PersistFile_IsDirty,
547 PersistFile_Load,
548 PersistFile_Save,
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");
589 return S_FALSE;
592 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
594 HTMLDocument *This = PERSTRINIT_THIS(iface);
595 IMoniker *mon;
596 HRESULT hres;
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);
603 if(FAILED(hres)) {
604 WARN("CreateURLMoniker failed: %08x\n", hres);
605 return hres;
608 hres = set_moniker(This, mon, NULL, NULL);
609 IMoniker_Release(mon);
610 if(FAILED(hres))
611 return hres;
613 return load_stream(This->bscallback, pStm);
616 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
617 BOOL fClearDirty)
619 HTMLDocument *This = PERSTRINIT_THIS(iface);
620 char *str;
621 DWORD len, written=0;
622 HRESULT hres;
624 WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
626 hres = get_doc_string(This, &str, &len);
627 if(FAILED(hres))
628 return hres;
631 hres = IStream_Write(pStm, str, len, &written);
632 if(FAILED(hres))
633 FIXME("Write failed: %08x\n", hres);
635 mshtml_free(str);
636 return S_OK;
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);
644 return E_NOTIMPL;
647 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
649 HTMLDocument *This = PERSTRINIT_THIS(iface);
650 FIXME("(%p)\n", This);
651 return E_NOTIMPL;
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;
676 This->mon = NULL;