oleaut32: Rewrite RollUdate to be easier to change and to support more conversions.
[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     }
112
113     if(doc->usermode == EDITMODE && doc->hostui) {
114         DOCHOSTUIINFO hostinfo;
115
116         memset(&hostinfo, 0, sizeof(DOCHOSTUIINFO));
117         hostinfo.cbSize = sizeof(DOCHOSTUIINFO);
118         hres = IDocHostUIHandler_GetHostInfo(doc->hostui, &hostinfo);
119         if(SUCCEEDED(hres))
120             /* FIXME: use hostinfo */
121             TRACE("hostinfo = {%u %08x %08x %s %s}\n",
122                     hostinfo.cbSize, hostinfo.dwFlags, hostinfo.dwDoubleClick,
123                     debugstr_w(hostinfo.pchHostCss), debugstr_w(hostinfo.pchHostNS));
124     }
125 }
126
127 static void set_downloading_proc(task_t *_task)
128 {
129     download_proc_task_t *task = (download_proc_task_t*)_task;
130     HTMLDocumentObj *doc = task->doc;
131     IOleCommandTarget *olecmd;
132     HRESULT hres;
133
134     TRACE("(%p)\n", doc);
135
136     if(doc->frame)
137         IOleInPlaceFrame_SetStatusText(doc->frame, NULL /* FIXME */);
138
139     if(!doc->client)
140         return;
141
142     if(task->set_download) {
143         hres = IOleClientSite_QueryInterface(doc->client, &IID_IOleCommandTarget, (void**)&olecmd);
144         if(SUCCEEDED(hres)) {
145             VARIANT var;
146
147             V_VT(&var) = VT_I4;
148             V_I4(&var) = 1;
149
150             IOleCommandTarget_Exec(olecmd, NULL, OLECMDID_SETDOWNLOADSTATE,
151                     OLECMDEXECOPT_DONTPROMPTUSER, &var, NULL);
152             IOleCommandTarget_Release(olecmd);
153         }
154
155         doc->download_state = 1;
156     }
157
158     if(doc->hostui) {
159         IDropTarget *drop_target = NULL;
160
161         hres = IDocHostUIHandler_GetDropTarget(doc->hostui, NULL /* FIXME */, &drop_target);
162         if(drop_target) {
163             FIXME("Use IDropTarget\n");
164             IDropTarget_Release(drop_target);
165         }
166     }
167 }
168
169 static HRESULT set_moniker(HTMLDocument *This, IMoniker *mon, IBindCtx *pibc, BOOL set_download)
170 {
171     nsChannelBSC *bscallback;
172     LPOLESTR url = NULL;
173     docobj_task_t *task;
174     download_proc_task_t *download_task;
175     nsIWineURI *nsuri;
176     HRESULT hres;
177
178     if(pibc) {
179         IUnknown *unk = NULL;
180
181         /* FIXME:
182          * Use params:
183          * "__PrecreatedObject"
184          * "BIND_CONTEXT_PARAM"
185          * "__HTMLLOADOPTIONS"
186          * "__DWNBINDINFO"
187          * "URL Context"
188          * "CBinding Context"
189          * "_ITransData_Object_"
190          * "_EnumFORMATETC_"
191          */
192
193         IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
194         if(unk) {
195             IOleClientSite *client = NULL;
196
197             hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
198             if(SUCCEEDED(hres)) {
199                 TRACE("Got client site %p\n", client);
200                 IOleObject_SetClientSite(OLEOBJ(This), client);
201                 IOleClientSite_Release(client);
202             }
203
204             IUnknown_Release(unk);
205         }
206     }
207
208     set_ready_state(This->window, READYSTATE_LOADING);
209     update_doc(This, UPDATE_TITLE);
210
211     HTMLDocument_LockContainer(This->doc_obj, TRUE);
212     
213     hres = IMoniker_GetDisplayName(mon, pibc, NULL, &url);
214     if(FAILED(hres)) {
215         WARN("GetDiaplayName failed: %08x\n", hres);
216         return hres;
217     }
218
219     TRACE("got url: %s\n", debugstr_w(url));
220
221     set_current_mon(This->window, mon);
222
223     if(This->doc_obj->client) {
224         VARIANT silent, offline;
225         IOleCommandTarget *cmdtrg = NULL;
226
227         hres = get_client_disp_property(This->doc_obj->client, DISPID_AMBIENT_SILENT, &silent);
228         if(SUCCEEDED(hres)) {
229             if(V_VT(&silent) != VT_BOOL)
230                 WARN("V_VT(silent) = %d\n", V_VT(&silent));
231             else if(V_BOOL(&silent))
232                 FIXME("silent == true\n");
233         }
234
235         hres = get_client_disp_property(This->doc_obj->client,
236                 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
237         if(SUCCEEDED(hres)) {
238             if(V_VT(&silent) != VT_BOOL)
239                 WARN("V_VT(offline) = %d\n", V_VT(&silent));
240             else if(V_BOOL(&silent))
241                 FIXME("offline == true\n");
242         }
243
244         hres = IOleClientSite_QueryInterface(This->doc_obj->client, &IID_IOleCommandTarget,
245                 (void**)&cmdtrg);
246         if(SUCCEEDED(hres)) {
247             VARIANT var;
248
249             V_VT(&var) = VT_I4;
250             V_I4(&var) = 0;
251             IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
252
253             IOleCommandTarget_Release(cmdtrg);
254         }
255     }
256
257     hres = create_doc_uri(This->window, url, &nsuri);
258     CoTaskMemFree(url);
259     if(FAILED(hres))
260         return hres;
261
262     bscallback = create_channelbsc(mon);
263
264     nsIWineURI_SetChannelBSC(nsuri, bscallback);
265     hres = load_nsuri(This->window, nsuri, LOAD_INITIAL_DOCUMENT_URI);
266     nsIWineURI_SetChannelBSC(nsuri, NULL);
267     if(SUCCEEDED(hres))
268         set_window_bscallback(This->window, bscallback);
269     IUnknown_Release((IUnknown*)bscallback);
270     if(FAILED(hres))
271         return hres;
272
273     if(This->doc_obj->frame) {
274         task = heap_alloc(sizeof(docobj_task_t));
275         task->doc = This->doc_obj;
276         push_task(&task->header, set_progress_proc, This->doc_obj->basedoc.task_magic);
277     }
278
279     download_task = heap_alloc(sizeof(download_proc_task_t));
280     download_task->doc = This->doc_obj;
281     download_task->set_download = set_download;
282     push_task(&download_task->header, set_downloading_proc, This->doc_obj->basedoc.task_magic);
283
284     return S_OK;
285 }
286
287 void set_ready_state(HTMLWindow *window, READYSTATE readystate)
288 {
289     window->readystate = readystate;
290     if(window->doc_obj && window->doc_obj->basedoc.window == window)
291         call_property_onchanged(&window->doc_obj->basedoc.cp_propnotif, DISPID_READYSTATE);
292     if(window->frame_element)
293         fire_event(window->frame_element->element.node.doc, EVENTID_READYSTATECHANGE,
294                    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     hres = set_moniker(This, pimkName, pibc, TRUE);
379     if(FAILED(hres))
380         return hres;
381
382     return start_binding(This->window, NULL, (BSCallback*)This->window->bscallback, pibc);
383 }
384
385 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
386         LPBC pbc, BOOL fRemember)
387 {
388     HTMLDocument *This = PERSISTMON_THIS(iface);
389     FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
390     return E_NOTIMPL;
391 }
392
393 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
394 {
395     HTMLDocument *This = PERSISTMON_THIS(iface);
396     FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
397     return E_NOTIMPL;
398 }
399
400 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
401 {
402     HTMLDocument *This = PERSISTMON_THIS(iface);
403
404     TRACE("(%p)->(%p)\n", This, ppimkName);
405
406     if(!This->window || !This->window->mon)
407         return E_UNEXPECTED;
408
409     IMoniker_AddRef(This->window->mon);
410     *ppimkName = This->window->mon;
411     return S_OK;
412 }
413
414 static const IPersistMonikerVtbl PersistMonikerVtbl = {
415     PersistMoniker_QueryInterface,
416     PersistMoniker_AddRef,
417     PersistMoniker_Release,
418     PersistMoniker_GetClassID,
419     PersistMoniker_IsDirty,
420     PersistMoniker_Load,
421     PersistMoniker_Save,
422     PersistMoniker_SaveCompleted,
423     PersistMoniker_GetCurMoniker
424 };
425
426 /**********************************************************
427  * IMonikerProp implementation
428  */
429
430 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
431
432 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
433 {
434     HTMLDocument *This = MONPROP_THIS(iface);
435     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
436 }
437
438 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
439 {
440     HTMLDocument *This = MONPROP_THIS(iface);
441     return IHTMLDocument2_AddRef(HTMLDOC(This));
442 }
443
444 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
445 {
446     HTMLDocument *This = MONPROP_THIS(iface);
447     return IHTMLDocument_Release(HTMLDOC(This));
448 }
449
450 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
451 {
452     HTMLDocument *This = MONPROP_THIS(iface);
453
454     TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
455
456     switch(mkp) {
457     case MIMETYPEPROP:
458         heap_free(This->doc_obj->mime);
459         This->doc_obj->mime = heap_strdupW(val);
460         break;
461
462     case CLASSIDPROP:
463         break;
464
465     default:
466         FIXME("mkp %d\n", mkp);
467         return E_NOTIMPL;
468     }
469
470     return S_OK;
471 }
472
473 static const IMonikerPropVtbl MonikerPropVtbl = {
474     MonikerProp_QueryInterface,
475     MonikerProp_AddRef,
476     MonikerProp_Release,
477     MonikerProp_PutProperty
478 };
479
480 /**********************************************************
481  * IPersistFile implementation
482  */
483
484 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
485
486 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
487 {
488     HTMLDocument *This = PERSISTFILE_THIS(iface);
489     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
490 }
491
492 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
493 {
494     HTMLDocument *This = PERSISTFILE_THIS(iface);
495     return IHTMLDocument2_AddRef(HTMLDOC(This));
496 }
497
498 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
499 {
500     HTMLDocument *This = PERSISTFILE_THIS(iface);
501     return IHTMLDocument2_Release(HTMLDOC(This));
502 }
503
504 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
505 {
506     HTMLDocument *This = PERSISTFILE_THIS(iface);
507
508     TRACE("(%p)->(%p)\n", This, pClassID);
509
510     if(!pClassID)
511         return E_INVALIDARG;
512
513     *pClassID = CLSID_HTMLDocument;
514     return S_OK;
515 }
516
517 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
518 {
519     HTMLDocument *This = PERSISTFILE_THIS(iface);
520
521     TRACE("(%p)\n", This);
522
523     return IPersistStreamInit_IsDirty(PERSTRINIT(This));
524 }
525
526 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
527 {
528     HTMLDocument *This = PERSISTFILE_THIS(iface);
529     FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
530     return E_NOTIMPL;
531 }
532
533 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
534 {
535     HTMLDocument *This = PERSISTFILE_THIS(iface);
536     char *str;
537     DWORD written=0;
538     HANDLE file;
539     HRESULT hres;
540
541     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
542
543     file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
544                        FILE_ATTRIBUTE_NORMAL, NULL);
545     if(file == INVALID_HANDLE_VALUE) {
546         WARN("Could not create file: %u\n", GetLastError());
547         return E_FAIL;
548     }
549
550     hres = get_doc_string(This->doc_node, &str);
551     if(SUCCEEDED(hres))
552         WriteFile(file, str, strlen(str), &written, NULL);
553
554     CloseHandle(file);
555     return hres;
556 }
557
558 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
559 {
560     HTMLDocument *This = PERSISTFILE_THIS(iface);
561     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
562     return E_NOTIMPL;
563 }
564
565 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
566 {
567     HTMLDocument *This = PERSISTFILE_THIS(iface);
568     FIXME("(%p)->(%p)\n", This, pszFileName);
569     return E_NOTIMPL;
570 }
571
572 static const IPersistFileVtbl PersistFileVtbl = {
573     PersistFile_QueryInterface,
574     PersistFile_AddRef,
575     PersistFile_Release,
576     PersistFile_GetClassID,
577     PersistFile_IsDirty,
578     PersistFile_Load,
579     PersistFile_Save,
580     PersistFile_SaveCompleted,
581     PersistFile_GetCurFile
582 };
583
584 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
585
586 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
587                                                        REFIID riid, void **ppv)
588 {
589     HTMLDocument *This = PERSTRINIT_THIS(iface);
590     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
591 }
592
593 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
594 {
595     HTMLDocument *This = PERSTRINIT_THIS(iface);
596     return IHTMLDocument2_AddRef(HTMLDOC(This));
597 }
598
599 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
600 {
601     HTMLDocument *This = PERSTRINIT_THIS(iface);
602     return IHTMLDocument2_Release(HTMLDOC(This));
603 }
604
605 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
606 {
607     HTMLDocument *This = PERSTRINIT_THIS(iface);
608     return IPersist_GetClassID(PERSIST(This), pClassID);
609 }
610
611 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
612 {
613     HTMLDocument *This = PERSTRINIT_THIS(iface);
614
615     TRACE("(%p)\n", This);
616
617     if(This->doc_obj->usermode == EDITMODE)
618         return editor_is_dirty(This);
619
620     return S_FALSE;
621 }
622
623 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
624 {
625     HTMLDocument *This = PERSTRINIT_THIS(iface);
626     IMoniker *mon;
627     HRESULT hres;
628
629     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
630
631     TRACE("(%p)->(%p)\n", This, pStm);
632
633     hres = CreateURLMoniker(NULL, about_blankW, &mon);
634     if(FAILED(hres)) {
635         WARN("CreateURLMoniker failed: %08x\n", hres);
636         return hres;
637     }
638
639     hres = set_moniker(This, mon, NULL, TRUE);
640     IMoniker_Release(mon);
641     if(FAILED(hres))
642         return hres;
643
644     return channelbsc_load_stream(This->window->bscallback, pStm);
645 }
646
647 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
648                                              BOOL fClearDirty)
649 {
650     HTMLDocument *This = PERSTRINIT_THIS(iface);
651     char *str;
652     DWORD written=0;
653     HRESULT hres;
654
655     TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
656
657     hres = get_doc_string(This->doc_node, &str);
658     if(FAILED(hres))
659         return hres;
660
661     hres = IStream_Write(pStm, str, strlen(str), &written);
662     if(FAILED(hres))
663         FIXME("Write failed: %08x\n", hres);
664
665     heap_free(str);
666
667     if(fClearDirty)
668         set_dirty(This, VARIANT_FALSE);
669
670     return S_OK;
671 }
672
673 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
674                                                    ULARGE_INTEGER *pcbSize)
675 {
676     HTMLDocument *This = PERSTRINIT_THIS(iface);
677     FIXME("(%p)->(%p)\n", This, pcbSize);
678     return E_NOTIMPL;
679 }
680
681 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
682 {
683     HTMLDocument *This = PERSTRINIT_THIS(iface);
684     IMoniker *mon;
685     HGLOBAL body;
686     LPSTREAM stream;
687     HRESULT hres;
688
689     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
690     static const WCHAR html_bodyW[] = {'<','H','T','M','L','>','<','/','H','T','M','L','>',0};
691
692     TRACE("(%p)\n", This);
693
694     body = GlobalAlloc(0, sizeof(html_bodyW));
695     if(!body)
696         return E_OUTOFMEMORY;
697     memcpy(body, html_bodyW, sizeof(html_bodyW));
698
699     hres = CreateURLMoniker(NULL, about_blankW, &mon);
700     if(FAILED(hres)) {
701         WARN("CreateURLMoniker failed: %08x\n", hres);
702         GlobalFree(body);
703         return hres;
704     }
705
706     hres = set_moniker(This, mon, NULL, FALSE);
707     IMoniker_Release(mon);
708     if(FAILED(hres)) {
709         GlobalFree(body);
710         return hres;
711     }
712
713     hres = CreateStreamOnHGlobal(body, TRUE, &stream);
714     if(FAILED(hres)) {
715         GlobalFree(body);
716         return hres;
717     }
718
719     hres = channelbsc_load_stream(This->window->bscallback, stream);
720
721     IStream_Release(stream);
722     return hres;
723 }
724
725 #undef PERSTRINIT_THIS
726
727 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
728     PersistStreamInit_QueryInterface,
729     PersistStreamInit_AddRef,
730     PersistStreamInit_Release,
731     PersistStreamInit_GetClassID,
732     PersistStreamInit_IsDirty,
733     PersistStreamInit_Load,
734     PersistStreamInit_Save,
735     PersistStreamInit_GetSizeMax,
736     PersistStreamInit_InitNew
737 };
738
739 /**********************************************************
740  * IPersistHistory implementation
741  */
742
743 #define PERSISTHIST_THIS(iface) DEFINE_THIS(HTMLDocument, PersistHistory, iface)
744
745 static HRESULT WINAPI PersistHistory_QueryInterface(IPersistHistory *iface, REFIID riid, void **ppvObject)
746 {
747     HTMLDocument *This = PERSISTHIST_THIS(iface);
748     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
749 }
750
751 static ULONG WINAPI PersistHistory_AddRef(IPersistHistory *iface)
752 {
753     HTMLDocument *This = PERSISTHIST_THIS(iface);
754     return IHTMLDocument2_AddRef(HTMLDOC(This));
755 }
756
757 static ULONG WINAPI PersistHistory_Release(IPersistHistory *iface)
758 {
759     HTMLDocument *This = PERSISTHIST_THIS(iface);
760     return IHTMLDocument2_Release(HTMLDOC(This));
761 }
762
763 static HRESULT WINAPI PersistHistory_GetClassID(IPersistHistory *iface, CLSID *pClassID)
764 {
765     HTMLDocument *This = PERSISTHIST_THIS(iface);
766     return IPersist_GetClassID(PERSIST(This), pClassID);
767 }
768
769 static HRESULT WINAPI PersistHistory_LoadHistory(IPersistHistory *iface, IStream *pStream, IBindCtx *pbc)
770 {
771     HTMLDocument *This = PERSISTHIST_THIS(iface);
772     FIXME("(%p)->(%p %p)\n", This, pStream, pbc);
773     return E_NOTIMPL;
774 }
775
776 static HRESULT WINAPI PersistHistory_SaveHistory(IPersistHistory *iface, IStream *pStream)
777 {
778     HTMLDocument *This = PERSISTHIST_THIS(iface);
779     FIXME("(%p)->(%p)\n", This, pStream);
780     return E_NOTIMPL;
781 }
782
783 static HRESULT WINAPI PersistHistory_SetPositionCookie(IPersistHistory *iface, DWORD dwPositioncookie)
784 {
785     HTMLDocument *This = PERSISTHIST_THIS(iface);
786     FIXME("(%p)->(%x)\n", This, dwPositioncookie);
787     return E_NOTIMPL;
788 }
789
790 static HRESULT WINAPI PersistHistory_GetPositionCookie(IPersistHistory *iface, DWORD *pdwPositioncookie)
791 {
792     HTMLDocument *This = PERSISTHIST_THIS(iface);
793     FIXME("(%p)->(%p)\n", This, pdwPositioncookie);
794     return E_NOTIMPL;
795 }
796
797 #undef PERSISTHIST_THIS
798
799 static const IPersistHistoryVtbl PersistHistoryVtbl = {
800     PersistHistory_QueryInterface,
801     PersistHistory_AddRef,
802     PersistHistory_Release,
803     PersistHistory_GetClassID,
804     PersistHistory_LoadHistory,
805     PersistHistory_SaveHistory,
806     PersistHistory_SetPositionCookie,
807     PersistHistory_GetPositionCookie
808 };
809
810 void HTMLDocument_Persist_Init(HTMLDocument *This)
811 {
812     This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
813     This->lpPersistFileVtbl = &PersistFileVtbl;
814     This->lpMonikerPropVtbl = &MonikerPropVtbl;
815     This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
816     This->lpPersistHistoryVtbl = &PersistHistoryVtbl;
817 }