Commit 35a15516 authored by Guillaume Jenkins's avatar Guillaume Jenkins Committed by Commit Bot

[Spellcheck] Move WindowsSpellChecker to its own file

This is the first step towards making the native spell checker class
(WindowsSpellChecker) more testable. Right now, it can't be easily
tested because its life cycle is tied to a base::NoDestructor pointer,
but it holds references to thread pools. So, between tests, the
WindowsSpellChecker is kept alive, but its references to the thread
pools are invalidated.

The next step will be to move the WindowsSpellChecker from a static
base::NoDestructor pointer to the BrowserProcessImpl object, which is
destroyed and recreated for every test.

In addition to the above, the spellcheck_platform_win.cc file was
getting very large, so that's another reason to split it up.

There are no functional change as part of this; only code being
moved around to new files.


Bug: 463364, 1035044
Change-Id: I0b621ad2beba6ed57856dd3cff1910aebebbb710
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2008031Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Guillaume Jenkins <gujen@google.com>
Cr-Commit-Position: refs/heads/master@{#735425}
parent 44d70077
...@@ -32,7 +32,11 @@ source_set("browser") { ...@@ -32,7 +32,11 @@ source_set("browser") {
} }
if (is_win) { if (is_win) {
sources += [ "spellcheck_platform_win.cc" ] sources += [
"spellcheck_platform_win.cc",
"windows_spell_checker.cc",
"windows_spell_checker.h",
]
} }
public_deps = [ public_deps = [
......
This diff is collapsed.
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_SPELLCHECK_BROWSER_WINDOWS_SPELL_CHECKER_H_
#define COMPONENTS_SPELLCHECK_BROWSER_WINDOWS_SPELL_CHECKER_H_
#include <spellcheck.h>
#include <wrl/client.h>
#include <string>
#include "base/callback.h"
#include "build/build_config.h"
#include "components/spellcheck/browser/spellcheck_host_metrics.h"
#include "components/spellcheck/browser/spellcheck_platform.h"
#include "components/spellcheck/spellcheck_buildflags.h"
namespace base {
class SingleThreadTaskRunner;
}
// WindowsSpellChecker class is used to store all the COM objects and
// control their lifetime. The class also provides wrappers for
// ISpellCheckerFactory and ISpellChecker APIs. All COM calls are on the
// background thread.
class WindowsSpellChecker {
public:
WindowsSpellChecker(
const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner> background_task_runner);
WindowsSpellChecker(const WindowsSpellChecker&) = delete;
WindowsSpellChecker& operator=(const WindowsSpellChecker&) = delete;
~WindowsSpellChecker();
void CreateSpellChecker(const std::string& lang_tag,
base::OnceCallback<void(bool)> callback);
void DisableSpellChecker(const std::string& lang_tag);
void RequestTextCheckForAllLanguages(
int document_tag,
const base::string16& text,
spellcheck_platform::TextCheckCompleteCallback callback);
void GetPerLanguageSuggestions(
const base::string16& word,
spellcheck_platform::GetSuggestionsCallback callback);
void AddWordForAllLanguages(const base::string16& word);
void RemoveWordForAllLanguages(const base::string16& word);
void IgnoreWordForAllLanguages(const base::string16& word);
void RecordChromeLocalesStats(const std::vector<std::string> chrome_locales,
SpellCheckHostMetrics* metrics);
void RecordSpellcheckLocalesStats(
const std::vector<std::string> spellcheck_locales,
SpellCheckHostMetrics* metrics);
void IsLanguageSupported(const std::string& lang_tag,
base::OnceCallback<void(bool)> callback);
#if BUILDFLAG(USE_WINDOWS_PREFERRED_LANGUAGES_FOR_SPELLCHECK)
// Retrieve language tags for installed Windows language packs that also have
// spellchecking support.
void RetrieveSupportedWindowsPreferredLanguages(
spellcheck_platform::RetrieveSupportedLanguagesCompleteCallback callback);
#endif // BUILDFLAG(USE_WINDOWS_PREFERRED_LANGUAGES_FOR_SPELLCHECK)
private:
void CreateSpellCheckerFactoryInBackgroundThread();
// Creates ISpellchecker for given language |lang_tag| and run callback with
// the creation result. This function must run on the background thread.
void CreateSpellCheckerWithCallbackInBackgroundThread(
const std::string& lang_tag,
base::OnceCallback<void(bool)> callback);
// Removes the Windows Spellchecker for the given language |lang_tag|. This
// function must run on the background thread.
void DisableSpellCheckerInBackgroundThread(const std::string& lang_tag);
// Request spell checking of string |text| for all available spellchecking
// languages and run callback with spellchecking results. This function must
// run on the background thread.
void RequestTextCheckForAllLanguagesInBackgroundThread(
int document_tag,
const base::string16& text,
spellcheck_platform::TextCheckCompleteCallback callback);
void GetPerLanguageSuggestionsInBackgroundThread(
const base::string16& word,
spellcheck_platform::GetSuggestionsCallback callback);
// Fills the given vector |optional_suggestions| with a number (up to
// kMaxSuggestions) of suggestions for the string |wrong_word| of language
// |lang_tag|. This function must run on the background thread.
void FillSuggestionListInBackgroundThread(
const std::string& lang_tag,
const base::string16& wrong_word,
std::vector<base::string16>* optional_suggestions);
void AddWordForAllLanguagesInBackgroundThread(const base::string16& word);
void RemoveWordForAllLanguagesInBackgroundThread(const base::string16& word);
void IgnoreWordForAllLanguagesInBackgroundThread(const base::string16& word);
// Returns true if a spellchecker is available for the given language
// |lang_tag|. This function must run on the background thread.
bool IsLanguageSupportedInBackgroundThread(const std::string& lang_tag);
// Checks if a spellchecker is available for the given language |lang_tag| and
// posts the result to a callback on the main thread.
void IsLanguageSupportedWithCallbackInBackgroundThread(
const std::string& lang_tag,
base::OnceCallback<void(bool)> callback);
// Returns true if ISpellCheckerFactory has been initialized.
bool IsSpellCheckerFactoryInitialized();
// Returns true if the ISpellChecker has been initialized for given laugnage
// |lang_tag|.
bool SpellCheckerReady(const std::string& lang_tag);
// Returns the ISpellChecker pointer for given language |lang_tag|.
Microsoft::WRL::ComPtr<ISpellChecker> GetSpellChecker(
const std::string& lang_tag);
// Records statistics about spell check support for the user's Chrome locales.
void RecordChromeLocalesStatsInBackgroundThread(
const std::vector<std::string> chrome_locales,
SpellCheckHostMetrics* metrics);
// Records statistics about which spell checker supports which of the user's
// enabled spell check locales.
void RecordSpellcheckLocalesStatsInBackgroundThread(
const std::vector<std::string> spellcheck_locales,
SpellCheckHostMetrics* metrics);
#if BUILDFLAG(USE_WINDOWS_PREFERRED_LANGUAGES_FOR_SPELLCHECK)
// Async retrieval of supported preferred languages.
void RetrieveSupportedWindowsPreferredLanguagesInBackgroundThread(
spellcheck_platform::RetrieveSupportedLanguagesCompleteCallback callback);
#endif // BUILDFLAG(USE_WINDOWS_PREFERRED_LANGUAGES_FOR_SPELLCHECK)
// Sorts the specified locales into 4 buckets of spell check support (both,
// Hunspell only, native only, and none). The results are returned as out
// variables.
LocalesSupportInfo DetermineLocalesSupportInBackgroundThread(
const std::vector<std::string>& locales);
// Spellchecker objects are owned by WindowsSpellChecker class.
Microsoft::WRL::ComPtr<ISpellCheckerFactory> spell_checker_factory_;
std::map<std::string, Microsoft::WRL::ComPtr<ISpellChecker>>
spell_checker_map_;
// |main_task_runner_| is running on the main thread, which is used to post
// task to the main thread from the background thread.
// |background_task_runner_| is running on the background thread, which is
// used to post task to the background thread from main thread.
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> background_task_runner_;
base::WeakPtrFactory<WindowsSpellChecker> weak_ptr_factory_{this};
};
#endif // COMPONENTS_SPELLCHECK_BROWSER_WINDOWS_SPELL_CHECKER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment