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 "WebPreferences.h" 28 29 #include <wtf/RetainPtr.h> 30 #include <wtf/text/StringConcatenate.h> 31 32 #if !PLATFORM(MAC) 33 34 namespace WebKit { 35 36 static RetainPtr<CFStringRef> cfStringFromWebCoreString(const String& string) 37 { 38 return RetainPtr<CFStringRef>(AdoptCF, CFStringCreateWithCharacters(0, reinterpret_cast<const UniChar*>(string.characters()), string.length())); 39 } 40 41 static inline RetainPtr<CFStringRef> makeKey(const String& identifier, const String& baseKey) 42 { 43 return cfStringFromWebCoreString(makeString(identifier, ".WebKit2", baseKey)); 44 } 45 46 static void setStringValueIfInUserDefaults(const String& identifier, const String& baseKey, WebPreferencesStore& store) 47 { 48 RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(makeKey(identifier, baseKey).get(), kCFPreferencesCurrentApplication)); 49 if (!value) 50 return; 51 if (CFGetTypeID(value.get()) != CFStringGetTypeID()) 52 return; 53 54 store.setStringValueForKey(baseKey, (CFStringRef)value.get()); 55 } 56 57 static void setBoolValueIfInUserDefaults(const String& identifier, const String& baseKey, WebPreferencesStore& store) 58 { 59 RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(makeKey(identifier, baseKey).get(), kCFPreferencesCurrentApplication)); 60 if (!value) 61 return; 62 if (CFGetTypeID(value.get()) != CFBooleanGetTypeID()) 63 return; 64 65 store.setBoolValueForKey(baseKey, CFBooleanGetValue((CFBooleanRef)value.get()) ? true : false); 66 } 67 68 static void setUInt32ValueIfInUserDefaults(const String& identifier, const String& baseKey, WebPreferencesStore& store) 69 { 70 RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(makeKey(identifier, baseKey).get(), kCFPreferencesCurrentApplication)); 71 if (!value) 72 return; 73 if (CFGetTypeID(value.get()) != CFNumberGetTypeID()) 74 return; 75 int32_t intValue = 0; 76 if (!CFNumberGetValue((CFNumberRef)value.get(), kCFNumberSInt32Type, &intValue)) 77 return; 78 79 store.setUInt32ValueForKey(baseKey, intValue); 80 } 81 82 static void setDoubleValueIfInUserDefaults(const String& identifier, const String& baseKey, WebPreferencesStore& store) 83 { 84 RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(makeKey(identifier, baseKey).get(), kCFPreferencesCurrentApplication)); 85 if (!value) 86 return; 87 if (CFGetTypeID(value.get()) != CFNumberGetTypeID()) 88 return; 89 double doubleValue = 0; 90 if (!CFNumberGetValue((CFNumberRef)value.get(), kCFNumberDoubleType, &doubleValue)) 91 return; 92 93 store.setDoubleValueForKey(baseKey, doubleValue); 94 } 95 96 void WebPreferences::platformInitializeStore() 97 { 98 if (!m_identifier) 99 return; 100 101 #define INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \ 102 set##TypeName##ValueIfInUserDefaults(m_identifier, WebPreferencesKey::KeyLower##Key(), m_store); 103 104 FOR_EACH_WEBKIT_PREFERENCE(INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS) 105 106 #undef INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS 107 108 } 109 110 void WebPreferences::platformUpdateStringValueForKey(const String& key, const String& value) 111 { 112 if (!m_identifier) 113 return; 114 115 CFPreferencesSetAppValue(makeKey(m_identifier, key).get(), cfStringFromWebCoreString(value).get(), kCFPreferencesCurrentApplication); 116 } 117 118 void WebPreferences::platformUpdateBoolValueForKey(const String& key, bool value) 119 { 120 if (!m_identifier) 121 return; 122 123 CFPreferencesSetAppValue(makeKey(m_identifier, key).get(), value ? kCFBooleanTrue : kCFBooleanFalse, kCFPreferencesCurrentApplication); 124 } 125 126 void WebPreferences::platformUpdateUInt32ValueForKey(const String& key, uint32_t value) 127 { 128 if (!m_identifier) 129 return; 130 131 RetainPtr<CFNumberRef> number(AdoptCF, CFNumberCreate(0, kCFNumberSInt32Type, &value)); 132 CFPreferencesSetAppValue(makeKey(m_identifier, key).get(), number.get(), kCFPreferencesCurrentApplication); 133 } 134 135 void WebPreferences::platformUpdateDoubleValueForKey(const String& key, double value) 136 { 137 if (!m_identifier) 138 return; 139 140 RetainPtr<CFNumberRef> number(AdoptCF, CFNumberCreate(0, kCFNumberDoubleType, &value)); 141 CFPreferencesSetAppValue(makeKey(m_identifier, key).get(), number.get(), kCFPreferencesCurrentApplication); 142 } 143 144 } // namespace WebKit 145 146 #endif // !PLATFORM(MAC) 147