|
1 /* |
|
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. |
|
3 * |
|
4 * Redistribution and use in source and binary forms, with or without |
|
5 * modification, are permitted provided that the following conditions |
|
6 * are met: |
|
7 * 1. Redistributions of source code must retain the above copyright |
|
8 * notice, this list of conditions and the following disclaimer. |
|
9 * 2. Redistributions in binary form must reproduce the above copyright |
|
10 * notice, this list of conditions and the following disclaimer in the |
|
11 * documentation and/or other materials provided with the distribution. |
|
12 * |
|
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
|
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
|
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
|
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
24 */ |
|
25 |
|
26 #include "config.h" |
|
27 #include "WebKitDLL.h" |
|
28 #include "CFDictionaryPropertyBag.h" |
|
29 |
|
30 #include "MarshallingHelpers.h" |
|
31 |
|
32 #include <initguid.h> |
|
33 // {675E712B-8253-4121-A1F0-6A804D095668} |
|
34 DEFINE_GUID(IID_CFDictionaryPropertyBag, 0x675e712b, 0x8253, 0x4121, 0xa1, 0xf0, 0x6a, 0x80, 0x4d, 0x9, 0x56, 0x68); |
|
35 |
|
36 // CFDictionaryPropertyBag ----------------------------------------------- |
|
37 |
|
38 CFDictionaryPropertyBag::CFDictionaryPropertyBag() |
|
39 : m_refCount(1) |
|
40 { |
|
41 } |
|
42 |
|
43 CFDictionaryPropertyBag* CFDictionaryPropertyBag::createInstance() |
|
44 { |
|
45 CFDictionaryPropertyBag* instance = new CFDictionaryPropertyBag(); |
|
46 return instance; |
|
47 } |
|
48 |
|
49 void CFDictionaryPropertyBag::setDictionary(CFMutableDictionaryRef dictionary) |
|
50 { |
|
51 m_dictionary = dictionary; |
|
52 } |
|
53 |
|
54 CFMutableDictionaryRef CFDictionaryPropertyBag::dictionary() const |
|
55 { |
|
56 return m_dictionary.get(); |
|
57 } |
|
58 |
|
59 // IUnknown ------------------------------------------------------------------- |
|
60 |
|
61 HRESULT STDMETHODCALLTYPE CFDictionaryPropertyBag::QueryInterface(REFIID riid, void** ppvObject) |
|
62 { |
|
63 *ppvObject = 0; |
|
64 if (IsEqualGUID(riid, IID_IUnknown)) |
|
65 *ppvObject = static_cast<IPropertyBag*>(this); |
|
66 else if (IsEqualGUID(riid, IID_IPropertyBag)) |
|
67 *ppvObject = static_cast<IPropertyBag*>(this); |
|
68 else if (IsEqualGUID(riid, IID_CFDictionaryPropertyBag)) |
|
69 *ppvObject = this; |
|
70 else |
|
71 return E_NOINTERFACE; |
|
72 |
|
73 AddRef(); |
|
74 return S_OK; |
|
75 } |
|
76 |
|
77 ULONG STDMETHODCALLTYPE CFDictionaryPropertyBag::AddRef(void) |
|
78 { |
|
79 return ++m_refCount; |
|
80 } |
|
81 |
|
82 ULONG STDMETHODCALLTYPE CFDictionaryPropertyBag::Release(void) |
|
83 { |
|
84 ULONG newRef = --m_refCount; |
|
85 if (!newRef) |
|
86 delete(this); |
|
87 |
|
88 return newRef; |
|
89 } |
|
90 |
|
91 // IPropertyBag ------------------------------------------------------------ |
|
92 |
|
93 static bool ConvertCFTypeToVariant(VARIANT* pVar, void* cfObj) |
|
94 { |
|
95 if (!cfObj) { |
|
96 V_VT(pVar) = VT_NULL; |
|
97 return true; |
|
98 } |
|
99 else { |
|
100 // if caller expects a string, retrieve BSTR from CFStringRef |
|
101 if (V_VT(pVar) == VT_BSTR) { |
|
102 V_BSTR(pVar) = MarshallingHelpers::CFStringRefToBSTR((CFStringRef) cfObj); |
|
103 return true; |
|
104 } else if (V_VT(pVar) == VT_I4) { |
|
105 V_I4(pVar) = MarshallingHelpers::CFNumberRefToInt((CFNumberRef) cfObj); |
|
106 return true; |
|
107 } else if (!!(V_VT(pVar)&VT_ARRAY)) { |
|
108 if ((V_VT(pVar)&~VT_ARRAY) == VT_BSTR) { |
|
109 V_ARRAY(pVar) = MarshallingHelpers::stringArrayToSafeArray((CFArrayRef) cfObj); |
|
110 return true; |
|
111 } else if ((V_VT(pVar)&~VT_ARRAY) == VT_I4) { |
|
112 V_ARRAY(pVar) = MarshallingHelpers::intArrayToSafeArray((CFArrayRef) cfObj); |
|
113 return true; |
|
114 } else if ((V_VT(pVar)&~VT_ARRAY) == VT_UNKNOWN) { |
|
115 V_ARRAY(pVar) = MarshallingHelpers::iunknownArrayToSafeArray((CFArrayRef) cfObj); |
|
116 return true; |
|
117 } |
|
118 } |
|
119 } |
|
120 return false; |
|
121 } |
|
122 |
|
123 static bool ConvertVariantToCFType(VARIANT* pVar, void** cfObj) |
|
124 { |
|
125 if (V_VT(pVar) == VT_NULL) { |
|
126 *cfObj = 0; |
|
127 return true; |
|
128 } |
|
129 else { |
|
130 // if caller expects a string, retrieve BSTR from CFStringRef |
|
131 if (V_VT(pVar) == VT_BSTR) { |
|
132 *cfObj = (void*) MarshallingHelpers::BSTRToCFStringRef(V_BSTR(pVar)); |
|
133 return true; |
|
134 } else if (V_VT(pVar) == VT_I4) { |
|
135 *cfObj = (void*) MarshallingHelpers::intToCFNumberRef(V_I4(pVar)); |
|
136 return true; |
|
137 } else if (!!(V_VT(pVar)&VT_ARRAY)) { |
|
138 if ((V_VT(pVar)&~VT_ARRAY) == VT_BSTR) { |
|
139 *cfObj = (void*) MarshallingHelpers::safeArrayToStringArray(V_ARRAY(pVar)); |
|
140 return true; |
|
141 } else if ((V_VT(pVar)&~VT_ARRAY) == VT_I4) { |
|
142 *cfObj = (void*) MarshallingHelpers::safeArrayToIntArray(V_ARRAY(pVar)); |
|
143 return true; |
|
144 } else if ((V_VT(pVar)&~VT_ARRAY) == VT_UNKNOWN) { |
|
145 *cfObj = (void*) MarshallingHelpers::safeArrayToIUnknownArray(V_ARRAY(pVar)); |
|
146 return true; |
|
147 } |
|
148 } |
|
149 } |
|
150 return false; |
|
151 } |
|
152 |
|
153 HRESULT STDMETHODCALLTYPE CFDictionaryPropertyBag::Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog * /*pErrorLog*/) |
|
154 { |
|
155 if (!pszPropName) |
|
156 return E_POINTER; |
|
157 if (m_dictionary) { |
|
158 void* value; |
|
159 CFStringRef key = MarshallingHelpers::LPCOLESTRToCFStringRef(pszPropName); |
|
160 HRESULT hr = E_FAIL; |
|
161 if (CFDictionaryGetValueIfPresent(m_dictionary.get(), key, (const void**) &value)) { |
|
162 if (ConvertCFTypeToVariant(pVar, value)) |
|
163 hr = S_OK; |
|
164 } else |
|
165 hr = E_INVALIDARG; |
|
166 CFRelease(key); |
|
167 return hr; |
|
168 } |
|
169 return E_FAIL; |
|
170 } |
|
171 |
|
172 HRESULT STDMETHODCALLTYPE CFDictionaryPropertyBag::Write(LPCOLESTR pszPropName, VARIANT* pVar) |
|
173 { |
|
174 if (!pszPropName || !pVar) |
|
175 return E_POINTER; |
|
176 if (!m_dictionary) { |
|
177 m_dictionary = CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); |
|
178 } |
|
179 void* cfObj; |
|
180 if (ConvertVariantToCFType(pVar, &cfObj)) { |
|
181 CFStringRef key = MarshallingHelpers::LPCOLESTRToCFStringRef(pszPropName); |
|
182 CFDictionaryAddValue(m_dictionary.get(), key, cfObj); |
|
183 // CFDictionaryAddValue should automatically retain the CF objects passed in, so release them here |
|
184 CFRelease(key); |
|
185 CFRelease(cfObj); |
|
186 return S_OK; |
|
187 } |
|
188 return E_FAIL; |
|
189 } |