ddraw: Avoid LPDIRECTDRAWSURFACE7.
[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         hres = push_task(&task->header, set_progress_proc, NULL, This->doc_obj->basedoc.task_magic);
392         if(FAILED(hres)) {
393             CoTaskMemFree(url);
394             return hres;
395         }
396     }
397
398     download_task = heap_alloc(sizeof(download_proc_task_t));
399     download_task->doc = This->doc_obj;
400     download_task->set_download = set_download;
401     download_task->url = url;
402     return push_task(&download_task->header, set_downloading_proc, set_downloading_task_destr, This->doc_obj->basedoc.task_magic);
403 }
404
405 void set_ready_state(HTMLOuterWindow *window, READYSTATE readystate)
406 {
407     window->readystate = readystate;
408
409     if(window->doc_obj && window->doc_obj->basedoc.window == window)
410         call_property_onchanged(&window->doc_obj->basedoc.cp_propnotif, DISPID_READYSTATE);
411
412     fire_event(window->base.inner_window->doc, EVENTID_READYSTATECHANGE, FALSE,
413             window->base.inner_window->doc->node.nsnode, NULL, NULL);
414
415     if(window->frame_element)
416         fire_event(window->frame_element->element.node.doc, EVENTID_READYSTATECHANGE,
417                    TRUE, window->frame_element->element.node.nsnode, NULL, NULL);
418 }
419
420 static HRESULT get_doc_string(HTMLDocumentNode *This, char **str)
421 {
422     nsIDOMNode *nsnode;
423     LPCWSTR strw;
424     nsAString nsstr;
425     nsresult nsres;
426     HRESULT hres;
427
428     if(!This->nsdoc) {
429         WARN("NULL nsdoc\n");
430         return E_UNEXPECTED;
431     }
432
433     nsres = nsIDOMHTMLDocument_QueryInterface(This->nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
434     if(NS_FAILED(nsres)) {
435         ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
436         return E_FAIL;
437     }
438
439     nsAString_Init(&nsstr, NULL);
440     hres = nsnode_to_nsstring(nsnode, &nsstr);
441     nsIDOMNode_Release(nsnode);
442     if(FAILED(hres)) {
443         nsAString_Finish(&nsstr);
444         return hres;
445     }
446
447     nsAString_GetData(&nsstr, &strw);
448     TRACE("%s\n", debugstr_w(strw));
449
450     *str = heap_strdupWtoA(strw);
451
452     nsAString_Finish(&nsstr);
453
454     if(!*str)
455         return E_OUTOFMEMORY;
456     return S_OK;
457 }
458
459
460 /**********************************************************
461  * IPersistMoniker implementation
462  */
463
464 static inline HTMLDocument *impl_from_IPersistMoniker(IPersistMoniker *iface)
465 {
466     return CONTAINING_RECORD(iface, HTMLDocument, IPersistMoniker_iface);
467 }
468
469 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid, void **ppv)
470 {
471     HTMLDocument *This = impl_from_IPersistMoniker(iface);
472     return htmldoc_query_interface(This, riid, ppv);
473 }
474
475 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
476 {
477     HTMLDocument *This = impl_from_IPersistMoniker(iface);
478     return htmldoc_addref(This);
479 }
480
481 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
482 {
483     HTMLDocument *This = impl_from_IPersistMoniker(iface);
484     return htmldoc_release(This);
485 }
486
487 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
488 {
489     HTMLDocument *This = impl_from_IPersistMoniker(iface);
490     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
491 }
492
493 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
494 {
495     HTMLDocument *This = impl_from_IPersistMoniker(iface);
496
497     TRACE("(%p)\n", This);
498
499     return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
500 }
501
502 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
503         IMoniker *pimkName, LPBC pibc, DWORD grfMode)
504 {
505     HTMLDocument *This = impl_from_IPersistMoniker(iface);
506     HRESULT hres;
507
508     TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
509
510     if(pibc) {
511         IUnknown *unk = NULL;
512
513         /* FIXME:
514          * Use params:
515          * "__PrecreatedObject"
516          * "BIND_CONTEXT_PARAM"
517          * "__HTMLLOADOPTIONS"
518          * "__DWNBINDINFO"
519          * "URL Context"
520          * "_ITransData_Object_"
521          * "_EnumFORMATETC_"
522          */
523
524         hres = IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
525         if(SUCCEEDED(hres) && unk) {
526             IOleClientSite *client = NULL;
527
528             hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
529             if(SUCCEEDED(hres)) {
530                 TRACE("Got client site %p\n", client);
531                 IOleObject_SetClientSite(&This->IOleObject_iface, client);
532                 IOleClientSite_Release(client);
533             }
534
535             IUnknown_Release(unk);
536         }
537     }
538
539     prepare_for_binding(This, pimkName, FALSE);
540     call_docview_84(This->doc_obj);
541     hres = set_moniker(This, pimkName, pibc, NULL, TRUE);
542     if(FAILED(hres))
543         return hres;
544
545     return start_binding(This->window->pending_window, (BSCallback*)This->window->pending_window->bscallback, pibc);
546 }
547
548 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
549         LPBC pbc, BOOL fRemember)
550 {
551     HTMLDocument *This = impl_from_IPersistMoniker(iface);
552     FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
553     return E_NOTIMPL;
554 }
555
556 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
557 {
558     HTMLDocument *This = impl_from_IPersistMoniker(iface);
559     FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
560     return E_NOTIMPL;
561 }
562
563 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
564 {
565     HTMLDocument *This = impl_from_IPersistMoniker(iface);
566
567     TRACE("(%p)->(%p)\n", This, ppimkName);
568
569     if(!This->window || !This->window->mon)
570         return E_UNEXPECTED;
571
572     IMoniker_AddRef(This->window->mon);
573     *ppimkName = This->window->mon;
574     return S_OK;
575 }
576
577 static const IPersistMonikerVtbl PersistMonikerVtbl = {
578     PersistMoniker_QueryInterface,
579     PersistMoniker_AddRef,
580     PersistMoniker_Release,
581     PersistMoniker_GetClassID,
582     PersistMoniker_IsDirty,
583     PersistMoniker_Load,
584     PersistMoniker_Save,
585     PersistMoniker_SaveCompleted,
586     PersistMoniker_GetCurMoniker
587 };
588
589 /**********************************************************
590  * IMonikerProp implementation
591  */
592
593 static inline HTMLDocument *impl_from_IMonikerProp(IMonikerProp *iface)
594 {
595     return CONTAINING_RECORD(iface, HTMLDocument, IMonikerProp_iface);
596 }
597
598 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppv)
599 {
600     HTMLDocument *This = impl_from_IMonikerProp(iface);
601     return htmldoc_query_interface(This, riid, ppv);
602 }
603
604 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
605 {
606     HTMLDocument *This = impl_from_IMonikerProp(iface);
607     return htmldoc_addref(This);
608 }
609
610 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
611 {
612     HTMLDocument *This = impl_from_IMonikerProp(iface);
613     return htmldoc_release(This);
614 }
615
616 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
617 {
618     HTMLDocument *This = impl_from_IMonikerProp(iface);
619
620     TRACE("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
621
622     switch(mkp) {
623     case MIMETYPEPROP:
624         heap_free(This->doc_obj->mime);
625         This->doc_obj->mime = heap_strdupW(val);
626         break;
627
628     case CLASSIDPROP:
629         break;
630
631     default:
632         FIXME("mkp %d\n", mkp);
633         return E_NOTIMPL;
634     }
635
636     return S_OK;
637 }
638
639 static const IMonikerPropVtbl MonikerPropVtbl = {
640     MonikerProp_QueryInterface,
641     MonikerProp_AddRef,
642     MonikerProp_Release,
643     MonikerProp_PutProperty
644 };
645
646 /**********************************************************
647  * IPersistFile implementation
648  */
649
650 static inline HTMLDocument *impl_from_IPersistFile(IPersistFile *iface)
651 {
652     return CONTAINING_RECORD(iface, HTMLDocument, IPersistFile_iface);
653 }
654
655 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppv)
656 {
657     HTMLDocument *This = impl_from_IPersistFile(iface);
658     return htmldoc_query_interface(This, riid, ppv);
659 }
660
661 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
662 {
663     HTMLDocument *This = impl_from_IPersistFile(iface);
664     return htmldoc_addref(This);
665 }
666
667 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
668 {
669     HTMLDocument *This = impl_from_IPersistFile(iface);
670     return htmldoc_release(This);
671 }
672
673 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
674 {
675     HTMLDocument *This = impl_from_IPersistFile(iface);
676
677     TRACE("(%p)->(%p)\n", This, pClassID);
678
679     if(!pClassID)
680         return E_INVALIDARG;
681
682     *pClassID = CLSID_HTMLDocument;
683     return S_OK;
684 }
685
686 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
687 {
688     HTMLDocument *This = impl_from_IPersistFile(iface);
689
690     TRACE("(%p)\n", This);
691
692     return IPersistStreamInit_IsDirty(&This->IPersistStreamInit_iface);
693 }
694
695 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
696 {
697     HTMLDocument *This = impl_from_IPersistFile(iface);
698     FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
699     return E_NOTIMPL;
700 }
701
702 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
703 {
704     HTMLDocument *This = impl_from_IPersistFile(iface);
705     char *str;
706     DWORD written=0;
707     HANDLE file;
708     HRESULT hres;
709
710     TRACE("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
711
712     file = CreateFileW(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
713                        FILE_ATTRIBUTE_NORMAL, NULL);
714     if(file == INVALID_HANDLE_VALUE) {
715         WARN("Could not create file: %u\n", GetLastError());
716         return E_FAIL;
717     }
718
719     hres = get_doc_string(This->doc_node, &str);
720     if(SUCCEEDED(hres))
721         WriteFile(file, str, strlen(str), &written, NULL);
722
723     CloseHandle(file);
724     return hres;
725 }
726
727 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
728 {
729     HTMLDocument *This = impl_from_IPersistFile(iface);
730     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
731     return E_NOTIMPL;
732 }
733
734 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
735 {
736     HTMLDocument *This = impl_from_IPersistFile(iface);
737     FIXME("(%p)->(%p)\n", This, pszFileName);
738     return E_NOTIMPL;
739 }
740
741 static const IPersistFileVtbl PersistFileVtbl = {
742     PersistFile_QueryInterface,
743     PersistFile_AddRef,
744     PersistFile_Release,
745     PersistFile_GetClassID,
746     PersistFile_IsDirty,
747     PersistFile_Load,
748     PersistFile_Save,
749     PersistFile_SaveCompleted,
750     PersistFile_GetCurFile
751 };
752
753 static inline HTMLDocument *impl_from_IPersistStreamInit(IPersistStreamInit *iface)
754 {
755     return CONTAINING_RECORD(iface, HTMLDocument, IPersistStreamInit_iface);
756 }
757
758 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
759                                                        REFIID riid, void **ppv)
760 {
761     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
762     return htmldoc_query_interface(This, riid, ppv);
763 }
764
765 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
766 {
767     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
768     return htmldoc_addref(This);
769 }
770
771 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
772 {
773     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
774     return htmldoc_release(This);
775 }
776
777 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
778 {
779     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
780     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
781 }
782
783 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
784 {
785     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
786
787     TRACE("(%p)\n", This);
788
789     if(This->doc_obj->usermode == EDITMODE)
790         return editor_is_dirty(This);
791
792     return S_FALSE;
793 }
794
795 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
796 {
797     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
798     IMoniker *mon;
799     HRESULT hres;
800
801     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
802
803     TRACE("(%p)->(%p)\n", This, pStm);
804
805     hres = CreateURLMoniker(NULL, about_blankW, &mon);
806     if(FAILED(hres)) {
807         WARN("CreateURLMoniker failed: %08x\n", hres);
808         return hres;
809     }
810
811     prepare_for_binding(This, mon, FALSE);
812     hres = set_moniker(This, mon, NULL, NULL, TRUE);
813     IMoniker_Release(mon);
814     if(FAILED(hres))
815         return hres;
816
817     return channelbsc_load_stream(This->window->pending_window, pStm);
818 }
819
820 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
821                                              BOOL fClearDirty)
822 {
823     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
824     char *str;
825     DWORD written=0;
826     HRESULT hres;
827
828     TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty);
829
830     hres = get_doc_string(This->doc_node, &str);
831     if(FAILED(hres))
832         return hres;
833
834     hres = IStream_Write(pStm, str, strlen(str), &written);
835     if(FAILED(hres))
836         FIXME("Write failed: %08x\n", hres);
837
838     heap_free(str);
839
840     if(fClearDirty)
841         set_dirty(This, VARIANT_FALSE);
842
843     return S_OK;
844 }
845
846 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
847                                                    ULARGE_INTEGER *pcbSize)
848 {
849     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
850     FIXME("(%p)->(%p)\n", This, pcbSize);
851     return E_NOTIMPL;
852 }
853
854 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
855 {
856     HTMLDocument *This = impl_from_IPersistStreamInit(iface);
857     IMoniker *mon;
858     HRESULT hres;
859
860     static const WCHAR about_blankW[] = {'a','b','o','u','t',':','b','l','a','n','k',0};
861
862     TRACE("(%p)\n", This);
863
864     hres = CreateURLMoniker(NULL, about_blankW, &mon);
865     if(FAILED(hres)) {
866         WARN("CreateURLMoniker failed: %08x\n", hres);
867         return hres;
868     }
869
870     prepare_for_binding(This, mon, FALSE);
871     hres = set_moniker(This, mon, NULL, NULL, FALSE);
872     IMoniker_Release(mon);
873     if(FAILED(hres))
874         return hres;
875
876     return channelbsc_load_stream(This->window->pending_window, NULL);
877 }
878
879 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
880     PersistStreamInit_QueryInterface,
881     PersistStreamInit_AddRef,
882     PersistStreamInit_Release,
883     PersistStreamInit_GetClassID,
884     PersistStreamInit_IsDirty,
885     PersistStreamInit_Load,
886     PersistStreamInit_Save,
887     PersistStreamInit_GetSizeMax,
888     PersistStreamInit_InitNew
889 };
890
891 /**********************************************************
892  * IPersistHistory implementation
893  */
894
895 static inline HTMLDocument *impl_from_IPersistHistory(IPersistHistory *iface)
896 {
897     return CONTAINING_RECORD(iface, HTMLDocument, IPersistHistory_iface);
898 }
899
900 static HRESULT WINAPI PersistHistory_QueryInterface(IPersistHistory *iface, REFIID riid, void **ppv)
901 {
902     HTMLDocument *This = impl_from_IPersistHistory(iface);
903     return htmldoc_query_interface(This, riid, ppv);
904 }
905
906 static ULONG WINAPI PersistHistory_AddRef(IPersistHistory *iface)
907 {
908     HTMLDocument *This = impl_from_IPersistHistory(iface);
909     return htmldoc_addref(This);
910 }
911
912 static ULONG WINAPI PersistHistory_Release(IPersistHistory *iface)
913 {
914     HTMLDocument *This = impl_from_IPersistHistory(iface);
915     return htmldoc_release(This);
916 }
917
918 static HRESULT WINAPI PersistHistory_GetClassID(IPersistHistory *iface, CLSID *pClassID)
919 {
920     HTMLDocument *This = impl_from_IPersistHistory(iface);
921     return IPersistFile_GetClassID(&This->IPersistFile_iface, pClassID);
922 }
923
924 static HRESULT WINAPI PersistHistory_LoadHistory(IPersistHistory *iface, IStream *pStream, IBindCtx *pbc)
925 {
926     HTMLDocument *This = impl_from_IPersistHistory(iface);
927     FIXME("(%p)->(%p %p)\n", This, pStream, pbc);
928     return E_NOTIMPL;
929 }
930
931 static HRESULT WINAPI PersistHistory_SaveHistory(IPersistHistory *iface, IStream *pStream)
932 {
933     HTMLDocument *This = impl_from_IPersistHistory(iface);
934     FIXME("(%p)->(%p)\n", This, pStream);
935     return E_NOTIMPL;
936 }
937
938 static HRESULT WINAPI PersistHistory_SetPositionCookie(IPersistHistory *iface, DWORD dwPositioncookie)
939 {
940     HTMLDocument *This = impl_from_IPersistHistory(iface);
941     FIXME("(%p)->(%x)\n", This, dwPositioncookie);
942     return E_NOTIMPL;
943 }
944
945 static HRESULT WINAPI PersistHistory_GetPositionCookie(IPersistHistory *iface, DWORD *pdwPositioncookie)
946 {
947     HTMLDocument *This = impl_from_IPersistHistory(iface);
948     FIXME("(%p)->(%p)\n", This, pdwPositioncookie);
949     return E_NOTIMPL;
950 }
951
952 static const IPersistHistoryVtbl PersistHistoryVtbl = {
953     PersistHistory_QueryInterface,
954     PersistHistory_AddRef,
955     PersistHistory_Release,
956     PersistHistory_GetClassID,
957     PersistHistory_LoadHistory,
958     PersistHistory_SaveHistory,
959     PersistHistory_SetPositionCookie,
960     PersistHistory_GetPositionCookie
961 };
962
963 void HTMLDocument_Persist_Init(HTMLDocument *This)
964 {
965     This->IPersistMoniker_iface.lpVtbl = &PersistMonikerVtbl;
966     This->IPersistFile_iface.lpVtbl = &PersistFileVtbl;
967     This->IMonikerProp_iface.lpVtbl = &MonikerPropVtbl;
968     This->IPersistStreamInit_iface.lpVtbl = &PersistStreamInitVtbl;
969     This->IPersistHistory_iface.lpVtbl = &PersistHistoryVtbl;
970 }