1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/spellcheck/browser/spellcheck_platform.h"
6 
7 #include <string>
8 
9 #include "base/callback.h"
10 #include "base/no_destructor.h"
11 #include "base/task/post_task.h"
12 #include "base/threading/thread_task_runner_handle.h"
13 #include "build/build_config.h"
14 #include "components/spellcheck/browser/windows_spell_checker.h"
15 #include "components/spellcheck/common/spellcheck_common.h"
16 #include "components/spellcheck/common/spellcheck_features.h"
17 #include "components/spellcheck/spellcheck_buildflags.h"
18 
19 class PlatformSpellChecker;
20 
21 namespace spellcheck_platform {
22 
SpellCheckerAvailable()23 bool SpellCheckerAvailable() {
24   return true;
25 }
26 
PlatformSupportsLanguage(PlatformSpellChecker * spell_checker_instance,const std::string & lang_tag,base::OnceCallback<void (bool)> callback)27 void PlatformSupportsLanguage(PlatformSpellChecker* spell_checker_instance,
28                               const std::string& lang_tag,
29                               base::OnceCallback<void(bool)> callback) {
30   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
31       ->IsLanguageSupported(lang_tag, std::move(callback));
32 }
33 
SetLanguage(PlatformSpellChecker * spell_checker_instance,const std::string & lang_to_set,base::OnceCallback<void (bool)> callback)34 void SetLanguage(PlatformSpellChecker* spell_checker_instance,
35                  const std::string& lang_to_set,
36                  base::OnceCallback<void(bool)> callback) {
37   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
38       ->CreateSpellChecker(lang_to_set, std::move(callback));
39 }
40 
DisableLanguage(PlatformSpellChecker * spell_checker_instance,const std::string & lang_to_disable)41 void DisableLanguage(PlatformSpellChecker* spell_checker_instance,
42                      const std::string& lang_to_disable) {
43   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
44       ->DisableSpellChecker(lang_to_disable);
45 }
46 
CheckSpelling(const base::string16 & word_to_check,int tag)47 bool CheckSpelling(const base::string16& word_to_check, int tag) {
48   return true;  // Not used in the Windows native spell checker.
49 }
50 
FillSuggestionList(const base::string16 & wrong_word,std::vector<base::string16> * optional_suggestions)51 void FillSuggestionList(const base::string16& wrong_word,
52                         std::vector<base::string16>* optional_suggestions) {
53   // Not used in the Windows native spell checker.
54 }
55 
RequestTextCheck(PlatformSpellChecker * spell_checker_instance,int document_tag,const base::string16 & text,TextCheckCompleteCallback callback)56 void RequestTextCheck(PlatformSpellChecker* spell_checker_instance,
57                       int document_tag,
58                       const base::string16& text,
59                       TextCheckCompleteCallback callback) {
60   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
61       ->RequestTextCheck(document_tag, text, std::move(callback));
62 }
63 
64 #if BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER)
GetPerLanguageSuggestions(PlatformSpellChecker * spell_checker_instance,const base::string16 & word,GetSuggestionsCallback callback)65 void GetPerLanguageSuggestions(PlatformSpellChecker* spell_checker_instance,
66                                const base::string16& word,
67                                GetSuggestionsCallback callback) {
68   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
69       ->GetPerLanguageSuggestions(word, std::move(callback));
70 }
71 #endif  // BUILDFLAG(USE_WIN_HYBRID_SPELLCHECKER)
72 
AddWord(PlatformSpellChecker * spell_checker_instance,const base::string16 & word)73 void AddWord(PlatformSpellChecker* spell_checker_instance,
74              const base::string16& word) {
75   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
76       ->AddWordForAllLanguages(word);
77 }
78 
RemoveWord(PlatformSpellChecker * spell_checker_instance,const base::string16 & word)79 void RemoveWord(PlatformSpellChecker* spell_checker_instance,
80                 const base::string16& word) {
81   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
82       ->RemoveWordForAllLanguages(word);
83 }
84 
IgnoreWord(PlatformSpellChecker * spell_checker_instance,const base::string16 & word)85 void IgnoreWord(PlatformSpellChecker* spell_checker_instance,
86                 const base::string16& word) {
87   reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
88       ->IgnoreWordForAllLanguages(word);
89 }
90 
GetAvailableLanguages(std::vector<std::string> * spellcheck_languages)91 void GetAvailableLanguages(std::vector<std::string>* spellcheck_languages) {
92   // Not used in Windows
93 }
94 
GetDocumentTag()95 int GetDocumentTag() {
96   return 1;  // Not used in Windows
97 }
98 
CloseDocumentWithTag(int tag)99 void CloseDocumentWithTag(int tag) {
100   // Not implemented since Windows spellchecker doesn't have this concept
101 }
102 
SpellCheckerProvidesPanel()103 bool SpellCheckerProvidesPanel() {
104   return false;  // Windows doesn't have a spelling panel
105 }
106 
SpellingPanelVisible()107 bool SpellingPanelVisible() {
108   return false;  // Windows doesn't have a spelling panel
109 }
110 
ShowSpellingPanel(bool show)111 void ShowSpellingPanel(bool show) {
112   // Not implemented since Windows doesn't have spelling panel like Mac
113 }
114 
UpdateSpellingPanelWithMisspelledWord(const base::string16 & word)115 void UpdateSpellingPanelWithMisspelledWord(const base::string16& word) {
116   // Not implemented since Windows doesn't have spelling panel like Mac
117 }
118 
RecordChromeLocalesStats(PlatformSpellChecker * spell_checker_instance,const std::vector<std::string> chrome_locales,SpellCheckHostMetrics * metrics)119 void RecordChromeLocalesStats(PlatformSpellChecker* spell_checker_instance,
120                               const std::vector<std::string> chrome_locales,
121                               SpellCheckHostMetrics* metrics) {
122   if (spellcheck::WindowsVersionSupportsSpellchecker()) {
123     reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
124         ->RecordChromeLocalesStats(std::move(chrome_locales), metrics);
125   }
126 }
127 
RecordSpellcheckLocalesStats(PlatformSpellChecker * spell_checker_instance,const std::vector<std::string> spellcheck_locales,SpellCheckHostMetrics * metrics)128 void RecordSpellcheckLocalesStats(
129     PlatformSpellChecker* spell_checker_instance,
130     const std::vector<std::string> spellcheck_locales,
131     SpellCheckHostMetrics* metrics) {
132   if (spellcheck::WindowsVersionSupportsSpellchecker()) {
133     reinterpret_cast<WindowsSpellChecker*>(spell_checker_instance)
134         ->RecordSpellcheckLocalesStats(std::move(spellcheck_locales), metrics);
135   }
136 }
137 
138 }  // namespace spellcheck_platform
139