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