Release 1.5.29.
[wine] / dlls / mshtml / persist.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "ole2.h"
32 #include "shlguid.h"
33 #include "idispids.h"
34
35 #define NO_SHLWAPI_REG
36 #include "shlwapi.h"
37
38 #include "wine/debug.h"
39
40 #include "mshtml_private.h"
41 #include "htmlscript.h"
42 #include "htmlevent.h"
43 #include "binding.h"
44 #include "resource.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
47
48 /* Undocumented notification, see tests */
49 #define CMDID_EXPLORER_UPDATEHISTORY 38
50
51 static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
52
53 typedef struct {
54     task_t header;
55     HTMLDocumentObj *doc;
56     BOOL set_download;
57     LPOLESTR url;
58 } download_proc_task_t;
59
60 static BOOL use_gecko_script(HTMLOuterWindow *window)
61 {
62     DWORD zone;
63     HRESULT hres;
64
65     hres = IInternetSecurityManager_MapUrlToZone(window->secmgr, window->url, &zone, 0);
66     if(FAILED(hres)) {
67         WARN("Could not map %s to zone: %08x\n", debugstr_w(window->url), hres);
68         return TRUE;
69     }
70
71     TRACE("zone %d\n", zone);
72     return zone == URLZONE_UNTRUSTED;
73 }
74
75 static void notify_travellog_update(HTMLDocumentObj *doc)
76 {
77     IOleCommandTarget *cmdtrg;
78     HRESULT hres;
79
80     if(!doc->webbrowser)
81         return;
82
83     hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&cmdtrg);
84     if(SUCCEEDED(hres)) {
85         VARIANT vin;
86
87         V_VT(&vin) = VT_I4;
88         V_I4(&vin) = 0;
89
90         IOleCommandTarget_Exec(cmdtrg, &CGID_Explorer, CMDID_EXPLORER_UPDATEHISTORY, 0, &vin, NULL);
91         IOleCommandTarget_Release(cmdtrg);
92     }
93 }
94
95 void set_current_uri(HTMLOuterWindow *window, IUri *uri)
96 {
97     if(window->uri) {
98         IUri_Release(window->uri);
99         window->uri = NULL;
100     }
101
102     if(window->uri_nofrag) {
103         IUri_Release(window->uri_nofrag);
104         window->uri_nofrag = NULL;
105     }
106
107     SysFreeString(window->url);
108     window->url = NULL;
109
110     if(!uri)
111         return;
112
113     IUri_AddRef(uri);
114     window->uri = uri;
115
116     window->uri_nofrag = get_uri_nofrag(uri);
117     if(!window->uri_nofrag) {
118         FIXME("get_uri_nofrag failed\n");
119         IUri_AddRef(uri);
120         window->uri_nofrag = uri;
121     }
122
123     IUri_GetDisplayUri(uri, &window->url);
124 }
125
126 void set_current_mon(HTMLOuterWindow *This, IMoniker *mon, DWORD flags)
127 {
128     IUriContainer *uri_container;
129     IUri *uri = NULL;
130     HRESULT hres;
131
132     if(This->mon) {
133         if(This->doc_obj && !(flags & (BINDING_REPLACE|BINDING_REFRESH)))
134             notify_travellog_update(This->doc_obj);
135         IMoniker_Release(This->mon);
136         This->mon = NULL;
137     }
138
139     This->load_flags = flags;
140     if(!mon)
141         return;
142
143     IMoniker_AddRef(mon);
144     This->mon = mon;
145
146     hres = IMoniker_QueryInterface(mon, &IID_IUriContainer, (void**)&uri_container);
147     if(SUCCEEDED(hres)) {
148         hres = IUriContainer_GetIUri(uri_container, &uri);
149         IUriContainer_Release(uri_container);
150         if(hres != S_OK) {
151             WARN("GetIUri failed: %08x\n", hres);
152             uri = NULL;
153         }
154     }
155
156     if(!uri) {
157         WCHAR *url;
158
159         hres = IMoniker_GetDisplayName(mon, NULL, NULL, &url);
160         if(SUCCEEDED(hres)) {
161             hres = create_uri(url, 0, &uri);
162             if(FAILED(hres)) {
163                 WARN("CrateUri failed: %08x\n", hres);
164                 set_current_uri(This, NULL);
165                 This->url = SysAllocString(url);
166                 CoTaskMemFree(url);
167                 return;
168             }
169             CoTaskMemFree(url);
170         }else {
171             WARN("GetDisplayName failed: %08x\n", hres);
172         }
173     }
174
175     set_current_uri(This, uri);
176     if(uri)
177         IUri_Release(uri);
178     set_script_mode(This, use_gecko_script(This) ? SCRIPTMODE_GECKO : SCRIPTMODE_ACTIVESCRIPT);
179 }
180
181 HRESULT create_uri(const WCHAR *uri_str, DWORD flags, IUri **uri)
182 {
183     return CreateUri(uri_str, flags | Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME, 0, uri);
184 }
185
186 HRESULT create_relative_uri(HTMLOuterWindow *window, const WCHAR *rel_uri, IUri **uri)
187 {
188     return window->uri
189         ? CoInternetCombineUrlEx(window->uri, rel_uri, URL_ESCAPE_SPACES_ONLY|URL_DONT_ESCAPE_EXTRA_INFO, uri, 0)
190         : create_uri(rel_uri, 0, uri);
191 }
192
193 void set_download_state(HTMLDocumentObj *doc, int state)
194 {
195     if(doc->client) {
196         IOleCommandTarget *olecmd;
197         HRESULT hres;
198
199         hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
200         if(SUCCEEDED(hres)) {
201             VARIANT var;
202
203             V_VT(&var) = VT_I4;
204             V_I4(&var) = state;
205
206             IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
207                     OLECMDEXECOPT_DONTPROMPTUSER, &var, NULL);
208             IOleCommandTarget_Release(olecmd);
209         }
210     }
211
212     doc->download_state = state;
213 }
214
215 static void set_progress_proc(task_t *_task)
216 {
217     docobj_task_t *task = (docobj_task_t*)_task;
218     IOleCommandTarget *olecmd = NULL;
219     HTMLDocumentObj *doc = task->doc;
220     HRESULT hres;
221
222     TRACE("(%p)\n", doc);
223
224     if(doc->client)
225         IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
226
227     if(olecmd) {
228         VARIANT progress_max, progress;
229
230         V_VT(&progress_max) = VT_I4;
231         V_I4(&progress_max) = 0; /* FIXME */
232         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSMAX, OLECMDEXECOPT_DONTPROMPTUSER,
233                                &progress_max, NULL);
234
235         V_VT(&progress) = VT_I4;
236         V_I4(&progress) = 0; /* FIXME */
237         IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETPROGRESSPOS, OLECMDEXECOPT_DONTPROMPTUSER,
238                                &progress, NULL);
239         IOleCommandTarget_Release(olecmd);
240     }
241
242     if(doc->usermode == EDITMODE && doc->hostui) {
243         DOCHOSTUIINFO hostinfo;
244
245         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
246         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
247         hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
248         if(SUCCEEDED(hres))
249             /* FIXME: use hostinfo */
250             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
251                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
252                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
253     }
254 }
255
256 static void set_downloading_proc(task_t *_task)
257 {
258     download_proc_task_t *task = (download_proc_task_t*)_task;
259     HTMLDocumentObj *doc = task->doc;
260     HRESULT hres;
261
262     TRACE("(%p)\n", doc);
263
264     set_statustext(doc, IDS_STATUS_DOWNLOADINGFROM, task->url);
265
266     if(task->set_download)
267         set_download_state(doc, 1);
268
269     if(!doc->client)
270         return;
271
272     if(doc->view_sink)
273         IAdviseSink_OnViewChange(doc->view_sink, DVASPECT_CONTENT, -1);
274
275     if(doc->hostui) {
276         IDropTarget *drop_target = NULL;
277
278         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
279         if(SUCCEEDED(hres) && drop_target) {
280             FIXME("Use IDropTarget\n");
281             IDropTarget_Release(drop_target);
282         }
283     }
284 }
285
286 static void set_downloading_task_destr(task_t *_task)
287 {
288     download_proc_task_t *task = (download_proc_task_t*)_task;
289
290     CoTaskMemFree(task->url);
291     heap_free(task);
292 }
293
294 void prepare_for_binding(HTMLDocument *This, IMoniker *mon, DWORD flags)
295 {
296     HRESULT hres;
297
298     if(This->doc_obj->client) {
299         VARIANT silent, offline;
300
301         hres = get_client_disp_property(This->doc_obj->client, DISPID_AMBIENT_SILENT, &silent);
302         if(SUCCEEDED(hres)) {
303             if(V_VT(&silent) != VT_BOOL)
304                 WARN("silent = %s\n", debugstr_variant(&silent));
305             else if(V_BOOL(&silent))
306                 FIXME("silent == true\n");
307         }
308
309         hres = get_client_disp_property(This->doc_obj->client,
310                 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
311         if(SUCCEEDED(hres)) {
312             if(V_VT(&offline) != VT_BOOL)
313                 WARN("offline = %s\n", debugstr_variant(&offline));
314             else if(V_BOOL(&offline))
315                 FIXME("offline == true\n");
316         }
317     }
318
319     if(This->window->mon) {
320         update_doc(This, UPDATE_TITLE|UPDATE_UI);
321     }else {
322         update_doc(This, UPDATE_TITLE);
323         set_current_mon(This->window, mon, flags);
324     }
325
326     if(This->doc_obj->client) {
327         IOleCommandTarget *cmdtrg = NULL;
328
329         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget,
330                 (void**)&cmdtrg);
331         if(SUCCEEDED(hres)) {
332             VARIANT var, out;
333
334             if(flags & BINDING_NAVIGATED) {
335                 V_VT(&var) = VT_UNKNOWN;
336                 V_UNKNOWN(&var) = (IUnknown*)&This->window->base.IHTMLWindow2_iface;
337                 V_VT(&out) = VT_EMPTY;
338                 hres = IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 63, 0, &var, &out);
339                 if(SUCCEEDED(hres))
340                     VariantClear(&out);
341             }else if(!(flags & BINDING_FROMHIST)) {
342                 V_VT(&var) = VT_I4;
343                 V_I4(&var) = 0;
344                 IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
345             }
346
347             IOleCommandTarget_Release(cmdtrg);
348         }
349     }
350 }
351
352 HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IUri *nav_uri, IBindCtx *pibc, nsChannelBSC *async_bsc, BOOL set_download)
353 {
354     download_proc_task_t *download_task;
355     nsChannelBSC *bscallback;
356     nsWineURI *nsuri;
357     LPOLESTR url;
358     IUri *uri;
359     HRESULT hres;
360
361     hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
362     if(FAILED(hres)) {
363         WARN("GetDiaplayName failed: %08x\n", hres);
364         return hres;
365     }
366
367     if(nav_uri) {
368         uri = nav_uri;
369     }else {
370         hres = create_uri(url, 0, &uri);
371         if(FAILED(hres)) {
372             CoTaskMemFree(url);
373             return hres;
374         }
375     }
376
377     TRACE("got url: %s\n", debugstr_w(url));
378
379     set_ready_state(This->window, READYSTATE_LOADING);
380
381     hres = create_doc_uri(This->window, uri, &nsuri);
382     if(!nav_uri)
383         IUri_Release(uri);
384     if(SUCCEEDED(hres)) {
385         if(async_bsc)
386             bscallback = async_bsc;
387         else
388             hres = create_channelbsc(mon, NULL, NULL, 0, TRUE, &bscallback);
389     }
390
391     if(SUCCEEDED(hres)) {
392         remove_target_tasks(This->task_magic);
393         abort_window_bindings(This->window->base.inner_window);
394
395         hres = load_nsuri(This->window, nsuri, bscallback, LOAD_FLAGS_BYPASS_CACHE);
396         nsISupports_Release((nsISupports*)nsuri); /* FIXME */
397         if(SUCCEEDED(hres))
398             hres = create_pending_window(This->window, bscallback);
399         if(bscallback != async_bsc)
400             IBindStatusCallback_Release(&bscallback->bsc.IBindStatusCallback_iface);
401     }
402
403     if(FAILED(hres)) {
404         CoTaskMemFree(url);
405         return hres;
406     }
407
408     HTMLDocument_LockContainer(This->doc_obj, TRUE);
409
410     if(This->doc_obj->frame) {
411         docobj_task_t *task;
412
413         task = heap_alloc(sizeof(docobj_task_t));
414         task->doc = This->doc_obj;
415         hres = push_task(&task->header, set_progress_proc, NULL, This->doc_obj->basedoc.task_magic);
416         if(FAILED(hres)) {
417             CoTaskMemFree(url);
418             return hres;
419         }
420     }
421
422     download_task = heap_alloc(sizeof(download_proc_task_t));
423     download_task->doc = This->doc_obj;
424     download_task->set_download = set_download;
425     download_task->url = url;
426     return push_task(&download_task->header, set_downloading_proc, set_downloading_task_destr, This->doc_obj->basedoc.task_magic);
427 }
428
429 void set_ready_state(HTMLOuterWindow *window, READYSTATE readystate)
430 {
431     window->readystate = readystate;
432
433     if(window->doc_obj && window->doc_obj->basedoc.window == window)
434         call_property_onchanged(&window->doc_obj->basedoc.cp_container, DISPID_READYSTATE);
435
436     fire_event(window->base.inner_window->doc, EVENTID_READYSTATECHANGE, FALSE,
437             window->base.inner_window->doc->node.nsnode, NULL, NULL);
438
439     if(window->frame_element)
440         fire_event(window->frame_element->element.node.doc, EVENTID_READYSTATECHANGE,
441                    TRUE, window->frame_element->element.node.nsnode, NULL, NULL);
442 }
443
444 static HRESULT get_doc_string(HTMLDocumentNode *This, char **str)
445 {
446     nsIDOMNode *nsnode;
447     LPCWSTR strw;
448     nsAString nsstr;
449     nsresult nsres;
450     HRESULT hres;
451
452     if(!This->nsdoc) {
453         WARN("NULL nsdoc\n");
454         return E_UNEXPECTED;
455     }
456
457     nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
458     if(NS_FAILED(nsres)) {
459         ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
460         return E_FAIL;
461     }
462
463     nsAString_Init(&nsstr, NULL);
464     hres = nsnode_to_nsstring(nsnode, &nsstr);
465     nsIDOMNode_Release(nsnode);
466     if(FAILED(hres)) {
467         nsAString_Finish(&nsstr);
468         return hres;
469     }
470
471     nsAString_GetData(&nsstr, &strw);
472     TRACE("%s\n", debugstr_w(strw));
473
474     *str = heap_strdupWtoA(strw);
475
476     nsAString_Finish(&nsstr);
477
478     if(!*str)
479         return E_OUTOFMEMORY;
480     return S_OK;
481 }
482
483
484 /**********************************************************
485  * IPersistMoniker implementation
486  */
487
488 static inline HTMLDocument *impl_from_IPersistMoniker(IPersistMoniker *iface)
489 {
490     return CONTAINING_RECORD(iface, HTMLDocument, IPersistMoniker_iface);
491 }
492
493 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid, void **ppv)
494 {
495     HTMLDocument *This = impl_from_IPersistMoniker(iface);
496     return htmldoc_query_interface(This, riid, ppv);
497 }
498
499 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
500 {
501     HTMLDocument *This = impl_from_IPersistMoniker(iface);
502     return htmldoc_addref(This);
503 }
504
505 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
506 {
507     HTMLDocument *This = impl_from_IPersistMoniker(iface);
508     return htmldoc_release(This);
509 }
510
511 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
512 {
513     HTMLDocument *This = impl_from_IPersistMoniker(iface);
514     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
515 }
516
517 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
518 {
519     HTMLDocument *This = impl_from_IPersistMoniker(iface);
520
521     TRACE("(%p)\n", This);
522
523     return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
524 }
525
526 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
527         IMoniker *pimkName, LPBC pibc, DWORD grfMode)
528 {
529     HTMLDocument *This = impl_from_IPersistMoniker(iface);
530     HRESULT hres;
531
532     TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
533
534     if(pibc) {
535         IUnknown *unk = NULL;
536
537         /* FIXME:
538          * Use params:
539          * "__PrecreatedObject"
540          * "BIND_CONTEXT_PARAM"
541          * "__HTMLLOADOPTIONS"
542          * "__DWNBINDINFO"
543          * "URL Context"
544          * "_ITransData_Object_"
545          * "_EnumFORMATETC_"
546          */
547
548         hres = IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
549         if(SUCCEEDED(hres) && unk) {
550             IOleClientSite *client = NULL;
551
552             hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
553             if(SUCCEEDED(hres)) {
554                 TRACE("Got client site %p\n", client);
555                 IOleObject_SetClientSite(&This->IOleObject_iface, client);
556                 IOleClientSite_Release(client);
557             }
558
559             IUnknown_Release(unk);
560         }
561     }
562
563     prepare_for_binding(This, pimkName, FALSE);
564     call_docview_84(This->doc_obj);
565     hres = set_moniker(This, pimkName, NULL, pibc, NULL, TRUE);
566     if(FAILED(hres))
567         return hres;
568
569     return start_binding(This->window->pending_window, (BSCallback*)This->window->pending_window->bscallback, pibc);
570 }
571
572 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
573         LPBC pbc, BOOL fRemember)
574 {
575     HTMLDocument *This = impl_from_IPersistMoniker(iface);
576     FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
577     return E_NOTIMPL;
578 }
579
580 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
581 {
582     HTMLDocument *This = impl_from_IPersistMoniker(iface);
583     FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
584     return E_NOTIMPL;
585 }
586
587 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
588 {
589     HTMLDocument *This = impl_from_IPersistMoniker(iface);
590
591     TRACE("(%p)->(%p)\n", This, ppimkName);
592
593     if(!This->window || !This->window->mon)
594         return E_UNEXPECTED;
595
596     IMoniker_AddRef(This->window->mon);
597     *ppimkName = This->window->mon;
598     return S_OK;
599 }
600
601 static const IPersistMonikerVtbl PersistMonikerVtbl = {
602     PersistMoniker_QueryInterface,
603     PersistMoniker_AddRef,
604     PersistMoniker_Release,
605     PersistMoniker_GetClassID,
606     PersistMoniker_IsDirty,
607     PersistMoniker_Load,
608     PersistMoniker_Save,
609     PersistMoniker_SaveCompleted,
610     PersistMoniker_GetCurMoniker
611 };
612
613 /**********************************************************
614  * IMonikerProp implementation
615  */
616
617 static inline HTMLDocument *impl_from_IMonikerProp(IMonikerProp *iface)
618 {
619     return CONTAINING_RECORD(iface, HTMLDocument, IMonikerProp_iface);
620 }
621
622 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppv)
623 {
624     HTMLDocument *This = impl_from_IMonikerProp(iface);
625     return htmldoc_query_interface(This, riid, ppv);
626 }
627
628 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
629 {
630     HTMLDocument *This = impl_from_IMonikerProp(iface);
631     return htmldoc_addref(This);
632 }
633
634 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
635 {
636     HTMLDocument *This = impl_from_IMonikerProp(iface);
637     return htmldoc_release(This);
638 }
639
640 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
641 {
642     HTMLDocument *This = impl_from_IMonikerProp(iface);
643
644     TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
645
646     switch(mkp) {
647     case MIMETYPEPROP:
648         heap_free(This->doc_obj->mime);
649         This->doc_obj->mime = heap_strdupW(val);
650         break;
651
652     case CLASSIDPROP:
653         break;
654
655     default:
656         FIXME("mkp %d\n", mkp);
657         return E_NOTIMPL;
658     }
659
660     return S_OK;
661 }
662
663 static const IMonikerPropVtbl MonikerPropVtbl = {
664     MonikerProp_QueryInterface,
665     MonikerProp_AddRef,
666     MonikerProp_Release,
667     MonikerProp_PutProperty
668 };
669
670 /**********************************************************
671  * IPersistFile implementation
672  */
673
674 static inline HTMLDocument *impl_from_IPersistFile(IPersistFile *iface)
675 {
676     return CONTAINING_RECORD(iface, HTMLDocument, IPersistFile_iface);
677 }
678
679 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppv)
680 {
681     HTMLDocument *This = impl_from_IPersistFile(iface);
682     return htmldoc_query_interface(This, riid, ppv);
683 }
684
685 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
686 {
687     HTMLDocument *This = impl_from_IPersistFile(iface);
688     return htmldoc_addref(This);
689 }
690
691 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
692 {
693     HTMLDocument *This = impl_from_IPersistFile(iface);
694     return htmldoc_release(This);
695 }
696
697 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
698 {
699     HTMLDocument *This = impl_from_IPersistFile(iface);
700
701     TRACE("(%p)->(%p)\n", This, pClassID);
702
703     if(!pClassID)
704         return E_INVALIDARG;
705
706     *pClassID = CLSID_HTMLDocument;
707     return S_OK;
708 }
709
710 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
711 {
712     HTMLDocument *This = impl_from_IPersistFile(iface);
713
714     TRACE("(%p)\n", This);
715
716     return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
717 }
718
719 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
720 {
721     HTMLDocument *This = impl_from_IPersistFile(iface);
722     FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
723     return E_NOTIMPL;
724 }
725
726 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
727 {
728     HTMLDocument *This = impl_from_IPersistFile(iface);
729     char *str;
730     DWORD written=0;
731     HANDLE file;
732     HRESULT hres;
733
734     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
735
736     file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
737                        FILE_ATTRIBUTE_NORMAL, NULL);
738     if(file == INVALID_HANDLE_VALUE) {
739         WARN("Could not create file: %u\n", GetLastError());
740         return E_FAIL;
741     }
742
743     hres = get_doc_string(This->doc_node, &str);
744     if(SUCCEEDED(hres))
745         WriteFile(file, str, strlen(str), &written, NULL);
746
747     CloseHandle(file);
748     return hres;
749 }
750
751 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
752 {
753     HTMLDocument *This = impl_from_IPersistFile(iface);
754     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
755     return E_NOTIMPL;
756 }
757
758 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
759 {
760     HTMLDocument *This = impl_from_IPersistFile(iface);
761     FIXME("(%p)->(%p)\n", This, pszFileName);
762     return E_NOTIMPL;
763 }
764
765 static const IPersistFileVtbl PersistFileVtbl = {
766     PersistFile_QueryInterface,
767     PersistFile_AddRef,
768     PersistFile_Release,
769     PersistFile_GetClassID,
770     PersistFile_IsDirty,
771     PersistFile_Load,
772     PersistFile_Save,
773     PersistFile_SaveCompleted,
774     PersistFile_GetCurFile
775 };
776
777 static inline HTMLDocument *impl_from_IPersistStreamInit(IPersistStreamInit *iface)
778 {
779     return CONTAINING_RECORD(iface, HTMLDocument, IPersistStreamInit_iface);
780 }
781
782 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
783                                                        REFIID riid, void **ppv)
784 {
785     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
786     return htmldoc_query_interface(This, riid, ppv);
787 }
788
789 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
790 {
791     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
792     return htmldoc_addref(This);
793 }
794
795 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
796 {
797     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
798     return htmldoc_release(This);
799 }
800
801 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
802 {
803     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
804     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
805 }
806
807 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
808 {
809     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
810
811     TRACE("(%p)\n", This);
812
813     if(This->doc_obj->usermode == EDITMODE)
814         return editor_is_dirty(This);
815
816     return S_FALSE;
817 }
818
819 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
820 {
821     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
822     IMoniker *mon;
823     HRESULT hres;
824
825     TRACE("(%p)->(%p)\n", This, pStm);
826
827     hres = CreateURLMoniker(NULL, about_blankW, &mon);
828     if(FAILED(hres)) {
829         WARN("CreateURLMoniker failed: %08x\n", hres);
830         return hres;
831     }
832
833     prepare_for_binding(This, mon, FALSE);
834     hres = set_moniker(This, mon, NULL, NULL, NULL, TRUE);
835     IMoniker_Release(mon);
836     if(FAILED(hres))
837         return hres;
838
839     return channelbsc_load_stream(This->window->pending_window, pStm);
840 }
841
842 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
843                                              BOOL fClearDirty)
844 {
845     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
846     char *str;
847     DWORD written=0;
848     HRESULT hres;
849
850     TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
851
852     hres = get_doc_string(This->doc_node, &str);
853     if(FAILED(hres))
854         return hres;
855
856     hres = IStream_Write(pStm, str, strlen(str), &written);
857     if(FAILED(hres))
858         FIXME("Write failed: %08x\n", hres);
859
860     heap_free(str);
861
862     if(fClearDirty)
863         set_dirty(This, VARIANT_FALSE);
864
865     return S_OK;
866 }
867
868 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
869                                                    ULARGE_INTEGER *pcbSize)
870 {
871     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
872     FIXME("(%p)->(%p)\n", This, pcbSize);
873     return E_NOTIMPL;
874 }
875
876 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
877 {
878     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
879     IMoniker *mon;
880     HRESULT hres;
881
882     TRACE("(%p)\n", This);
883
884     hres = CreateURLMoniker(NULL, about_blankW, &mon);
885     if(FAILED(hres)) {
886         WARN("CreateURLMoniker failed: %08x\n", hres);
887         return hres;
888     }
889
890     prepare_for_binding(This, mon, FALSE);
891     hres = set_moniker(This, mon, NULL, NULL, NULL, FALSE);
892     IMoniker_Release(mon);
893     if(FAILED(hres))
894         return hres;
895
896     return channelbsc_load_stream(This->window->pending_window, NULL);
897 }
898
899 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
900     PersistStreamInit_QueryInterface,
901     PersistStreamInit_AddRef,
902     PersistStreamInit_Release,
903     PersistStreamInit_GetClassID,
904     PersistStreamInit_IsDirty,
905     PersistStreamInit_Load,
906     PersistStreamInit_Save,
907     PersistStreamInit_GetSizeMax,
908     PersistStreamInit_InitNew
909 };
910
911 /**********************************************************
912  * IPersistHistory implementation
913  */
914
915 static inline HTMLDocument *impl_from_IPersistHistory(IPersistHistory *iface)
916 {
917     return CONTAINING_RECORD(iface, HTMLDocument, IPersistHistory_iface);
918 }
919
920 static HRESULT WINAPI PersistHistory_QueryInterface(IPersistHistory *iface, REFIID riid, void **ppv)
921 {
922     HTMLDocument *This = impl_from_IPersistHistory(iface);
923     return htmldoc_query_interface(This, riid, ppv);
924 }
925
926 static ULONG WINAPI PersistHistory_AddRef(IPersistHistory *iface)
927 {
928     HTMLDocument *This = impl_from_IPersistHistory(iface);
929     return htmldoc_addref(This);
930 }
931
932 static ULONG WINAPI PersistHistory_Release(IPersistHistory *iface)
933 {
934     HTMLDocument *This = impl_from_IPersistHistory(iface);
935     return htmldoc_release(This);
936 }
937
938 static HRESULT WINAPI PersistHistory_GetClassID(IPersistHistory *iface, CLSID *pClassID)
939 {
940     HTMLDocument *This = impl_from_IPersistHistory(iface);
941     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
942 }
943
944 static HRESULT WINAPI PersistHistory_LoadHistory(IPersistHistory *iface, IStream *pStream, IBindCtx *pbc)
945 {
946     HTMLDocument *This = impl_from_IPersistHistory(iface);
947     ULONG str_len, read;
948     WCHAR *uri_str;
949     IUri *uri;
950     HRESULT hres;
951
952     TRACE("(%p)->(%p %p)\n", This, pStream, pbc);
953
954     if(!This->window) {
955         FIXME("No current window\n");
956         return E_UNEXPECTED;
957     }
958
959     if(pbc)
960         FIXME("pbc not supported\n");
961
962     if(This->doc_obj->client) {
963         IOleCommandTarget *cmdtrg = NULL;
964
965         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget,
966                 (void**)&cmdtrg);
967         if(SUCCEEDED(hres)) {
968             IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 138, 0, NULL, NULL);
969             IOleCommandTarget_Release(cmdtrg);
970         }
971     }
972
973     hres = IStream_Read(pStream, &str_len, sizeof(str_len), &read);
974     if(FAILED(hres))
975         return hres;
976     if(read != sizeof(str_len))
977         return E_FAIL;
978
979     uri_str = heap_alloc((str_len+1)*sizeof(WCHAR));
980     if(!uri_str)
981         return E_OUTOFMEMORY;
982
983     hres = IStream_Read(pStream, uri_str, str_len*sizeof(WCHAR), &read);
984     if(SUCCEEDED(hres) && read != str_len*sizeof(WCHAR))
985         hres = E_FAIL;
986     if(SUCCEEDED(hres)) {
987         uri_str[str_len] = 0;
988         hres = create_uri(uri_str, 0, &uri);
989     }
990     heap_free(uri_str);
991     if(FAILED(hres))
992         return hres;
993
994     hres = load_uri(This->window, uri, BINDING_FROMHIST);
995     IUri_Release(uri);
996     return hres;
997 }
998
999 static HRESULT WINAPI PersistHistory_SaveHistory(IPersistHistory *iface, IStream *pStream)
1000 {
1001     HTMLDocument *This = impl_from_IPersistHistory(iface);
1002     ULONG len, written;
1003     BSTR display_uri;
1004     HRESULT hres;
1005
1006     TRACE("(%p)->(%p)\n", This, pStream);
1007
1008     if(!This->window || !This->window->uri) {
1009         FIXME("No current URI\n");
1010         return E_FAIL;
1011     }
1012
1013     /* NOTE: The format we store is *not* compatible with native MSHTML. We currently
1014      * store only URI of the page (as a length followed by a string) */
1015     hres = IUri_GetDisplayUri(This->window->uri, &display_uri);
1016     if(FAILED(hres))
1017         return hres;
1018
1019     len = SysStringLen(display_uri);
1020     hres = IStream_Write(pStream, &len, sizeof(len), &written);
1021     if(SUCCEEDED(hres))
1022         hres = IStream_Write(pStream, display_uri, len*sizeof(WCHAR), &written);
1023     SysFreeString(display_uri);
1024     return hres;
1025 }
1026
1027 static HRESULT WINAPI PersistHistory_SetPositionCookie(IPersistHistory *iface, DWORD dwPositioncookie)
1028 {
1029     HTMLDocument *This = impl_from_IPersistHistory(iface);
1030     FIXME("(%p)->(%x)\n", This, dwPositioncookie);
1031     return E_NOTIMPL;
1032 }
1033
1034 static HRESULT WINAPI PersistHistory_GetPositionCookie(IPersistHistory *iface, DWORD *pdwPositioncookie)
1035 {
1036     HTMLDocument *This = impl_from_IPersistHistory(iface);
1037     FIXME("(%p)->(%p)\n", This, pdwPositioncookie);
1038     return E_NOTIMPL;
1039 }
1040
1041 static const IPersistHistoryVtbl PersistHistoryVtbl = {
1042     PersistHistory_QueryInterface,
1043     PersistHistory_AddRef,
1044     PersistHistory_Release,
1045     PersistHistory_GetClassID,
1046     PersistHistory_LoadHistory,
1047     PersistHistory_SaveHistory,
1048     PersistHistory_SetPositionCookie,
1049     PersistHistory_GetPositionCookie
1050 };
1051
1052 void HTMLDocument_Persist_Init(HTMLDocument *This)
1053 {
1054     This->IPersistMoniker_iface.lpVtbl = &PersistMonikerVtbl;
1055     This->IPersistFile_iface.lpVtbl = &PersistFileVtbl;
1056     This->IMonikerProp_iface.lpVtbl = &MonikerPropVtbl;
1057     This->IPersistStreamInit_iface.lpVtbl = &PersistStreamInitVtbl;
1058     This->IPersistHistory_iface.lpVtbl = &PersistHistoryVtbl;
1059 }