hlink: Implement IHlinkBrowseContext::GetBrowseWindowInfo.
[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     if(window->doc_obj && window->doc_obj->basedoc.window == window)
287         call_property_onchanged(&window->doc_obj->basedoc.cp_propnotif, DISPID_READYSTATE);
288     if(window->frame_element)
289         fire_event(window->frame_element->element.node.doc, EVENTID_READYSTATECHANGE,
290                    window->frame_element->element.node.nsnode, NULL);
291 }
292
293 static HRESULT get_doc_string(HTMLDocumentNode *This, char **str)
294 {
295     nsIDOMNode *nsnode;
296     LPCWSTR strw;
297     nsAString nsstr;
298     nsresult nsres;
299
300     if(!This->nsdoc) {
301         WARN("NULL nsdoc\n");
302         return E_UNEXPECTED;
303     }
304
305     nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
306     if(NS_FAILED(nsres)) {
307         ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
308         return E_FAIL;
309     }
310
311     nsAString_Init(&nsstr, NULL);
312     nsnode_to_nsstring(nsnode, &nsstr);
313     nsIDOMNode_Release(nsnode);
314
315     nsAString_GetData(&nsstr, &strw);
316     TRACE("%s\n", debugstr_w(strw));
317
318     *str = heap_strdupWtoA(strw);
319
320     nsAString_Finish(&nsstr);
321
322     return S_OK;
323 }
324
325
326 /**********************************************************
327  * IPersistMoniker implementation
328  */
329
330 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
331
332 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
333                                                             void **ppvObject)
334 {
335     HTMLDocument *This = PERSISTMON_THIS(iface);
336     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
337 }
338
339 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
340 {
341     HTMLDocument *This = PERSISTMON_THIS(iface);
342     return IHTMLDocument2_AddRef(HTMLDOC(This));
343 }
344
345 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
346 {
347     HTMLDocument *This = PERSISTMON_THIS(iface);
348     return IHTMLDocument2_Release(HTMLDOC(This));
349 }
350
351 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
352 {
353     HTMLDocument *This = PERSISTMON_THIS(iface);
354     return IPersist_GetClassID(PERSIST(This), pClassID);
355 }
356
357 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
358 {
359     HTMLDocument *This = PERSISTMON_THIS(iface);
360
361     TRACE("(%p)\n", This);
362
363     return IPersistStreamInit_IsDirty(PERSTRINIT(This));
364 }
365
366 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
367         IMoniker *pimkName, LPBC pibc, DWORD grfMode)
368 {
369     HTMLDocument *This = PERSISTMON_THIS(iface);
370     HRESULT hres;
371
372     TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
373
374     if(pibc) {
375         IUnknown *unk = NULL;
376
377         /* FIXME:
378          * Use params:
379          * "__PrecreatedObject"
380          * "BIND_CONTEXT_PARAM"
381          * "__HTMLLOADOPTIONS"
382          * "__DWNBINDINFO"
383          * "URL Context"
384          * "_ITransData_Object_"
385          * "_EnumFORMATETC_"
386          */
387
388         IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
389         if(unk) {
390             IOleClientSite *client = NULL;
391
392             hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
393             if(SUCCEEDED(hres)) {
394                 TRACE("Got client site %p\n", client);
395                 IOleObject_SetClientSite(OLEOBJ(This), client);
396                 IOleClientSite_Release(client);
397             }
398
399             IUnknown_Release(unk);
400         }
401     }
402
403     hres = set_moniker(This, pimkName, pibc, NULL, TRUE);
404     if(FAILED(hres))
405         return hres;
406
407     return start_binding(This->window, NULL, (BSCallback*)This->window->bscallback, pibc);
408 }
409
410 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
411         LPBC pbc, BOOL fRemember)
412 {
413     HTMLDocument *This = PERSISTMON_THIS(iface);
414     FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
415     return E_NOTIMPL;
416 }
417
418 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
419 {
420     HTMLDocument *This = PERSISTMON_THIS(iface);
421     FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
422     return E_NOTIMPL;
423 }
424
425 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
426 {
427     HTMLDocument *This = PERSISTMON_THIS(iface);
428
429     TRACE("(%p)->(%p)\n", This, ppimkName);
430
431     if(!This->window || !This->window->mon)
432         return E_UNEXPECTED;
433
434     IMoniker_AddRef(This->window->mon);
435     *ppimkName = This->window->mon;
436     return S_OK;
437 }
438
439 static const IPersistMonikerVtbl PersistMonikerVtbl = {
440     PersistMoniker_QueryInterface,
441     PersistMoniker_AddRef,
442     PersistMoniker_Release,
443     PersistMoniker_GetClassID,
444     PersistMoniker_IsDirty,
445     PersistMoniker_Load,
446     PersistMoniker_Save,
447     PersistMoniker_SaveCompleted,
448     PersistMoniker_GetCurMoniker
449 };
450
451 /**********************************************************
452  * IMonikerProp implementation
453  */
454
455 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
456
457 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
458 {
459     HTMLDocument *This = MONPROP_THIS(iface);
460     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
461 }
462
463 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
464 {
465     HTMLDocument *This = MONPROP_THIS(iface);
466     return IHTMLDocument2_AddRef(HTMLDOC(This));
467 }
468
469 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
470 {
471     HTMLDocument *This = MONPROP_THIS(iface);
472     return IHTMLDocument_Release(HTMLDOC(This));
473 }
474
475 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
476 {
477     HTMLDocument *This = MONPROP_THIS(iface);
478
479     TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
480
481     switch(mkp) {
482     case MIMETYPEPROP:
483         heap_free(This->doc_obj->mime);
484         This->doc_obj->mime = heap_strdupW(val);
485         break;
486
487     case CLASSIDPROP:
488         break;
489
490     default:
491         FIXME("mkp %d\n", mkp);
492         return E_NOTIMPL;
493     }
494
495     return S_OK;
496 }
497
498 static const IMonikerPropVtbl MonikerPropVtbl = {
499     MonikerProp_QueryInterface,
500     MonikerProp_AddRef,
501     MonikerProp_Release,
502     MonikerProp_PutProperty
503 };
504
505 /**********************************************************
506  * IPersistFile implementation
507  */
508
509 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
510
511 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
512 {
513     HTMLDocument *This = PERSISTFILE_THIS(iface);
514     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
515 }
516
517 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
518 {
519     HTMLDocument *This = PERSISTFILE_THIS(iface);
520     return IHTMLDocument2_AddRef(HTMLDOC(This));
521 }
522
523 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
524 {
525     HTMLDocument *This = PERSISTFILE_THIS(iface);
526     return IHTMLDocument2_Release(HTMLDOC(This));
527 }
528
529 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
530 {
531     HTMLDocument *This = PERSISTFILE_THIS(iface);
532
533     TRACE("(%p)->(%p)\n", This, pClassID);
534
535     if(!pClassID)
536         return E_INVALIDARG;
537
538     *pClassID = CLSID_HTMLDocument;
539     return S_OK;
540 }
541
542 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
543 {
544     HTMLDocument *This = PERSISTFILE_THIS(iface);
545
546     TRACE("(%p)\n", This);
547
548     return IPersistStreamInit_IsDirty(PERSTRINIT(This));
549 }
550
551 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
552 {
553     HTMLDocument *This = PERSISTFILE_THIS(iface);
554     FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
555     return E_NOTIMPL;
556 }
557
558 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
559 {
560     HTMLDocument *This = PERSISTFILE_THIS(iface);
561     char *str;
562     DWORD written=0;
563     HANDLE file;
564     HRESULT hres;
565
566     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
567
568     file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
569                        FILE_ATTRIBUTE_NORMAL, NULL);
570     if(file == INVALID_HANDLE_VALUE) {
571         WARN("Could not create file: %u\n", GetLastError());
572         return E_FAIL;
573     }
574
575     hres = get_doc_string(This->doc_node, &str);
576     if(SUCCEEDED(hres))
577         WriteFile(file, str, strlen(str), &written, NULL);
578
579     CloseHandle(file);
580     return hres;
581 }
582
583 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
584 {
585     HTMLDocument *This = PERSISTFILE_THIS(iface);
586     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
587     return E_NOTIMPL;
588 }
589
590 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
591 {
592     HTMLDocument *This = PERSISTFILE_THIS(iface);
593     FIXME("(%p)->(%p)\n", This, pszFileName);
594     return E_NOTIMPL;
595 }
596
597 static const IPersistFileVtbl PersistFileVtbl = {
598     PersistFile_QueryInterface,
599     PersistFile_AddRef,
600     PersistFile_Release,
601     PersistFile_GetClassID,
602     PersistFile_IsDirty,
603     PersistFile_Load,
604     PersistFile_Save,
605     PersistFile_SaveCompleted,
606     PersistFile_GetCurFile
607 };
608
609 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
610
611 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
612                                                        REFIID riid, void **ppv)
613 {
614     HTMLDocument *This = PERSTRINIT_THIS(iface);
615     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
616 }
617
618 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
619 {
620     HTMLDocument *This = PERSTRINIT_THIS(iface);
621     return IHTMLDocument2_AddRef(HTMLDOC(This));
622 }
623
624 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
625 {
626     HTMLDocument *This = PERSTRINIT_THIS(iface);
627     return IHTMLDocument2_Release(HTMLDOC(This));
628 }
629
630 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
631 {
632     HTMLDocument *This = PERSTRINIT_THIS(iface);
633     return IPersist_GetClassID(PERSIST(This), pClassID);
634 }
635
636 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
637 {
638     HTMLDocument *This = PERSTRINIT_THIS(iface);
639
640     TRACE("(%p)\n", This);
641
642     if(This->doc_obj->usermode == EDITMODE)
643         return editor_is_dirty(This);
644
645     return S_FALSE;
646 }
647
648 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
649 {
650     HTMLDocument *This = PERSTRINIT_THIS(iface);
651     IMoniker *mon;
652     HRESULT hres;
653
654     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
655
656     TRACE("(%p)->(%p)\n", This, pStm);
657
658     hres = CreateURLMoniker(NULL, about_blankW, &mon);
659     if(FAILED(hres)) {
660         WARN("CreateURLMoniker failed: %08x\n", hres);
661         return hres;
662     }
663
664     hres = set_moniker(This, mon, NULL, NULL, TRUE);
665     IMoniker_Release(mon);
666     if(FAILED(hres))
667         return hres;
668
669     return channelbsc_load_stream(This->window->bscallback, pStm);
670 }
671
672 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
673                                              BOOL fClearDirty)
674 {
675     HTMLDocument *This = PERSTRINIT_THIS(iface);
676     char *str;
677     DWORD written=0;
678     HRESULT hres;
679
680     TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
681
682     hres = get_doc_string(This->doc_node, &str);
683     if(FAILED(hres))
684         return hres;
685
686     hres = IStream_Write(pStm, str, strlen(str), &written);
687     if(FAILED(hres))
688         FIXME("Write failed: %08x\n", hres);
689
690     heap_free(str);
691
692     if(fClearDirty)
693         set_dirty(This, VARIANT_FALSE);
694
695     return S_OK;
696 }
697
698 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
699                                                    ULARGE_INTEGER *pcbSize)
700 {
701     HTMLDocument *This = PERSTRINIT_THIS(iface);
702     FIXME("(%p)->(%p)\n", This, pcbSize);
703     return E_NOTIMPL;
704 }
705
706 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
707 {
708     HTMLDocument *This = PERSTRINIT_THIS(iface);
709     IMoniker *mon;
710     HRESULT hres;
711
712     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
713
714     TRACE("(%p)\n", This);
715
716     hres = CreateURLMoniker(NULL, about_blankW, &mon);
717     if(FAILED(hres)) {
718         WARN("CreateURLMoniker failed: %08x\n", hres);
719         return hres;
720     }
721
722     hres = set_moniker(This, mon, NULL, NULL, FALSE);
723     IMoniker_Release(mon);
724     if(FAILED(hres))
725         return hres;
726
727     return start_binding(This->window, NULL, (BSCallback*)This->window->bscallback, NULL);
728 }
729
730 #undef PERSTRINIT_THIS
731
732 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
733     PersistStreamInit_QueryInterface,
734     PersistStreamInit_AddRef,
735     PersistStreamInit_Release,
736     PersistStreamInit_GetClassID,
737     PersistStreamInit_IsDirty,
738     PersistStreamInit_Load,
739     PersistStreamInit_Save,
740     PersistStreamInit_GetSizeMax,
741     PersistStreamInit_InitNew
742 };
743
744 /**********************************************************
745  * IPersistHistory implementation
746  */
747
748 #define PERSISTHIST_THIS(iface) DEFINE_THIS(HTMLDocument, PersistHistory, iface)
749
750 static HRESULT WINAPI PersistHistory_QueryInterface(IPersistHistory *iface, REFIID riid, void **ppvObject)
751 {
752     HTMLDocument *This = PERSISTHIST_THIS(iface);
753     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
754 }
755
756 static ULONG WINAPI PersistHistory_AddRef(IPersistHistory *iface)
757 {
758     HTMLDocument *This = PERSISTHIST_THIS(iface);
759     return IHTMLDocument2_AddRef(HTMLDOC(This));
760 }
761
762 static ULONG WINAPI PersistHistory_Release(IPersistHistory *iface)
763 {
764     HTMLDocument *This = PERSISTHIST_THIS(iface);
765     return IHTMLDocument2_Release(HTMLDOC(This));
766 }
767
768 static HRESULT WINAPI PersistHistory_GetClassID(IPersistHistory *iface, CLSID *pClassID)
769 {
770     HTMLDocument *This = PERSISTHIST_THIS(iface);
771     return IPersist_GetClassID(PERSIST(This), pClassID);
772 }
773
774 static HRESULT WINAPI PersistHistory_LoadHistory(IPersistHistory *iface, IStream *pStream, IBindCtx *pbc)
775 {
776     HTMLDocument *This = PERSISTHIST_THIS(iface);
777     FIXME("(%p)->(%p %p)\n", This, pStream, pbc);
778     return E_NOTIMPL;
779 }
780
781 static HRESULT WINAPI PersistHistory_SaveHistory(IPersistHistory *iface, IStream *pStream)
782 {
783     HTMLDocument *This = PERSISTHIST_THIS(iface);
784     FIXME("(%p)->(%p)\n", This, pStream);
785     return E_NOTIMPL;
786 }
787
788 static HRESULT WINAPI PersistHistory_SetPositionCookie(IPersistHistory *iface, DWORD dwPositioncookie)
789 {
790     HTMLDocument *This = PERSISTHIST_THIS(iface);
791     FIXME("(%p)->(%x)\n", This, dwPositioncookie);
792     return E_NOTIMPL;
793 }
794
795 static HRESULT WINAPI PersistHistory_GetPositionCookie(IPersistHistory *iface, DWORD *pdwPositioncookie)
796 {
797     HTMLDocument *This = PERSISTHIST_THIS(iface);
798     FIXME("(%p)->(%p)\n", This, pdwPositioncookie);
799     return E_NOTIMPL;
800 }
801
802 #undef PERSISTHIST_THIS
803
804 static const IPersistHistoryVtbl PersistHistoryVtbl = {
805     PersistHistory_QueryInterface,
806     PersistHistory_AddRef,
807     PersistHistory_Release,
808     PersistHistory_GetClassID,
809     PersistHistory_LoadHistory,
810     PersistHistory_SaveHistory,
811     PersistHistory_SetPositionCookie,
812     PersistHistory_GetPositionCookie
813 };
814
815 void HTMLDocument_Persist_Init(HTMLDocument *This)
816 {
817     This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
818     This->lpPersistFileVtbl = &PersistFileVtbl;
819     This->lpMonikerPropVtbl = &MonikerPropVtbl;
820     This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
821     This->lpPersistHistoryVtbl = &PersistHistoryVtbl;
822 }