kernel32: GlobalMemoryStatusEx: return the size of physical memory + swapsize in...
[wine] / dlls / mshtml / persist.c
1 /*
2  * Copyright 2005 Jacek Caban
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "ole2.h"
32 #include "shlguid.h"
33 #include "idispids.h"
34
35 #include "wine/debug.h"
36 #include "wine/unicode.h"
37
38 #include "mshtml_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41
42 /**********************************************************
43  * IPersistMoniker implementation
44  */
45
46 #define PERSISTMON_THIS(iface) DEFINE_THIS(HTMLDocument, PersistMoniker, iface)
47
48 static HRESULT WINAPI PersistMoniker_QueryInterface(IPersistMoniker *iface, REFIID riid,
49                                                             void **ppvObject)
50 {
51     HTMLDocument *This = PERSISTMON_THIS(iface);
52     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
53 }
54
55 static ULONG WINAPI PersistMoniker_AddRef(IPersistMoniker *iface)
56 {
57     HTMLDocument *This = PERSISTMON_THIS(iface);
58     return IHTMLDocument2_AddRef(HTMLDOC(This));
59 }
60
61 static ULONG WINAPI PersistMoniker_Release(IPersistMoniker *iface)
62 {
63     HTMLDocument *This = PERSISTMON_THIS(iface);
64     return IHTMLDocument2_Release(HTMLDOC(This));
65 }
66
67 static HRESULT WINAPI PersistMoniker_GetClassID(IPersistMoniker *iface, CLSID *pClassID)
68 {
69     HTMLDocument *This = PERSISTMON_THIS(iface);
70     return IPersist_GetClassID(PERSIST(This), pClassID);
71 }
72
73 static HRESULT WINAPI PersistMoniker_IsDirty(IPersistMoniker *iface)
74 {
75     HTMLDocument *This = PERSISTMON_THIS(iface);
76     FIXME("(%p)\n", This);
77     return E_NOTIMPL;
78 }
79
80 static nsIInputStream *get_post_data_stream(IBindCtx *bctx)
81 {
82     nsIInputStream *ret = NULL;
83     IBindStatusCallback *callback;
84     IHttpNegotiate *http_negotiate;
85     BINDINFO bindinfo;
86     DWORD bindf = 0;
87     DWORD post_len = 0, headers_len = 0;
88     LPWSTR headers = NULL;
89     WCHAR emptystr[] = {0};
90     char *data;
91     HRESULT hres;
92
93     static WCHAR _BSCB_Holder_[] =
94         {'_','B','S','C','B','_','H','o','l','d','e','r','_',0};
95
96
97     /* FIXME: This should be done in URLMoniker */
98     if(!bctx)
99         return NULL;
100
101     hres = IBindCtx_GetObjectParam(bctx, _BSCB_Holder_, (IUnknown**)&callback);
102     if(FAILED(hres))
103         return NULL;
104
105     hres = IBindStatusCallback_QueryInterface(callback, &IID_IHttpNegotiate,
106                                               (void**)&http_negotiate);
107     if(SUCCEEDED(hres)) {
108         hres = IHttpNegotiate_BeginningTransaction(http_negotiate, emptystr,
109                                                    emptystr, 0, &headers);
110         IHttpNegotiate_Release(http_negotiate);
111
112         if(SUCCEEDED(hres) && headers)
113             headers_len = WideCharToMultiByte(CP_ACP, 0, headers, -1, NULL, 0, NULL, NULL);
114     }
115
116     memset(&bindinfo, 0, sizeof(bindinfo));
117     bindinfo.cbSize = sizeof(bindinfo);
118
119     hres = IBindStatusCallback_GetBindInfo(callback, &bindf, &bindinfo);
120
121     if(SUCCEEDED(hres) && bindinfo.dwBindVerb == BINDVERB_POST)
122         post_len = bindinfo.cbstgmedData;
123
124     if(headers_len || post_len) {
125         int len = headers_len ? headers_len-1 : 0;
126
127         static const char content_length[] = "Content-Length: %u\r\n\r\n";
128
129         data = mshtml_alloc(headers_len+post_len+sizeof(content_length)+8);
130
131         if(headers_len) {
132             WideCharToMultiByte(CP_ACP, 0, headers, -1, data, -1, NULL, NULL);
133             CoTaskMemFree(headers);
134         }
135
136         if(post_len) {
137             sprintf(data+len, content_length, post_len);
138             len = strlen(data);
139
140             memcpy(data+len, bindinfo.stgmedData.u.hGlobal, post_len);
141         }
142
143         TRACE("data = %s\n", debugstr_an(data, len+post_len));
144
145         ret = create_nsstream(data, len+post_len);
146     }
147
148     ReleaseBindInfo(&bindinfo);
149     IBindStatusCallback_Release(callback);
150
151     return ret;
152 }
153
154 static HRESULT WINAPI PersistMoniker_Load(IPersistMoniker *iface, BOOL fFullyAvailable,
155         IMoniker *pimkName, LPBC pibc, DWORD grfMode)
156 {
157     HTMLDocument *This = PERSISTMON_THIS(iface);
158     BSCallback *bscallback;
159     LPOLESTR url = NULL;
160     task_t *task;
161     HRESULT hres;
162     nsresult nsres;
163
164     TRACE("(%p)->(%x %p %p %08x)\n", This, fFullyAvailable, pimkName, pibc, grfMode);
165
166     if(pibc) {
167         IUnknown *unk = NULL;
168
169         /* FIXME:
170          * Use params:
171          * "__PrecreatedObject"
172          * "BIND_CONTEXT_PARAM"
173          * "__HTMLLOADOPTIONS"
174          * "__DWNBINDINFO"
175          * "URL Context"
176          * "CBinding Context"
177          * "_ITransData_Object_"
178          * "_EnumFORMATETC_"
179          */
180
181         IBindCtx_GetObjectParam(pibc, (LPOLESTR)SZ_HTML_CLIENTSITE_OBJECTPARAM, &unk);
182         if(unk) {
183             IOleClientSite *client = NULL;
184
185             hres = IUnknown_QueryInterface(unk, &IID_IOleClientSite, (void**)&client);
186             if(SUCCEEDED(hres)) {
187                 TRACE("Got client site %p\n", client);
188                 IOleObject_SetClientSite(OLEOBJ(This), client);
189                 IOleClientSite_Release(client);
190             }
191
192             IUnknown_Release(unk);
193         }
194     }
195
196     This->readystate = READYSTATE_LOADING;
197     call_property_onchanged(This->cp_propnotif, DISPID_READYSTATE);
198
199     HTMLDocument_LockContainer(This, TRUE);
200     
201     hres = IMoniker_GetDisplayName(pimkName, pibc, NULL, &url);
202     if(FAILED(hres)) {
203         WARN("GetDiaplayName failed: %08x\n", hres);
204         return hres;
205     }
206
207     TRACE("got url: %s\n", debugstr_w(url));
208
209     if(This->client) {
210         IOleCommandTarget *cmdtrg = NULL;
211
212         hres = IOleClientSite_QueryInterface(This->client, &IID_IOleCommandTarget,
213                 (void**)&cmdtrg);
214         if(SUCCEEDED(hres)) {
215             VARIANT var;
216
217             V_VT(&var) = VT_I4;
218             V_I4(&var) = 0;
219             IOleCommandTarget_Exec(cmdtrg, &CGID_ShellDocView, 37, 0, &var, NULL);
220
221             IOleCommandTarget_Release(cmdtrg);
222         }
223     }
224
225     if(This->client) {
226         VARIANT silent, offline;
227
228         hres = get_client_disp_property(This->client, DISPID_AMBIENT_SILENT, &silent);
229         if(SUCCEEDED(hres)) {
230             if(V_VT(&silent) != VT_BOOL)
231                 WARN("V_VT(silent) = %d\n", V_VT(&silent));
232             else if(V_BOOL(&silent))
233                 FIXME("silent == true\n");
234         }
235
236         hres = get_client_disp_property(This->client,
237                 DISPID_AMBIENT_OFFLINEIFNOTCONNECTED, &offline);
238         if(SUCCEEDED(hres)) {
239             if(V_VT(&silent) != VT_BOOL)
240                 WARN("V_VT(offline) = %d\n", V_VT(&silent));
241             else if(V_BOOL(&silent))
242                 FIXME("offline == true\n");
243         }
244     }
245
246     bscallback = create_bscallback(pimkName);
247
248     if(This->frame) {
249         task = mshtml_alloc(sizeof(task_t));
250
251         task->doc = This;
252         task->task_id = TASK_SETPROGRESS;
253         task->next = NULL;
254
255         push_task(task);
256     }
257
258     task = mshtml_alloc(sizeof(task_t));
259
260     task->doc = This;
261     task->task_id = TASK_SETDOWNLOADSTATE;
262     task->next = NULL;
263
264     push_task(task);
265
266     if(This->nscontainer) {
267         nsIInputStream *post_data_stream = get_post_data_stream(pibc);
268
269         This->nscontainer->bscallback = bscallback;
270         nsres = nsIWebNavigation_LoadURI(This->nscontainer->navigation, url,
271                 LOAD_FLAGS_NONE, NULL, post_data_stream, NULL);
272         This->nscontainer->bscallback = NULL;
273
274         if(post_data_stream)
275             nsIInputStream_Release(post_data_stream);
276
277         if(NS_SUCCEEDED(nsres)) {
278             /* FIXME: don't return here (URL Moniker needs to be good enough) */
279
280             IBindStatusCallback_Release(STATUSCLB(bscallback));
281             CoTaskMemFree(url);
282             return S_OK;
283         }else if(nsres != WINE_NS_LOAD_FROM_MONIKER) {
284             WARN("LoadURI failed: %08x\n", nsres);
285         }
286     }
287
288     set_document_bscallback(This, bscallback);
289     hres = start_binding(bscallback);
290
291     IBindStatusCallback_Release(STATUSCLB(bscallback));
292     CoTaskMemFree(url);
293
294     return hres;
295 }
296
297 static HRESULT WINAPI PersistMoniker_Save(IPersistMoniker *iface, IMoniker *pimkName,
298         LPBC pbc, BOOL fRemember)
299 {
300     HTMLDocument *This = PERSISTMON_THIS(iface);
301     FIXME("(%p)->(%p %p %x)\n", This, pimkName, pbc, fRemember);
302     return E_NOTIMPL;
303 }
304
305 static HRESULT WINAPI PersistMoniker_SaveCompleted(IPersistMoniker *iface, IMoniker *pimkName, LPBC pibc)
306 {
307     HTMLDocument *This = PERSISTMON_THIS(iface);
308     FIXME("(%p)->(%p %p)\n", This, pimkName, pibc);
309     return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI PersistMoniker_GetCurMoniker(IPersistMoniker *iface, IMoniker **ppimkName)
313 {
314     HTMLDocument *This = PERSISTMON_THIS(iface);
315     FIXME("(%p)->(%p)\n", This, ppimkName);
316     return E_NOTIMPL;
317 }
318
319 static const IPersistMonikerVtbl PersistMonikerVtbl = {
320     PersistMoniker_QueryInterface,
321     PersistMoniker_AddRef,
322     PersistMoniker_Release,
323     PersistMoniker_GetClassID,
324     PersistMoniker_IsDirty,
325     PersistMoniker_Load,
326     PersistMoniker_Save,
327     PersistMoniker_SaveCompleted,
328     PersistMoniker_GetCurMoniker
329 };
330
331 /**********************************************************
332  * IMonikerProp implementation
333  */
334
335 #define MONPROP_THIS(iface) DEFINE_THIS(HTMLDocument, MonikerProp, iface)
336
337 static HRESULT WINAPI MonikerProp_QueryInterface(IMonikerProp *iface, REFIID riid, void **ppvObject)
338 {
339     HTMLDocument *This = MONPROP_THIS(iface);
340     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
341 }
342
343 static ULONG WINAPI MonikerProp_AddRef(IMonikerProp *iface)
344 {
345     HTMLDocument *This = MONPROP_THIS(iface);
346     return IHTMLDocument2_AddRef(HTMLDOC(This));
347 }
348
349 static ULONG WINAPI MonikerProp_Release(IMonikerProp *iface)
350 {
351     HTMLDocument *This = MONPROP_THIS(iface);
352     return IHTMLDocument_Release(HTMLDOC(This));
353 }
354
355 static HRESULT WINAPI MonikerProp_PutProperty(IMonikerProp *iface, MONIKERPROPERTY mkp, LPCWSTR val)
356 {
357     HTMLDocument *This = MONPROP_THIS(iface);
358     FIXME("(%p)->(%d %s)\n", This, mkp, debugstr_w(val));
359     return E_NOTIMPL;
360 }
361
362 static const IMonikerPropVtbl MonikerPropVtbl = {
363     MonikerProp_QueryInterface,
364     MonikerProp_AddRef,
365     MonikerProp_Release,
366     MonikerProp_PutProperty
367 };
368
369 /**********************************************************
370  * IPersistFile implementation
371  */
372
373 #define PERSISTFILE_THIS(iface) DEFINE_THIS(HTMLDocument, PersistFile, iface)
374
375 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *iface, REFIID riid, void **ppvObject)
376 {
377     HTMLDocument *This = PERSISTFILE_THIS(iface);
378     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppvObject);
379 }
380
381 static ULONG WINAPI PersistFile_AddRef(IPersistFile *iface)
382 {
383     HTMLDocument *This = PERSISTFILE_THIS(iface);
384     return IHTMLDocument2_AddRef(HTMLDOC(This));
385 }
386
387 static ULONG WINAPI PersistFile_Release(IPersistFile *iface)
388 {
389     HTMLDocument *This = PERSISTFILE_THIS(iface);
390     return IHTMLDocument2_Release(HTMLDOC(This));
391 }
392
393 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *iface, CLSID *pClassID)
394 {
395     HTMLDocument *This = PERSISTFILE_THIS(iface);
396
397     TRACE("(%p)->(%p)\n", This, pClassID);
398
399     if(!pClassID)
400         return E_INVALIDARG;
401
402     memcpy(pClassID, &CLSID_HTMLDocument, sizeof(CLSID));
403     return S_OK;
404 }
405
406 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *iface)
407 {
408     HTMLDocument *This = PERSISTFILE_THIS(iface);
409     FIXME("(%p)\n", This);
410     return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI PersistFile_Load(IPersistFile *iface, LPCOLESTR pszFileName, DWORD dwMode)
414 {
415     HTMLDocument *This = PERSISTFILE_THIS(iface);
416     FIXME("(%p)->(%s %08x)\n", This, debugstr_w(pszFileName), dwMode);
417     return E_NOTIMPL;
418 }
419
420 static HRESULT WINAPI PersistFile_Save(IPersistFile *iface, LPCOLESTR pszFileName, BOOL fRemember)
421 {
422     HTMLDocument *This = PERSISTFILE_THIS(iface);
423     FIXME("(%p)->(%s %x)\n", This, debugstr_w(pszFileName), fRemember);
424     return E_NOTIMPL;
425 }
426
427 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *iface, LPCOLESTR pszFileName)
428 {
429     HTMLDocument *This = PERSISTFILE_THIS(iface);
430     FIXME("(%p)->(%s)\n", This, debugstr_w(pszFileName));
431     return E_NOTIMPL;
432 }
433
434 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *iface, LPOLESTR *pszFileName)
435 {
436     HTMLDocument *This = PERSISTFILE_THIS(iface);
437     FIXME("(%p)->(%p)\n", This, pszFileName);
438     return E_NOTIMPL;
439 }
440
441 static const IPersistFileVtbl PersistFileVtbl = {
442     PersistFile_QueryInterface,
443     PersistFile_AddRef,
444     PersistFile_Release,
445     PersistFile_GetClassID,
446     PersistFile_IsDirty,
447     PersistFile_Load,
448     PersistFile_Save,
449     PersistFile_SaveCompleted,
450     PersistFile_GetCurFile
451 };
452
453 #define PERSTRINIT_THIS(iface) DEFINE_THIS(HTMLDocument, PersistStreamInit, iface)
454
455 static HRESULT WINAPI PersistStreamInit_QueryInterface(IPersistStreamInit *iface,
456                                                        REFIID riid, void **ppv)
457 {
458     HTMLDocument *This = PERSTRINIT_THIS(iface);
459     return IHTMLDocument2_QueryInterface(HTMLDOC(This), riid, ppv);
460 }
461
462 static ULONG WINAPI PersistStreamInit_AddRef(IPersistStreamInit *iface)
463 {
464     HTMLDocument *This = PERSTRINIT_THIS(iface);
465     return IHTMLDocument2_AddRef(HTMLDOC(This));
466 }
467
468 static ULONG WINAPI PersistStreamInit_Release(IPersistStreamInit *iface)
469 {
470     HTMLDocument *This = PERSTRINIT_THIS(iface);
471     return IHTMLDocument2_AddRef(HTMLDOC(This));
472 }
473
474 static HRESULT WINAPI PersistStreamInit_GetClassID(IPersistStreamInit *iface, CLSID *pClassID)
475 {
476     HTMLDocument *This = PERSTRINIT_THIS(iface);
477     return IPersist_GetClassID(PERSIST(This), pClassID);
478 }
479
480 static HRESULT WINAPI PersistStreamInit_IsDirty(IPersistStreamInit *iface)
481 {
482     HTMLDocument *This = PERSTRINIT_THIS(iface);
483     FIXME("(%p)\n", This);
484     return E_NOTIMPL;
485 }
486
487 static HRESULT WINAPI PersistStreamInit_Load(IPersistStreamInit *iface, LPSTREAM pStm)
488 {
489     HTMLDocument *This = PERSTRINIT_THIS(iface);
490     FIXME("(%p)->(%p)\n", This, pStm);
491     return E_NOTIMPL;
492 }
493
494 static HRESULT WINAPI PersistStreamInit_Save(IPersistStreamInit *iface, LPSTREAM pStm,
495                                              BOOL fClearDirty)
496 {
497     HTMLDocument *This = PERSTRINIT_THIS(iface);
498     nsIDOMDocument *nsdoc;
499     nsIDOMNode *nsnode;
500     nsAString nsstr;
501     LPCWSTR strw;
502     char *str;
503     DWORD len, written=0;
504     nsresult nsres;
505     HRESULT hres;
506
507     WARN("(%p)->(%p %x) needs more work\n", This, pStm, fClearDirty);
508
509     if(!This->nscontainer)
510         return S_OK;
511
512     nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
513     if(NS_FAILED(nsres)) {
514         ERR("GetDocument failed: %08x\n", nsres);
515         return E_FAIL;
516     }
517
518     nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMNode, (void**)&nsnode);
519     nsIDOMDocument_Release(nsdoc);
520     if(NS_FAILED(nsres)) {
521         ERR("Could not get nsIDOMNode failed: %08x\n", nsres);
522         return E_FAIL;
523     }
524
525     nsAString_Init(&nsstr, NULL);
526     nsnode_to_nsstring(nsnode, &nsstr);
527     nsIDOMNode_Release(nsnode);
528
529     nsAString_GetData(&nsstr, &strw, NULL);
530
531     len = WideCharToMultiByte(CP_ACP, 0, strw, -1, NULL, 0, NULL, NULL);
532     str = mshtml_alloc(len);
533     WideCharToMultiByte(CP_ACP, 0, strw, -1, str, len, NULL, NULL);
534
535     nsAString_Finish(&nsstr);
536
537     TRACE("%s\n", debugstr_a(str));
538
539     hres = IStream_Write(pStm, str, len, &written);
540     if(FAILED(hres))
541         FIXME("Write failed: %08x\n", hres);
542
543     mshtml_free(str);
544
545     return S_OK;
546 }
547
548 static HRESULT WINAPI PersistStreamInit_GetSizeMax(IPersistStreamInit *iface,
549                                                    ULARGE_INTEGER *pcbSize)
550 {
551     HTMLDocument *This = PERSTRINIT_THIS(iface);
552     FIXME("(%p)->(%p)\n", This, pcbSize);
553     return E_NOTIMPL;
554 }
555
556 static HRESULT WINAPI PersistStreamInit_InitNew(IPersistStreamInit *iface)
557 {
558     HTMLDocument *This = PERSTRINIT_THIS(iface);
559     FIXME("(%p)\n", This);
560     return E_NOTIMPL;
561 }
562
563 #undef PERSTRINIT_THIS
564
565 static const IPersistStreamInitVtbl PersistStreamInitVtbl = {
566     PersistStreamInit_QueryInterface,
567     PersistStreamInit_AddRef,
568     PersistStreamInit_Release,
569     PersistStreamInit_GetClassID,
570     PersistStreamInit_IsDirty,
571     PersistStreamInit_Load,
572     PersistStreamInit_Save,
573     PersistStreamInit_GetSizeMax,
574     PersistStreamInit_InitNew
575 };
576
577 void HTMLDocument_Persist_Init(HTMLDocument *This)
578 {
579     This->lpPersistMonikerVtbl = &PersistMonikerVtbl;
580     This->lpPersistFileVtbl = &PersistFileVtbl;
581     This->lpMonikerPropVtbl = &MonikerPropVtbl;
582     This->lpPersistStreamInitVtbl = &PersistStreamInitVtbl;
583
584     This->bscallback = NULL;
585 }