2008-12-09 Dimitri Glazkov <dglazkov@chromium.org>
[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 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 #include "WebInspectorClient.h"
41
42 #include <CoreFoundation/CoreFoundation.h>
43
44 #include <WebCore/PlatformString.h>
45
46 #include <wtf/RetainPtr.h>
47 #include <wtf/Vector.h>
48
49 using namespace WebCore;
50
51 static inline CFStringRef createKeyForPreferences(const String& key)
52 {
53     RetainPtr<CFStringRef> keyCFString(AdoptCF, key.createCFString());
54     return CFStringCreateWithFormat(0, 0, CFSTR("WebKit Web Inspector Setting - %@"), keyCFString.get());
55 }
56
57 void WebInspectorClient::populateSetting(const String& key, InspectorController::Setting& setting)
58 {
59     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
60     RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(preferencesKey.get(), kCFPreferencesCurrentApplication));
61
62     if (!value)
63         return;
64
65     CFTypeID type = CFGetTypeID(value.get());
66     if (type == CFStringGetTypeID())
67         setting.set(static_cast<String>(static_cast<CFStringRef>(value.get())));
68     else if (type == CFBooleanGetTypeID())
69         setting.set(static_cast<bool>(CFBooleanGetValue(static_cast<CFBooleanRef>(value.get()))));
70     else if (type == CFNumberGetTypeID()) {
71         CFNumberRef number = static_cast<CFNumberRef>(value.get());
72         if (CFNumberIsFloatType(number)) {
73             double doubleNumber = 0.0;
74             CFNumberGetValue(static_cast<CFNumberRef>(value.get()), kCFNumberDoubleType, &doubleNumber);
75             setting.set(doubleNumber);
76         } else {
77             long longNumber = 0;
78             CFNumberGetValue(static_cast<CFNumberRef>(value.get()), kCFNumberLongType, &longNumber);
79             setting.set(longNumber);
80         }
81     } else if (type == CFArrayGetTypeID()) {
82         Vector<String> strings;
83
84         CFArrayRef array = static_cast<CFArrayRef>(value.get());
85         unsigned length = CFArrayGetCount(array);
86         for (unsigned i = 0; i < length; ++i) {
87             CFStringRef string = static_cast<CFStringRef>(CFArrayGetValueAtIndex(array, i));
88             if (CFGetTypeID(string) == CFStringGetTypeID())
89                 strings.append(static_cast<String>(static_cast<CFStringRef>(string)));
90         }
91
92         setting.set(strings);
93     } else
94         ASSERT_NOT_REACHED();
95 }
96
97 void WebInspectorClient::storeSetting(const String& key, const InspectorController::Setting& setting)
98 {
99     RetainPtr<CFPropertyListRef> objectToStore;
100
101     switch (setting.type()) {
102         default:
103         case InspectorController::Setting::NoType:
104             ASSERT_NOT_REACHED();
105             break;
106         case InspectorController::Setting::StringType:
107             objectToStore.adoptCF(setting.string().createCFString());
108             break;
109         case InspectorController::Setting::BooleanType:
110             objectToStore = (setting.booleanValue() ? kCFBooleanTrue : kCFBooleanFalse);
111             break;
112
113         case InspectorController::Setting::DoubleType: {
114             double value = setting.doubleValue();
115             objectToStore.adoptCF(CFNumberCreate(0, kCFNumberDoubleType, &value));
116             break;
117         }
118
119         case InspectorController::Setting::IntegerType: {
120             long value = setting.integerValue();
121             objectToStore.adoptCF(CFNumberCreate(0, kCFNumberLongType, &value));
122             break;
123         }
124
125         case InspectorController::Setting::StringVectorType: {
126             const Vector<String>& strings = setting.stringVector();
127             const unsigned length = strings.size();
128
129             RetainPtr<CFMutableArrayRef> array(AdoptCF, CFArrayCreateMutable(0, length, &kCFTypeArrayCallBacks));
130
131             for (unsigned i = 0; i < length; ++i) {
132                 RetainPtr<CFStringRef> string(AdoptCF, strings[i].createCFString());
133                 CFArraySetValueAtIndex(array.get(), i, string.get());
134             }
135
136             objectToStore = array;
137             break;
138         }
139     }
140
141     ASSERT(objectToStore);
142
143     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
144     CFPreferencesSetAppValue(preferencesKey.get(), objectToStore.get(), kCFPreferencesCurrentApplication);
145 }
146
147 void WebInspectorClient::removeSetting(const String& key)
148 {
149     RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
150     CFPreferencesSetAppValue(preferencesKey.get(), 0, kCFPreferencesCurrentApplication);
151 }