include: Added inspectable.idl file.
[wine.git] / dlls / mshtml / persist.c
blob20f3191a94be54a0ae9bf6dcf938995e3161bc3b
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
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "ole2.h"
30 #include "shlguid.h"
31 #include "idispids.h"
33 #define NO_SHLWAPI_REG
34 #include "shlwapi.h"
36 #include "wine/debug.h"
38 #include "mshtml_private.h"
39 #include "htmlscript.h"
40 #include "htmlevent.h"
41 #include "binding.h"
42 #include "resource.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
46 /* Undocumented notification, see tests */
47 #define CMDID_EXPLORER_UPDATEHISTORY 38
49 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
51 typedef struct {
52 task_t header;
53 HTMLDocumentObj *doc;
54 BOOL set_download;
55 LPOLESTR url;
56 } download_proc_task_t;
58 static BOOL use_gecko_script(HTMLOuterWindow *window)
60 DWORD zone;
61 HRESULT hres;
63 hres = IInternetSecurityManager_MapUrlToZone(window->secmgr, window->url, &zone, 0);
64 if(FAILED(hres)) {
65 WARN("Could not map %s to zone: %08x\n", debugstr_w(window->url), hres);
66 return TRUE;
69 TRACE("zone %d\n", zone);
70 return zone == URLZONE_UNTRUSTED;
73 static void notify_travellog_update(HTMLDocumentObj *doc)
75 IOleCommandTarget *cmdtrg;
76 HRESULT hres;
78 if(!doc->webbrowser)
79 return;
81 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
82 if(SUCCEEDED(hres)) {
83 VARIANT vin;
85 V_VT(&vin) = VT_I4;
86 V_I4(&vin) = 0;
88 IOleCommandTarget_Exec(cmdtrg, &CGID_Explorer, CMDID_EXPLORER_UPDATEHISTORY, 0, &vin, NULL);
89 IOleCommandTarget_Release(cmdtrg);
93 void set_current_uri(HTMLOuterWindow *window, IUri *uri)
95 if(window->uri) {
96 IUri_Release(window->uri);
97 window->uri = NULL;
100 if(window->uri_nofrag) {
101 IUri_Release(window->uri_nofrag);
102 window->uri_nofrag = NULL;
105 SysFreeString(window->url);
106 window->url = NULL;
108 if(!uri)
109 return;
111 IUri_AddRef(uri);
112 window->uri = uri;
114 window->uri_nofrag = get_uri_nofrag(uri);
115 if(!window->uri_nofrag) {
116 FIXME("get_uri_nofrag failed\n");
117 IUri_AddRef(uri);
118 window->uri_nofrag = uri;
121 IUri_GetDisplayUri(uri, &window->url);
124 void set_current_mon(HTMLOuterWindow *This, IMoniker *mon, DWORD flags)
126 IUriContainer *uri_container;
127 IUri *uri = NULL;
128 HRESULT hres;
130 if(This->mon) {
131 if(This->doc_obj && !(flags & (BINDING_REPLACE|BINDING_REFRESH)))
132 notify_travellog_update(This->doc_obj);
133 IMoniker_Release(This->mon);
134 This->mon = NULL;
137 This->load_flags = flags;
138 if(!mon)
139 return;
141 IMoniker_AddRef(mon);
142 This->mon = mon;
144 hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
145 if(SUCCEEDED(hres)) {
146 hres = IUriContainer_GetIUri(uri_container, &uri);
147 IUriContainer_Release(uri_container);
148 if(hres != S_OK) {
149 WARN("GetIUri failed: %08x\n", hres);
150 uri = NULL;
154 if(!uri) {
155 WCHAR *url;
157 hres = IMoniker_GetDisplayName(mon, NULL, NULL, &url);
158 if(SUCCEEDED(hres)) {
159 hres = create_uri(url, 0, &uri);
160 if(FAILED(hres)) {
161 WARN("CrateUri failed: %08x\n", hres);
162 set_current_uri(This, NULL);
163 This->url = SysAllocString(url);
164 CoTaskMemFree(url);
165 return;
167 CoTaskMemFree(url);
168 }else {
169 WARN("GetDisplayName failed: %08x\n", hres);
173 set_current_uri(This, uri);
174 if(uri)
175 IUri_Release(uri);
176 set_script_mode(This, use_gecko_script(This) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT);
179 HRESULT create_uri(const WCHAR *uri_str, DWORD flags, IUri **uri)
181 return CreateUri(uri_str, flags | Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, uri);
184 HRESULT create_relative_uri(HTMLOuterWindow *window, const WCHAR *rel_uri, IUri **uri)
186 return window->uri
187 ? CoInternetCombineUrlEx(window->uri, rel_uri, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO, uri, 0)
188 : create_uri(rel_uri, 0, uri);
191 void set_download_state(HTMLDocumentObj *doc, int state)
193 if(doc->client) {
194 IOleCommandTarget *olecmd;
195 HRESULT hres;
197 hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
198 if(SUCCEEDED(hres)) {
199 VARIANT var;
201 V_VT(&var) = VT_I4;
202 V_I4(&var) = state;
204 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
205 OLECMDEXECOPT_DONTPROMPTUSER, &var, NULL);
206 IOleCommandTarget_Release(olecmd);
210 doc->download_state = state;
213 static void set_progress_proc(task_t *_task)
215 docobj_task_t *task = (docobj_task_t*)_task;
216 IOleCommandTarget *olecmd = NULL;
217 HTMLDocumentObj *doc = task->doc;
218 HRESULT hres;
220 TRACE("(%p)\n", doc);
222 if(doc->client)
223 IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
225 if(olecmd) {
226 VARIANT progress_max, progress;
228 V_VT(&progress_max) = VT_I4;
229 V_I4(&progress_max) = 0; /* FIXME */
230 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
231 &progress_max, NULL);
233 V_VT(&progress) = VT_I4;
234 V_I4(&progress) = 0; /* FIXME */
235 IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
236 &progress, NULL);
237 IOleCommandTarget_Release(olecmd);
240 if(doc->usermode == EDITMODE && doc->hostui) {
241 DOCHOSTUIINFO hostinfo;
243 memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
244 hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
245 hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
246 if(SUCCEEDED(hres))
247 /* FIXME: use hostinfo */
248 TRACE("hostinfo = {%u %08x %08x %s %s}\n",
249 hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
250 debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
254 static void set_downloading_proc(task_t *_task)
256 download_proc_task_t *task = (download_proc_task_t*)_task;
257 HTMLDocumentObj *doc = task->doc;
258 HRESULT hres;
260 TRACE("(%p)\n", doc);
262 set_statustext(doc, IDS_STATUS_DOWNLOADINGFROM, task->url);
264 if(task->set_download)
265 set_download_state(doc, 1);
267 if(!doc->client)
268 return;
270 if(doc->view_sink)
271 IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
273 if(doc->hostui) {
274 IDropTarget *drop_target = NULL;
276 hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
277 if(SUCCEEDED(hres) && drop_target) {
278 FIXME("Use IDropTarget\n");
279 IDropTarget_Release(drop_target);
284 static void set_downloading_task_destr(task_t *_task)
286 download_proc_task_t *task = (download_proc_task_t*)_task;
288 CoTaskMemFree(task->url);
289 heap_free(task);
292 void prepare_for_binding(HTMLDocument *This, IMoniker *mon, DWORD flags)
294 HRESULT hres;
296 if(This->doc_obj->client) {
297 VARIANT silent, offline;
299 hres = get_client_disp_property(This->doc_obj->client, DISPID_AMBIENT_SILENT, &silent);
300 if(SUCCEEDED(hres)) {
301 if(V_VT(&silent) != VT_BOOL)
302 WARN("silent = %s\n", debugstr_variant(&silent));
303 else if(V_BOOL(&silent))
304 FIXME("silent == true\n");
307 hres = get_client_disp_property(This->doc_obj->client,
308 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
309 if(SUCCEEDED(hres)) {
310 if(V_VT(&offline) != VT_BOOL)
311 WARN("offline = %s\n", debugstr_variant(&offline));
312 else if(V_BOOL(&offline))
313 FIXME("offline == true\n");
317 if(This->window->mon) {
318 update_doc(This, UPDATE_TITLE|UPDATE_UI);
319 }else {
320 update_doc(This, UPDATE_TITLE);
321 set_current_mon(This->window, mon, flags);
324 if(This->doc_obj->client) {
325 IOleCommandTarget *cmdtrg = NULL;
327 hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget,
328 (void**)&cmdtrg);
329 if(SUCCEEDED(hres)) {
330 VARIANT var, out;
332 if(flags & BINDING_NAVIGATED) {
333 V_VT(&var) = VT_UNKNOWN;
334 V_UNKNOWN(&var) = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
335 V_VT(&out) = VT_EMPTY;
336 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 63, 0, &var, &out);
337 if(SUCCEEDED(hres))
338 VariantClear(&out);
339 }else if(!(flags & BINDING_FROMHIST)) {
340 V_VT(&var) = VT_I4;
341 V_I4(&var) = 0;
342 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
345 IOleCommandTarget_Release(cmdtrg);
350 HRESULT set_moniker(HTMLOuterWindow *window, IMoniker *mon, IUri *nav_uri, IBindCtx *pibc, nsChannelBSC *async_bsc,
351 BOOL set_download)
353 download_proc_task_t *download_task;
354 HTMLDocumentObj *doc_obj = NULL;
355 nsChannelBSC *bscallback;
356 nsWineURI *nsuri;
357 LPOLESTR url;
358 IUri *uri;
359 HRESULT hres;
361 if(window->doc_obj && window->doc_obj->basedoc.window == window)
362 doc_obj = window->doc_obj;
364 hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
365 if(FAILED(hres)) {
366 WARN("GetDiaplayName failed: %08x\n", hres);
367 return hres;
370 if(nav_uri) {
371 uri = nav_uri;
372 }else {
373 hres = create_uri(url, 0, &uri);
374 if(FAILED(hres)) {
375 CoTaskMemFree(url);
376 return hres;
380 TRACE("got url: %s\n", debugstr_w(url));
382 set_ready_state(window, READYSTATE_LOADING);
384 hres = create_doc_uri(window, uri, &nsuri);
385 if(!nav_uri)
386 IUri_Release(uri);
387 if(SUCCEEDED(hres)) {
388 if(async_bsc)
389 bscallback = async_bsc;
390 else
391 hres = create_channelbsc(mon, NULL, NULL, 0, TRUE, &bscallback);
394 if(SUCCEEDED(hres)) {
395 if(window->base.inner_window->doc)
396 remove_target_tasks(window->base.inner_window->task_magic);
397 abort_window_bindings(window->base.inner_window);
399 hres = load_nsuri(window, nsuri, NULL, bscallback, LOAD_FLAGS_BYPASS_CACHE);
400 nsISupports_Release((nsISupports*)nsuri); /* FIXME */
401 if(SUCCEEDED(hres)) {
402 hres = create_pending_window(window, bscallback);
403 TRACE("pending window for %p %p %p\n", window, bscallback, window->pending_window);
405 if(bscallback != async_bsc)
406 IBindStatusCallback_Release(&bscallback->bsc.IBindStatusCallback_iface);
409 if(FAILED(hres)) {
410 CoTaskMemFree(url);
411 return hres;
414 if(doc_obj) {
415 HTMLDocument_LockContainer(doc_obj, TRUE);
417 if(doc_obj->frame) {
418 docobj_task_t *task;
420 task = heap_alloc(sizeof(docobj_task_t));
421 task->doc = doc_obj;
422 hres = push_task(&task->header, set_progress_proc, NULL, doc_obj->basedoc.task_magic);
423 if(FAILED(hres)) {
424 CoTaskMemFree(url);
425 return hres;
429 download_task = heap_alloc(sizeof(download_proc_task_t));
430 download_task->doc = doc_obj;
431 download_task->set_download = set_download;
432 download_task->url = url;
433 return push_task(&download_task->header, set_downloading_proc, set_downloading_task_destr, doc_obj->basedoc.task_magic);
436 return S_OK;
439 static void notif_readystate(HTMLOuterWindow *window)
441 window->readystate_pending = FALSE;
443 if(window->doc_obj && window->doc_obj->basedoc.window == window)
444 call_property_onchanged(&window->doc_obj->basedoc.cp_container, DISPID_READYSTATE);
446 fire_event(window->base.inner_window->doc, EVENTID_READYSTATECHANGE, FALSE,
447 window->base.inner_window->doc->node.nsnode, NULL, NULL);
449 if(window->frame_element)
450 fire_event(window->frame_element->element.node.doc, EVENTID_READYSTATECHANGE,
451 TRUE, window->frame_element->element.node.nsnode, NULL, NULL);
454 typedef struct {
455 task_t header;
456 HTMLOuterWindow *window;
457 } readystate_task_t;
459 static void notif_readystate_proc(task_t *_task)
461 readystate_task_t *task = (readystate_task_t*)_task;
462 notif_readystate(task->window);
465 static void notif_readystate_destr(task_t *_task)
467 readystate_task_t *task = (readystate_task_t*)_task;
468 IHTMLWindow2_Release(&task->window->base.IHTMLWindow2_iface);
471 void set_ready_state(HTMLOuterWindow *window, READYSTATE readystate)
473 READYSTATE prev_state = window->readystate;
475 window->readystate = readystate;
477 if(window->readystate_locked) {
478 readystate_task_t *task;
479 HRESULT hres;
481 if(window->readystate_pending || prev_state == readystate)
482 return;
484 task = heap_alloc(sizeof(*task));
485 if(!task)
486 return;
488 IHTMLWindow2_AddRef(&window->base.IHTMLWindow2_iface);
489 task->window = window;
491 hres = push_task(&task->header, notif_readystate_proc, notif_readystate_destr, window->task_magic);
492 if(SUCCEEDED(hres))
493 window->readystate_pending = TRUE;
494 return;
497 notif_readystate(window);
500 static HRESULT get_doc_string(HTMLDocumentNode *This, char **str)
502 nsIDOMNode *nsnode;
503 LPCWSTR strw;
504 nsAString nsstr;
505 nsresult nsres;
506 HRESULT hres;
508 if(!This->nsdoc) {
509 WARN("NULL nsdoc\n");
510 return E_UNEXPECTED;
513 nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
514 if(NS_FAILED(nsres)) {
515 ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
516 return E_FAIL;
519 nsAString_Init(&nsstr, NULL);
520 hres = nsnode_to_nsstring(nsnode, &nsstr);
521 nsIDOMNode_Release(nsnode);
522 if(FAILED(hres)) {
523 nsAString_Finish(&nsstr);
524 return hres;
527 nsAString_GetData(&nsstr, &strw);
528 TRACE("%s\n", debugstr_w(strw));
530 *str = heap_strdupWtoA(strw);
532 nsAString_Finish(&nsstr);
534 if(!*str)
535 return E_OUTOFMEMORY;
536 return S_OK;
540 /**********************************************************
541 * IPersistMoniker implementation
544 static inline HTMLDocument *impl_from_IPersistMoniker(IPersistMoniker *iface)
546 return CONTAINING_RECORD(iface, HTMLDocument, IPersistMoniker_iface);
549 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid, void **ppv)
551 HTMLDocument *This = impl_from_IPersistMoniker(iface);
552 return htmldoc_query_interface(This, riid, ppv);
555 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
557 HTMLDocument *This = impl_from_IPersistMoniker(iface);
558 return htmldoc_addref(This);
561 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
563 HTMLDocument *This = impl_from_IPersistMoniker(iface);
564 return htmldoc_release(This);
567 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
569 HTMLDocument *This = impl_from_IPersistMoniker(iface);
570 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
573 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
575 HTMLDocument *This = impl_from_IPersistMoniker(iface);
577 TRACE("(%p)\n", This);
579 return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
582 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
583 IMoniker *pimkName, LPBC pibc, DWORD grfMode)
585 HTMLDocument *This = impl_from_IPersistMoniker(iface);
586 HRESULT hres;
588 TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
590 if(pibc) {
591 IUnknown *unk = NULL;
593 /* FIXME:
594 * Use params:
595 * "__PrecreatedObject"
596 * "BIND_CONTEXT_PARAM"
597 * "__HTMLLOADOPTIONS"
598 * "__DWNBINDINFO"
599 * "URL Context"
600 * "_ITransData_Object_"
601 * "_EnumFORMATETC_"
604 hres = IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
605 if(SUCCEEDED(hres) && unk) {
606 IOleClientSite *client = NULL;
608 hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
609 if(SUCCEEDED(hres)) {
610 TRACE("Got client site %p\n", client);
611 IOleObject_SetClientSite(&This->IOleObject_iface, client);
612 IOleClientSite_Release(client);
615 IUnknown_Release(unk);
619 prepare_for_binding(This, pimkName, FALSE);
620 call_docview_84(This->doc_obj);
621 hres = set_moniker(This->window, pimkName, NULL, pibc, NULL, TRUE);
622 if(FAILED(hres))
623 return hres;
625 return start_binding(This->window->pending_window, (BSCallback*)This->window->pending_window->bscallback, pibc);
628 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
629 LPBC pbc, BOOL fRemember)
631 HTMLDocument *This = impl_from_IPersistMoniker(iface);
632 FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
633 return E_NOTIMPL;
636 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
638 HTMLDocument *This = impl_from_IPersistMoniker(iface);
639 FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
640 return E_NOTIMPL;
643 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
645 HTMLDocument *This = impl_from_IPersistMoniker(iface);
647 TRACE("(%p)->(%p)\n", This, ppimkName);
649 if(!This->window || !This->window->mon)
650 return E_UNEXPECTED;
652 IMoniker_AddRef(This->window->mon);
653 *ppimkName = This->window->mon;
654 return S_OK;
657 static const IPersistMonikerVtbl PersistMonikerVtbl = {
658 PersistMoniker_QueryInterface,
659 PersistMoniker_AddRef,
660 PersistMoniker_Release,
661 PersistMoniker_GetClassID,
662 PersistMoniker_IsDirty,
663 PersistMoniker_Load,
664 PersistMoniker_Save,
665 PersistMoniker_SaveCompleted,
666 PersistMoniker_GetCurMoniker
669 /**********************************************************
670 * IMonikerProp implementation
673 static inline HTMLDocument *impl_from_IMonikerProp(IMonikerProp *iface)
675 return CONTAINING_RECORD(iface, HTMLDocument, IMonikerProp_iface);
678 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppv)
680 HTMLDocument *This = impl_from_IMonikerProp(iface);
681 return htmldoc_query_interface(This, riid, ppv);
684 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
686 HTMLDocument *This = impl_from_IMonikerProp(iface);
687 return htmldoc_addref(This);
690 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
692 HTMLDocument *This = impl_from_IMonikerProp(iface);
693 return htmldoc_release(This);
696 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
698 HTMLDocument *This = impl_from_IMonikerProp(iface);
700 TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
702 switch(mkp) {
703 case MIMETYPEPROP:
704 heap_free(This->doc_obj->mime);
705 This->doc_obj->mime = heap_strdupW(val);
706 break;
708 case CLASSIDPROP:
709 break;
711 default:
712 FIXME("mkp %d\n", mkp);
713 return E_NOTIMPL;
716 return S_OK;
719 static const IMonikerPropVtbl MonikerPropVtbl = {
720 MonikerProp_QueryInterface,
721 MonikerProp_AddRef,
722 MonikerProp_Release,
723 MonikerProp_PutProperty
726 /**********************************************************
727 * IPersistFile implementation
730 static inline HTMLDocument *impl_from_IPersistFile(IPersistFile *iface)
732 return CONTAINING_RECORD(iface, HTMLDocument, IPersistFile_iface);
735 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppv)
737 HTMLDocument *This = impl_from_IPersistFile(iface);
738 return htmldoc_query_interface(This, riid, ppv);
741 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
743 HTMLDocument *This = impl_from_IPersistFile(iface);
744 return htmldoc_addref(This);
747 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
749 HTMLDocument *This = impl_from_IPersistFile(iface);
750 return htmldoc_release(This);
753 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
755 HTMLDocument *This = impl_from_IPersistFile(iface);
757 TRACE("(%p)->(%p)\n", This, pClassID);
759 if(!pClassID)
760 return E_INVALIDARG;
762 *pClassID = CLSID_HTMLDocument;
763 return S_OK;
766 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
768 HTMLDocument *This = impl_from_IPersistFile(iface);
770 TRACE("(%p)\n", This);
772 return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
775 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
777 HTMLDocument *This = impl_from_IPersistFile(iface);
778 FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
779 return E_NOTIMPL;
782 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
784 HTMLDocument *This = impl_from_IPersistFile(iface);
785 char *str;
786 DWORD written=0;
787 HANDLE file;
788 HRESULT hres;
790 TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
792 file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
793 FILE_ATTRIBUTE_NORMAL, NULL);
794 if(file == INVALID_HANDLE_VALUE) {
795 WARN("Could not create file: %u\n", GetLastError());
796 return E_FAIL;
799 hres = get_doc_string(This->doc_node, &str);
800 if(SUCCEEDED(hres))
801 WriteFile(file, str, strlen(str), &written, NULL);
803 CloseHandle(file);
804 return hres;
807 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
809 HTMLDocument *This = impl_from_IPersistFile(iface);
810 FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
811 return E_NOTIMPL;
814 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
816 HTMLDocument *This = impl_from_IPersistFile(iface);
817 FIXME("(%p)->(%p)\n", This, pszFileName);
818 return E_NOTIMPL;
821 static const IPersistFileVtbl PersistFileVtbl = {
822 PersistFile_QueryInterface,
823 PersistFile_AddRef,
824 PersistFile_Release,
825 PersistFile_GetClassID,
826 PersistFile_IsDirty,
827 PersistFile_Load,
828 PersistFile_Save,
829 PersistFile_SaveCompleted,
830 PersistFile_GetCurFile
833 static inline HTMLDocument *impl_from_IPersistStreamInit(IPersistStreamInit *iface)
835 return CONTAINING_RECORD(iface, HTMLDocument, IPersistStreamInit_iface);
838 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
839 REFIID riid, void **ppv)
841 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
842 return htmldoc_query_interface(This, riid, ppv);
845 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
847 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
848 return htmldoc_addref(This);
851 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
853 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
854 return htmldoc_release(This);
857 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
859 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
860 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
863 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
865 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
867 TRACE("(%p)\n", This);
869 if(This->doc_obj->usermode == EDITMODE)
870 return editor_is_dirty(This);
872 return S_FALSE;
875 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
877 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
878 IMoniker *mon;
879 HRESULT hres;
881 TRACE("(%p)->(%p)\n", This, pStm);
883 hres = CreateURLMoniker(NULL, about_blankW, &mon);
884 if(FAILED(hres)) {
885 WARN("CreateURLMoniker failed: %08x\n", hres);
886 return hres;
889 prepare_for_binding(This, mon, FALSE);
890 hres = set_moniker(This->window, mon, NULL, NULL, NULL, TRUE);
891 if(FAILED(hres))
892 return hres;
894 hres = channelbsc_load_stream(This->window->pending_window, mon, pStm);
895 IMoniker_Release(mon);
896 return hres;
899 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
900 BOOL fClearDirty)
902 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
903 char *str;
904 DWORD written=0;
905 HRESULT hres;
907 TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
909 hres = get_doc_string(This->doc_node, &str);
910 if(FAILED(hres))
911 return hres;
913 hres = IStream_Write(pStm, str, strlen(str), &written);
914 if(FAILED(hres))
915 FIXME("Write failed: %08x\n", hres);
917 heap_free(str);
919 if(fClearDirty)
920 set_dirty(This, VARIANT_FALSE);
922 return S_OK;
925 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
926 ULARGE_INTEGER *pcbSize)
928 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
929 FIXME("(%p)->(%p)\n", This, pcbSize);
930 return E_NOTIMPL;
933 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
935 HTMLDocument *This = impl_from_IPersistStreamInit(iface);
936 IMoniker *mon;
937 HRESULT hres;
939 TRACE("(%p)\n", This);
941 hres = CreateURLMoniker(NULL, about_blankW, &mon);
942 if(FAILED(hres)) {
943 WARN("CreateURLMoniker failed: %08x\n", hres);
944 return hres;
947 prepare_for_binding(This, mon, FALSE);
948 hres = set_moniker(This->window, mon, NULL, NULL, NULL, FALSE);
949 if(FAILED(hres))
950 return hres;
952 hres = channelbsc_load_stream(This->window->pending_window, mon, NULL);
953 IMoniker_Release(mon);
954 return hres;
957 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
958 PersistStreamInit_QueryInterface,
959 PersistStreamInit_AddRef,
960 PersistStreamInit_Release,
961 PersistStreamInit_GetClassID,
962 PersistStreamInit_IsDirty,
963 PersistStreamInit_Load,
964 PersistStreamInit_Save,
965 PersistStreamInit_GetSizeMax,
966 PersistStreamInit_InitNew
969 /**********************************************************
970 * IPersistHistory implementation
973 static inline HTMLDocument *impl_from_IPersistHistory(IPersistHistory *iface)
975 return CONTAINING_RECORD(iface, HTMLDocument, IPersistHistory_iface);
978 static HRESULT WINAPI PersistHistory_QueryInterface(IPersistHistory *iface, REFIID riid, void **ppv)
980 HTMLDocument *This = impl_from_IPersistHistory(iface);
981 return htmldoc_query_interface(This, riid, ppv);
984 static ULONG WINAPI PersistHistory_AddRef(IPersistHistory *iface)
986 HTMLDocument *This = impl_from_IPersistHistory(iface);
987 return htmldoc_addref(This);
990 static ULONG WINAPI PersistHistory_Release(IPersistHistory *iface)
992 HTMLDocument *This = impl_from_IPersistHistory(iface);
993 return htmldoc_release(This);
996 static HRESULT WINAPI PersistHistory_GetClassID(IPersistHistory *iface, CLSID *pClassID)
998 HTMLDocument *This = impl_from_IPersistHistory(iface);
999 return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
1002 static HRESULT WINAPI PersistHistory_LoadHistory(IPersistHistory *iface, IStream *pStream, IBindCtx *pbc)
1004 HTMLDocument *This = impl_from_IPersistHistory(iface);
1005 ULONG str_len, read;
1006 WCHAR *uri_str;
1007 IUri *uri;
1008 HRESULT hres;
1010 TRACE("(%p)->(%p %p)\n", This, pStream, pbc);
1012 if(!This->window) {
1013 FIXME("No current window\n");
1014 return E_UNEXPECTED;
1017 if(pbc)
1018 FIXME("pbc not supported\n");
1020 if(This->doc_obj->client) {
1021 IOleCommandTarget *cmdtrg = NULL;
1023 hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget,
1024 (void**)&cmdtrg);
1025 if(SUCCEEDED(hres)) {
1026 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 138, 0, NULL, NULL);
1027 IOleCommandTarget_Release(cmdtrg);
1031 hres = IStream_Read(pStream, &str_len, sizeof(str_len), &read);
1032 if(FAILED(hres))
1033 return hres;
1034 if(read != sizeof(str_len))
1035 return E_FAIL;
1037 uri_str = heap_alloc((str_len+1)*sizeof(WCHAR));
1038 if(!uri_str)
1039 return E_OUTOFMEMORY;
1041 hres = IStream_Read(pStream, uri_str, str_len*sizeof(WCHAR), &read);
1042 if(SUCCEEDED(hres) && read != str_len*sizeof(WCHAR))
1043 hres = E_FAIL;
1044 if(SUCCEEDED(hres)) {
1045 uri_str[str_len] = 0;
1046 hres = create_uri(uri_str, 0, &uri);
1048 heap_free(uri_str);
1049 if(FAILED(hres))
1050 return hres;
1052 hres = load_uri(This->window, uri, BINDING_FROMHIST);
1053 IUri_Release(uri);
1054 return hres;
1057 static HRESULT WINAPI PersistHistory_SaveHistory(IPersistHistory *iface, IStream *pStream)
1059 HTMLDocument *This = impl_from_IPersistHistory(iface);
1060 ULONG len, written;
1061 BSTR display_uri;
1062 HRESULT hres;
1064 TRACE("(%p)->(%p)\n", This, pStream);
1066 if(!This->window || !This->window->uri) {
1067 FIXME("No current URI\n");
1068 return E_FAIL;
1071 /* NOTE: The format we store is *not* compatible with native MSHTML. We currently
1072 * store only URI of the page (as a length followed by a string) */
1073 hres = IUri_GetDisplayUri(This->window->uri, &display_uri);
1074 if(FAILED(hres))
1075 return hres;
1077 len = SysStringLen(display_uri);
1078 hres = IStream_Write(pStream, &len, sizeof(len), &written);
1079 if(SUCCEEDED(hres))
1080 hres = IStream_Write(pStream, display_uri, len*sizeof(WCHAR), &written);
1081 SysFreeString(display_uri);
1082 return hres;
1085 static HRESULT WINAPI PersistHistory_SetPositionCookie(IPersistHistory *iface, DWORD dwPositioncookie)
1087 HTMLDocument *This = impl_from_IPersistHistory(iface);
1088 FIXME("(%p)->(%x)\n", This, dwPositioncookie);
1089 return E_NOTIMPL;
1092 static HRESULT WINAPI PersistHistory_GetPositionCookie(IPersistHistory *iface, DWORD *pdwPositioncookie)
1094 HTMLDocument *This = impl_from_IPersistHistory(iface);
1095 FIXME("(%p)->(%p)\n", This, pdwPositioncookie);
1096 return E_NOTIMPL;
1099 static const IPersistHistoryVtbl PersistHistoryVtbl = {
1100 PersistHistory_QueryInterface,
1101 PersistHistory_AddRef,
1102 PersistHistory_Release,
1103 PersistHistory_GetClassID,
1104 PersistHistory_LoadHistory,
1105 PersistHistory_SaveHistory,
1106 PersistHistory_SetPositionCookie,
1107 PersistHistory_GetPositionCookie
1110 void HTMLDocument_Persist_Init(HTMLDocument *This)
1112 This->IPersistMoniker_iface.lpVtbl = &PersistMonikerVtbl;
1113 This->IPersistFile_iface.lpVtbl = &PersistFileVtbl;
1114 This->IMonikerProp_iface.lpVtbl = &MonikerPropVtbl;
1115 This->IPersistStreamInit_iface.lpVtbl = &PersistStreamInitVtbl;
1116 This->IPersistHistory_iface.lpVtbl = &PersistHistoryVtbl;