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