Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / language_dictionary_overlay_handler.cc
blob4ce1839def83fcf5b0aed8b655c9af2a46c72d6e
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"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/values.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/spellchecker/spellcheck_factory.h"
14 #include "chrome/browser/spellchecker/spellcheck_service.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "content/public/browser/web_ui.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace options {
21 LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
22 : overlay_initialized_(false),
23 dictionary_(NULL) {
26 LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() {
29 void LanguageDictionaryOverlayHandler::GetLocalizedValues(
30 base::DictionaryValue* localized_strings) {
31 RegisterTitle(localized_strings,
32 "languageDictionaryOverlayPage",
33 IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE);
34 localized_strings->SetString(
35 "languageDictionaryOverlayTitle",
36 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE));
37 localized_strings->SetString(
38 "languageDictionaryOverlayAddWordLabel",
39 l10n_util::GetStringUTF16(
40 IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL));
41 localized_strings->SetString(
42 "languageDictionaryOverlaySearchPlaceholder",
43 l10n_util::GetStringUTF16(
44 IDS_LANGUAGE_DICTIONARY_OVERLAY_SEARCH_PLACEHOLDER));
45 localized_strings->SetString(
46 "languageDictionaryOverlayNoMatches",
47 l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_NO_MATCHES));
50 void LanguageDictionaryOverlayHandler::RegisterMessages() {
51 web_ui()->RegisterMessageCallback(
52 "addDictionaryWord",
53 base::Bind(&LanguageDictionaryOverlayHandler::AddWord,
54 base::Unretained(this)));
55 web_ui()->RegisterMessageCallback(
56 "removeDictionaryWord",
57 base::Bind(&LanguageDictionaryOverlayHandler::RemoveWord,
58 base::Unretained(this)));
59 web_ui()->RegisterMessageCallback(
60 "refreshDictionaryWords",
61 base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords,
62 base::Unretained(this)));
65 void LanguageDictionaryOverlayHandler::Uninitialize() {
66 overlay_initialized_ = false;
67 if (dictionary_)
68 dictionary_->RemoveObserver(this);
71 void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() {
72 ResetDictionaryWords();
75 void LanguageDictionaryOverlayHandler::OnCustomDictionaryChanged(
76 const SpellcheckCustomDictionary::Change& dictionary_change) {
77 base::ListValue add_words;
78 for (const std::string& word : dictionary_change.to_add()) {
79 add_words.AppendString(word);
82 base::ListValue remove_words;
83 for (const std::string& word : dictionary_change.to_remove()) {
84 remove_words.AppendString(word);
87 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.updateWords",
88 add_words, remove_words);
91 void LanguageDictionaryOverlayHandler::ResetDictionaryWords() {
92 if (!overlay_initialized_)
93 return;
95 if (!dictionary_) {
96 SpellcheckService* service = SpellcheckServiceFactory::GetForContext(
97 Profile::FromWebUI(web_ui()));
98 dictionary_ = service->GetCustomDictionary();
99 dictionary_->AddObserver(this);
102 base::ListValue list_value;
103 for (const std::string& word : dictionary_->GetWords()) {
104 list_value.AppendString(word);
106 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
107 list_value);
110 void LanguageDictionaryOverlayHandler::RefreshWords(
111 const base::ListValue* args) {
112 overlay_initialized_ = true;
113 ResetDictionaryWords();
116 void LanguageDictionaryOverlayHandler::AddWord(const base::ListValue* args) {
117 std::string new_word;
118 if (!args->GetString(0, &new_word) || new_word.empty() || !dictionary_) {
119 NOTREACHED();
120 return;
122 dictionary_->AddWord(new_word);
125 void LanguageDictionaryOverlayHandler::RemoveWord(const base::ListValue* args) {
126 std::string old_word;
127 if (!args->GetString(0, &old_word) || old_word.empty() || !dictionary_) {
128 NOTREACHED();
129 return;
131 dictionary_->RemoveWord(old_word);
134 } // namespace options