Windows build fix
[WebKit-https.git] / WebKit / cf / WebCoreSupport / WebInspectorClientCF.cpp
1 /*
2  * Copyright (C) 2008 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. ``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 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 // FIXME: On Windows, we require all WebKit source files to include config.h
27 // before including any other files. Failing to include config.h will leave
28 // WTF_PLATFORM_CF undefined, causing build failures in this file. But Mac
29 // doesn't have a config.h for WebKit, so we can't include the Windows one
30 // here. For now we can just define WTF_PLATFORM_CF manually, but we need a
31 // better long-term solution.
32 #ifndef WTF_PLATFORM_CF
33 #define WTF_PLATFORM_CF 1
34 #endif
35
36 #include "WebInspectorClient.h"
37
38 #include <CoreFoundation/CoreFoundation.h>
39
40 #include <WebCore/PlatformString.h>
41
42 #include <wtf/RetainPtr.h>
43 #include <wtf/Vector.h>
44
45 using namespace WebCore;
46
47 static inline CFStringRef createKeyForPreferences(const String& key)
48 {
49     RetainPtr<CFStringRef> keyCFString(AdoptCF, key.createCFString());
50     return CFStringCreateWithFormat(0, 0, CFSTR("WebKit Web Inspector Setting - %@"), keyCFString.get());
51 }
52
53 void WebInspectorClient::populateSetting(const String& key, InspectorController::Setting& setting)
54 {
55     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
56     RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(preferencesKey.get(), kCFPreferencesCurrentApplication));
57
58     if (!value)
59         return;
60
61     CFTypeID type = CFGetTypeID(value.get());
62     if (type == CFStringGetTypeID())
63         setting.set(static_cast<String>(static_cast<CFStringRef>(value.get())));
64     else if (type == CFBooleanGetTypeID())
65         setting.set(static_cast<bool>(CFBooleanGetValue(static_cast<CFBooleanRef>(value.get()))));
66     else if (type == CFNumberGetTypeID()) {
67         CFNumberRef number = static_cast<CFNumberRef>(value.get());
68         if (CFNumberIsFloatType(number)) {
69             double doubleNumber = 0.0;
70             CFNumberGetValue(static_cast<CFNumberRef>(value.get()), kCFNumberDoubleType, &doubleNumber);
71             setting.set(doubleNumber);
72         } else {
73             long longNumber = 0;
74             CFNumberGetValue(static_cast<CFNumberRef>(value.get()), kCFNumberLongType, &longNumber);
75             setting.set(longNumber);
76         }
77     } else if (type == CFArrayGetTypeID()) {
78         Vector<String> strings;
79
80         CFArrayRef array = static_cast<CFArrayRef>(value.get());
81         unsigned length = CFArrayGetCount(array);
82         for (unsigned i = 0; i < length; ++i) {
83             CFStringRef string = static_cast<CFStringRef>(CFArrayGetValueAtIndex(array, i));
84             if (CFGetTypeID(string) == CFStringGetTypeID())
85                 strings.append(static_cast<String>(static_cast<CFStringRef>(string)));
86         }
87
88         setting.set(strings);
89     } else
90         ASSERT_NOT_REACHED();
91 }
92
93 void WebInspectorClient::storeSetting(const String& key, const InspectorController::Setting& setting)
94 {
95     RetainPtr<CFPropertyListRef> objectToStore;
96
97     switch (setting.type()) {
98         default:
99         case InspectorController::Setting::NoType:
100             ASSERT_NOT_REACHED();
101             break;
102         case InspectorController::Setting::StringType:
103             objectToStore.adoptCF(setting.string().createCFString());
104             break;
105         case InspectorController::Setting::BooleanType:
106             objectToStore = (setting.booleanValue() ? kCFBooleanTrue : kCFBooleanFalse);
107             break;
108
109         case InspectorController::Setting::DoubleType: {
110             double value = setting.doubleValue();
111             objectToStore.adoptCF(CFNumberCreate(0, kCFNumberDoubleType, &value));
112             break;
113         }
114
115         case InspectorController::Setting::IntegerType: {
116             long value = setting.integerValue();
117             objectToStore.adoptCF(CFNumberCreate(0, kCFNumberLongType, &value));
118             break;
119         }
120
121         case InspectorController::Setting::StringVectorType: {
122             const Vector<String>& strings = setting.stringVector();
123             const unsigned length = strings.size();
124
125             RetainPtr<CFMutableArrayRef> array(AdoptCF, CFArrayCreateMutable(0, length, &kCFTypeArrayCallBacks));
126
127             for (unsigned i = 0; i < length; ++i) {
128                 RetainPtr<CFStringRef> string(AdoptCF, strings[i].createCFString());
129                 CFArraySetValueAtIndex(array.get(), i, string.get());
130             }
131
132             objectToStore = array;
133             break;
134         }
135     }
136
137     ASSERT(objectToStore);
138
139     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
140     CFPreferencesSetAppValue(preferencesKey.get(), objectToStore.get(), kCFPreferencesCurrentApplication);
141 }
142
143 void WebInspectorClient::removeSetting(const String& key)
144 {
145     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
146     CFPreferencesSetAppValue(preferencesKey.get(), 0, kCFPreferencesCurrentApplication);
147 }