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 "chrome/grit/generated_resources.h" 13 #include "content/public/browser/web_ui.h" 14 #include "ui/base/l10n/l10n_util.h" 15 16 namespace options { 17 18 LanguageDictionaryOverlayHandler::LanguageDictionaryOverlayHandler() 19 : overlay_initialized_(false), 20 dictionary_(NULL) { 21 } 22 23 LanguageDictionaryOverlayHandler::~LanguageDictionaryOverlayHandler() { 24 } 25 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)); 45 } 46 47 void 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 62 void LanguageDictionaryOverlayHandler::Uninitialize() { 63 overlay_initialized_ = false; 64 if (dictionary_) 65 dictionary_->RemoveObserver(this); 66 } 67 68 void LanguageDictionaryOverlayHandler::OnCustomDictionaryLoaded() { 69 ResetDictionaryWords(); 70 } 71 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", 79 add_words, 80 remove_words); 81 } 82 83 void LanguageDictionaryOverlayHandler::ResetDictionaryWords() { 84 if (!overlay_initialized_) 85 return; 86 87 if (!dictionary_) { 88 SpellcheckService* service = SpellcheckServiceFactory::GetForContext( 89 Profile::FromWebUI(web_ui())); 90 dictionary_ = service->GetCustomDictionary(); 91 dictionary_->AddObserver(this); 92 } 93 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); 99 } 100 web_ui()->CallJavascriptFunction("EditDictionaryOverlay.setWordList", 101 list_value); 102 } 103 104 void LanguageDictionaryOverlayHandler::RefreshWords( 105 const base::ListValue* args) { 106 overlay_initialized_ = true; 107 ResetDictionaryWords(); 108 } 109 110 void LanguageDictionaryOverlayHandler::AddWord(const base::ListValue* args) { 111 std::string new_word; 112 if (!args->GetString(0, &new_word) || new_word.empty() || !dictionary_) { 113 NOTREACHED(); 114 return; 115 } 116 dictionary_->AddWord(new_word); 117 } 118 119 void LanguageDictionaryOverlayHandler::RemoveWord(const base::ListValue* args) { 120 std::string old_word; 121 if (!args->GetString(0, &old_word) || old_word.empty() || !dictionary_) { 122 NOTREACHED(); 123 return; 124 } 125 dictionary_->RemoveWord(old_word); 126 } 127 128 } // namespace options 129