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