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.
4
5#include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h"
6
7#include "base/bind.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"
15
16namespace options {
17
18LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
19    : overlay_initialized_(false),
20      dictionary_(NULL) {
21}
22
23LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() {
24}
25
26void 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));
45}
46
47void LanguageDictionaryOverlayHandler::RegisterMessages() {
48  web_ui()->RegisterMessageCallback(
49      "addDictionaryWord",
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)));
60}
61
62void LanguageDictionaryOverlayHandler::Uninitialize() {
63  overlay_initialized_ = false;
64  if (dictionary_)
65    dictionary_->RemoveObserver(this);
66}
67
68void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() {
69  ResetDictionaryWords();
70}
71
72void LanguageDictionaryOverlayHandler::OnCustomDictionaryChanged(
73    const SpellcheckCustomDictionary::Change& dictionary_change) {
74  ListValue add_words;
75  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",
79                                   add_words,
80                                   remove_words);
81}
82
83void LanguageDictionaryOverlayHandler::ResetDictionaryWords() {
84  if (!overlay_initialized_)
85    return;
86
87  if (!dictionary_) {
88    dictionary_ = SpellcheckServiceFactory::GetForProfile(
89        Profile::FromWebUI(web_ui()))->GetCustomDictionary();
90    dictionary_->AddObserver(this);
91  }
92
93  ListValue list_value;
94  const chrome::spellcheck_common::WordSet& words = dictionary_->GetWords();
95  for (chrome::spellcheck_common::WordSet::const_iterator it = words.begin();
96       it != words.end(); ++it) {
97    list_value.AppendString(*it);
98  }
99  web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
100                                   list_value);
101}
102
103void LanguageDictionaryOverlayHandler::RefreshWords(const ListValue* args) {
104  overlay_initialized_ = true;
105  ResetDictionaryWords();
106}
107
108void LanguageDictionaryOverlayHandler::AddWord(const ListValue* args) {
109  std::string new_word;
110  if (!args->GetString(0, &new_word) || new_word.empty() || !dictionary_) {
111    NOTREACHED();
112    return;
113  }
114  dictionary_->AddWord(new_word);
115}
116
117void LanguageDictionaryOverlayHandler::RemoveWord(const ListValue* args) {
118  std::string old_word;
119  if (!args->GetString(0, &old_word) || old_word.empty() || !dictionary_) {
120    NOTREACHED();
121    return;
122  }
123  dictionary_->RemoveWord(old_word);
124}
125
126}  // namespace options
127