mshtml: Fix classList toggle() when return value pointer is NULL.
[wine.git] / dlls / mshtml / loadopts.c
blob5237e22f1aaf9dd22df08e51e0a794be8205578f
1 /*
2 * Copyright 2006 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
20 #include <stdio.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28 #include "optary.h"
30 #include "wine/debug.h"
32 #include "mshtml_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
36 typedef struct load_opt {
37 DWORD option;
38 PVOID buffer;
39 DWORD size;
41 struct load_opt *next;
42 } load_opt;
44 typedef struct {
45 IHtmlLoadOptions IHtmlLoadOptions_iface;
47 LONG ref;
49 load_opt *opts;
50 } HTMLLoadOptions;
52 static inline HTMLLoadOptions *impl_from_IHtmlLoadOptions(IHtmlLoadOptions *iface)
54 return CONTAINING_RECORD(iface, HTMLLoadOptions, IHtmlLoadOptions_iface);
57 static HRESULT WINAPI HtmlLoadOptions_QueryInterface(IHtmlLoadOptions *iface,
58 REFIID riid, void **ppv)
60 HTMLLoadOptions *This = impl_from_IHtmlLoadOptions(iface);
62 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
64 if(IsEqualGUID(&IID_IUnknown, riid)) {
65 *ppv = &This->IHtmlLoadOptions_iface;
66 }else if(IsEqualGUID(&IID_IOptionArray, riid)) {
67 *ppv = &This->IHtmlLoadOptions_iface;
68 }else if(IsEqualGUID(&IID_IHtmlLoadOptions, riid)) {
69 *ppv = &This->IHtmlLoadOptions_iface;
70 }else {
71 *ppv = NULL;
72 WARN("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
73 return E_NOINTERFACE;
76 IUnknown_AddRef((IUnknown*)*ppv);
77 return S_OK;
80 static ULONG WINAPI HtmlLoadOptions_AddRef(IHtmlLoadOptions *iface)
82 HTMLLoadOptions *This = impl_from_IHtmlLoadOptions(iface);
83 LONG ref = InterlockedIncrement(&This->ref);
85 TRACE("(%p) ref=%ld\n", This, ref);
87 return ref;
90 static ULONG WINAPI HtmlLoadOptions_Release(IHtmlLoadOptions *iface)
92 HTMLLoadOptions *This = impl_from_IHtmlLoadOptions(iface);
93 LONG ref = InterlockedDecrement(&This->ref);
95 TRACE("(%p) ref=%ld\n", This, ref);
97 if(!ref) {
98 load_opt *iter = This->opts, *last;
100 while(iter) {
101 last = iter;
102 iter = iter->next;
104 free(last->buffer);
105 free(last);
108 free(This);
111 return ref;
114 static HRESULT WINAPI HtmlLoadOptions_QueryOption(IHtmlLoadOptions *iface, DWORD dwOption,
115 LPVOID pBuffer, ULONG *pcbBuf)
117 HTMLLoadOptions *This = impl_from_IHtmlLoadOptions(iface);
118 load_opt *iter;
120 TRACE("(%p)->(%ld %p %p)\n", This, dwOption, pBuffer, pcbBuf);
122 for(iter = This->opts; iter; iter = iter->next) {
123 if(iter->option == dwOption)
124 break;
127 if(!iter) {
128 *pcbBuf = 0;
129 return S_OK;
132 if(*pcbBuf < iter->size) {
133 *pcbBuf = iter->size;
134 return E_FAIL;
137 memcpy(pBuffer, iter->buffer, iter->size);
138 *pcbBuf = iter->size;
140 return S_OK;
143 static HRESULT WINAPI HtmlLoadOptions_SetOption(IHtmlLoadOptions *iface, DWORD dwOption,
144 LPVOID pBuffer, ULONG cbBuf)
146 HTMLLoadOptions *This = impl_from_IHtmlLoadOptions(iface);
147 load_opt *iter = NULL;
149 TRACE("(%p)->(%ld %p %ld)\n", This, dwOption, pBuffer, cbBuf);
151 for(iter = This->opts; iter; iter = iter->next) {
152 if(iter->option == dwOption)
153 break;
156 if(!iter) {
157 iter = malloc(sizeof(load_opt));
158 iter->next = This->opts;
159 This->opts = iter;
161 iter->option = dwOption;
162 }else {
163 free(iter->buffer);
166 if(!cbBuf) {
167 iter->buffer = NULL;
168 iter->size = 0;
170 return S_OK;
173 iter->size = cbBuf;
174 iter->buffer = malloc(cbBuf);
175 memcpy(iter->buffer, pBuffer, iter->size);
177 return S_OK;
180 static const IHtmlLoadOptionsVtbl HtmlLoadOptionsVtbl = {
181 HtmlLoadOptions_QueryInterface,
182 HtmlLoadOptions_AddRef,
183 HtmlLoadOptions_Release,
184 HtmlLoadOptions_QueryOption,
185 HtmlLoadOptions_SetOption
188 HRESULT HTMLLoadOptions_Create(IUnknown *pUnkOuter, REFIID riid, void** ppv)
190 HTMLLoadOptions *ret;
191 HRESULT hres;
193 TRACE("(%p %s %p)\n", pUnkOuter, debugstr_mshtml_guid(riid), ppv);
195 ret = malloc(sizeof(HTMLLoadOptions));
196 if(!ret)
197 return E_OUTOFMEMORY;
199 ret->IHtmlLoadOptions_iface.lpVtbl = &HtmlLoadOptionsVtbl;
200 ret->ref = 1;
201 ret->opts = NULL;
203 hres = IHtmlLoadOptions_QueryInterface(&ret->IHtmlLoadOptions_iface, riid, ppv);
204 IHtmlLoadOptions_Release(&ret->IHtmlLoadOptions_iface);
205 return hres;