1 // Copyright (c) 2012 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 #include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h"
8 #include "base/values.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/spellchecker/spellcheck_factory.h"
11 #include "chrome/browser/spellchecker/spellcheck_service.h"
12 #include "content/public/browser/web_ui.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
18 LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
19 : overlay_initialized_(false),
23 LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() {
26 void LanguageDictionaryOverlayHandler::GetLocalizedValues(
27 base::DictionaryValue
* localized_strings
) {
28 RegisterTitle(localized_strings
,
29 "languageDictionaryOverlayPage",
30 IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE
);
31 localized_strings
->SetString(
32 "languageDictionaryOverlayTitle",
33 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE
));
34 localized_strings
->SetString(
35 "languageDictionaryOverlayAddWordLabel",
36 l10n_util::GetStringUTF16(
37 IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL
));
38 localized_strings
->SetString(
39 "languageDictionaryOverlaySearchPlaceholder",
40 l10n_util::GetStringUTF16(
41 IDS_LANGUAGE_DICTIONARY_OVERLAY_SEARCH_PLACEHOLDER
));
42 localized_strings
->SetString(
43 "languageDictionaryOverlayNoMatches",
44 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_NO_MATCHES
));
47 void LanguageDictionaryOverlayHandler::RegisterMessages() {
48 web_ui()->RegisterMessageCallback(
50 base::Bind(&LanguageDictionaryOverlayHandler::AddWord
,
51 base::Unretained(this)));
52 web_ui()->RegisterMessageCallback(
53 "removeDictionaryWord",
54 base::Bind(&LanguageDictionaryOverlayHandler::RemoveWord
,
55 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback(
57 "refreshDictionaryWords",
58 base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords
,
59 base::Unretained(this)));
62 void LanguageDictionaryOverlayHandler::Uninitialize() {
63 overlay_initialized_
= false;
65 dictionary_
->RemoveObserver(this);
68 void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() {
69 ResetDictionaryWords();
72 void LanguageDictionaryOverlayHandler::OnCustomDictionaryChanged(
73 const SpellcheckCustomDictionary::Change
& dictionary_change
) {
74 base::ListValue add_words
;
75 base::ListValue remove_words
;
76 add_words
.AppendStrings(dictionary_change
.to_add());
77 remove_words
.AppendStrings(dictionary_change
.to_remove());
78 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.updateWords",
83 void LanguageDictionaryOverlayHandler::ResetDictionaryWords() {
84 if (!overlay_initialized_
)
88 SpellcheckService
* service
= SpellcheckServiceFactory::GetForContext(
89 Profile::FromWebUI(web_ui()));
90 dictionary_
= service
->GetCustomDictionary();
91 dictionary_
->AddObserver(this);
94 base::ListValue list_value
;
95 const chrome::spellcheck_common::WordSet
& words
= dictionary_
->GetWords();
96 for (chrome::spellcheck_common::WordSet::const_iterator it
= words
.begin();
97 it
!= words
.end(); ++it
) {
98 list_value
.AppendString(*it
);
100 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
104 void LanguageDictionaryOverlayHandler::RefreshWords(
105 const base::ListValue
* args
) {
106 overlay_initialized_
= true;
107 ResetDictionaryWords();
110 void LanguageDictionaryOverlayHandler::AddWord(const base::ListValue
* args
) {
111 std::string new_word
;
112 if (!args
->GetString(0, &new_word
) || new_word
.empty() || !dictionary_
) {
116 dictionary_
->AddWord(new_word
);
119 void LanguageDictionaryOverlayHandler::RemoveWord(const base::ListValue
* args
) {
120 std::string old_word
;
121 if (!args
->GetString(0, &old_word
) || old_word
.empty() || !dictionary_
) {
125 dictionary_
->RemoveWord(old_word
);
128 } // namespace options