chromium/chrome/browser/spellchecker/spell_check_host_chrome_impl.cc

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/spellchecker/spell_check_host_chrome_impl.h"

#include "base/functional/bind.h"
#include "base/no_destructor.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "components/spellcheck/browser/spellcheck_host_metrics.h"
#include "components/spellcheck/browser/spellcheck_platform.h"
#include "components/spellcheck/common/spellcheck_features.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"

#if BUILDFLAG(USE_BROWSER_SPELLCHECKER) && BUILDFLAG(ENABLE_SPELLING_SERVICE)
#include "chrome/browser/spellchecker/spelling_request.h"
#endif
namespace {

SpellCheckHostChromeImpl::Binder& GetSpellCheckHostBinderOverride() {}

}  // namespace

SpellCheckHostChromeImpl::SpellCheckHostChromeImpl(int render_process_id)
    :{}

SpellCheckHostChromeImpl::~SpellCheckHostChromeImpl() {}

// static
void SpellCheckHostChromeImpl::Create(
    int render_process_id,
    mojo::PendingReceiver<spellcheck::mojom::SpellCheckHost> receiver) {}

// static
void SpellCheckHostChromeImpl::OverrideBinderForTesting(Binder binder) {}

void SpellCheckHostChromeImpl::NotifyChecked(const std::u16string& word,
                                             bool misspelled) {}

#if BUILDFLAG(USE_RENDERER_SPELLCHECKER)
void SpellCheckHostChromeImpl::CallSpellingService(
    const std::u16string& text,
    CallSpellingServiceCallback callback) {}

void SpellCheckHostChromeImpl::CallSpellingServiceDone(
    CallSpellingServiceCallback callback,
    bool success,
    const std::u16string& text,
    const std::vector<SpellCheckResult>& service_results) const {}

// static
std::vector<SpellCheckResult> SpellCheckHostChromeImpl::FilterCustomWordResults(
    const std::string& text,
    const SpellcheckCustomDictionary& custom_dictionary,
    const std::vector<SpellCheckResult>& service_results) {}
#endif  // BUILDFLAG(USE_RENDERER_SPELLCHECKER)

#if BUILDFLAG(USE_BROWSER_SPELLCHECKER) && BUILDFLAG(ENABLE_SPELLING_SERVICE)
void SpellCheckHostChromeImpl::CheckSpelling(const std::u16string& word,
                                             CheckSpellingCallback callback) {
  bool correct = spellcheck_platform::CheckSpelling(word, document_tag_);
  std::move(callback).Run(correct);
}

void SpellCheckHostChromeImpl::FillSuggestionList(
    const std::u16string& word,
    FillSuggestionListCallback callback) {
  std::vector<std::u16string> suggestions;
  spellcheck_platform::FillSuggestionList(word, &suggestions);
  std::move(callback).Run(suggestions);
}

void SpellCheckHostChromeImpl::RequestTextCheck(
    const std::u16string& text,
    RequestTextCheckCallback callback) {
  DCHECK(!text.empty());
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // Initialize the spellcheck service if needed. The service will send the
  // language code for text breaking to the renderer. (Text breaking is required
  // for the context menu to show spelling suggestions.) Initialization must
  // happen on UI thread.
  SpellcheckService* spellcheck = GetSpellcheckService();

  if (!spellcheck) {  // Teardown.
    std::move(callback).Run({});
    return;
  }

  // OK to store unretained |this| in a |SpellingRequest| owned by |this|.
  requests_.insert(std::make_unique<SpellingRequest>(
      spellcheck->platform_spell_checker(), &client_, text, render_process_id_,
      document_tag_, std::move(callback),
      base::BindOnce(&SpellCheckHostChromeImpl::OnRequestFinished,
                     base::Unretained(this))));
}

#if BUILDFLAG(IS_WIN)
void SpellCheckHostChromeImpl::InitializeDictionaries(
    InitializeDictionariesCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (base::FeatureList::IsEnabled(
          spellcheck::kWinDelaySpellcheckServiceInit)) {
    // Initialize the spellcheck service if needed. Initialization must
    // happen on UI thread.
    SpellcheckService* spellcheck = GetSpellcheckService();

    if (!spellcheck) {  // Teardown.
      std::move(callback).Run(/*dictionaries=*/{}, /*custom_words=*/{},
                              /*enable=*/false);
      return;
    }

    dictionaries_loaded_callback_ = std::move(callback);

    spellcheck->InitializeDictionaries(
        base::BindOnce(&SpellCheckHostChromeImpl::OnDictionariesInitialized,
                       weak_factory_.GetWeakPtr()));
    return;
  }

  NOTREACHED_IN_MIGRATION();
  std::move(callback).Run(/*dictionaries=*/{}, /*custom_words=*/{},
                          /*enable=*/false);
}

void SpellCheckHostChromeImpl::OnDictionariesInitialized() {
  DCHECK(dictionaries_loaded_callback_);
  SpellcheckService* spellcheck = GetSpellcheckService();

  if (!spellcheck) {  // Teardown.
    std::move(dictionaries_loaded_callback_)
        .Run(/*dictionaries=*/{}, /*custom_words=*/{},
             /*enable=*/false);
    return;
  }

  const bool enable = spellcheck->IsSpellcheckEnabled();

  std::vector<spellcheck::mojom::SpellCheckBDictLanguagePtr> dictionaries;
  std::vector<std::string> custom_words;
  if (enable) {
    for (const auto& hunspell_dictionary :
         spellcheck->GetHunspellDictionaries()) {
      dictionaries.push_back(spellcheck::mojom::SpellCheckBDictLanguage::New(
          hunspell_dictionary->GetDictionaryFile().Duplicate(),
          hunspell_dictionary->GetLanguage()));
    }

    SpellcheckCustomDictionary* custom_dictionary =
        spellcheck->GetCustomDictionary();
    custom_words.assign(custom_dictionary->GetWords().begin(),
                        custom_dictionary->GetWords().end());
  }

  std::move(dictionaries_loaded_callback_)
      .Run(std::move(dictionaries), custom_words, enable);
}
#endif  // BUILDFLAG(IS_WIN)

void SpellCheckHostChromeImpl::OnRequestFinished(SpellingRequest* request) {
  auto iterator = requests_.find(request);
  requests_.erase(iterator);
}

// static
void SpellCheckHostChromeImpl::CombineResultsForTesting(
    std::vector<SpellCheckResult>* remote_results,
    const std::vector<SpellCheckResult>& local_results) {
  SpellingRequest::CombineResults(remote_results, local_results);
}
#endif  //  BUILDFLAG(USE_BROWSER_SPELLCHECKER) &&
        //  BUILDFLAG(ENABLE_SPELLING_SERVICE)

SpellcheckService* SpellCheckHostChromeImpl::GetSpellcheckService() const {}