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