summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-25 20:44:08 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-25 20:44:08 +0000
commitbd17b70920f5a327eada613ef32e501b043cad51 (patch)
tree7d6deb4e24373ceb2ea9affb9fdd9dab5a1f6482 /chrome/browser
parenta12f3755769dea04a41785657c37dc90b443caaf (diff)
downloadchromium_src-bd17b70920f5a327eada613ef32e501b043cad51.zip
chromium_src-bd17b70920f5a327eada613ef32e501b043cad51.tar.gz
chromium_src-bd17b70920f5a327eada613ef32e501b043cad51.tar.bz2
Ports SpellChecker to use FilePaths instead of wstring.
by Rohit Rao (rohitrao@google.com) issue: http://codereview.chromium.org/28013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10374 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/profile.cc4
-rw-r--r--chrome/browser/spellcheck_unittest.cc37
-rw-r--r--chrome/browser/spellchecker.cc57
-rw-r--r--chrome/browser/spellchecker.h28
4 files changed, 60 insertions, 66 deletions
diff --git a/chrome/browser/profile.cc b/chrome/browser/profile.cc
index e3fe08f..b2ad2f4 100644
--- a/chrome/browser/profile.cc
+++ b/chrome/browser/profile.cc
@@ -758,14 +758,14 @@ void ProfileImpl::InitializeSpellChecker(bool need_to_broadcast) {
bool enable_spellcheck = prefs->GetBoolean(prefs::kEnableSpellCheck);
if (enable_spellcheck) {
- std::wstring dict_dir;
+ FilePath dict_dir;
PathService::Get(chrome::DIR_APP_DICTIONARIES, &dict_dir);
// Note that, as the object pointed to by previously by spellchecker_
// is being deleted in the io thread, the spellchecker_ can be made to point
// to a new object (RE-initialized) in parallel in this UI thread.
spellchecker_ = new SpellChecker(dict_dir,
prefs->GetString(prefs::kSpellCheckDictionary), GetRequestContext(),
- std::wstring());
+ FilePath());
spellchecker_->AddRef(); // Manual refcounting.
} else {
spellchecker_ = NULL;
diff --git a/chrome/browser/spellcheck_unittest.cc b/chrome/browser/spellcheck_unittest.cc
index 745cd6e..d0fa608 100644
--- a/chrome/browser/spellcheck_unittest.cc
+++ b/chrome/browser/spellcheck_unittest.cc
@@ -17,7 +17,8 @@ class SpellCheckTest : public testing::Test {
MessageLoop message_loop_;
};
-const std::wstring kTempCustomDictionaryFile(L"temp_custom_dictionary.txt");
+const FilePath::CharType kTempCustomDictionaryFile[] =
+ FILE_PATH_LITERAL("temp_custom_dictionary.txt");
} // namespace
@@ -249,12 +250,12 @@ TEST_F(SpellCheckTest, SpellCheckStrings_EN_US) {
{L"ifmmp:ifmmp", false, 0, 11},
};
- std::wstring hunspell_directory;
+ FilePath hunspell_directory;
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
- hunspell_directory, L"en-US", NULL, std::wstring()));
+ hunspell_directory, L"en-US", NULL, FilePath()));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
size_t input_length = 0;
@@ -268,9 +269,9 @@ TEST_F(SpellCheckTest, SpellCheckStrings_EN_US) {
&misspelling_start,
&misspelling_length, NULL);
- EXPECT_EQ(result, kTestCases[i].expected_result);
- EXPECT_EQ(misspelling_start, kTestCases[i].misspelling_start);
- EXPECT_EQ(misspelling_length, kTestCases[i].misspelling_length);
+ EXPECT_EQ(kTestCases[i].expected_result, result);
+ EXPECT_EQ(kTestCases[i].misspelling_start, misspelling_start);
+ EXPECT_EQ(kTestCases[i].misspelling_length, misspelling_length);
}
}
@@ -303,12 +304,12 @@ TEST_F(SpellCheckTest, SpellCheckSuggestions_EN_US) {
// TODO (Sidchat): add many more examples.
};
- std::wstring hunspell_directory;
+ FilePath hunspell_directory;
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
- hunspell_directory, L"en-US", NULL, std::wstring()));
+ hunspell_directory, L"en-US", NULL, FilePath()));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
std::vector<std::wstring> suggestions;
@@ -325,7 +326,7 @@ TEST_F(SpellCheckTest, SpellCheckSuggestions_EN_US) {
&suggestions);
// Check for spelling.
- EXPECT_EQ(result, kTestCases[i].expected_result);
+ EXPECT_EQ(kTestCases[i].expected_result, result);
// Check if the suggested words occur.
bool suggested_word_is_present = false;
@@ -351,12 +352,13 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckAddToDictionary_EN_US) {
{L"Googler"},
};
- std::wstring hunspell_directory;
+ FilePath hunspell_directory;
+ FilePath custom_dictionary_file(kTempCustomDictionaryFile);
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
- hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile));
+ hunspell_directory, L"en-US", NULL, custom_dictionary_file));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
// Add the word to spellchecker.
@@ -382,7 +384,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckAddToDictionary_EN_US) {
// Now initialize another spellchecker to see that AddToWord is permanent.
scoped_refptr<SpellChecker> spell_checker_new(new SpellChecker(
- hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile));
+ hunspell_directory, L"en-US", NULL, custom_dictionary_file));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
// Now check whether it is added to Spellchecker.
@@ -405,7 +407,7 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckAddToDictionary_EN_US) {
}
// Remove the temp custom dictionary file.
- file_util::Delete(kTempCustomDictionaryFile, false);
+ file_util::Delete(custom_dictionary_file, false);
}
// SpellChecker should suggest custome words for misspelled words.
@@ -419,19 +421,20 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckSuggestionsAddToDictionary_EN_US) {
{L"Googler"},
};
- std::wstring hunspell_directory;
+ FilePath hunspell_directory;
+ FilePath custom_dictionary_file(kTempCustomDictionaryFile);
ASSERT_TRUE(PathService::Get(chrome::DIR_APP_DICTIONARIES,
&hunspell_directory));
scoped_refptr<SpellChecker> spell_checker(new SpellChecker(
- hunspell_directory, L"en-US", NULL, kTempCustomDictionaryFile));
+ hunspell_directory, L"en-US", NULL, custom_dictionary_file));
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
// Add the word to spellchecker.
spell_checker->AddWord(std::wstring(kTestCases[i].word_to_add));
}
- // Now check to see whether the custom words are suggested for
+ // Now check to see whether the custom words are suggested for
// misspelled but similar words.
static const struct {
// A string to be tested.
@@ -483,5 +486,5 @@ TEST_F(SpellCheckTest, DISABLED_SpellCheckSuggestionsAddToDictionary_EN_US) {
}
// Remove the temp custom dictionary file.
- file_util::Delete(kTempCustomDictionaryFile, false);
+ file_util::Delete(custom_dictionary_file, false);
}
diff --git a/chrome/browser/spellchecker.cc b/chrome/browser/spellchecker.cc
index 4d0cee5..5a8a85f 100644
--- a/chrome/browser/spellchecker.cc
+++ b/chrome/browser/spellchecker.cc
@@ -31,6 +31,7 @@ static const int kMaxSuggestions = 5; // Max number of dictionary suggestions.
namespace {
+// TODO(rohitrao): Convert these constants from wchar_t* to char*.
static const struct {
// The language.
const wchar_t* language;
@@ -242,27 +243,27 @@ class SpellChecker::DictionaryDownloadController
public:
DictionaryDownloadController(
Task* spellchecker_flag_set_task,
- const std::wstring& dic_file_path,
+ const FilePath& dic_file_path,
URLRequestContext* url_request_context,
MessageLoop* ui_loop)
: spellchecker_flag_set_task_(spellchecker_flag_set_task),
url_request_context_(url_request_context),
- download_server_url_(
- L"http://cache.pack.google.com/edgedl/chrome/dict/"),
ui_loop_(ui_loop) {
// Determine dictionary file path and name.
fetcher_.reset(NULL);
- dic_zip_file_path_ = file_util::GetDirectoryFromPath(dic_file_path);
- file_name_ = file_util::GetFilenameFromPath(dic_file_path);
-
- name_of_file_to_download_ = l10n_util::ToLower(file_name_);
+ dic_zip_file_path_ = dic_file_path.DirName();
+ file_name_ = dic_file_path.BaseName();
}
// Save the file in memory buffer to the designated dictionary file.
// returns the number of bytes it could save.
// Invoke this on the file thread.
void StartDownload() {
- GURL url(WideToUTF8(download_server_url_ + name_of_file_to_download_));
+ static const char kDownloadServerUrl[] =
+ "http://cache.pack.google.com/edgedl/chrome/dict/";
+
+ GURL url = GURL(std::string(kDownloadServerUrl) + WideToUTF8(
+ l10n_util::ToLower(file_name_.ToWStringHack())));
fetcher_.reset(new URLFetcher(url, URLFetcher::GET, this));
fetcher_->set_request_context(url_request_context_);
fetcher_->Start();
@@ -271,8 +272,7 @@ class SpellChecker::DictionaryDownloadController
private:
// The file has been downloaded in memory - need to write it down to file.
bool SaveBufferToFile(const std::string& data) {
- std::wstring file_to_write = dic_zip_file_path_;
- file_util::AppendToPath(&file_to_write, file_name_);
+ FilePath file_to_write = dic_zip_file_path_.Append(file_name_);
int num_bytes = data.length();
return file_util::WriteFile(file_to_write, data.data(), num_bytes) ==
num_bytes;
@@ -311,16 +311,10 @@ class SpellChecker::DictionaryDownloadController
scoped_ptr<URLFetcher> fetcher_;
// The file path where both the dic files have to be written locally.
- std::wstring dic_zip_file_path_;
-
- // The name of the file in the server which has to be downloaded.
- std::wstring name_of_file_to_download_;
+ FilePath dic_zip_file_path_;
// The name of the file which has to be stored locally.
- std::wstring file_name_;
-
- // The URL of the server from where the file has to be downloaded.
- const std::wstring download_server_url_;
+ FilePath file_name_;
// this invokes back to io loop when downloading is over.
MessageLoop* ui_loop_;
@@ -335,8 +329,8 @@ void SpellChecker::set_file_is_downloading(bool value) {
// This part of the code is used for spell checking.
// ################################################################
-std::wstring SpellChecker::GetVersionedFileName(const Language& input_language,
- const std::wstring& dict_dir) {
+FilePath SpellChecker::GetVersionedFileName(const Language& input_language,
+ const FilePath& dict_dir) {
// The default version string currently in use.
static const wchar_t kDefaultVersionString[] = L"-1-2";
@@ -379,15 +373,13 @@ std::wstring SpellChecker::GetVersionedFileName(const Language& input_language,
}
}
- std::wstring bdict_file_name(dict_dir);
- file_util::AppendToPath(&bdict_file_name, versioned_bdict_file_name);
- return bdict_file_name;
+ return dict_dir.Append(FilePath::FromWStringHack(versioned_bdict_file_name));
}
-SpellChecker::SpellChecker(const std::wstring& dict_dir,
+SpellChecker::SpellChecker(const FilePath& dict_dir,
const std::wstring& language,
URLRequestContext* request_context,
- const std::wstring& custom_dictionary_file_name)
+ const FilePath& custom_dictionary_file_name)
: custom_dictionary_file_name_(custom_dictionary_file_name),
tried_to_init_(false),
#ifndef NDEBUG
@@ -411,11 +403,10 @@ SpellChecker::SpellChecker(const std::wstring& dict_dir,
// Get the path to the custom dictionary file.
if (custom_dictionary_file_name_.empty()) {
- std::wstring personal_file_directory;
+ FilePath personal_file_directory;
PathService::Get(chrome::DIR_USER_DATA, &personal_file_directory);
- custom_dictionary_file_name_ = personal_file_directory;
- file_util::AppendToPath(&custom_dictionary_file_name_,
- chrome::kCustomDictionaryFileName);
+ custom_dictionary_file_name_ =
+ personal_file_directory.Append(chrome::kCustomDictionaryFileName);
}
// Use this dictionary language as the default one of the
@@ -471,7 +462,7 @@ bool SpellChecker::Initialize() {
// Control has come so far - both files probably exist.
TimeTicks begin_time = TimeTicks::Now();
bdict_file_.reset(new file_util::MemoryMappedFile());
- if (bdict_file_->Initialize(FilePath::FromWStringHack(bdict_file_name_))) {
+ if (bdict_file_->Initialize(bdict_file_name_)) {
hunspell_.reset(new Hunspell(bdict_file_->data(), bdict_file_->length()));
AddCustomWordsToHunspell();
}
@@ -601,9 +592,9 @@ bool SpellChecker::SpellCheckWord(
// dictionary in disc.
class AddWordToCustomDictionaryTask : public Task {
public:
- AddWordToCustomDictionaryTask(const std::wstring& file_name,
+ AddWordToCustomDictionaryTask(const FilePath& file_name,
const std::wstring& word)
- : file_name_(WideToUTF8(file_name)),
+ : file_name_(file_name),
word_(WideToUTF8(word)) {
}
@@ -620,7 +611,7 @@ class AddWordToCustomDictionaryTask : public Task {
file_util::CloseFile(f);
}
- std::string file_name_;
+ FilePath file_name_;
std::string word_;
};
diff --git a/chrome/browser/spellchecker.h b/chrome/browser/spellchecker.h
index 42279c2..ede737a 100644
--- a/chrome/browser/spellchecker.h
+++ b/chrome/browser/spellchecker.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_SPELLCHECKER_H__
-#define CHROME_BROWSER_SPELLCHECKER_H__
+#ifndef CHROME_BROWSER_SPELLCHECKER_H_
+#define CHROME_BROWSER_SPELLCHECKER_H_
#include <vector>
@@ -18,6 +18,7 @@
#include "base/task.h"
#include "unicode/uscript.h"
+class FilePath;
class Hunspell;
class PrefService;
class Profile;
@@ -51,10 +52,10 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// The |custom_dictionary_file_name| should be left blank so that Spellchecker
// can figure out the custom dictionary file. It is non empty only for unit
// testing.
- SpellChecker(const std::wstring& dict_dir,
+ SpellChecker(const FilePath& dict_dir,
const Language& language,
URLRequestContext* request_context,
- const std::wstring& custom_dictionary_file_name);
+ const FilePath& custom_dictionary_file_name);
// Only delete on the I/O thread (see above).
~SpellChecker();
@@ -81,13 +82,13 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// Get SpellChecker supported languages.
static void SpellCheckLanguages(Languages* languages);
- // This function computes a vector of strings which are to be displayed in
+ // 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,
Languages* display_languages);
-
+
// This function returns the corresponding language-region code for the
// spell check language. For example, for hi, it returns hi-IN.
static Language GetSpellCheckLanguageRegion(Language input_language);
@@ -111,7 +112,7 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
void set_file_is_downloading(bool value);
// Memory maps the given .bdic file. On success, it will return true and will
- // place the data and lenght into the given out parameters.
+ // place the data and length into the given out parameters.
bool MapBdictFile(const unsigned char** data, size_t* length);
// Returns whether or not the given word is a contraction of valid words
@@ -120,16 +121,16 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
// Return the file name of the dictionary, including the path and the version
// numbers.
- std::wstring GetVersionedFileName(const Language& language,
- const std::wstring& dict_dir);
+ FilePath GetVersionedFileName(const Language& language,
+ const FilePath& dict_dir);
static Language GetCorrespondingSpellCheckLanguage(const Language& language);
-
+
// Path to the spellchecker file.
- std::wstring bdict_file_name_;
+ FilePath bdict_file_name_;
// Path to the custom dictionary file.
- std::wstring custom_dictionary_file_name_;
+ FilePath custom_dictionary_file_name_;
// We memory-map the BDict file.
scoped_ptr<file_util::MemoryMappedFile> bdict_file_;
@@ -182,5 +183,4 @@ class SpellChecker : public base::RefCountedThreadSafe<SpellChecker> {
DISALLOW_COPY_AND_ASSIGN(SpellChecker);
};
-#endif // #ifndef CHROME_BROWSER_SPELLCHECKER_H__
-
+#endif // #ifndef CHROME_BROWSER_SPELLCHECKER_H_