Message decoding functions should take a MessageDecoder reference
[WebKit-https.git] / Source / WebKit2 / Shared / cf / ArgumentCodersCF.cpp
1 /*
2  * Copyright (C) 2010 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "ArgumentCodersCF.h"
28
29 #include "ArgumentDecoder.h"
30 #include "ArgumentEncoder.h"
31 #include "DataReference.h"
32 #include <wtf/Vector.h>
33
34 #if PLATFORM(MAC)
35 #import <Foundation/Foundation.h>
36 #endif
37
38 namespace CoreIPC {
39
40 CFTypeRef tokenNullTypeRef()
41 {
42     static CFStringRef tokenNullType = CFSTR("WKNull");
43     return tokenNullType;
44 }
45
46 enum CFType {
47     CFArray,
48     CFBoolean,
49     CFData,
50     CFDate,
51     CFDictionary,
52     CFNull,
53     CFNumber,
54     CFString,
55     CFURL,
56 #if USE(SECURITY_FRAMEWORK)
57     SecCertificate,
58     SecKeychainItem,
59 #endif
60     Null,
61     Unknown,
62 };
63
64 static CFType typeFromCFTypeRef(CFTypeRef type)
65 {
66     ASSERT(type);
67
68     if (type == tokenNullTypeRef())
69         return Null;
70
71     CFTypeID typeID = CFGetTypeID(type);
72     if (typeID == CFArrayGetTypeID())
73         return CFArray;
74     if (typeID == CFBooleanGetTypeID())
75         return CFBoolean;
76     if (typeID == CFDataGetTypeID())
77         return CFData;
78     if (typeID == CFDateGetTypeID())
79         return CFDate;
80     if (typeID == CFDictionaryGetTypeID())
81         return CFDictionary;
82     if (typeID == CFNullGetTypeID())
83         return CFNull;
84     if (typeID == CFNumberGetTypeID())
85         return CFNumber;
86     if (typeID == CFStringGetTypeID())
87         return CFString;
88     if (typeID == CFURLGetTypeID())
89         return CFURL;
90 #if USE(SECURITY_FRAMEWORK)
91     if (typeID == SecCertificateGetTypeID())
92         return SecCertificate;
93     if (typeID == SecKeychainItemGetTypeID())
94         return SecKeychainItem;
95 #endif
96
97     ASSERT_NOT_REACHED();
98     return Unknown;
99 }
100
101 void encode(ArgumentEncoder& encoder, CFTypeRef typeRef)
102 {
103     CFType type = typeFromCFTypeRef(typeRef);
104     encoder.encodeEnum(type);
105
106     switch (type) {
107     case CFArray:
108         encode(encoder, static_cast<CFArrayRef>(typeRef));
109         return;
110     case CFBoolean:
111         encode(encoder, static_cast<CFBooleanRef>(typeRef));
112         return;
113     case CFData:
114         encode(encoder, static_cast<CFDataRef>(typeRef));
115         return;
116     case CFDate:
117         encode(encoder, static_cast<CFDateRef>(typeRef));
118         return;
119     case CFDictionary:
120         encode(encoder, static_cast<CFDictionaryRef>(typeRef));
121         return;
122     case CFNull:
123         return;
124     case CFNumber:
125         encode(encoder, static_cast<CFNumberRef>(typeRef));
126         return;
127     case CFString:
128         encode(encoder, static_cast<CFStringRef>(typeRef));
129         return;
130     case CFURL:
131         encode(encoder, static_cast<CFURLRef>(typeRef));
132         return;
133 #if USE(SECURITY_FRAMEWORK)
134     case SecCertificate:
135         encode(encoder, (SecCertificateRef)typeRef);
136         return;
137     case SecKeychainItem:
138         encode(encoder, (SecKeychainItemRef)typeRef);
139         return;
140 #endif
141     case Null:
142         return;
143     case Unknown:
144         break;
145     }
146
147     ASSERT_NOT_REACHED();
148 }
149
150 bool decode(ArgumentDecoder& decoder, RetainPtr<CFTypeRef>& result)
151 {
152     CFType type;
153     if (!decoder.decodeEnum(type))
154         return false;
155
156     switch (type) {
157     case CFArray: {
158         RetainPtr<CFArrayRef> array;
159         if (!decode(decoder, array))
160             return false;
161         result.adoptCF(array.leakRef());
162         return true;
163     }
164     case CFBoolean: {
165         RetainPtr<CFBooleanRef> boolean;
166         if (!decode(decoder, boolean))
167             return false;
168         result.adoptCF(boolean.leakRef());
169         return true;
170     }
171     case CFData: {
172         RetainPtr<CFDataRef> data;
173         if (!decode(decoder, data))
174             return false;
175         result.adoptCF(data.leakRef());
176         return true;
177     }
178     case CFDate: {
179         RetainPtr<CFDateRef> date;
180         if (!decode(decoder, date))
181             return false;
182         result.adoptCF(date.leakRef());
183         return true;
184     }
185     case CFDictionary: {
186         RetainPtr<CFDictionaryRef> dictionary;
187         if (!decode(decoder, dictionary))
188             return false;
189         result.adoptCF(dictionary.leakRef());
190         return true;
191     }
192     case CFNull:
193         result.adoptCF(kCFNull);
194         return true;
195     case CFNumber: {
196         RetainPtr<CFNumberRef> number;
197         if (!decode(decoder, number))
198             return false;
199         result.adoptCF(number.leakRef());
200         return true;
201     }
202     case CFString: {
203         RetainPtr<CFStringRef> string;
204         if (!decode(decoder, string))
205             return false;
206         result.adoptCF(string.leakRef());
207         return true;
208     }
209     case CFURL: {
210         RetainPtr<CFURLRef> url;
211         if (!decode(decoder, url))
212             return false;
213         result.adoptCF(url.leakRef());
214         return true;
215     }
216 #if USE(SECURITY_FRAMEWORK)
217     case SecCertificate: {
218         RetainPtr<SecCertificateRef> certificate;
219         if (!decode(decoder, certificate))
220             return false;
221         result.adoptCF(certificate.leakRef());
222         return true;
223     }
224     case SecKeychainItem: {
225         RetainPtr<SecKeychainItemRef> keychainItem;
226         if (!decode(decoder, keychainItem))
227             return false;
228         result.adoptCF(keychainItem.leakRef());
229         return true;
230     }
231 #endif
232     case Null:
233         result = tokenNullTypeRef();
234         return true;
235     case Unknown:
236         ASSERT_NOT_REACHED();
237         return false;
238     }
239
240     return false;
241 }
242
243 void encode(ArgumentEncoder& encoder, CFArrayRef array)
244 {
245     CFIndex size = CFArrayGetCount(array);
246     Vector<CFTypeRef, 32> values(size);
247
248     CFArrayGetValues(array, CFRangeMake(0, size), values.data());
249
250     encoder << static_cast<uint64_t>(size);
251
252     for (CFIndex i = 0; i < size; ++i) {
253         ASSERT(values[i]);
254
255         encode(encoder, values[i]);
256     }
257 }
258
259 bool decode(ArgumentDecoder& decoder, RetainPtr<CFArrayRef>& result)
260 {
261     uint64_t size;
262     if (!decoder.decode(size))
263         return false;
264
265     RetainPtr<CFMutableArrayRef> array(AdoptCF, CFArrayCreateMutable(0, 0, &kCFTypeArrayCallBacks));
266
267     for (size_t i = 0; i < size; ++i) {
268         RetainPtr<CFTypeRef> element;
269         if (!decode(decoder, element))
270             return false;
271
272         CFArrayAppendValue(array.get(), element.get());
273     }
274
275     result.adoptCF(array.leakRef());
276     return true;
277 }
278
279 void encode(ArgumentEncoder& encoder, CFBooleanRef boolean)
280 {
281     encoder << static_cast<bool>(CFBooleanGetValue(boolean));
282 }
283
284 bool decode(ArgumentDecoder& decoder, RetainPtr<CFBooleanRef>& result)
285 {
286     bool boolean;
287     if (!decoder.decode(boolean))
288         return false;
289
290     result.adoptCF(boolean ? kCFBooleanTrue : kCFBooleanFalse);
291     return true;
292 }
293
294 void encode(ArgumentEncoder& encoder, CFDataRef data)
295 {
296     CFIndex length = CFDataGetLength(data);
297     const UInt8* bytePtr = CFDataGetBytePtr(data);
298
299     encoder << CoreIPC::DataReference(bytePtr, length);
300 }
301
302 bool decode(ArgumentDecoder& decoder, RetainPtr<CFDataRef>& result)
303 {
304     CoreIPC::DataReference dataReference;
305     if (!decoder.decode(dataReference))
306         return false;
307
308     result.adoptCF(CFDataCreate(0, dataReference.data(), dataReference.size()));
309     return true;
310 }
311
312 void encode(ArgumentEncoder& encoder, CFDateRef date)
313 {
314     encoder << static_cast<double>(CFDateGetAbsoluteTime(date));
315 }
316
317 bool decode(ArgumentDecoder& decoder, RetainPtr<CFDateRef>& result)
318 {
319     double absoluteTime;
320     if (!decoder.decode(absoluteTime))
321         return false;
322
323     result.adoptCF(CFDateCreate(0, absoluteTime));
324     return true;
325 }
326
327 void encode(ArgumentEncoder& encoder, CFDictionaryRef dictionary)
328 {
329     CFIndex size = CFDictionaryGetCount(dictionary);
330     Vector<CFTypeRef, 32> keys(size);
331     Vector<CFTypeRef, 32> values(size);
332     
333     CFDictionaryGetKeysAndValues(dictionary, keys.data(), values.data());
334
335     encoder << static_cast<uint64_t>(size);
336
337     for (CFIndex i = 0; i < size; ++i) {
338         ASSERT(keys[i]);
339         ASSERT(CFGetTypeID(keys[i]) == CFStringGetTypeID());
340         ASSERT(values[i]);
341
342         // Ignore values we don't recognize.
343         if (typeFromCFTypeRef(values[i]) == Unknown)
344             continue;
345
346         encode(encoder, static_cast<CFStringRef>(keys[i]));
347         encode(encoder, values[i]);
348     }
349 }
350
351 bool decode(ArgumentDecoder& decoder, RetainPtr<CFDictionaryRef>& result)
352 {
353     uint64_t size;
354     if (!decoder.decode(size))
355         return false;
356
357     RetainPtr<CFMutableDictionaryRef> dictionary(AdoptCF, CFDictionaryCreateMutable(0, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
358     for (uint64_t i = 0; i < size; ++i) {
359         // Try to decode the key name.
360         RetainPtr<CFStringRef> key;
361         if (!decode(decoder, key))
362             return false;
363
364         RetainPtr<CFTypeRef> value;
365         if (!decode(decoder, value))
366             return false;
367
368         CFDictionarySetValue(dictionary.get(), key.get(), value.get());
369     }
370
371     result.adoptCF(dictionary.leakRef());
372     return true;
373 }
374
375 void encode(ArgumentEncoder& encoder, CFNumberRef number)
376 {
377     CFNumberType numberType = CFNumberGetType(number);
378
379     Vector<uint8_t> buffer(CFNumberGetByteSize(number));
380     bool result = CFNumberGetValue(number, numberType, buffer.data());
381     ASSERT_UNUSED(result, result);
382
383     encoder.encodeEnum(numberType);
384     encoder << CoreIPC::DataReference(buffer);
385 }
386
387 static size_t sizeForNumberType(CFNumberType numberType)
388 {
389     switch (numberType) {
390     case kCFNumberSInt8Type:
391         return sizeof(SInt8);
392     case kCFNumberSInt16Type:
393         return sizeof(SInt16);
394     case kCFNumberSInt32Type:
395         return sizeof(SInt32);
396     case kCFNumberSInt64Type:
397         return sizeof(SInt64);
398     case kCFNumberFloat32Type:
399         return sizeof(Float32);
400     case kCFNumberFloat64Type:
401         return sizeof(Float64);
402     case kCFNumberCharType:
403         return sizeof(char);
404     case kCFNumberShortType:
405         return sizeof(short);
406     case kCFNumberIntType:
407         return sizeof(int);
408     case kCFNumberLongType:
409         return sizeof(long);
410     case kCFNumberLongLongType:
411         return sizeof(long long);
412     case kCFNumberFloatType:
413         return sizeof(float);
414     case kCFNumberDoubleType:
415         return sizeof(double);
416     case kCFNumberCFIndexType:
417         return sizeof(CFIndex);
418     case kCFNumberNSIntegerType:
419 #ifdef __LP64__
420         return sizeof(long);
421 #else
422         return sizeof(int);
423 #endif
424     case kCFNumberCGFloatType:
425 #ifdef __LP64__
426         return sizeof(double);
427 #else
428         return sizeof(float);
429 #endif
430     }
431
432     return 0;
433 }
434
435 bool decode(ArgumentDecoder& decoder, RetainPtr<CFNumberRef>& result)
436 {
437     CFNumberType numberType;
438     if (!decoder.decodeEnum(numberType))
439         return false;
440
441     CoreIPC::DataReference dataReference;
442     if (!decoder.decode(dataReference))
443         return false;
444
445     size_t neededBufferSize = sizeForNumberType(numberType);
446     if (!neededBufferSize || dataReference.size() != neededBufferSize)
447         return false;
448
449     ASSERT(dataReference.data());
450     CFNumberRef number = CFNumberCreate(0, numberType, dataReference.data());
451     result.adoptCF(number);
452
453     return true;
454 }
455
456 void encode(ArgumentEncoder& encoder, CFStringRef string)
457 {
458     CFIndex length = CFStringGetLength(string);
459     CFStringEncoding encoding = CFStringGetFastestEncoding(string);
460
461     CFRange range = CFRangeMake(0, length);
462     CFIndex bufferLength = 0;
463
464     CFIndex numConvertedBytes = CFStringGetBytes(string, range, encoding, 0, false, 0, 0, &bufferLength);
465     ASSERT(numConvertedBytes == length);
466
467     Vector<UInt8, 128> buffer(bufferLength);
468     numConvertedBytes = CFStringGetBytes(string, range, encoding, 0, false, buffer.data(), buffer.size(), &bufferLength);
469     ASSERT(numConvertedBytes == length);
470
471     encoder.encodeEnum(encoding);
472     encoder << CoreIPC::DataReference(buffer);
473 }
474
475 bool decode(ArgumentDecoder& decoder, RetainPtr<CFStringRef>& result)
476 {
477     CFStringEncoding encoding;
478     if (!decoder.decodeEnum(encoding))
479         return false;
480
481     if (!CFStringIsEncodingAvailable(encoding))
482         return false;
483     
484     CoreIPC::DataReference dataReference;
485     if (!decoder.decode(dataReference))
486         return false;
487
488     CFStringRef string = CFStringCreateWithBytes(0, dataReference.data(), dataReference.size(), encoding, false);
489     if (!string)
490         return false;
491
492     result.adoptCF(string);
493     return true;
494 }
495
496 void encode(ArgumentEncoder& encoder, CFURLRef url)
497 {
498     CFURLRef baseURL = CFURLGetBaseURL(url);
499     encoder << static_cast<bool>(baseURL);
500     if (baseURL)
501         encode(encoder, baseURL);
502
503     encode(encoder, CFURLGetString(url));
504 }
505
506 bool decode(ArgumentDecoder& decoder, RetainPtr<CFURLRef>& result)
507 {
508     RetainPtr<CFURLRef> baseURL;
509     bool hasBaseURL;
510     if (!decoder.decode(hasBaseURL))
511         return false;
512     if (hasBaseURL) {
513         if (!decode(decoder, baseURL))
514             return false;
515     }
516
517     RetainPtr<CFStringRef> string;
518     if (!decode(decoder, string))
519         return false;
520
521 #if PLATFORM(MAC)
522     // FIXME: Move this to ArgumentCodersCFMac.mm and change this file back to be C++
523     // instead of Objective-C++.
524     if (!CFStringGetLength(string.get())) {
525         // CFURL can't hold an empty URL, unlike NSURL.
526         result = reinterpret_cast<CFURLRef>([NSURL URLWithString:@""]);
527         return true;
528     }
529 #endif
530                     
531     CFURLRef url = CFURLCreateWithString(0, string.get(), baseURL.get());
532     if (!url)
533         return false;
534
535     result.adoptCF(url);
536     return true;
537 }
538
539 #if USE(SECURITY_FRAMEWORK)
540 void encode(ArgumentEncoder& encoder, SecCertificateRef certificate)
541 {
542     RetainPtr<CFDataRef> data(AdoptCF, SecCertificateCopyData(certificate));
543     encode(encoder, data.get());
544 }
545
546 bool decode(ArgumentDecoder& decoder, RetainPtr<SecCertificateRef>& result)
547 {
548     RetainPtr<CFDataRef> data;
549     if (!decode(decoder, data))
550         return false;
551
552     result.adoptCF(SecCertificateCreateWithData(0, data.get()));
553     return true;
554 }
555
556 void encode(ArgumentEncoder& encoder, SecKeychainItemRef keychainItem)
557 {
558     CFDataRef data;
559     if (SecKeychainItemCreatePersistentReference(keychainItem, &data) == errSecSuccess) {
560         encode(encoder, data);
561         CFRelease(data);
562     }
563 }
564
565 bool decode(ArgumentDecoder& decoder, RetainPtr<SecKeychainItemRef>& result)
566 {
567     RetainPtr<CFDataRef> data;
568     if (!CoreIPC::decode(decoder, data))
569         return false;
570
571     SecKeychainItemRef item;
572     if (SecKeychainItemCopyFromPersistentReference(data.get(), &item) != errSecSuccess || !item)
573         return false;
574     
575     result.adoptCF(item);
576     return true;
577 }
578 #endif
579
580 } // namespace CoreIPC