1/*
2 * Copyright (C) 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#include "config.h"
27#include "WebTextCheckerClient.h"
28
29#include "ImmutableArray.h"
30#include "WKAPICast.h"
31#include "WKSharedAPICast.h"
32#include "WebGrammarDetail.h"
33#include "WebPageProxy.h"
34#include <wtf/text/WTFString.h>
35
36namespace WebKit {
37
38bool WebTextCheckerClient::continuousSpellCheckingAllowed()
39{
40    if (!m_client.continuousSpellCheckingAllowed)
41        return false;
42
43    return m_client.continuousSpellCheckingAllowed(m_client.clientInfo);
44}
45
46bool WebTextCheckerClient::continuousSpellCheckingEnabled()
47{
48    if (!m_client.continuousSpellCheckingEnabled)
49        return false;
50
51    return m_client.continuousSpellCheckingEnabled(m_client.clientInfo);
52}
53
54void WebTextCheckerClient::setContinuousSpellCheckingEnabled(bool enabled)
55{
56    if (!m_client.setContinuousSpellCheckingEnabled)
57        return;
58
59    m_client.setContinuousSpellCheckingEnabled(enabled, m_client.clientInfo);
60}
61
62bool WebTextCheckerClient::grammarCheckingEnabled()
63{
64    if (!m_client.grammarCheckingEnabled)
65        return false;
66
67    return m_client.grammarCheckingEnabled(m_client.clientInfo);
68}
69
70void WebTextCheckerClient::setGrammarCheckingEnabled(bool enabled)
71{
72    if (!m_client.setGrammarCheckingEnabled)
73        return;
74
75    m_client.setGrammarCheckingEnabled(enabled, m_client.clientInfo);
76}
77
78uint64_t WebTextCheckerClient::uniqueSpellDocumentTag(WebPageProxy* page)
79{
80    if (!m_client.uniqueSpellDocumentTag)
81        return 0;
82
83    return m_client.uniqueSpellDocumentTag(toAPI(page), m_client.clientInfo);
84}
85
86void WebTextCheckerClient::closeSpellDocumentWithTag(uint64_t tag)
87{
88    if (!m_client.closeSpellDocumentWithTag)
89        return;
90
91    m_client.closeSpellDocumentWithTag(tag, m_client.clientInfo);
92}
93
94void WebTextCheckerClient::checkSpellingOfString(uint64_t tag, const String& text, int32_t& misspellingLocation, int32_t& misspellingLength)
95{
96    if (!m_client.checkSpellingOfString)
97        return;
98
99    m_client.checkSpellingOfString(tag, toAPI(text.impl()), &misspellingLocation, &misspellingLength, m_client.clientInfo);
100}
101
102void WebTextCheckerClient::checkGrammarOfString(uint64_t tag, const String& text, Vector<WebCore::GrammarDetail>& grammarDetails, int32_t& badGrammarLocation, int32_t& badGrammarLength)
103{
104    if (!m_client.checkGrammarOfString)
105        return;
106
107    WKArrayRef wkGrammarDetailsRef = 0;
108    m_client.checkGrammarOfString(tag, toAPI(text.impl()), &wkGrammarDetailsRef, &badGrammarLocation, &badGrammarLength, m_client.clientInfo);
109
110    RefPtr<ImmutableArray> wkGrammarDetails = adoptRef(toImpl(wkGrammarDetailsRef));
111    size_t numGrammarDetails = wkGrammarDetails->size();
112    for (size_t i = 0; i < numGrammarDetails; ++i)
113        grammarDetails.append(wkGrammarDetails->at<WebGrammarDetail>(i)->grammarDetail());
114}
115
116bool WebTextCheckerClient::spellingUIIsShowing()
117{
118    if (!m_client.spellingUIIsShowing)
119        return false;
120
121    return m_client.spellingUIIsShowing(m_client.clientInfo);
122}
123
124void WebTextCheckerClient::toggleSpellingUIIsShowing()
125{
126    if (!m_client.toggleSpellingUIIsShowing)
127        return;
128
129    return m_client.toggleSpellingUIIsShowing(m_client.clientInfo);
130}
131
132void WebTextCheckerClient::updateSpellingUIWithMisspelledWord(uint64_t tag, const String& misspelledWord)
133{
134    if (!m_client.updateSpellingUIWithMisspelledWord)
135        return;
136
137    m_client.updateSpellingUIWithMisspelledWord(tag, toAPI(misspelledWord.impl()), m_client.clientInfo);
138}
139
140void WebTextCheckerClient::updateSpellingUIWithGrammarString(uint64_t tag, const String& badGrammarPhrase, const WebCore::GrammarDetail& grammarDetail)
141{
142    if (!m_client.updateSpellingUIWithGrammarString)
143        return;
144
145    m_client.updateSpellingUIWithGrammarString(tag, toAPI(badGrammarPhrase.impl()), toAPI(grammarDetail), m_client.clientInfo);
146}
147
148void WebTextCheckerClient::guessesForWord(uint64_t tag, const String& word, Vector<String>& guesses)
149{
150    if (!m_client.guessesForWord)
151        return;
152
153    RefPtr<ImmutableArray> wkGuesses = adoptRef(toImpl(m_client.guessesForWord(tag, toAPI(word.impl()), m_client.clientInfo)));
154    size_t numGuesses = wkGuesses->size();
155    for (size_t i = 0; i < numGuesses; ++i)
156        guesses.append(wkGuesses->at<WebString>(i)->string());
157}
158
159void WebTextCheckerClient::learnWord(uint64_t tag, const String& word)
160{
161    if (!m_client.learnWord)
162        return;
163
164    m_client.learnWord(tag, toAPI(word.impl()), m_client.clientInfo);
165}
166
167void WebTextCheckerClient::ignoreWord(uint64_t tag, const String& word)
168{
169    if (!m_client.ignoreWord)
170        return;
171
172    m_client.ignoreWord(tag, toAPI(word.impl()), m_client.clientInfo);
173}
174
175} // namespace WebKit
176