• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2010, 2011 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#import "config.h"
27#import "WebPreferences.h"
28
29#import "PageClientImpl.h"
30#import <wtf/text/StringConcatenate.h>
31
32namespace WebKit {
33
34static inline NSString* makeKey(const String& identifier, const String& baseKey)
35{
36    return nsStringFromWebCoreString(makeString(identifier, ".WebKit2", baseKey));
37}
38
39static void setStringValueIfInUserDefaults(const String& identifier, const String& key, WebPreferencesStore& store)
40{
41    id object = [[NSUserDefaults standardUserDefaults] objectForKey:makeKey(identifier, key)];
42    if (!object)
43        return;
44    if (![object isKindOfClass:[NSString class]])
45        return;
46
47    store.setStringValueForKey(key, (NSString *)object);
48}
49
50static void setBoolValueIfInUserDefaults(const String& identifier, const String& key, WebPreferencesStore& store)
51{
52    id object = [[NSUserDefaults standardUserDefaults] objectForKey:makeKey(identifier, key)];
53    if (!object)
54        return;
55    if (![object respondsToSelector:@selector(boolValue)])
56        return;
57
58    store.setBoolValueForKey(key, [object boolValue]);
59}
60
61static void setUInt32ValueIfInUserDefaults(const String& identifier, const String& key, WebPreferencesStore& store)
62{
63    id object = [[NSUserDefaults standardUserDefaults] objectForKey:makeKey(identifier, key)];
64    if (!object)
65        return;
66    if (![object respondsToSelector:@selector(intValue)])
67        return;
68
69    store.setUInt32ValueForKey(key, [object intValue]);
70}
71
72static void setDoubleValueIfInUserDefaults(const String& identifier, const String& key, WebPreferencesStore& store)
73{
74    id object = [[NSUserDefaults standardUserDefaults] objectForKey:makeKey(identifier, key)];
75    if (!object)
76        return;
77    if (![object respondsToSelector:@selector(doubleValue)])
78        return;
79
80    store.setDoubleValueForKey(key, [object doubleValue]);
81}
82
83void WebPreferences::platformInitializeStore()
84{
85    if (!m_identifier)
86        return;
87
88#define INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS(KeyUpper, KeyLower, TypeName, Type, DefaultValue) \
89    set##TypeName##ValueIfInUserDefaults(m_identifier, WebPreferencesKey::KeyLower##Key(), m_store);
90
91    FOR_EACH_WEBKIT_PREFERENCE(INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS)
92
93#undef INITIALIZE_PREFERENCE_FROM_NSUSERDEFAULTS
94}
95
96void WebPreferences::platformUpdateStringValueForKey(const String& key, const String& value)
97{
98    if (!m_identifier)
99        return;
100
101    [[NSUserDefaults standardUserDefaults] setObject:nsStringFromWebCoreString(value) forKey:makeKey(m_identifier, key)];
102}
103
104void WebPreferences::platformUpdateBoolValueForKey(const String& key, bool value)
105{
106    if (!m_identifier)
107        return;
108
109    [[NSUserDefaults standardUserDefaults] setBool:value forKey:makeKey(m_identifier, key)];
110}
111
112void WebPreferences::platformUpdateUInt32ValueForKey(const String& key, uint32_t value)
113{
114    if (!m_identifier)
115        return;
116
117    [[NSUserDefaults standardUserDefaults] setInteger:value forKey:makeKey(m_identifier, key)];
118}
119
120void WebPreferences::platformUpdateDoubleValueForKey(const String& key, double value)
121{
122    if (!m_identifier)
123        return;
124
125    [[NSUserDefaults standardUserDefaults] setDouble:value forKey:makeKey(m_identifier, key)];
126}
127
128} // namespace WebKit
129