1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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/spellchecker/spellcheck.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;
using WebKit::WebVector;
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) {
if (render_view) // NULL in unit tests.
render_view->webview()->setSpellCheckClient(this);
}
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(
const WebString& text,
int document_tag,
WebTextCheckingCompletion* completion) {
// Text check (unified request for grammar and spell check) is only
// available for browser process, so we ask the system sellchecker
// over IPC or return an empty result if the checker is not
// available.
if (!is_using_platform_spelling_engine()) {
completion->didFinishCheckingText
(std::vector<WebTextCheckingResult>());
return;
}
Send(new SpellCheckHostMsg_PlatformRequestTextCheck(
routing_id(),
text_check_completions_.Add(completion),
document_tag,
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::FocusedNodeChanged(const WebKit::WebNode& unused) {
bool enabled = false;
WebKit::WebNode node = render_view()->GetFocusedNode();
if (!node.isNull())
enabled = render_view()->IsEditableNode(node);
bool checked = false;
if (enabled && render_view()->webview()) {
WebFrame* frame = render_view()->webview()->focusedFrame();
if (frame->isContinuousSpellCheckingEnabled())
checked = true;
}
Send(new SpellCheckHostMsg_ToggleSpellCheck(routing_id(), enabled, checked));
}
void SpellCheckProvider::spellCheck(
const WebString& text,
int& offset,
int& length,
WebVector<WebString>* optional_suggestions) {
EnsureDocumentTag();
string16 word(text);
// Will be NULL during unit tests.
if (spellcheck_) {
std::vector<string16> suggestions;
spellcheck_->SpellCheckWord(
word.c_str(), word.size(), document_tag_,
&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(), 0 < length));
}
}
}
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();
// Will be NULL during unit tests.
if (spellcheck_)
return spellcheck_->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) {
WebTextCheckingCompletion* completion =
text_check_completions_.Lookup(identifier);
if (!completion)
return;
text_check_completions_.Remove(identifier);
completion->didFinishCheckingText(results);
}
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"));
}
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
}
|