ntdll : Implement CDROM_Verify to work on Mac OS X.
[wine] / dlls / mshtml / propbag.c
1 /*
2  * Copyright 2010 Jacek Caban for CodeWeavers
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
23 #define COBJMACROS
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "shlobj.h"
30
31 #include "mshtml_private.h"
32 #include "pluginhost.h"
33
34 #include "wine/debug.h"
35 #include "wine/unicode.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 typedef struct {
40     IPropertyBag  IPropertyBag_iface;
41     IPropertyBag2 IPropertyBag2_iface;
42
43     LONG ref;
44
45     struct list props;
46 } PropertyBag;
47
48 typedef struct {
49     struct list entry;
50     WCHAR *name;
51     WCHAR *value;
52 } param_prop_t;
53
54 static void free_prop(param_prop_t *prop)
55 {
56     list_remove(&prop->entry);
57
58     heap_free(prop->name);
59     heap_free(prop->value);
60     heap_free(prop);
61 }
62
63 static param_prop_t *find_prop(PropertyBag *prop_bag, const WCHAR *name)
64 {
65     param_prop_t *iter;
66
67     LIST_FOR_EACH_ENTRY(iter, &prop_bag->props, param_prop_t, entry) {
68         if(!strcmpiW(iter->name, name))
69             return iter;
70     }
71
72     return NULL;
73 }
74
75 static HRESULT add_prop(PropertyBag *prop_bag, const WCHAR *name, const WCHAR *value)
76 {
77     param_prop_t *prop;
78
79     if(!name || !value)
80         return S_OK;
81
82     TRACE("%p %s %s\n", prop_bag, debugstr_w(name), debugstr_w(value));
83
84     prop = heap_alloc(sizeof(*prop));
85     if(!prop)
86         return E_OUTOFMEMORY;
87
88     prop->name = heap_strdupW(name);
89     prop->value = heap_strdupW(value);
90     if(!prop->name || !prop->value) {
91         list_init(&prop->entry);
92         free_prop(prop);
93         return E_OUTOFMEMORY;
94     }
95
96     list_add_tail(&prop_bag->props, &prop->entry);
97     return S_OK;
98 }
99
100 static inline PropertyBag *impl_from_IPropertyBag(IPropertyBag *iface)
101 {
102     return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag_iface);
103 }
104
105 static HRESULT WINAPI PropertyBag_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
106 {
107     PropertyBag *This = impl_from_IPropertyBag(iface);
108
109     if(IsEqualGUID(&IID_IUnknown, riid)) {
110         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
111         *ppv = &This->IPropertyBag_iface;
112     }else if(IsEqualGUID(&IID_IPropertyBag, riid)) {
113         TRACE("(%p)->(IID_IPropertyBag %p)\n", This, ppv);
114         *ppv = &This->IPropertyBag_iface;
115     }else if(IsEqualGUID(&IID_IPropertyBag2, riid)) {
116         TRACE("(%p)->(IID_IPropertyBag2 %p)\n", This, ppv);
117         *ppv = &This->IPropertyBag2_iface;
118     }else {
119         WARN("Unsopported interface %s\n", debugstr_guid(riid));
120         *ppv = NULL;
121         return E_NOINTERFACE;
122     }
123
124     IUnknown_AddRef((IUnknown*)*ppv);
125     return S_OK;
126 }
127
128 static ULONG WINAPI PropertyBag_AddRef(IPropertyBag *iface)
129 {
130     PropertyBag *This = impl_from_IPropertyBag(iface);
131     LONG ref = InterlockedIncrement(&This->ref);
132
133     TRACE("(%p) ref=%d\n", This, ref);
134
135     return ref;
136 }
137
138 static ULONG WINAPI PropertyBag_Release(IPropertyBag *iface)
139 {
140     PropertyBag *This = impl_from_IPropertyBag(iface);
141     LONG ref = InterlockedDecrement(&This->ref);
142
143     TRACE("(%p) ref=%d\n", This, ref);
144
145     if(!ref) {
146         while(!list_empty(&This->props))
147             free_prop(LIST_ENTRY(This->props.next, param_prop_t, entry));
148         heap_free(This);
149     }
150
151     return ref;
152 }
153
154 static HRESULT WINAPI PropertyBag_Read(IPropertyBag *iface, LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog *pErrorLog)
155 {
156     PropertyBag *This = impl_from_IPropertyBag(iface);
157     param_prop_t *prop;
158     VARIANT v;
159
160     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(pszPropName), pVar, pErrorLog);
161
162     prop = find_prop(This, pszPropName);
163     if(!prop) {
164         TRACE("Not found\n");
165         return E_INVALIDARG;
166     }
167
168     V_BSTR(&v) = SysAllocString(prop->value);
169     if(!V_BSTR(&v))
170         return E_OUTOFMEMORY;
171
172     if(V_VT(pVar) != VT_BSTR) {
173         HRESULT hres;
174
175         V_VT(&v) = VT_BSTR;
176         hres = VariantChangeType(pVar, &v, 0, V_VT(pVar));
177         SysFreeString(V_BSTR(&v));
178         return hres;
179     }
180
181     V_BSTR(pVar) = V_BSTR(&v);
182     return S_OK;
183 }
184
185 static HRESULT WINAPI PropertyBag_Write(IPropertyBag *iface, LPCOLESTR pszPropName, VARIANT *pVar)
186 {
187     PropertyBag *This = impl_from_IPropertyBag(iface);
188     FIXME("(%p)->(%s %s)\n", This, debugstr_w(pszPropName), debugstr_variant(pVar));
189     return E_NOTIMPL;
190 }
191
192 static const IPropertyBagVtbl PropertyBagVtbl = {
193     PropertyBag_QueryInterface,
194     PropertyBag_AddRef,
195     PropertyBag_Release,
196     PropertyBag_Read,
197     PropertyBag_Write
198 };
199
200 static inline PropertyBag *impl_from_IPropertyBag2(IPropertyBag2 *iface)
201 {
202     return CONTAINING_RECORD(iface, PropertyBag, IPropertyBag2_iface);
203 }
204
205 static HRESULT WINAPI PropertyBag2_QueryInterface(IPropertyBag2 *iface, REFIID riid, void **ppv)
206 {
207     PropertyBag *This = impl_from_IPropertyBag2(iface);
208     return IPropertyBag_QueryInterface(&This->IPropertyBag_iface, riid, ppv);
209 }
210
211 static ULONG WINAPI PropertyBag2_AddRef(IPropertyBag2 *iface)
212 {
213     PropertyBag *This = impl_from_IPropertyBag2(iface);
214     return IPropertyBag_AddRef(&This->IPropertyBag_iface);
215 }
216
217 static ULONG WINAPI PropertyBag2_Release(IPropertyBag2 *iface)
218 {
219     PropertyBag *This = impl_from_IPropertyBag2(iface);
220     return IPropertyBag_Release(&This->IPropertyBag_iface);
221 }
222
223 static HRESULT WINAPI PropertyBag2_Read(IPropertyBag2 *iface, ULONG cProperties, PROPBAG2 *pPropBag,
224         IErrorLog *pErrLog, VARIANT *pvarValue, HRESULT *phrError)
225 {
226     PropertyBag *This = impl_from_IPropertyBag2(iface);
227     FIXME("(%p)->(%d %p %p %p %p)\n", This, cProperties, pPropBag, pErrLog, pvarValue, phrError);
228     return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI PropertyBag2_Write(IPropertyBag2 *iface, ULONG cProperties, PROPBAG2 *pPropBag, VARIANT *pvarValue)
232 {
233     PropertyBag *This = impl_from_IPropertyBag2(iface);
234     FIXME("(%p)->(%d %p %s)\n", This, cProperties, pPropBag, debugstr_variant(pvarValue));
235     return E_NOTIMPL;
236 }
237
238 static HRESULT WINAPI PropertyBag2_CountProperties(IPropertyBag2 *iface, ULONG *pcProperties)
239 {
240     PropertyBag *This = impl_from_IPropertyBag2(iface);
241     FIXME("(%p)->(%p)\n", This, pcProperties);
242     return E_NOTIMPL;
243 }
244
245 static HRESULT WINAPI PropertyBag2_GetPropertyInfo(IPropertyBag2 *iface, ULONG iProperty, ULONG cProperties,
246         PROPBAG2 *pPropBag, ULONG *pcProperties)
247 {
248     PropertyBag *This = impl_from_IPropertyBag2(iface);
249     FIXME("(%p)->(%u %u %p %p)\n", This, iProperty, cProperties, pPropBag, pcProperties);
250     return E_NOTIMPL;
251 }
252
253 static HRESULT WINAPI PropertyBag2_LoadObject(IPropertyBag2 *iface, LPCOLESTR pstrName, DWORD dwHint,
254         IUnknown *pUnkObject, IErrorLog *pErrLog)
255 {
256     PropertyBag *This = impl_from_IPropertyBag2(iface);
257     FIXME("(%p)->(%s %x %p %p)\n", This, debugstr_w(pstrName), dwHint, pUnkObject, pErrLog);
258     return E_NOTIMPL;
259 }
260
261 static const IPropertyBag2Vtbl PropertyBag2Vtbl = {
262     PropertyBag2_QueryInterface,
263     PropertyBag2_AddRef,
264     PropertyBag2_Release,
265     PropertyBag2_Read,
266     PropertyBag2_Write,
267     PropertyBag2_CountProperties,
268     PropertyBag2_GetPropertyInfo,
269     PropertyBag2_LoadObject
270 };
271
272 static HRESULT fill_props(nsIDOMHTMLElement *nselem, PropertyBag *prop_bag)
273 {
274     nsIDOMHTMLParamElement *nsparam;
275     nsAString name_str, value_str;
276     nsIDOMNodeList *params;
277     PRUint32 length, i;
278     nsIDOMNode *nsnode;
279     nsresult nsres;
280     HRESULT hres = S_OK;
281
282     static const PRUnichar paramW[] = {'p','a','r','a','m',0};
283
284     nsAString_InitDepend(&name_str, paramW);
285     nsres = nsIDOMHTMLElement_GetElementsByTagName(nselem, &name_str, &params);
286     nsAString_Finish(&name_str);
287     if(NS_FAILED(nsres))
288         return E_FAIL;
289
290     nsres = nsIDOMNodeList_GetLength(params, &length);
291     if(NS_FAILED(nsres))
292         return S_OK;
293
294     for(i=0; i < length; i++) {
295         nsres = nsIDOMNodeList_Item(params, i, &nsnode);
296         if(NS_FAILED(nsres)) {
297             hres = E_FAIL;
298             break;
299         }
300
301         nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMHTMLParamElement, (void**)&nsparam);
302         nsIDOMNode_Release(nsnode);
303         if(NS_FAILED(nsres)) {
304             hres = E_FAIL;
305             break;
306         }
307
308         nsAString_Init(&name_str, NULL);
309         nsres = nsIDOMHTMLParamElement_GetName(nsparam, &name_str);
310         if(NS_SUCCEEDED(nsres)) {
311             nsAString_Init(&value_str, NULL);
312             nsres = nsIDOMHTMLParamElement_GetValue(nsparam, &value_str);
313             if(NS_SUCCEEDED(nsres)) {
314                 const PRUnichar *name, *value;
315
316                 nsAString_GetData(&name_str, &name);
317                 nsAString_GetData(&value_str, &value);
318
319                 hres = add_prop(prop_bag, name, value);
320             }
321             nsAString_Finish(&value_str);
322         }
323
324         nsAString_Finish(&name_str);
325         nsIDOMHTMLParamElement_Release(nsparam);
326         if(FAILED(hres))
327             break;
328         if(NS_FAILED(nsres)) {
329             hres = E_FAIL;
330             break;
331         }
332     }
333
334     return hres;
335 }
336
337 HRESULT create_param_prop_bag(nsIDOMHTMLElement *nselem, IPropertyBag **ret)
338 {
339     PropertyBag *prop_bag;
340     HRESULT hres;
341
342     prop_bag = heap_alloc(sizeof(*prop_bag));
343     if(!prop_bag)
344         return E_OUTOFMEMORY;
345
346     prop_bag->IPropertyBag_iface.lpVtbl  = &PropertyBagVtbl;
347     prop_bag->IPropertyBag2_iface.lpVtbl = &PropertyBag2Vtbl;
348     prop_bag->ref = 1;
349
350     list_init(&prop_bag->props);
351     hres = fill_props(nselem, prop_bag);
352     if(FAILED(hres) || list_empty(&prop_bag->props)) {
353         IPropertyBag_Release(&prop_bag->IPropertyBag_iface);
354         *ret = NULL;
355         return hres;
356     }
357
358     *ret = &prop_bag->IPropertyBag_iface;
359     return S_OK;
360 }