diff options
author | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 01:57:42 +0000 |
---|---|---|
committer | groby@chromium.org <groby@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-11-14 01:57:42 +0000 |
commit | f1da29a3f857ace298bed2a5e19a174d58916b75 (patch) | |
tree | 94c4451cbd7cb5b5275746aded95f4b58c23b3d0 /chrome/renderer | |
parent | 5adc1133139a1ad9c0d8e61e0ffbe9cedf765500 (diff) | |
download | chromium_src-f1da29a3f857ace298bed2a5e19a174d58916b75.zip chromium_src-f1da29a3f857ace298bed2a5e19a174d58916b75.tar.gz chromium_src-f1da29a3f857ace298bed2a5e19a174d58916b75.tar.bz2 |
Revert 167563 - [Spellcheck] Removing cruft.
Broken unit test, reverted.
BUG=154918
Review URL: https://codereview.chromium.org/11377045
TBR=groby@chromium.org
Review URL: https://codereview.chromium.org/11365245
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@167569 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
11 files changed, 95 insertions, 85 deletions
diff --git a/chrome/renderer/chrome_content_renderer_client.cc b/chrome/renderer/chrome_content_renderer_client.cc index 2ea0fee..522b9a6 100644 --- a/chrome/renderer/chrome_content_renderer_client.cc +++ b/chrome/renderer/chrome_content_renderer_client.cc @@ -977,20 +977,15 @@ bool ChromeContentRendererClient::CrossesExtensionExtents( should_consider_workaround); } -void ChromeContentRendererClient::SetSpellcheck(SpellCheck* spellcheck) { +void ChromeContentRendererClient::OnPurgeMemory() { + DVLOG(1) << "Resetting spellcheck in renderer client"; RenderThread* thread = RenderThread::Get(); - if (spellcheck_.get() && thread) + if (spellcheck_.get()) thread->RemoveObserver(spellcheck_.get()); - spellcheck_.reset(spellcheck); + spellcheck_.reset(new SpellCheck()); SpellCheckReplacer replacer(spellcheck_.get()); content::RenderView::ForEach(&replacer); - if (thread) - thread->AddObserver(spellcheck_.get()); -} - -void ChromeContentRendererClient::OnPurgeMemory() { - DVLOG(1) << "Resetting spellcheck in renderer client"; - SetSpellcheck(new SpellCheck()); + thread->AddObserver(spellcheck_.get()); } bool ChromeContentRendererClient::IsAdblockInstalled() { diff --git a/chrome/renderer/chrome_content_renderer_client.h b/chrome/renderer/chrome_content_renderer_client.h index 9f5da32..bf34928 100644 --- a/chrome/renderer/chrome_content_renderer_client.h +++ b/chrome/renderer/chrome_content_renderer_client.h @@ -130,10 +130,6 @@ class ChromeContentRendererClient : public content::ContentRendererClient { // For testing. void SetExtensionDispatcher(extensions::Dispatcher* extension_dispatcher); - // Sets a new |spellcheck|. Used for low-mem restart and testing only. - // Takes ownership of |spellcheck|. - void SetSpellcheck(SpellCheck* spellcheck); - // Called in low-memory conditions to dump the memory used by the spellchecker // and start over. void OnPurgeMemory(); diff --git a/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.cc b/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.cc index 8030768..9c3ba9d 100644 --- a/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.cc +++ b/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.cc @@ -13,11 +13,6 @@ SpellingEngine* CreateNativeSpellingEngine() { return new CocoaSpellingEngine(); } -void CocoaSpellingEngine::Init(base::PlatformFile bdict_file, - const std::vector<std::string>&) { - DCHECK(bdict_file == base::kInvalidPlatformFileValue); -} - bool CocoaSpellingEngine::InitializeIfNeeded() { return false; // We never need to initialize. } diff --git a/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.h b/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.h index f150648..1b5183b 100644 --- a/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.h +++ b/chrome/renderer/spellchecker/cocoa_spelling_engine_mac.h @@ -10,8 +10,6 @@ class CocoaSpellingEngine : public SpellingEngine { public: - virtual void Init(base::PlatformFile bdict_file, - const std::vector<std::string>& custom_words) OVERRIDE; virtual bool InitializeIfNeeded() OVERRIDE; virtual bool IsEnabled() OVERRIDE; virtual bool CheckSpelling(const string16& word_to_check, int tag) OVERRIDE; diff --git a/chrome/renderer/spellchecker/hunspell_engine.h b/chrome/renderer/spellchecker/hunspell_engine.h index 6dd7581..0d91b24 100644 --- a/chrome/renderer/spellchecker/hunspell_engine.h +++ b/chrome/renderer/spellchecker/hunspell_engine.h @@ -21,8 +21,8 @@ class HunspellEngine : public SpellingEngine { HunspellEngine(); virtual ~HunspellEngine(); - virtual void Init(base::PlatformFile file, - const std::vector<std::string>& custom_words) OVERRIDE; + void Init(base::PlatformFile file, + const std::vector<std::string>& custom_words); virtual bool InitializeIfNeeded() OVERRIDE; virtual bool IsEnabled() OVERRIDE; diff --git a/chrome/renderer/spellchecker/spellcheck.cc b/chrome/renderer/spellchecker/spellcheck.cc index 619cbea..4b45c2b 100644 --- a/chrome/renderer/spellchecker/spellcheck.cc +++ b/chrome/renderer/spellchecker/spellcheck.cc @@ -46,7 +46,6 @@ class SpellCheck::SpellcheckRequest { }; SpellCheck::SpellCheck() : auto_spell_correct_turned_on_(false) { - platform_spelling_engine_.reset(CreateNativeSpellingEngine()); } SpellCheck::~SpellCheck() { @@ -88,8 +87,19 @@ void SpellCheck::OnEnableAutoSpellCorrect(bool enable) { void SpellCheck::Init(base::PlatformFile file, const std::vector<std::string>& custom_words, const std::string& language) { - platform_spelling_engine_->Init(file, custom_words); - + bool use_platform_spelling_engine = + file == base::kInvalidPlatformFileValue && !language.empty(); + + // Some tests under OSX still exercise hunspell. Only init native engine + // when no dictionary was specified. + // TODO(groby): Figure out if we can kill the hunspell dependency for OSX. + if (use_platform_spelling_engine) { + platform_spelling_engine_.reset(CreateNativeSpellingEngine()); + } else { + HunspellEngine* engine = new HunspellEngine; + engine->Init(file, custom_words); + platform_spelling_engine_.reset(engine); + } character_attributes_.SetDefaultLanguage(language); text_iterator_.Reset(); contraction_iterator_.Reset(); @@ -267,8 +277,13 @@ void SpellCheck::RequestTextChecking( } #endif + bool SpellCheck::InitializeIfNeeded() { - DCHECK(platform_spelling_engine_.get()); + // TODO(groby): OSX creates a hunspell engine here, too. That seems + // wrong, but is (AFAICT) the existing flow. Fix that. + if (!platform_spelling_engine_.get()) + platform_spelling_engine_.reset(new HunspellEngine); + return platform_spelling_engine_->InitializeIfNeeded(); } @@ -337,8 +352,8 @@ bool SpellCheck::IsValidContraction(const string16& contraction, int tag) { return true; } +#if !defined(OS_MACOSX) void SpellCheck::CreateTextCheckingResults( - ResultFilter filter, int line_offset, const string16& line_text, const std::vector<SpellCheckResult>& spellcheck_results, @@ -353,8 +368,7 @@ void SpellCheck::CreateTextCheckingResults( static_cast<WebTextCheckingType>(spellcheck_results[i].type); int word_location = spellcheck_results[i].location; int word_length = spellcheck_results[i].length; - if (type == WebKit::WebTextCheckingTypeSpelling && - filter == USE_NATIVE_CHECKER) { + if (type == WebKit::WebTextCheckingTypeSpelling) { int misspelling_start = 0; int misspelling_length = 0; if (SpellCheckWord(text + word_location, word_length, 0, @@ -369,3 +383,4 @@ void SpellCheck::CreateTextCheckingResults( } textcheck_results->swap(list); } +#endif diff --git a/chrome/renderer/spellchecker/spellcheck.h b/chrome/renderer/spellchecker/spellcheck.h index 7a2253a..6292005 100644 --- a/chrome/renderer/spellchecker/spellcheck.h +++ b/chrome/renderer/spellchecker/spellcheck.h @@ -37,11 +37,6 @@ struct WebTextCheckingResult; class SpellCheck : public content::RenderProcessObserver, public base::SupportsWeakPtr<SpellCheck> { public: - enum ResultFilter { - DO_NOT_MODIFY = 1, // Do not modify results. - USE_NATIVE_CHECKER, // Use native checker to double-check. - }; - SpellCheck(); virtual ~SpellCheck(); @@ -94,7 +89,6 @@ class SpellCheck : public content::RenderProcessObserver, // checks misspelled words returned by the Spelling service and changes the // underline colors of contextually-misspelled words. void CreateTextCheckingResults( - ResultFilter filter, int line_offset, const string16& line_text, const std::vector<SpellCheckResult>& spellcheck_results, diff --git a/chrome/renderer/spellchecker/spellcheck_provider.cc b/chrome/renderer/spellchecker/spellcheck_provider.cc index c71e415a..9f643b4 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider.cc +++ b/chrome/renderer/spellchecker/spellcheck_provider.cc @@ -41,6 +41,28 @@ COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeCorrection) == COMPILE_ASSERT(int(WebKit::WebTextCheckingTypeShowCorrectionPanel) == int(SpellCheckResult::SHOWCORRECTIONPANEL), mismatching_enums); +namespace { + +// Converts a vector of SpellCheckResult objects (used by Chrome) to a vector of +// WebTextCheckingResult objects (used by WebKit). +void CreateTextCheckingResults( + int offset, + const std::vector<SpellCheckResult>& spellcheck_results, + WebKit::WebVector<WebKit::WebTextCheckingResult>* textcheck_results) { + size_t result_size = spellcheck_results.size(); + WebKit::WebVector<WebKit::WebTextCheckingResult> list(result_size); + for (size_t i = 0; i < result_size; ++i) { + list[i] = WebTextCheckingResult( + static_cast<WebTextCheckingType>(spellcheck_results[i].type), + spellcheck_results[i].location + offset, + spellcheck_results[i].length, + spellcheck_results[i].replacement); + } + textcheck_results->swap(list); +} + +} // namespace + SpellCheckProvider::SpellCheckProvider( content::RenderView* render_view, SpellCheck* spellcheck) @@ -48,14 +70,14 @@ SpellCheckProvider::SpellCheckProvider( content::RenderViewObserverTracker<SpellCheckProvider>(render_view), spelling_panel_visible_(false), spellcheck_(spellcheck) { - DCHECK(spellcheck_); if (render_view) // NULL in unit tests. render_view->GetWebView()->setSpellCheckClient(this); } SpellCheckProvider::~SpellCheckProvider() { #if defined(OS_MACOSX) - Send(new SpellCheckHostMsg_DocumentClosed(routing_id(), routing_id())); + Send(new SpellCheckHostMsg_DocumentClosed( + routing_id(), routing_id())); #endif } @@ -89,6 +111,7 @@ void SpellCheckProvider::RequestTextChecking( last_request_.clear(); last_results_.assign(WebKit::WebVector<WebKit::WebTextCheckingResult>()); + Send(new SpellCheckHostMsg_CallSpellingService( routing_id(), text_check_completions_.Add(completion), @@ -140,16 +163,19 @@ void SpellCheckProvider::spellCheck( int& length, WebVector<WebString>* optional_suggestions) { string16 word(text); - std::vector<string16> suggestions; - spellcheck_->SpellCheckWord( - word.c_str(), word.size(), routing_id(), - &offset, &length, optional_suggestions ? & suggestions : NULL); - if (optional_suggestions) - *optional_suggestions = suggestions; - if (!optional_suggestions) { - // If optional_suggestions is not requested, the API is called - // for marking. So we use this for counting markable words. - Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); + // Will be NULL during unit tests. + if (spellcheck_) { + std::vector<string16> suggestions; + spellcheck_->SpellCheckWord( + word.c_str(), word.size(), routing_id(), + &offset, &length, optional_suggestions ? & suggestions : NULL); + if (optional_suggestions) + *optional_suggestions = suggestions; + if (!optional_suggestions) { + // If optional_suggestions is not requested, the API is called + // for marking. So we use this for counting markable words. + Send(new SpellCheckHostMsg_NotifyChecked(routing_id(), word, 0 < length)); + } } } @@ -166,6 +192,10 @@ void SpellCheckProvider::checkTextOfParagraph( if (!(mask & WebKit::WebTextCheckingTypeSpelling)) return; + // Will be NULL during unit tets. + if (!spellcheck_) + return; + spellcheck_->SpellCheckParagraph(string16(text), results); #endif } @@ -178,8 +208,12 @@ void SpellCheckProvider::requestCheckingOfText( WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { const CommandLine& command_line = *CommandLine::ForCurrentProcess(); - if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) - return spellcheck_->GetAutoCorrectionWord(word, routing_id()); + if (command_line.HasSwitch(switches::kEnableSpellingAutoCorrect)) { + // Will be NULL during unit tests. + if (spellcheck_) { + return spellcheck_->GetAutoCorrectionWord(word, routing_id()); + } + } return string16(); } @@ -216,18 +250,22 @@ void SpellCheckProvider::OnRespondSpellingService( // If |succeeded| is false, we use local spellcheck as a fallback. if (!succeeded) { - spellcheck_->RequestTextChecking(line, offset, completion); - return; + // |spellcheck_| may be NULL in unit tests. + if (spellcheck_) { + spellcheck_->RequestTextChecking(line, offset, completion); + return; + } } // Double-check the returned spellchecking results with our spellchecker to // visualize the differences between ours and the on-line spellchecker. WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; - spellcheck_->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, - offset, - line, - results, - &textcheck_results); + if (spellcheck_) { + spellcheck_->CreateTextCheckingResults( + offset, line, results, &textcheck_results); + } else { + CreateTextCheckingResults(offset, results, &textcheck_results); + } completion->didFinishCheckingText(textcheck_results); // Cache the request and the converted results. @@ -262,18 +300,13 @@ void SpellCheckProvider::OnAdvanceToNextMisspelling() { void SpellCheckProvider::OnRespondTextCheck( int identifier, const std::vector<SpellCheckResult>& results) { - DCHECK(spellcheck_); WebTextCheckingCompletion* completion = text_check_completions_.Lookup(identifier); if (!completion) return; text_check_completions_.Remove(identifier); WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; - spellcheck_->CreateTextCheckingResults(SpellCheck::DO_NOT_MODIFY, - 0, - string16(), - results, - &textcheck_results); + CreateTextCheckingResults(0, results, &textcheck_results); completion->didFinishCheckingText(textcheck_results); } diff --git a/chrome/renderer/spellchecker/spellcheck_provider_test.cc b/chrome/renderer/spellchecker/spellcheck_provider_test.cc index 22bd6a2..0762899 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider_test.cc +++ b/chrome/renderer/spellchecker/spellcheck_provider_test.cc @@ -6,12 +6,8 @@ #include "base/stl_util.h" #include "chrome/common/spellcheck_messages.h" -#include "chrome/renderer/spellchecker/spellcheck.h" #include "ipc/ipc_message_macros.h" -class MockSpellcheck: public SpellCheck { -}; - FakeTextCheckingCompletion::FakeTextCheckingCompletion() : completion_count_(0), cancellation_count_(0) { @@ -31,13 +27,12 @@ void FakeTextCheckingCompletion::didCancelCheckingText() { } TestingSpellCheckProvider::TestingSpellCheckProvider() - : SpellCheckProvider(NULL, new MockSpellcheck), + : SpellCheckProvider(NULL, NULL), offset_(-1) { } TestingSpellCheckProvider::~TestingSpellCheckProvider() { - STLDeleteContainerPointers(messages_.begin(), messages_.end()); - delete spellcheck_; + STLDeleteContainerPointers(messages_.begin(), messages_.end()); } bool TestingSpellCheckProvider::Send(IPC::Message* message) { diff --git a/chrome/renderer/spellchecker/spellcheck_unittest.cc b/chrome/renderer/spellchecker/spellcheck_unittest.cc index 3a9a673..7f2c1b0 100644 --- a/chrome/renderer/spellchecker/spellcheck_unittest.cc +++ b/chrome/renderer/spellchecker/spellcheck_unittest.cc @@ -1064,11 +1064,8 @@ TEST_F(SpellCheckTest, CreateTextCheckingResults) { spellcheck_results.push_back(SpellCheckResult( SpellCheckResult::SPELLING, 0, 2, string16())); WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; - spell_check()->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, - 0, - text, - spellcheck_results, - &textcheck_results); + spell_check()->CreateTextCheckingResults( + 0, text, spellcheck_results, &textcheck_results); EXPECT_EQ(spellcheck_results.size(), textcheck_results.size()); EXPECT_EQ(WebKit::WebTextCheckingTypeSpelling, textcheck_results[0].type); EXPECT_EQ(spellcheck_results[0].location, textcheck_results[0].location); @@ -1083,11 +1080,8 @@ TEST_F(SpellCheckTest, CreateTextCheckingResults) { spellcheck_results.push_back(SpellCheckResult( SpellCheckResult::SPELLING, 7, 4, string16())); WebKit::WebVector<WebKit::WebTextCheckingResult> textcheck_results; - spell_check()->CreateTextCheckingResults(SpellCheck::USE_NATIVE_CHECKER, - 0, - text, - spellcheck_results, - &textcheck_results); + spell_check()->CreateTextCheckingResults( + 0, text, spellcheck_results, &textcheck_results); EXPECT_EQ(spellcheck_results.size(), textcheck_results.size()); EXPECT_EQ(WebKit::WebTextCheckingTypeGrammar, textcheck_results[0].type); EXPECT_EQ(spellcheck_results[0].location, textcheck_results[0].location); diff --git a/chrome/renderer/spellchecker/spelling_engine.h b/chrome/renderer/spellchecker/spelling_engine.h index af19a65..13d3b28 100644 --- a/chrome/renderer/spellchecker/spelling_engine.h +++ b/chrome/renderer/spellchecker/spelling_engine.h @@ -8,7 +8,6 @@ #include <string> #include <vector> -#include "base/platform_file.h" #include "base/string16.h" // Creates the platform's "native" spelling engine. @@ -19,10 +18,6 @@ class SpellingEngine { public: virtual ~SpellingEngine() {} - // Initialize spelling engine with browser-side info. Must be called before - // any other functions are called. - virtual void Init(base::PlatformFile bdict_file, - const std::vector<std::string>& custom_words) = 0; virtual bool InitializeIfNeeded() = 0; virtual bool IsEnabled() = 0; virtual bool CheckSpelling(const string16& word_to_check, int tag) = 0; |