OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/spellchecker/spellcheck_provider.h" | 5 #include "chrome/renderer/spellchecker/spellcheck_provider.h" |
6 | 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/common/chrome_switches.h" |
7 #include "chrome/common/spellcheck_messages.h" | 9 #include "chrome/common/spellcheck_messages.h" |
8 #include "chrome/renderer/render_thread.h" | 10 #include "chrome/renderer/render_thread.h" |
9 #include "chrome/renderer/spellchecker/spellcheck.h" | 11 #include "chrome/renderer/spellchecker/spellcheck.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" | 12 #include "content/renderer/render_view.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple
tion.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple
tion.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult
.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
14 | 18 |
| 19 using WebKit::WebFrame; |
15 using WebKit::WebString; | 20 using WebKit::WebString; |
16 using WebKit::WebTextCheckingCompletion; | 21 using WebKit::WebTextCheckingCompletion; |
17 using WebKit::WebTextCheckingResult; | 22 using WebKit::WebTextCheckingResult; |
18 | 23 |
19 SpellCheckProvider::SpellCheckProvider(RenderView* render_view, | 24 SpellCheckProvider::SpellCheckProvider(RenderView* render_view, |
20 SpellCheck* spellcheck) | 25 SpellCheck* spellcheck) |
21 : RenderViewObserver(render_view), | 26 : RenderViewObserver(render_view), |
| 27 #if defined(OS_MACOSX) |
| 28 has_document_tag_(false), |
| 29 #endif |
| 30 document_tag_(0), |
| 31 spelling_panel_visible_(false), |
22 spellcheck_(spellcheck) { | 32 spellcheck_(spellcheck) { |
23 } | 33 } |
24 | 34 |
25 SpellCheckProvider::~SpellCheckProvider() { | 35 SpellCheckProvider::~SpellCheckProvider() { |
| 36 #if defined(OS_MACOSX) |
| 37 // Tell the spellchecker that the document is closed. |
| 38 if (has_document_tag_) { |
| 39 Send(new SpellCheckHostMsg_DocumentWithTagClosed( |
| 40 routing_id(), document_tag_)); |
| 41 } |
| 42 #endif |
26 } | 43 } |
27 | 44 |
28 void SpellCheckProvider::RequestTextChecking( | 45 void SpellCheckProvider::RequestTextChecking( |
29 const WebString& text, | 46 const WebString& text, |
30 int document_tag, | 47 int document_tag, |
31 WebTextCheckingCompletion* completion) { | 48 WebTextCheckingCompletion* completion) { |
32 // Text check (unified request for grammar and spell check) is only | 49 // Text check (unified request for grammar and spell check) is only |
33 // available for browser process, so we ask the system sellchecker | 50 // available for browser process, so we ask the system sellchecker |
34 // over IPC or return an empty result if the checker is not | 51 // over IPC or return an empty result if the checker is not |
35 // available. | 52 // available. |
36 if (!is_using_platform_spelling_engine()) { | 53 if (!is_using_platform_spelling_engine()) { |
37 completion->didFinishCheckingText | 54 completion->didFinishCheckingText |
38 (std::vector<WebTextCheckingResult>()); | 55 (std::vector<WebTextCheckingResult>()); |
39 return; | 56 return; |
40 } | 57 } |
41 | 58 |
42 Send(new SpellCheckHostMsg_PlatformRequestTextCheck( | 59 Send(new SpellCheckHostMsg_PlatformRequestTextCheck( |
43 routing_id(), | 60 routing_id(), |
44 text_check_completions_.Add(completion), | 61 text_check_completions_.Add(completion), |
45 document_tag, | 62 document_tag, |
46 text)); | 63 text)); |
47 } | 64 } |
48 | 65 |
| 66 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { |
| 67 bool handled = true; |
| 68 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) |
| 69 IPC_MESSAGE_HANDLER(SpellCheckMsg_AdvanceToNextMisspelling, |
| 70 OnAdvanceToNextMisspelling) |
| 71 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) |
| 72 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellPanel, OnToggleSpellPanel) |
| 73 IPC_MESSAGE_HANDLER(SpellCheckMsg_ToggleSpellCheck, OnToggleSpellCheck) |
| 74 IPC_MESSAGE_UNHANDLED(handled = false) |
| 75 IPC_END_MESSAGE_MAP() |
| 76 return handled; |
| 77 } |
| 78 |
| 79 void SpellCheckProvider::spellCheck(const WebString& text, |
| 80 int& misspelled_offset, |
| 81 int& misspelled_length) { |
| 82 EnsureDocumentTag(); |
| 83 |
| 84 string16 word(text); |
| 85 RenderThread* thread = RenderThread::current(); |
| 86 // Will be NULL during unit tests. |
| 87 if (thread) { |
| 88 thread->spellchecker()->SpellCheckWord( |
| 89 word.c_str(), word.size(), document_tag_, |
| 90 &misspelled_offset, &misspelled_length, NULL); |
| 91 } |
| 92 } |
| 93 |
| 94 void SpellCheckProvider::requestCheckingOfText( |
| 95 const WebString& text, |
| 96 WebTextCheckingCompletion* completion) { |
| 97 RequestTextChecking(text, document_tag_, completion); |
| 98 } |
| 99 |
| 100 WebString SpellCheckProvider::autoCorrectWord(const WebString& word) { |
| 101 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 102 if (command_line.HasSwitch(switches::kExperimentalSpellcheckerFeatures)) { |
| 103 EnsureDocumentTag(); |
| 104 RenderThread* thread = RenderThread::current(); |
| 105 // Will be NULL during unit tests. |
| 106 if (thread) |
| 107 return thread->spellchecker()->GetAutoCorrectionWord(word, document_tag_); |
| 108 } |
| 109 return string16(); |
| 110 } |
| 111 |
| 112 void SpellCheckProvider::showSpellingUI(bool show) { |
| 113 Send(new SpellCheckHostMsg_ShowSpellingPanel(routing_id(), show)); |
| 114 } |
| 115 |
| 116 bool SpellCheckProvider::isShowingSpellingUI() { |
| 117 return spelling_panel_visible_; |
| 118 } |
| 119 |
| 120 void SpellCheckProvider::updateSpellingUIWithMisspelledWord( |
| 121 const WebString& word) { |
| 122 Send(new SpellCheckHostMsg_UpdateSpellingPanelWithMisspelledWord(routing_id(), |
| 123 word)); |
| 124 } |
| 125 |
| 126 bool SpellCheckProvider::is_using_platform_spelling_engine() const { |
| 127 return spellcheck_ && spellcheck_->is_using_platform_spelling_engine(); |
| 128 } |
| 129 |
| 130 void SpellCheckProvider::OnAdvanceToNextMisspelling() { |
| 131 if (!render_view()->webview()) |
| 132 return; |
| 133 render_view()->webview()->focusedFrame()->executeCommand( |
| 134 WebString::fromUTF8("AdvanceToNextMisspelling")); |
| 135 } |
| 136 |
49 void SpellCheckProvider::OnRespondTextCheck( | 137 void SpellCheckProvider::OnRespondTextCheck( |
50 int identifier, | 138 int identifier, |
51 int tag, | 139 int tag, |
52 const std::vector<WebTextCheckingResult>& results) { | 140 const std::vector<WebTextCheckingResult>& results) { |
53 WebKit::WebTextCheckingCompletion* completion = | 141 WebTextCheckingCompletion* completion = |
54 text_check_completions_.Lookup(identifier); | 142 text_check_completions_.Lookup(identifier); |
55 if (!completion) | 143 if (!completion) |
56 return; | 144 return; |
57 text_check_completions_.Remove(identifier); | 145 text_check_completions_.Remove(identifier); |
58 completion->didFinishCheckingText(results); | 146 completion->didFinishCheckingText(results); |
59 } | 147 } |
60 | 148 |
61 bool SpellCheckProvider::OnMessageReceived(const IPC::Message& message) { | 149 void SpellCheckProvider::OnToggleSpellPanel(bool is_currently_visible) { |
62 bool handled = true; | 150 if (!render_view()->webview()) |
63 IPC_BEGIN_MESSAGE_MAP(SpellCheckProvider, message) | 151 return; |
64 IPC_MESSAGE_HANDLER(SpellCheckMsg_RespondTextCheck, OnRespondTextCheck) | 152 // We need to tell the webView whether the spelling panel is visible or not so |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 153 // that it won't need to make ipc calls later. |
66 IPC_END_MESSAGE_MAP() | 154 spelling_panel_visible_ = is_currently_visible; |
67 return handled; | 155 render_view()->webview()->focusedFrame()->executeCommand( |
| 156 WebString::fromUTF8("ToggleSpellPanel")); |
68 } | 157 } |
69 | 158 |
70 bool SpellCheckProvider::is_using_platform_spelling_engine() const { | 159 void SpellCheckProvider::OnToggleSpellCheck() { |
71 return spellcheck_ && spellcheck_->is_using_platform_spelling_engine(); | 160 if (!render_view()->webview()) |
| 161 return; |
| 162 |
| 163 WebFrame* frame = render_view()->webview()->focusedFrame(); |
| 164 frame->enableContinuousSpellChecking( |
| 165 !frame->isContinuousSpellCheckingEnabled()); |
72 } | 166 } |
| 167 |
| 168 void SpellCheckProvider::EnsureDocumentTag() { |
| 169 // TODO(darin): There's actually no reason for this to be here. We should |
| 170 // have the browser side manage the document tag. |
| 171 #if defined(OS_MACOSX) |
| 172 if (!has_document_tag_) { |
| 173 // Make the call to get the tag. |
| 174 Send(new SpellCheckHostMsg_GetDocumentTag(routing_id(), &document_tag_)); |
| 175 has_document_tag_ = true; |
| 176 } |
| 177 #endif |
| 178 } |
OLD | NEW |