summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/options/language_dictionary_overlay_handler.cc
blob: 11f17c19e9227a70849259b4995c357f58eecc93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/options/language_dictionary_overlay_handler.h"

#include "base/bind.h"
#include "base/values.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/spellchecker/spellcheck_factory.h"
#include "chrome/browser/spellchecker/spellcheck_service.h"
#include "content/public/browser/web_ui.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace options {

LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler()
    : overlay_initialized_(false),
      dictionary_(NULL) {
}

LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() {
}

void LanguageDictionaryOverlayHandler::GetLocalizedValues(
    base::DictionaryValue* localized_strings) {
  RegisterTitle(localized_strings,
                "languageDictionaryOverlayPage",
                IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE);
  localized_strings->SetString(
      "languageDictionaryOverlayTitle",
      l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_TITLE));
  localized_strings->SetString(
      "languageDictionaryOverlayAddWordLabel",
      l10n_util::GetStringUTF16(
          IDS_LANGUAGE_DICTIONARY_OVERLAY_ADD_WORD_LABEL));
  localized_strings->SetString(
      "languageDictionaryOverlaySearchPlaceholder",
      l10n_util::GetStringUTF16(
          IDS_LANGUAGE_DICTIONARY_OVERLAY_SEARCH_PLACEHOLDER));
  localized_strings->SetString(
      "languageDictionaryOverlayNoMatches",
      l10n_util::GetStringUTF16(IDS_LANGUAGE_DICTIONARY_OVERLAY_NO_MATCHES));
}

void LanguageDictionaryOverlayHandler::RegisterMessages() {
  web_ui()->RegisterMessageCallback(
      "addDictionaryWord",
      base::Bind(&LanguageDictionaryOverlayHandler::AddWord,
                 base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "removeDictionaryWord",
      base::Bind(&LanguageDictionaryOverlayHandler::RemoveWord,
                 base::Unretained(this)));
  web_ui()->RegisterMessageCallback(
      "refreshDictionaryWords",
      base::Bind(&LanguageDictionaryOverlayHandler::RefreshWords,
                 base::Unretained(this)));
}

void LanguageDictionaryOverlayHandler::Uninitialize() {
  overlay_initialized_ = false;
  if (dictionary_)
    dictionary_->RemoveObserver(this);
}

void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() {
  ResetDictionaryWords();
}

void LanguageDictionaryOverlayHandler::OnCustomDictionaryChanged(
    const SpellcheckCustomDictionary::Change& dictionary_change) {
  ListValue add_words;
  ListValue remove_words;
  add_words.AppendStrings(dictionary_change.to_add());
  remove_words.AppendStrings(dictionary_change.to_remove());
  web_ui()->CallJavascriptFunction("EditDictionaryOverlay.updateWords",
                                   add_words,
                                   remove_words);
}

void LanguageDictionaryOverlayHandler::ResetDictionaryWords() {
  if (!overlay_initialized_)
    return;

  if (!dictionary_) {
    dictionary_ = SpellcheckServiceFactory::GetForProfile(
        Profile::FromWebUI(web_ui()))->GetCustomDictionary();
    dictionary_->AddObserver(this);
  }

  ListValue list_value;
  list_value.AppendStrings(dictionary_->GetWords());
  web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList",
                                   list_value);
}

void LanguageDictionaryOverlayHandler::RefreshWords(const ListValue* args) {
  overlay_initialized_ = true;
  ResetDictionaryWords();
}

void LanguageDictionaryOverlayHandler::AddWord(const ListValue* args) {
  std::string new_word;
  if (!args->GetString(0, &new_word) || new_word.empty() || !dictionary_) {
    NOTREACHED();
    return;
  }
  dictionary_->AddWord(new_word);
}

void LanguageDictionaryOverlayHandler::RemoveWord(const ListValue* args) {
  std::string old_word;
  if (!args->GetString(0, &old_word) || old_word.empty() || !dictionary_) {
    NOTREACHED();
    return;
  }
  dictionary_->RemoveWord(old_word);
}

}  // namespace options