diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-29 20:36:35 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-29 20:36:35 +0000 |
commit | 11fee2337da64930b4875f142abfe5503b0ac187 (patch) | |
tree | e5fa49025a823fe35e28a9de274ac5a93d9e3e93 /chrome | |
parent | d91233b7e37833ad9a2ed62d833caf1bb1714b11 (diff) | |
download | chromium_src-11fee2337da64930b4875f142abfe5503b0ac187.zip chromium_src-11fee2337da64930b4875f142abfe5503b0ac187.tar.gz chromium_src-11fee2337da64930b4875f142abfe5503b0ac187.tar.bz2 |
Update the code to have the SpellCheckProvider implement WebSpellCheckClient, in preparation for moving that code out of RenderView.
Review URL: http://codereview.chromium.org/6764001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79735 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_provider.cc | 128 | ||||
-rw-r--r-- | chrome/renderer/spellchecker/spellcheck_provider.h | 38 |
2 files changed, 153 insertions, 13 deletions
diff --git a/chrome/renderer/spellchecker/spellcheck_provider.cc b/chrome/renderer/spellchecker/spellcheck_provider.cc index da06c1e..5309ddf 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider.cc +++ b/chrome/renderer/spellchecker/spellcheck_provider.cc @@ -4,14 +4,19 @@ #include "chrome/renderer/spellchecker/spellcheck_provider.h" +#include "base/command_line.h" +#include "chrome/common/chrome_switches.h" #include "chrome/common/spellcheck_messages.h" #include "chrome/renderer/render_thread.h" #include "chrome/renderer/spellchecker/spellcheck.h" -#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" +#include "content/renderer/render_view.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingCompletion.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" +using WebKit::WebFrame; using WebKit::WebString; using WebKit::WebTextCheckingCompletion; using WebKit::WebTextCheckingResult; @@ -19,10 +24,22 @@ using WebKit::WebTextCheckingResult; SpellCheckProvider::SpellCheckProvider(RenderView* render_view, SpellCheck* spellcheck) : RenderViewObserver(render_view), +#if defined(OS_MACOSX) + has_document_tag_(false), +#endif + document_tag_(0), + spelling_panel_visible_(false), spellcheck_(spellcheck) { } SpellCheckProvider::~SpellCheckProvider() { +#if defined(OS_MACOSX) + // Tell the spellchecker that the document is closed. + if (has_document_tag_) { + Send(new SpellCheckHostMsg_DocumentWithTagClosed( + routing_id(), document_tag_)); + } +#endif } void SpellCheckProvider::RequestTextChecking( @@ -46,11 +63,82 @@ void SpellCheckProvider::RequestTextChecking( text)); } +bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) + IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, + OnAdvanceToNextMisspelling) + IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) + IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) + IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellCheck, OnToggleSpellCheck) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void SpellCheckProvider::spellCheck(const WebString& text, + int& misspelled_offset, + int& misspelled_length) { + EnsureDocumentTag(); + + string16 word(text); + RenderThread* thread = RenderThread::current(); + // Will be NULL during unit tests. + if (thread) { + thread->spellchecker()->SpellCheckWord( + word.c_str(), word.size(), document_tag_, + &misspelled_offset, &misspelled_length, NULL); + } +} + +void SpellCheckProvider::requestCheckingOfText( + const WebString& text, + WebTextCheckingCompletion* completion) { + RequestTextChecking(text, document_tag_, completion); +} + +WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); + if (command_line.HasSwitch(switches::kExperimentalSpellcheckerFeatures)) { + EnsureDocumentTag(); + RenderThread* thread = RenderThread::current(); + // Will be NULL during unit tests. + if (thread) + return thread->spellchecker()->GetAutoCorrectionWord(word, document_tag_); + } + return string16(); +} + +void SpellCheckProvider::showSpellingUI(bool show) { + Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); +} + +bool SpellCheckProvider::isShowingSpellingUI() { + return spelling_panel_visible_; +} + +void SpellCheckProvider::updateSpellingUIWithMisspelledWord( + const WebString& word) { + Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), + word)); +} + +bool SpellCheckProvider::is_using_platform_spelling_engine() const { + return spellcheck_ && spellcheck_->is_using_platform_spelling_engine(); +} + +void SpellCheckProvider::OnAdvanceToNextMisspelling() { + if (!render_view()->webview()) + return; + render_view()->webview()->focusedFrame()->executeCommand( + WebString::fromUTF8("AdvanceToNextMisspelling")); +} + void SpellCheckProvider::OnRespondTextCheck( int identifier, int tag, const std::vector<WebTextCheckingResult>& results) { - WebKit::WebTextCheckingCompletion* completion = + WebTextCheckingCompletion* completion = text_check_completions_.Lookup(identifier); if (!completion) return; @@ -58,15 +146,33 @@ void SpellCheckProvider::OnRespondTextCheck( completion->didFinishCheckingText(results); } -bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { - bool handled = true; - IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) - IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) - IPC_MESSAGE_UNHANDLED(handled = false) - IPC_END_MESSAGE_MAP() - return handled; +void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { + if (!render_view()->webview()) + return; + // We need to tell the webView whether the spelling panel is visible or not so + // that it won't need to make ipc calls later. + spelling_panel_visible_ = is_currently_visible; + render_view()->webview()->focusedFrame()->executeCommand( + WebString::fromUTF8("ToggleSpellPanel")); } -bool SpellCheckProvider::is_using_platform_spelling_engine() const { - return spellcheck_ && spellcheck_->is_using_platform_spelling_engine(); +void SpellCheckProvider::OnToggleSpellCheck() { + if (!render_view()->webview()) + return; + + WebFrame* frame = render_view()->webview()->focusedFrame(); + frame->enableContinuousSpellChecking( + !frame->isContinuousSpellCheckingEnabled()); +} + +void SpellCheckProvider::EnsureDocumentTag() { + // TODO(darin): There's actually no reason for this to be here. We should + // have the browser side manage the document tag. +#if defined(OS_MACOSX) + if (!has_document_tag_) { + // Make the call to get the tag. + Send(new SpellCheckHostMsg_GetDocumentTag(routing_id(), &document_tag_)); + has_document_tag_ = true; + } +#endif } diff --git a/chrome/renderer/spellchecker/spellcheck_provider.h b/chrome/renderer/spellchecker/spellcheck_provider.h index 7157f11..9697d62 100644 --- a/chrome/renderer/spellchecker/spellcheck_provider.h +++ b/chrome/renderer/spellchecker/spellcheck_provider.h @@ -10,6 +10,7 @@ #include "base/id_map.h" #include "content/renderer/render_view_observer.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebSpellCheckClient.h" class RenderView; class SpellCheck; @@ -22,7 +23,8 @@ struct WebTextCheckingResult; // This class deals with invoking browser-side spellcheck mechanism // which is done asynchronously. -class SpellCheckProvider : public RenderViewObserver { +class SpellCheckProvider : public RenderViewObserver, + public WebKit::WebSpellCheckClient { public: typedef IDMap<WebKit::WebTextCheckingCompletion> WebTextCheckCompletions; @@ -45,18 +47,50 @@ class SpellCheckProvider : public RenderViewObserver { return text_check_completions_.size(); } + int document_tag() const { return document_tag_; } + // RenderViewObserver implementation. virtual bool OnMessageReceived(const IPC::Message& message); private: - // A message handler that receives async results for RequestTextChecking(). + // WebKit::WebSpellCheckClient implementation. + virtual void spellCheck(const WebKit::WebString& text, + int& offset, + int& length); + virtual void requestCheckingOfText( + const WebKit::WebString& text, + WebKit::WebTextCheckingCompletion* completion); + virtual WebKit::WebString autoCorrectWord( + const WebKit::WebString& misspelled_word); + virtual void showSpellingUI(bool show); + virtual bool isShowingSpellingUI(); + virtual void updateSpellingUIWithMisspelledWord( + const WebKit::WebString& word); + + void OnAdvanceToNextMisspelling(); void OnRespondTextCheck( int identifier, int tag, const std::vector<WebKit::WebTextCheckingResult>& results); + void OnToggleSpellCheck(); + void OnToggleSpellPanel(bool is_currently_visible); + + // Initializes the document_tag_ member if necessary. + void EnsureDocumentTag(); // Holds ongoing spellchecking operations, assigns IDs for the IPC routing. WebTextCheckCompletions text_check_completions_; + +#if defined(OS_MACOSX) + // True if the current RenderView has been assigned a document tag. + bool has_document_tag_; +#endif + + int document_tag_; + + // True if the browser is showing the spelling panel for us. + bool spelling_panel_visible_; + // Spellcheck implementation for use. Weak reference. SpellCheck* spellcheck_; |