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_USE_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_USE_CF,  WTF_USE_JSC, and
31// WTF_USE_CFNETWORK manually, but we need a better long-term solution.
32#ifndef WTF_USE_CF
33#define WTF_USE_CF 1
34#endif
35
36#ifndef WTF_USE_JSC
37#define WTF_USE_JSC 1
38#endif
39
40// Leave these set to nothing until we switch Mac and Win ports over to
41// using the export macros.
42#define JS_EXPORT_PRIVATE
43#define WTF_EXPORT_PRIVATE
44
45#if defined(WIN32) || defined(_WIN32)
46#ifndef WTF_USE_CFNETWORK
47#define WTF_USE_CFNETWORK 1
48#endif
49#if defined(BUILDING_JavaScriptCore) || defined(BUILDING_WTF)
50#define JS_EXPORTDATA __declspec(dllexport)
51#else
52#define JS_EXPORTDATA __declspec(dllimport)
53#endif
54#define JS_EXPORTCLASS JS_EXPORTDATA
55#else
56#define JS_EXPORTDATA
57#define JS_EXPORTCLASS
58#endif
59
60#include "WebInspectorClient.h"
61
62#include <CoreFoundation/CoreFoundation.h>
63
64#include <WebCore/Frame.h>
65#include <WebCore/InspectorFrontendClientLocal.h>
66#include <WebCore/Page.h>
67#include <WebCore/PlatformString.h>
68
69#include <wtf/PassOwnPtr.h>
70#include <wtf/RetainPtr.h>
71#include <wtf/Vector.h>
72
73using namespace WebCore;
74
75static const char* inspectorStartsAttachedSetting = "inspectorStartsAttached";
76
77static inline CFStringRef createKeyForPreferences(const String& key)
78{
79    RetainPtr<CFStringRef> keyCFString(AdoptCF, key.createCFString());
80    return CFStringCreateWithFormat(0, 0, CFSTR("WebKit Web Inspector Setting - %@"), keyCFString.get());
81}
82
83static void populateSetting(const String& key, String* setting)
84{
85    RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
86    RetainPtr<CFPropertyListRef> value(AdoptCF, CFPreferencesCopyAppValue(preferencesKey.get(), kCFPreferencesCurrentApplication));
87
88    if (!value)
89        return;
90
91    CFTypeID type = CFGetTypeID(value.get());
92    if (type == CFStringGetTypeID())
93        *setting = static_cast<String>(static_cast<CFStringRef>(value.get()));
94    else if (type == CFBooleanGetTypeID())
95        *setting = static_cast<bool>(CFBooleanGetValue(static_cast<CFBooleanRef>(value.get()))) ? "true" : "false";
96    else
97        *setting = "";
98}
99
100static void storeSetting(const String& key, const String& setting)
101{
102    RetainPtr<CFPropertyListRef> objectToStore;
103    objectToStore.adoptCF(setting.createCFString());
104    ASSERT(objectToStore);
105
106    RetainPtr<CFStringRef> preferencesKey(AdoptCF, createKeyForPreferences(key));
107    CFPreferencesSetAppValue(preferencesKey.get(), objectToStore.get(), kCFPreferencesCurrentApplication);
108}
109
110bool WebInspectorClient::sendMessageToFrontend(const String& message)
111{
112    return doDispatchMessageOnFrontendPage(m_frontendPage, message);
113}
114
115bool WebInspectorClient::inspectorStartsAttached()
116{
117    String value;
118    populateSetting(inspectorStartsAttachedSetting, &value);
119    if (value.isEmpty())
120        return true;
121    return value == "true";
122}
123
124void WebInspectorClient::setInspectorStartsAttached(bool attached)
125{
126    storeSetting(inspectorStartsAttachedSetting, attached ? "true" : "false");
127}
128
129void WebInspectorClient::releaseFrontendPage()
130{
131    m_frontendPage = 0;
132}
133
134void WebInspectorClient::saveSessionSetting(const String& key, const String& value)
135{
136    if (!key.isEmpty())
137        m_sessionSettings.set(key, value);
138}
139
140void WebInspectorClient::loadSessionSetting(const String& key, String* value)
141{
142    if (!key.isEmpty())
143        *value = m_sessionSettings.get(key);
144}
145
146WTF::PassOwnPtr<WebCore::InspectorFrontendClientLocal::Settings> WebInspectorClient::createFrontendSettings()
147{
148    class InspectorFrontendSettingsCF : public WebCore::InspectorFrontendClientLocal::Settings {
149    public:
150        virtual ~InspectorFrontendSettingsCF() { }
151        virtual String getProperty(const String& name)
152        {
153            String value;
154            populateSetting(name, &value);
155            return value;
156        }
157
158        virtual void setProperty(const String& name, const String& value)
159        {
160            storeSetting(name, value);
161        }
162    };
163    return adoptPtr<WebCore::InspectorFrontendClientLocal::Settings>(new InspectorFrontendSettingsCF());
164}
165