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