summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-11 19:20:26 +0000
committerpkasting@chromium.org <pkasting@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-11 19:20:26 +0000
commit1ffed9387c38f75fe7ff13ec54a3cf893005b103 (patch)
treef87bf6b3ff3fb5b746b380b3cf209df2790c1971
parent1d0d8f176df9f7a9043a825541d016189754a27e (diff)
downloadchromium_src-1ffed9387c38f75fe7ff13ec54a3cf893005b103.zip
chromium_src-1ffed9387c38f75fe7ff13ec54a3cf893005b103.tar.gz
chromium_src-1ffed9387c38f75fe7ff13ec54a3cf893005b103.tar.bz2
Try and make some spellcheck code more obvious by using typedefs.
I am not convinced this patch is a net win. Please share your thoughts on if using a Language/Languages set of types is better or worse than what we're doing now. Review URL: http://codereview.chromium.org/13367 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6807 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/render_view_context_menu.cc10
-rw-r--r--chrome/browser/render_view_context_menu_controller.cc12
-rw-r--r--chrome/browser/spellchecker.cc57
-rw-r--r--chrome/browser/spellchecker.h16
-rw-r--r--chrome/browser/views/options/languages_page_view.cc2
5 files changed, 48 insertions, 49 deletions
diff --git a/chrome/browser/render_view_context_menu.cc b/chrome/browser/render_view_context_menu.cc
index 75bc4d8..9634197 100644
--- a/chrome/browser/render_view_context_menu.cc
+++ b/chrome/browser/render_view_context_menu.cc
@@ -151,15 +151,15 @@ void RenderViewContextMenu::AppendEditableItems() {
l10n_util::GetString(IDS_CONTENT_CONTEXT_SPELLCHECK_MENU));
// Add Spell Check languages to sub menu.
- std::vector<std::wstring> language_vector;
+ SpellChecker::Languages display_languages;
SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(profile_,
- &language_vector);
- DCHECK(language_vector.size() <
+ &display_languages);
+ DCHECK(display_languages.size() <
IDC_SPELLCHECK_LANGUAGES_LAST - IDC_SPELLCHECK_LANGUAGES_FIRST);
const std::wstring app_locale = g_browser_process->GetApplicationLocale();
- for (size_t i = 0; i < language_vector.size(); ++i) {
+ for (size_t i = 0; i < display_languages.size(); ++i) {
std::wstring local_language(l10n_util::GetLocalName(
- language_vector[i], app_locale, true));
+ display_languages[i], app_locale, true));
spellchecker_sub_menu_->AppendMenuItem(
IDC_SPELLCHECK_LANGUAGES_FIRST + i, local_language, RADIO);
}
diff --git a/chrome/browser/render_view_context_menu_controller.cc b/chrome/browser/render_view_context_menu_controller.cc
index cbcef59..186fe0b 100644
--- a/chrome/browser/render_view_context_menu_controller.cc
+++ b/chrome/browser/render_view_context_menu_controller.cc
@@ -209,9 +209,9 @@ bool RenderViewContextMenuController::IsItemChecked(int id) const {
(id >= IDC_SPELLCHECK_LANGUAGES_LAST))
return false;
- std::vector<std::wstring> display_language_vector;
+ SpellChecker::Languages display_languages;
return SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(
- source_web_contents_->profile(), &display_language_vector) ==
+ source_web_contents_->profile(), &display_languages) ==
(id - IDC_SPELLCHECK_LANGUAGES_FIRST);
}
@@ -254,14 +254,14 @@ void RenderViewContextMenuController::ExecuteCommand(int id) {
if (id >= IDC_SPELLCHECK_LANGUAGES_FIRST &&
id < IDC_SPELLCHECK_LANGUAGES_LAST) {
const size_t language_number = id - IDC_SPELLCHECK_LANGUAGES_FIRST;
- std::vector<std::wstring> display_language_vector;
+ SpellChecker::Languages display_languages;
SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(
- source_web_contents_->profile(), &display_language_vector);
- if (language_number < display_language_vector.size()) {
+ source_web_contents_->profile(), &display_languages);
+ if (language_number < display_languages.size()) {
StringPrefMember dictionary_language;
dictionary_language.Init(prefs::kSpellCheckDictionary,
source_web_contents_->profile()->GetPrefs(), NULL);
- dictionary_language.SetValue(display_language_vector[language_number]);
+ dictionary_language.SetValue(display_languages[language_number]);
}
return;
diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc
index aac3692..2598ad3 100644
--- a/chrome/browser/spellchecker.cc
+++ b/chrome/browser/spellchecker.cc
@@ -74,16 +74,16 @@ const wchar_t* const g_supported_spellchecker_languages[] = {
}
-void SpellChecker::SpellCheckLanguages(std::vector<std::wstring>* languages) {
+void SpellChecker::SpellCheckLanguages(Languages* languages) {
for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i)
languages->push_back(g_supported_spellchecker_languages[i]);
}
-std::wstring SpellChecker::GetCorrespondingSpellCheckLanguage(
- const std::wstring& language) {
+SpellChecker::Language SpellChecker::GetCorrespondingSpellCheckLanguage(
+ const Language& language) {
// Look for exact match in the Spell Check language list.
for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) {
- std::wstring spellcheck_language(g_supported_spellchecker_languages[i]);
+ Language spellcheck_language(g_supported_spellchecker_languages[i]);
if (spellcheck_language == language)
return language;
}
@@ -95,57 +95,55 @@ std::wstring SpellChecker::GetCorrespondingSpellCheckLanguage(
// 'az-Latn-AZ' vs 'az-Arab-AZ', either, but we don't use 3-part
// locale ids with a script code in the middle, yet.
// TODO(jungshik): Add a better fallback.
- std::wstring language_part(language, 0, language.find(L'-'));
+ Language language_part(language, 0, language.find(L'-'));
for (int i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) {
- std::wstring spellcheck_language(g_supported_spellchecker_languages[i]);
+ Language spellcheck_language(g_supported_spellchecker_languages[i]);
if (spellcheck_language.substr(0, spellcheck_language.find(L'-')) ==
language_part)
return spellcheck_language;
}
// No match found - return blank.
- return std::wstring();
+ return Language();
}
int SpellChecker::GetSpellCheckLanguagesToDisplayInContextMenu(
Profile* profile,
- std::vector<std::wstring>* display_language_list) {
+ Languages* display_languages) {
StringPrefMember accept_languages_pref;
StringPrefMember dictionary_language_pref;
accept_languages_pref.Init(prefs::kAcceptLanguages, profile->GetPrefs(),
NULL);
dictionary_language_pref.Init(prefs::kSpellCheckDictionary,
profile->GetPrefs(), NULL);
- std::wstring dictionary_language(dictionary_language_pref.GetValue());
+ Language dictionary_language(dictionary_language_pref.GetValue());
// The current dictionary language should be there.
- display_language_list->push_back(dictionary_language);
+ display_languages->push_back(dictionary_language);
// Now scan through the list of accept languages, and find possible mappings
// from this list to the existing list of spell check languages.
- std::vector<std::wstring> accept_language_vector;
- SplitString(accept_languages_pref.GetValue(), L',', &accept_language_vector);
- for (size_t i = 0; i < accept_language_vector.size(); ++i) {
- std::wstring language = GetCorrespondingSpellCheckLanguage(
- accept_language_vector[i]);
+ Languages accept_languages;
+ SplitString(accept_languages_pref.GetValue(), L',', &accept_languages);
+ for (Languages::const_iterator i(accept_languages.begin());
+ i != accept_languages.end(); ++i) {
+ Language language(GetCorrespondingSpellCheckLanguage(*i));
if (!language.empty()) {
// Check for duplication.
- if (std::find(display_language_list->begin(),
- display_language_list->end(), language) ==
- display_language_list->end())
- display_language_list->push_back(language);
+ if (std::find(display_languages->begin(), display_languages->end(),
+ language) == display_languages->end())
+ display_languages->push_back(language);
}
}
// Sort using locale specific sorter.
l10n_util::SortStrings(g_browser_process->GetApplicationLocale(),
- display_language_list);
+ display_languages);
- for (size_t i = 0; i < display_language_list->size(); ++i) {
- if ((*display_language_list)[i] == dictionary_language)
+ for (size_t i = 0; i < display_languages->size(); ++i) {
+ if ((*display_languages)[i] == dictionary_language)
return i;
}
-
return -1;
}
@@ -184,7 +182,7 @@ class UIProxyForIOTask : public Task {
// dictionary if required. This code is included in this file since dictionary
// is an integral part of spellchecker.
-// Design: The spellchecker initializes hunspell_ in the |Initialize()| method.
+// Design: The spellchecker initializes hunspell_ in the Initialize() method.
// This is done using the dictionary file on disk, for example, "en-US.bdic".
// If this file is missing, a |DictionaryDownloadController| object is used to
// download the missing files asynchronously (using URLFetcher) in the file
@@ -240,12 +238,11 @@ class SpellChecker::DictionaryDownloadController
private:
// The file has been downloaded in memory - need to write it down to file.
bool SaveBufferToFile(const std::string& data) {
- const char *file_char = data.c_str();
std::wstring file_to_write = dic_zip_file_path_;
file_util::AppendToPath(&file_to_write, file_name_);
- int save_file = file_util::WriteFile(file_to_write, file_char,
- static_cast<int>(data.length()));
- return (save_file > 0 ? true : false);
+ int num_bytes = data.length();
+ return file_util::WriteFile(file_to_write, data.data(), num_bytes) ==
+ num_bytes;
}
// URLFetcher::Delegate interface.
@@ -262,7 +259,7 @@ class SpellChecker::DictionaryDownloadController
response_code == 401 ||
response_code == 407) {
save_success = SaveBufferToFile(data);
- } // Unsuccessful save is taken care of spellchecker |Initialize|.
+ } // Unsuccessful save is taken care of in SpellChecker::Initialize().
// Set Flag that dictionary is not downloading anymore.
ui_loop_->PostTask(FROM_HERE,
@@ -305,7 +302,7 @@ void SpellChecker::set_file_is_downloading(bool value) {
// This part of the code is used for spell checking.
// ################################################################
-std::wstring SpellChecker::GetVersionedFileName(const std::wstring& language,
+std::wstring SpellChecker::GetVersionedFileName(const Language& language,
const std::wstring& dict_dir) {
// The default version string currently in use.
static const wchar_t kDefaultVersionString[] = L"-1-1";
diff --git a/chrome/browser/spellchecker.h b/chrome/browser/spellchecker.h
index 1283712..45eb73d 100644
--- a/chrome/browser/spellchecker.h
+++ b/chrome/browser/spellchecker.h
@@ -37,6 +37,9 @@ class URLRequestContext;
// deleted on the I/O thread itself.
class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
public:
+ typedef std::wstring Language;
+ typedef std::vector<Language> Languages;
+
// Creates the spellchecker by reading dictionaries from the given directory,
// and defaulting to the given language. Both strings must be provided.
//
@@ -46,7 +49,7 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// can figure out the custom dictionary file. It is non empty only for unit
// testing.
SpellChecker(const std::wstring& dict_dir,
- const std::wstring& language,
+ const Language& language,
URLRequestContext* request_context,
const std::wstring& custom_dictionary_file_name);
@@ -73,14 +76,14 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
void AddWord(const std::wstring& word);
// Get SpellChecker supported languages.
- static void SpellCheckLanguages(std::vector<std::wstring>* languages);
+ static void SpellCheckLanguages(Languages* languages);
// This function computes a vector of strings which are to be displayed in
// the context menu over a text area for changing spell check languages. It
// returns the index of the current spell check language in the vector.
static int GetSpellCheckLanguagesToDisplayInContextMenu(
Profile* profile,
- std::vector<std::wstring>* display_language_list);
+ Languages* display_languages);
private:
// Download dictionary files when required.
@@ -101,15 +104,14 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// Returns whether or not the given word is a contraction of valid words
// (e.g. "word:word").
- bool IsValidContraction(const std::wstring& word);
+ bool IsValidContraction(const Language& word);
// Return the file name of the dictionary, including the path and the version
// numbers.
- std::wstring GetVersionedFileName(const std::wstring& language,
+ std::wstring GetVersionedFileName(const Language& language,
const std::wstring& dict_dir);
- static std::wstring GetCorrespondingSpellCheckLanguage(
- const std::wstring& language);
+ static Language GetCorrespondingSpellCheckLanguage(const Language& language);
// Path to the spellchecker file.
std::wstring bdict_file_name_;
diff --git a/chrome/browser/views/options/languages_page_view.cc b/chrome/browser/views/options/languages_page_view.cc
index eabf1d7..b28d4cf 100644
--- a/chrome/browser/views/options/languages_page_view.cc
+++ b/chrome/browser/views/options/languages_page_view.cc
@@ -615,7 +615,7 @@ void LanguagesPageView::InitControlLayout() {
enable_spellchecking_checkbox_->SetMultiLine(true);
// Determine Locale Codes.
- std::vector<std::wstring> spell_check_languages;
+ SpellChecker::Languages spell_check_languages;
SpellChecker::SpellCheckLanguages(&spell_check_languages);
dictionary_language_model_.reset(new LanguageComboboxModel(profile(),
spell_check_languages));