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