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 "InjectedBundlePageUIClient.h"
28
29#include "InjectedBundleHitTestResult.h"
30#include "WKAPICast.h"
31#include "WKBundleAPICast.h"
32#include "WebSecurityOrigin.h"
33#include <wtf/text/WTFString.h>
34
35using namespace WebCore;
36
37namespace WebKit {
38
39InjectedBundlePageUIClient::InjectedBundlePageUIClient(const WKBundlePageUIClientBase* client)
40{
41    initialize(client);
42}
43
44void InjectedBundlePageUIClient::willAddMessageToConsole(WebPage* page, const String& message, int32_t lineNumber)
45{
46    if (m_client.willAddMessageToConsole)
47        m_client.willAddMessageToConsole(toAPI(page), toAPI(message.impl()), lineNumber, m_client.base.clientInfo);
48}
49
50void InjectedBundlePageUIClient::willSetStatusbarText(WebPage* page, const String& statusbarText)
51{
52    if (m_client.willSetStatusbarText)
53        m_client.willSetStatusbarText(toAPI(page), toAPI(statusbarText.impl()), m_client.base.clientInfo);
54}
55
56void InjectedBundlePageUIClient::willRunJavaScriptAlert(WebPage* page, const String& alertText, WebFrame* frame)
57{
58    if (m_client.willRunJavaScriptAlert)
59        m_client.willRunJavaScriptAlert(toAPI(page), toAPI(alertText.impl()), toAPI(frame), m_client.base.clientInfo);
60}
61
62void InjectedBundlePageUIClient::willRunJavaScriptConfirm(WebPage* page, const String& message, WebFrame* frame)
63{
64    if (m_client.willRunJavaScriptConfirm)
65        m_client.willRunJavaScriptConfirm(toAPI(page), toAPI(message.impl()), toAPI(frame), m_client.base.clientInfo);
66}
67
68void InjectedBundlePageUIClient::willRunJavaScriptPrompt(WebPage* page, const String& message, const String& defaultValue, WebFrame* frame)
69{
70    if (m_client.willRunJavaScriptPrompt)
71        m_client.willRunJavaScriptPrompt(toAPI(page), toAPI(message.impl()), toAPI(defaultValue.impl()), toAPI(frame), m_client.base.clientInfo);
72}
73
74void InjectedBundlePageUIClient::mouseDidMoveOverElement(WebPage* page, const HitTestResult& coreHitTestResult, WebEvent::Modifiers modifiers, RefPtr<API::Object>& userData)
75{
76    if (!m_client.mouseDidMoveOverElement)
77        return;
78
79    RefPtr<InjectedBundleHitTestResult> hitTestResult = InjectedBundleHitTestResult::create(coreHitTestResult);
80
81    WKTypeRef userDataToPass = 0;
82    m_client.mouseDidMoveOverElement(toAPI(page), toAPI(hitTestResult.get()), toAPI(modifiers), &userDataToPass, m_client.base.clientInfo);
83    userData = adoptRef(toImpl(userDataToPass));
84}
85
86void InjectedBundlePageUIClient::pageDidScroll(WebPage* page)
87{
88    if (!m_client.pageDidScroll)
89        return;
90
91    m_client.pageDidScroll(toAPI(page), m_client.base.clientInfo);
92}
93
94String InjectedBundlePageUIClient::shouldGenerateFileForUpload(WebPage* page, const String& originalFilePath)
95{
96    if (!m_client.shouldGenerateFileForUpload)
97        return String();
98    RefPtr<API::String> generatedFilePath = adoptRef(toImpl(m_client.shouldGenerateFileForUpload(toAPI(page), toAPI(originalFilePath.impl()), m_client.base.clientInfo)));
99    return generatedFilePath ? generatedFilePath->string() : String();
100}
101
102String InjectedBundlePageUIClient::generateFileForUpload(WebPage* page, const String& originalFilePath)
103{
104    if (!m_client.generateFileForUpload)
105        return String();
106    RefPtr<API::String> generatedFilePath = adoptRef(toImpl(m_client.generateFileForUpload(toAPI(page), toAPI(originalFilePath.impl()), m_client.base.clientInfo)));
107    return generatedFilePath ? generatedFilePath->string() : String();
108}
109
110static API::InjectedBundle::PageUIClient::UIElementVisibility toUIElementVisibility(WKBundlePageUIElementVisibility visibility)
111{
112    switch (visibility) {
113    case WKBundlePageUIElementVisibilityUnknown:
114        return API::InjectedBundle::PageUIClient::UIElementVisibility::Unknown;
115    case WKBundlePageUIElementVisible:
116        return API::InjectedBundle::PageUIClient::UIElementVisibility::Visible;
117    case WKBundlePageUIElementHidden:
118        return API::InjectedBundle::PageUIClient::UIElementVisibility::Hidden;
119    }
120
121    ASSERT_NOT_REACHED();
122    return API::InjectedBundle::PageUIClient::UIElementVisibility::Unknown;
123}
124
125API::InjectedBundle::PageUIClient::UIElementVisibility InjectedBundlePageUIClient::statusBarIsVisible(WebPage* page)
126{
127    if (!m_client.statusBarIsVisible)
128        return API::InjectedBundle::PageUIClient::UIElementVisibility::Unknown;
129
130    return toUIElementVisibility(m_client.statusBarIsVisible(toAPI(page), m_client.base.clientInfo));
131}
132
133API::InjectedBundle::PageUIClient::UIElementVisibility InjectedBundlePageUIClient::menuBarIsVisible(WebPage* page)
134{
135    if (!m_client.menuBarIsVisible)
136        return API::InjectedBundle::PageUIClient::UIElementVisibility::Unknown;
137
138    return toUIElementVisibility(m_client.menuBarIsVisible(toAPI(page), m_client.base.clientInfo));
139}
140
141API::InjectedBundle::PageUIClient::UIElementVisibility InjectedBundlePageUIClient::toolbarsAreVisible(WebPage* page)
142{
143    if (!m_client.toolbarsAreVisible)
144        return API::InjectedBundle::PageUIClient::UIElementVisibility::Unknown;
145
146    return toUIElementVisibility(m_client.toolbarsAreVisible(toAPI(page), m_client.base.clientInfo));
147}
148
149bool InjectedBundlePageUIClient::didReachApplicationCacheOriginQuota(WebPage* page, WebSecurityOrigin* origin, int64_t totalBytesNeeded)
150{
151    if (!m_client.didReachApplicationCacheOriginQuota)
152        return false;
153
154    m_client.didReachApplicationCacheOriginQuota(toAPI(page), toAPI(origin), totalBytesNeeded, m_client.base.clientInfo);
155    return true;
156}
157
158uint64_t InjectedBundlePageUIClient::didExceedDatabaseQuota(WebPage* page, WebSecurityOrigin* origin, const String& databaseName, const String& databaseDisplayName, uint64_t currentQuotaBytes, uint64_t currentOriginUsageBytes, uint64_t currentDatabaseUsageBytes, uint64_t expectedUsageBytes)
159{
160    if (!m_client.didExceedDatabaseQuota)
161        return 0;
162
163    return m_client.didExceedDatabaseQuota(toAPI(page), toAPI(origin), toAPI(databaseName.impl()), toAPI(databaseDisplayName.impl()), currentQuotaBytes, currentOriginUsageBytes, currentDatabaseUsageBytes, expectedUsageBytes, m_client.base.clientInfo);
164}
165
166String InjectedBundlePageUIClient::plugInStartLabelTitle(const String& mimeType) const
167{
168    if (!m_client.createPlugInStartLabelTitle)
169        return String();
170
171    RefPtr<API::String> title = adoptRef(toImpl(m_client.createPlugInStartLabelTitle(toAPI(mimeType.impl()), m_client.base.clientInfo)));
172    return title ? title->string() : String();
173}
174
175String InjectedBundlePageUIClient::plugInStartLabelSubtitle(const String& mimeType) const
176{
177    if (!m_client.createPlugInStartLabelSubtitle)
178        return String();
179
180    RefPtr<API::String> subtitle = adoptRef(toImpl(m_client.createPlugInStartLabelSubtitle(toAPI(mimeType.impl()), m_client.base.clientInfo)));
181    return subtitle ? subtitle->string() : String();
182}
183
184String InjectedBundlePageUIClient::plugInExtraStyleSheet() const
185{
186    if (!m_client.createPlugInExtraStyleSheet)
187        return String();
188
189    RefPtr<API::String> styleSheet = adoptRef(toImpl(m_client.createPlugInExtraStyleSheet(m_client.base.clientInfo)));
190    return styleSheet ? styleSheet->string() : String();
191}
192
193String InjectedBundlePageUIClient::plugInExtraScript() const
194{
195    if (!m_client.createPlugInExtraScript)
196        return String();
197
198    RefPtr<API::String> script = adoptRef(toImpl(m_client.createPlugInExtraScript(m_client.base.clientInfo)));
199    return script ? script->string() : String();
200}
201
202} // namespace WebKit
203