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"
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
{
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();
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() override
;
59 void OnHunspellDictionaryDownloadBegin() override
;
60 void OnHunspellDictionaryDownloadSuccess() override
;
61 void OnHunspellDictionaryDownloadFailure() override
;
63 // SpellcheckCustomDictionary::Observer implementation.
64 void OnCustomDictionaryLoaded() override
;
65 void OnCustomDictionaryChanged(
66 const SpellcheckCustomDictionary::Change
& dictionary_change
) override
;
69 typedef std::vector
<base::WeakPtr
<SpellcheckHunspellDictionary
>>
72 // Updates the dictionaries that are used for spellchecking.
73 void RefreshDictionaries(bool was_listening
, bool should_listen
);
75 // Returns the hunspell dictionaries that are used for spellchecking.
76 const WeakDictionaries
& GetHunspellDictionaries();
78 // If there are any JavaScript listeners registered for spellcheck events,
79 // ensures we are registered for change notifications. Otherwise, unregisters
81 void StartOrStopListeningForSpellcheckChanges();
83 // Handles the preference for which languages should be used for spellcheck
84 // by resetting the dictionaries and broadcasting an event.
85 void OnSpellcheckDictionariesChanged();
87 // Broadcasts an event with the list of spellcheck dictionary statuses.
88 void BroadcastDictionariesChangedEvent();
90 // Removes observers from hunspell_dictionaries_.
91 void RemoveDictionaryObservers();
93 // The hunspell dictionaries that are used for spellchecking.
94 WeakDictionaries hunspell_dictionaries_
;
96 // The custom dictionary that is used for spellchecking.
97 SpellcheckCustomDictionary
* custom_dictionary_
;
99 content::BrowserContext
* context_
;
101 // True if there are observers listening for spellcheck events.
102 bool listening_spellcheck_
;
104 // True if the profile has finished initializing.
107 content::NotificationRegistrar notification_registrar_
;
109 PrefChangeRegistrar pref_change_registrar_
;
111 DISALLOW_COPY_AND_ASSIGN(LanguageSettingsPrivateDelegate
);
114 } // namespace extensions
116 #endif // CHROME_BROWSER_EXTENSIONS_API_LANGUAGE_SETTINGS_PRIVATE_LANGUAGE_SETTINGS_PRIVATE_DELEGATE_H_