1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_
#define CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_
#include <string>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
#include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
#include "chrome/common/extensions/api/language_settings_private.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "extensions/browser/event_router.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "ui/base/ime/ash/input_method_manager.h"
#endif
namespace content {
class BrowserContext;
}
namespace extensions {
// Observes language settings and routes changes as events to listeners of the
// languageSettingsPrivate API.
class LanguageSettingsPrivateDelegate
: public KeyedService,
public EventRouter::Observer,
#if BUILDFLAG(IS_CHROMEOS)
public ash::input_method::InputMethodManager::Observer,
#endif // BUILDFLAG(IS_CHROMEOS)
public SpellcheckHunspellDictionary::Observer,
public SpellcheckCustomDictionary::Observer {
public:
static std::unique_ptr<LanguageSettingsPrivateDelegate> Create(
content::BrowserContext* browser_context);
explicit LanguageSettingsPrivateDelegate(content::BrowserContext* context);
LanguageSettingsPrivateDelegate(const LanguageSettingsPrivateDelegate&) =
delete;
LanguageSettingsPrivateDelegate& operator=(
const LanguageSettingsPrivateDelegate&) = delete;
~LanguageSettingsPrivateDelegate() override;
// Returns the languages and statuses of the enabled spellcheck dictionaries.
virtual std::vector<
api::language_settings_private::SpellcheckDictionaryStatus>
GetHunspellDictionaryStatuses();
// Retry downloading the spellcheck dictionary.
virtual void RetryDownloadHunspellDictionary(const std::string& language);
protected:
// KeyedService implementation.
void Shutdown() override;
// EventRouter::Observer implementation.
void OnListenerAdded(const EventListenerInfo& details) override;
void OnListenerRemoved(const EventListenerInfo& details) override;
#if BUILDFLAG(IS_CHROMEOS)
// ash::input_method::InputMethodManager::Observer implementation.
void InputMethodChanged(ash::input_method::InputMethodManager* manager,
Profile* profile,
bool show_message) override;
void OnInputMethodExtensionAdded(const std::string& extension_id) override;
void OnInputMethodExtensionRemoved(const std::string& extension_id) override;
#endif // BUILDFLAG(IS_CHROMEOS)
// SpellcheckHunspellDictionary::Observer implementation.
void OnHunspellDictionaryInitialized(const std::string& language) override;
void OnHunspellDictionaryDownloadBegin(const std::string& language) override;
void OnHunspellDictionaryDownloadSuccess(
const std::string& language) override;
void OnHunspellDictionaryDownloadFailure(
const std::string& language) override;
// SpellcheckCustomDictionary::Observer implementation.
void OnCustomDictionaryLoaded() override;
void OnCustomDictionaryChanged(
const SpellcheckCustomDictionary::Change& dictionary_change) override;
private:
typedef std::vector<base::WeakPtr<SpellcheckHunspellDictionary>>
WeakDictionaries;
// Updates the dictionaries that are used for spellchecking.
void RefreshDictionaries(bool was_listening, bool should_listen);
// Returns the hunspell dictionaries that are used for spellchecking.
const WeakDictionaries& GetHunspellDictionaries();
// If there are any JavaScript listeners registered for spellcheck events,
// ensures we are registered for change notifications. Otherwise, unregisters
// any observers.
void StartOrStopListeningForSpellcheckChanges();
#if BUILDFLAG(IS_CHROMEOS)
// If there are any JavaScript listeners registered for input method events,
// ensures we are registered for change notifications. Otherwise, unregisters
// any observers.
void StartOrStopListeningForInputMethodChanges();
#endif // BUILDFLAG(IS_CHROMEOS)
// Handles the preference for which languages should be used for spellcheck
// by resetting the dictionaries and broadcasting an event.
void OnSpellcheckDictionariesChanged();
// Broadcasts an event with the list of spellcheck dictionary statuses.
void BroadcastDictionariesChangedEvent();
// Removes observers from hunspell_dictionaries_.
void RemoveDictionaryObservers();
// The hunspell dictionaries that are used for spellchecking.
// TODO(aee): Consider replacing with
// |SpellcheckService::GetHunspellDictionaries()|.
WeakDictionaries hunspell_dictionaries_;
// The custom dictionary that is used for spellchecking.
raw_ptr<SpellcheckCustomDictionary> custom_dictionary_;
raw_ptr<content::BrowserContext> context_;
// True if there are observers listening for spellcheck events.
bool listening_spellcheck_;
// True if there are observers listening for input method events.
bool listening_input_method_;
PrefChangeRegistrar pref_change_registrar_;
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_
|