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