Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / language_settings_private / language_settings_private_delegate.h
blob9a15ffc8a2bf26d0809957fb4115c5c5418da878
1 // Copyright 2015 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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_
8 #include "base/memory/scoped_vector.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/prefs/pref_change_registrar.h"
11 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h"
12 #include "chrome/browser/spellchecker/spellcheck_hunspell_dictionary.h"
13 #include "chrome/common/extensions/api/language_settings_private.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/event_router.h"
19 namespace content {
20 class BrowserContext;
23 namespace extensions {
25 // Observes language settings and routes changes as events to listeners of the
26 // languageSettingsPrivate API.
27 class LanguageSettingsPrivateDelegate
28 : public KeyedService,
29 public EventRouter::Observer,
30 public content::NotificationObserver,
31 public SpellcheckHunspellDictionary::Observer,
32 public SpellcheckCustomDictionary::Observer {
33 public:
34 static LanguageSettingsPrivateDelegate* Create(
35 content::BrowserContext* browser_context);
36 ~LanguageSettingsPrivateDelegate() override;
38 // Returns the languages and statuses of the enabled spellcheck dictionaries.
39 ScopedVector<api::language_settings_private::SpellcheckDictionaryStatus>
40 GetHunspellDictionaryStatuses();
42 protected:
43 explicit LanguageSettingsPrivateDelegate(content::BrowserContext* context);
45 // KeyedService implementation.
46 void Shutdown() override;
48 // EventRouter::Observer implementation.
49 void OnListenerAdded(const EventListenerInfo& details) override;
50 void OnListenerRemoved(const EventListenerInfo& details) override;
52 // content::NotificationObserver implementation.
53 void Observe(int type,
54 const content::NotificationSource& source,
55 const content::NotificationDetails& details) override;
57 // SpellcheckHunspellDictionary::Observer implementation.
58 void OnHunspellDictionaryInitialized(const std::string& language) override;
59 void OnHunspellDictionaryDownloadBegin(const std::string& language) override;
60 void OnHunspellDictionaryDownloadSuccess(
61 const std::string& language) override;
62 void OnHunspellDictionaryDownloadFailure(
63 const std::string& language) override;
65 // SpellcheckCustomDictionary::Observer implementation.
66 void OnCustomDictionaryLoaded() override;
67 void OnCustomDictionaryChanged(
68 const SpellcheckCustomDictionary::Change& dictionary_change) override;
70 private:
71 typedef std::vector<base::WeakPtr<SpellcheckHunspellDictionary>>
72 WeakDictionaries;
74 // Updates the dictionaries that are used for spellchecking.
75 void RefreshDictionaries(bool was_listening, bool should_listen);
77 // Returns the hunspell dictionaries that are used for spellchecking.
78 const WeakDictionaries& GetHunspellDictionaries();
80 // If there are any JavaScript listeners registered for spellcheck events,
81 // ensures we are registered for change notifications. Otherwise, unregisters
82 // any observers.
83 void StartOrStopListeningForSpellcheckChanges();
85 // Handles the preference for which languages should be used for spellcheck
86 // by resetting the dictionaries and broadcasting an event.
87 void OnSpellcheckDictionariesChanged();
89 // Broadcasts an event with the list of spellcheck dictionary statuses.
90 void BroadcastDictionariesChangedEvent();
92 // Removes observers from hunspell_dictionaries_.
93 void RemoveDictionaryObservers();
95 // The hunspell dictionaries that are used for spellchecking.
96 WeakDictionaries hunspell_dictionaries_;
98 // The custom dictionary that is used for spellchecking.
99 SpellcheckCustomDictionary* custom_dictionary_;
101 content::BrowserContext* context_;
103 // True if there are observers listening for spellcheck events.
104 bool listening_spellcheck_;
106 // True if the profile has finished initializing.
107 bool profile_added_;
109 content::NotificationRegistrar notification_registrar_;
111 PrefChangeRegistrar pref_change_registrar_;
113 DISALLOW_COPY_AND_ASSIGN(LanguageSettingsPrivateDelegate);
116 } // namespace extensions
118 #endif // CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_