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