OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // The Mac interface forwards most of these commands to the application layer, | |
6 // and I'm not really sure what to do about most of them. | |
7 | |
8 #include "config.h" | |
9 | |
10 #include "Document.h" | |
11 #include "EditCommand.h" | |
12 #include "Editor.h" | |
13 #include "EventHandler.h" | |
14 #include "EventNames.h" | |
15 #include "Frame.h" | |
16 #include "KeyboardCodes.h" | |
17 #include "HTMLInputElement.h" | |
18 #include "HTMLNames.h" | |
19 #include "KeyboardEvent.h" | |
20 #include "PlatformKeyboardEvent.h" | |
21 #include "PlatformString.h" | |
22 #include "RenderObject.h" | |
23 #undef LOG | |
24 | |
25 #include "webkit/api/public/WebEditingAction.h" | |
26 #include "webkit/api/public/WebKit.h" | |
27 #include "webkit/api/public/WebNode.h" | |
28 #include "webkit/api/public/WebRange.h" | |
29 #include "webkit/api/public/WebTextAffinity.h" | |
30 #include "webkit/api/public/WebViewClient.h" | |
31 // Can include api/src since eventually editor_client_impl will be there too. | |
32 #include "webkit/api/src/DOMUtilitiesPrivate.h" | |
33 #include "webkit/api/src/PasswordAutocompleteListener.h" | |
34 #include "webkit/glue/editor_client_impl.h" | |
35 #include "webkit/glue/form_field_values.h" | |
36 #include "webkit/glue/glue_util.h" | |
37 #include "webkit/glue/webview_impl.h" | |
38 | |
39 using WebKit::PasswordAutocompleteListener; | |
40 using WebKit::WebEditingAction; | |
41 using WebKit::WebString; | |
42 using WebKit::WebTextAffinity; | |
43 | |
44 // Arbitrary depth limit for the undo stack, to keep it from using | |
45 // unbounded memory. This is the maximum number of distinct undoable | |
46 // actions -- unbroken stretches of typed characters are coalesced | |
47 // into a single action. | |
48 static const size_t kMaximumUndoStackDepth = 1000; | |
49 | |
50 // The size above which we stop triggering autofill for an input text field | |
51 // (so to avoid sending long strings through IPC). | |
52 static const size_t kMaximumTextSizeForAutofill = 1000; | |
53 | |
54 EditorClientImpl::EditorClientImpl(WebViewImpl* webview) | |
55 : webview_(webview), | |
56 in_redo_(false), | |
57 backspace_or_delete_pressed_(false), | |
58 spell_check_this_field_status_(SPELLCHECK_AUTOMATIC), | |
59 ALLOW_THIS_IN_INITIALIZER_LIST( | |
60 autofill_timer_(this, &EditorClientImpl::DoAutofill)) { | |
61 } | |
62 | |
63 EditorClientImpl::~EditorClientImpl() { | |
64 } | |
65 | |
66 void EditorClientImpl::pageDestroyed() { | |
67 // Our lifetime is bound to the WebViewImpl. | |
68 } | |
69 | |
70 bool EditorClientImpl::shouldShowDeleteInterface(WebCore::HTMLElement* elem) { | |
71 // Normally, we don't care to show WebCore's deletion UI, so we only enable | |
72 // it if in testing mode and the test specifically requests it by using this | |
73 // magic class name. | |
74 return WebKit::layoutTestMode() && | |
75 elem->getAttribute(WebCore::HTMLNames::classAttr) == "needsDeletionUI"; | |
76 } | |
77 | |
78 bool EditorClientImpl::smartInsertDeleteEnabled() { | |
79 if (webview_->client()) | |
80 return webview_->client()->isSmartInsertDeleteEnabled(); | |
81 return true; | |
82 } | |
83 | |
84 bool EditorClientImpl::isSelectTrailingWhitespaceEnabled() { | |
85 if (webview_->client()) | |
86 return webview_->client()->isSelectTrailingWhitespaceEnabled(); | |
87 | |
88 #if PLATFORM(WIN_OS) | |
89 return true; | |
90 #else | |
91 return false; | |
92 #endif | |
93 } | |
94 | |
95 bool EditorClientImpl::ShouldSpellcheckByDefault() { | |
96 // Spellcheck should be enabled for all editable areas (such as textareas, | |
97 // contentEditable regions, and designMode docs), except text inputs. | |
98 const WebCore::Frame* frame = webview_->GetFocusedWebCoreFrame(); | |
99 if (!frame) | |
100 return false; | |
101 const WebCore::Editor* editor = frame->editor(); | |
102 if (!editor) | |
103 return false; | |
104 if (editor->spellCheckingEnabledInFocusedNode()) | |
105 return true; | |
106 const WebCore::Document* document = frame->document(); | |
107 if (!document) | |
108 return false; | |
109 const WebCore::Node* node = document->focusedNode(); | |
110 // If |node| is NULL, we default to allowing spellchecking. This is done in | |
111 // order to mitigate the issue when the user clicks outside the textbox, as a | |
112 // result of which |node| becomes NULL, resulting in all the spell check | |
113 // markers being deleted. Also, the Frame will decide not to do spellchecking | |
114 // if the user can't edit - so returning true here will not cause any problems | |
115 // to the Frame's behavior. | |
116 if (!node) | |
117 return true; | |
118 const WebCore::RenderObject* renderer = node->renderer(); | |
119 if (!renderer) | |
120 return false; | |
121 | |
122 return !renderer->isTextField(); | |
123 } | |
124 | |
125 bool EditorClientImpl::isContinuousSpellCheckingEnabled() { | |
126 if (spell_check_this_field_status_ == SPELLCHECK_FORCED_OFF) | |
127 return false; | |
128 if (spell_check_this_field_status_ == SPELLCHECK_FORCED_ON) | |
129 return true; | |
130 return ShouldSpellcheckByDefault(); | |
131 } | |
132 | |
133 void EditorClientImpl::toggleContinuousSpellChecking() { | |
134 if (isContinuousSpellCheckingEnabled()) | |
135 spell_check_this_field_status_ = SPELLCHECK_FORCED_OFF; | |
136 else | |
137 spell_check_this_field_status_ = SPELLCHECK_FORCED_ON; | |
138 } | |
139 | |
140 bool EditorClientImpl::isGrammarCheckingEnabled() { | |
141 return false; | |
142 } | |
143 | |
144 void EditorClientImpl::toggleGrammarChecking() { | |
145 notImplemented(); | |
146 } | |
147 | |
148 int EditorClientImpl::spellCheckerDocumentTag() { | |
149 ASSERT_NOT_REACHED(); | |
150 return 0; | |
151 } | |
152 | |
153 bool EditorClientImpl::isEditable() { | |
154 return false; | |
155 } | |
156 | |
157 bool EditorClientImpl::shouldBeginEditing(WebCore::Range* range) { | |
158 if (webview_->client()) { | |
159 return webview_->client()->shouldBeginEditing( | |
160 webkit_glue::RangeToWebRange(range)); | |
161 } | |
162 return true; | |
163 } | |
164 | |
165 bool EditorClientImpl::shouldEndEditing(WebCore::Range* range) { | |
166 if (webview_->client()) { | |
167 return webview_->client()->shouldEndEditing( | |
168 webkit_glue::RangeToWebRange(range)); | |
169 } | |
170 return true; | |
171 } | |
172 | |
173 bool EditorClientImpl::shouldInsertNode(WebCore::Node* node, | |
174 WebCore::Range* range, | |
175 WebCore::EditorInsertAction action) { | |
176 if (webview_->client()) { | |
177 return webview_->client()->shouldInsertNode( | |
178 webkit_glue::NodeToWebNode(node), | |
179 webkit_glue::RangeToWebRange(range), | |
180 static_cast<WebEditingAction>(action)); | |
181 } | |
182 return true; | |
183 } | |
184 | |
185 bool EditorClientImpl::shouldInsertText(const WebCore::String& text, | |
186 WebCore::Range* range, | |
187 WebCore::EditorInsertAction action) { | |
188 if (webview_->client()) { | |
189 return webview_->client()->shouldInsertText( | |
190 webkit_glue::StringToWebString(text), | |
191 webkit_glue::RangeToWebRange(range), | |
192 static_cast<WebEditingAction>(action)); | |
193 } | |
194 return true; | |
195 } | |
196 | |
197 | |
198 bool EditorClientImpl::shouldDeleteRange(WebCore::Range* range) { | |
199 if (webview_->client()) { | |
200 return webview_->client()->shouldDeleteRange( | |
201 webkit_glue::RangeToWebRange(range)); | |
202 } | |
203 return true; | |
204 } | |
205 | |
206 bool EditorClientImpl::shouldChangeSelectedRange(WebCore::Range* from_range, | |
207 WebCore::Range* to_range, | |
208 WebCore::EAffinity affinity, | |
209 bool still_selecting) { | |
210 if (webview_->client()) { | |
211 return webview_->client()->shouldChangeSelectedRange( | |
212 webkit_glue::RangeToWebRange(from_range), | |
213 webkit_glue::RangeToWebRange(to_range), | |
214 static_cast<WebTextAffinity>(affinity), | |
215 still_selecting); | |
216 } | |
217 return true; | |
218 } | |
219 | |
220 bool EditorClientImpl::shouldApplyStyle(WebCore::CSSStyleDeclaration* style, | |
221 WebCore::Range* range) { | |
222 if (webview_->client()) { | |
223 // TODO(darin): Pass a reference to the CSSStyleDeclaration somehow. | |
224 return webview_->client()->shouldApplyStyle( | |
225 WebString(), | |
226 webkit_glue::RangeToWebRange(range)); | |
227 } | |
228 return true; | |
229 } | |
230 | |
231 bool EditorClientImpl::shouldMoveRangeAfterDelete( | |
232 WebCore::Range* /*range*/, | |
233 WebCore::Range* /*rangeToBeReplaced*/) { | |
234 return true; | |
235 } | |
236 | |
237 void EditorClientImpl::didBeginEditing() { | |
238 if (webview_->client()) | |
239 webview_->client()->didBeginEditing(); | |
240 } | |
241 | |
242 void EditorClientImpl::respondToChangedSelection() { | |
243 if (webview_->client()) { | |
244 WebCore::Frame* frame = webview_->GetFocusedWebCoreFrame(); | |
245 if (frame) | |
246 webview_->client()->didChangeSelection(!frame->selection()->isRange()); | |
247 } | |
248 } | |
249 | |
250 void EditorClientImpl::respondToChangedContents() { | |
251 if (webview_->client()) | |
252 webview_->client()->didChangeContents(); | |
253 } | |
254 | |
255 void EditorClientImpl::didEndEditing() { | |
256 if (webview_->client()) | |
257 webview_->client()->didEndEditing(); | |
258 } | |
259 | |
260 void EditorClientImpl::didWriteSelectionToPasteboard() { | |
261 } | |
262 | |
263 void EditorClientImpl::didSetSelectionTypesForPasteboard() { | |
264 } | |
265 | |
266 void EditorClientImpl::registerCommandForUndo( | |
267 PassRefPtr<WebCore::EditCommand> command) { | |
268 if (undo_stack_.size() == kMaximumUndoStackDepth) | |
269 undo_stack_.removeFirst(); // drop oldest item off the far end | |
270 if (!in_redo_) | |
271 redo_stack_.clear(); | |
272 undo_stack_.append(command); | |
273 } | |
274 | |
275 void EditorClientImpl::registerCommandForRedo( | |
276 PassRefPtr<WebCore::EditCommand> command) { | |
277 redo_stack_.append(command); | |
278 } | |
279 | |
280 void EditorClientImpl::clearUndoRedoOperations() { | |
281 undo_stack_.clear(); | |
282 redo_stack_.clear(); | |
283 } | |
284 | |
285 bool EditorClientImpl::canUndo() const { | |
286 return !undo_stack_.isEmpty(); | |
287 } | |
288 | |
289 bool EditorClientImpl::canRedo() const { | |
290 return !redo_stack_.isEmpty(); | |
291 } | |
292 | |
293 void EditorClientImpl::undo() { | |
294 if (canUndo()) { | |
295 EditCommandStack::iterator back = --undo_stack_.end(); | |
296 RefPtr<WebCore::EditCommand> command(*back); | |
297 undo_stack_.remove(back); | |
298 command->unapply(); | |
299 // unapply will call us back to push this command onto the redo stack. | |
300 } | |
301 } | |
302 | |
303 void EditorClientImpl::redo() { | |
304 if (canRedo()) { | |
305 EditCommandStack::iterator back = --redo_stack_.end(); | |
306 RefPtr<WebCore::EditCommand> command(*back); | |
307 redo_stack_.remove(back); | |
308 | |
309 ASSERT(!in_redo_); | |
310 in_redo_ = true; | |
311 command->reapply(); | |
312 // reapply will call us back to push this command onto the undo stack. | |
313 in_redo_ = false; | |
314 } | |
315 } | |
316 | |
317 // The below code was adapted from the WebKit file webview.cpp | |
318 // provided by Apple, Inc, and is subject to the following copyright | |
319 // notice and disclaimer. | |
320 | |
321 /* | |
322 * Copyright (C) 2006, 2007 Apple, Inc. All rights reserved. | |
323 * | |
324 * Redistribution and use in source and binary forms, with or without | |
325 * modification, are permitted provided that the following conditions | |
326 * are met: | |
327 * 1. Redistributions of source code must retain the above copyright | |
328 * notice, this list of conditions and the following disclaimer. | |
329 * 2. Redistributions in binary form must reproduce the above copyright | |
330 * notice, this list of conditions and the following disclaimer in the | |
331 * documentation and/or other materials provided with the distribution. | |
332 * | |
333 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
334 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
335 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
336 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
337 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
338 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
339 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
340 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
341 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
342 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
343 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
344 */ | |
345 | |
346 static const unsigned CtrlKey = 1 << 0; | |
347 static const unsigned AltKey = 1 << 1; | |
348 static const unsigned ShiftKey = 1 << 2; | |
349 static const unsigned MetaKey = 1 << 3; | |
350 #if PLATFORM(DARWIN) | |
351 // Aliases for the generic key defintions to make kbd shortcuts definitions more | |
352 // readable on OS X. | |
353 static const unsigned OptionKey = AltKey; | |
354 static const unsigned CommandKey = MetaKey; | |
355 #endif | |
356 | |
357 // Keys with special meaning. These will be delegated to the editor using | |
358 // the execCommand() method | |
359 struct KeyDownEntry { | |
360 unsigned virtualKey; | |
361 unsigned modifiers; | |
362 const char* name; | |
363 }; | |
364 | |
365 struct KeyPressEntry { | |
366 unsigned charCode; | |
367 unsigned modifiers; | |
368 const char* name; | |
369 }; | |
370 | |
371 static const KeyDownEntry keyDownEntries[] = { | |
372 { WebCore::VKEY_LEFT, 0, "MoveLeft" }, | |
373 { WebCore::VKEY_LEFT, ShiftKey, "MoveLeftAndModifySelection" }, | |
374 #if PLATFORM(DARWIN) | |
375 { WebCore::VKEY_LEFT, OptionKey, "MoveWordLeft" }, | |
376 { WebCore::VKEY_LEFT, OptionKey | ShiftKey, | |
377 "MoveWordLeftAndModifySelection" }, | |
378 #else | |
379 { WebCore::VKEY_LEFT, CtrlKey, "MoveWordLeft" }, | |
380 { WebCore::VKEY_LEFT, CtrlKey | ShiftKey, "MoveWordLeftAndModifySelection"}, | |
381 #endif | |
382 { WebCore::VKEY_RIGHT, 0, "MoveRight" }, | |
383 { WebCore::VKEY_RIGHT, ShiftKey, "MoveRightAndModifySelection" }, | |
384 #if PLATFORM(DARWIN) | |
385 { WebCore::VKEY_RIGHT, OptionKey, "MoveWordRight" }, | |
386 { WebCore::VKEY_RIGHT, OptionKey | ShiftKey, | |
387 "MoveWordRightAndModifySelection" }, | |
388 #else | |
389 { WebCore::VKEY_RIGHT, CtrlKey, "MoveWordRight" }, | |
390 { WebCore::VKEY_RIGHT, CtrlKey | ShiftKey, | |
391 "MoveWordRightAndModifySelection" }, | |
392 #endif | |
393 { WebCore::VKEY_UP, 0, "MoveUp" }, | |
394 { WebCore::VKEY_UP, ShiftKey, "MoveUpAndModifySelection" }, | |
395 { WebCore::VKEY_PRIOR, ShiftKey, "MovePageUpAndModifySelection" }, | |
396 { WebCore::VKEY_DOWN, 0, "MoveDown" }, | |
397 { WebCore::VKEY_DOWN, ShiftKey, "MoveDownAndModifySelection" }, | |
398 { WebCore::VKEY_NEXT, ShiftKey, "MovePageDownAndModifySelection"}, | |
399 { WebCore::VKEY_PRIOR, 0, "MovePageUp" }, | |
400 { WebCore::VKEY_NEXT, 0, "MovePageDown" }, | |
401 { WebCore::VKEY_HOME, 0, "MoveToBeginningOfLine" }, | |
402 { WebCore::VKEY_HOME, ShiftKey, | |
403 "MoveToBeginningOfLineAndModifySelection" }, | |
404 #if PLATFORM(DARWIN) | |
405 { WebCore::VKEY_LEFT, CommandKey, "MoveToBeginningOfLine" }, | |
406 { WebCore::VKEY_LEFT, CommandKey | ShiftKey, | |
407 "MoveToBeginningOfLineAndModifySelection" }, | |
408 #endif | |
409 #if PLATFORM(DARWIN) | |
410 { WebCore::VKEY_UP, CommandKey, "MoveToBeginningOfDocument" }, | |
411 { WebCore::VKEY_UP, CommandKey | ShiftKey, | |
412 "MoveToBeginningOfDocumentAndModifySelection" }, | |
413 #else | |
414 { WebCore::VKEY_HOME, CtrlKey, "MoveToBeginningOfDocument" }, | |
415 { WebCore::VKEY_HOME, CtrlKey | ShiftKey, | |
416 "MoveToBeginningOfDocumentAndModifySelection" }, | |
417 #endif | |
418 { WebCore::VKEY_END, 0, "MoveToEndOfLine" }, | |
419 { WebCore::VKEY_END, ShiftKey, | |
420 "MoveToEndOfLineAndModifySelection" }, | |
421 #if PLATFORM(DARWIN) | |
422 { WebCore::VKEY_DOWN, CommandKey, "MoveToEndOfDocument" }, | |
423 { WebCore::VKEY_DOWN, CommandKey | ShiftKey, | |
424 "MoveToEndOfDocumentAndModifySelection" }, | |
425 #else | |
426 { WebCore::VKEY_END, CtrlKey, "MoveToEndOfDocument" }, | |
427 { WebCore::VKEY_END, CtrlKey | ShiftKey, | |
428 "MoveToEndOfDocumentAndModifySelection" }, | |
429 #endif | |
430 #if PLATFORM(DARWIN) | |
431 { WebCore::VKEY_RIGHT, CommandKey, "MoveToEndOfLine" }, | |
432 { WebCore::VKEY_RIGHT, CommandKey | ShiftKey, | |
433 "MoveToEndOfLineAndModifySelection" }, | |
434 #endif | |
435 { WebCore::VKEY_BACK, 0, "DeleteBackward" }, | |
436 { WebCore::VKEY_BACK, ShiftKey, "DeleteBackward" }, | |
437 { WebCore::VKEY_DELETE, 0, "DeleteForward" }, | |
438 #if PLATFORM(DARWIN) | |
439 { WebCore::VKEY_BACK, OptionKey, "DeleteWordBackward" }, | |
440 { WebCore::VKEY_DELETE, OptionKey, "DeleteWordForward" }, | |
441 #else | |
442 { WebCore::VKEY_BACK, CtrlKey, "DeleteWordBackward" }, | |
443 { WebCore::VKEY_DELETE, CtrlKey, "DeleteWordForward" }, | |
444 #endif | |
445 { 'B', CtrlKey, "ToggleBold" }, | |
446 { 'I', CtrlKey, "ToggleItalic" }, | |
447 { 'U', CtrlKey, "ToggleUnderline" }, | |
448 { WebCore::VKEY_ESCAPE, 0, "Cancel" }, | |
449 { WebCore::VKEY_OEM_PERIOD, CtrlKey, "Cancel" }, | |
450 { WebCore::VKEY_TAB, 0, "InsertTab" }, | |
451 { WebCore::VKEY_TAB, ShiftKey, "InsertBacktab" }, | |
452 { WebCore::VKEY_RETURN, 0, "InsertNewline" }, | |
453 { WebCore::VKEY_RETURN, CtrlKey, "InsertNewline" }, | |
454 { WebCore::VKEY_RETURN, AltKey, "InsertNewline" }, | |
455 { WebCore::VKEY_RETURN, AltKey | ShiftKey, "InsertNewline" }, | |
456 { WebCore::VKEY_RETURN, ShiftKey, "InsertLineBreak" }, | |
457 { WebCore::VKEY_INSERT, CtrlKey, "Copy" }, | |
458 { WebCore::VKEY_INSERT, ShiftKey, "Paste" }, | |
459 { WebCore::VKEY_DELETE, ShiftKey, "Cut" }, | |
460 #if PLATFORM(DARWIN) | |
461 { 'C', CommandKey, "Copy" }, | |
462 { 'V', CommandKey, "Paste" }, | |
463 { 'V', CommandKey | ShiftKey, | |
464 "PasteAndMatchStyle" }, | |
465 { 'X', CommandKey, "Cut" }, | |
466 { 'A', CommandKey, "SelectAll" }, | |
467 { 'Z', CommandKey, "Undo" }, | |
468 { 'Z', CommandKey | ShiftKey, | |
469 "Redo" }, | |
470 { 'Y', CommandKey, "Redo" }, | |
471 #else | |
472 { 'C', CtrlKey, "Copy" }, | |
473 { 'V', CtrlKey, "Paste" }, | |
474 { 'V', CtrlKey | ShiftKey, "PasteAndMatchStyle" }, | |
475 { 'X', CtrlKey, "Cut" }, | |
476 { 'A', CtrlKey, "SelectAll" }, | |
477 { 'Z', CtrlKey, "Undo" }, | |
478 { 'Z', CtrlKey | ShiftKey, "Redo" }, | |
479 { 'Y', CtrlKey, "Redo" }, | |
480 #endif | |
481 }; | |
482 | |
483 static const KeyPressEntry keyPressEntries[] = { | |
484 { '\t', 0, "InsertTab" }, | |
485 { '\t', ShiftKey, "InsertBacktab" }, | |
486 { '\r', 0, "InsertNewline" }, | |
487 { '\r', CtrlKey, "InsertNewline" }, | |
488 { '\r', ShiftKey, "InsertLineBreak" }, | |
489 { '\r', AltKey, "InsertNewline" }, | |
490 { '\r', AltKey | ShiftKey, "InsertNewline" }, | |
491 }; | |
492 | |
493 const char* EditorClientImpl::interpretKeyEvent( | |
494 const WebCore::KeyboardEvent* evt) { | |
495 const WebCore::PlatformKeyboardEvent* keyEvent = evt->keyEvent(); | |
496 if (!keyEvent) | |
497 return ""; | |
498 | |
499 static HashMap<int, const char*>* keyDownCommandsMap = 0; | |
500 static HashMap<int, const char*>* keyPressCommandsMap = 0; | |
501 | |
502 if (!keyDownCommandsMap) { | |
503 keyDownCommandsMap = new HashMap<int, const char*>; | |
504 keyPressCommandsMap = new HashMap<int, const char*>; | |
505 | |
506 for (unsigned i = 0; i < arraysize(keyDownEntries); i++) { | |
507 keyDownCommandsMap->set( | |
508 keyDownEntries[i].modifiers << 16 | keyDownEntries[i].virtualKey, | |
509 keyDownEntries[i].name); | |
510 } | |
511 | |
512 for (unsigned i = 0; i < arraysize(keyPressEntries); i++) { | |
513 keyPressCommandsMap->set( | |
514 keyPressEntries[i].modifiers << 16 | keyPressEntries[i].charCode, | |
515 keyPressEntries[i].name); | |
516 } | |
517 } | |
518 | |
519 unsigned modifiers = 0; | |
520 if (keyEvent->shiftKey()) | |
521 modifiers |= ShiftKey; | |
522 if (keyEvent->altKey()) | |
523 modifiers |= AltKey; | |
524 if (keyEvent->ctrlKey()) | |
525 modifiers |= CtrlKey; | |
526 if (keyEvent->metaKey()) | |
527 modifiers |= MetaKey; | |
528 | |
529 if (keyEvent->type() == WebCore::PlatformKeyboardEvent::RawKeyDown) { | |
530 int mapKey = modifiers << 16 | evt->keyCode(); | |
531 return mapKey ? keyDownCommandsMap->get(mapKey) : 0; | |
532 } | |
533 | |
534 int mapKey = modifiers << 16 | evt->charCode(); | |
535 return mapKey ? keyPressCommandsMap->get(mapKey) : 0; | |
536 } | |
537 | |
538 bool EditorClientImpl::handleEditingKeyboardEvent( | |
539 WebCore::KeyboardEvent* evt) { | |
540 const WebCore::PlatformKeyboardEvent* keyEvent = evt->keyEvent(); | |
541 // do not treat this as text input if it's a system key event | |
542 if (!keyEvent || keyEvent->isSystemKey()) | |
543 return false; | |
544 | |
545 WebCore::Frame* frame = evt->target()->toNode()->document()->frame(); | |
546 if (!frame) | |
547 return false; | |
548 | |
549 WebCore::String command_name = interpretKeyEvent(evt); | |
550 WebCore::Editor::Command command = frame->editor()->command(command_name); | |
551 | |
552 if (keyEvent->type() == WebCore::PlatformKeyboardEvent::RawKeyDown) { | |
553 // WebKit doesn't have enough information about mode to decide how | |
554 // commands that just insert text if executed via Editor should be treated, | |
555 // so we leave it upon WebCore to either handle them immediately | |
556 // (e.g. Tab that changes focus) or let a keypress event be generated | |
557 // (e.g. Tab that inserts a Tab character, or Enter). | |
558 if (command.isTextInsertion() || command_name.isEmpty()) | |
559 return false; | |
560 if (command.execute(evt)) { | |
561 if (webview_->client()) { | |
562 webview_->client()->didExecuteCommand( | |
563 webkit_glue::StringToWebString(command_name)); | |
564 } | |
565 return true; | |
566 } | |
567 return false; | |
568 } | |
569 | |
570 if (command.execute(evt)) { | |
571 if (webview_->client()) { | |
572 webview_->client()->didExecuteCommand( | |
573 webkit_glue::StringToWebString(command_name)); | |
574 } | |
575 return true; | |
576 } | |
577 | |
578 // Here we need to filter key events. | |
579 // On Gtk/Linux, it emits key events with ASCII text and ctrl on for ctrl-<x>. | |
580 // In Webkit, EditorClient::handleKeyboardEvent in | |
581 // WebKit/gtk/WebCoreSupport/EditorClientGtk.cpp drop such events. | |
582 // On Mac, it emits key events with ASCII text and meta on for Command-<x>. | |
583 // These key events should not emit text insert event. | |
584 // Alt key would be used to insert alternative character, so we should let | |
585 // through. Also note that Ctrl-Alt combination equals to AltGr key which is | |
586 // also used to insert alternative character. | |
587 // http://code.google.com/p/chromium/issues/detail?id=10846 | |
588 // Windows sets both alt and meta are on when "Alt" key pressed. | |
589 // http://code.google.com/p/chromium/issues/detail?id=2215 | |
590 // Also, we should not rely on an assumption that keyboards don't | |
591 // send ASCII characters when pressing a control key on Windows, | |
592 // which may be configured to do it so by user. | |
593 // See also http://en.wikipedia.org/wiki/Keyboard_Layout | |
594 // TODO(ukai): investigate more detail for various keyboard layout. | |
595 if (evt->keyEvent()->text().length() == 1) { | |
596 UChar ch = evt->keyEvent()->text()[0U]; | |
597 | |
598 // Don't insert null or control characters as they can result in | |
599 // unexpected behaviour | |
600 if (ch < ' ') | |
601 return false; | |
602 #if !PLATFORM(WIN_OS) | |
603 // Don't insert ASCII character if ctrl w/o alt or meta is on. | |
604 // On Mac, we should ignore events when meta is on (Command-<x>). | |
605 if (ch < 0x80) { | |
606 if (evt->keyEvent()->ctrlKey() && !evt->keyEvent()->altKey()) | |
607 return false; | |
608 #if PLATFORM(DARWIN) | |
609 if (evt->keyEvent()->metaKey()) | |
610 return false; | |
611 #endif | |
612 } | |
613 #endif | |
614 } | |
615 | |
616 if (!frame->editor()->canEdit()) | |
617 return false; | |
618 | |
619 return frame->editor()->insertText(evt->keyEvent()->text(), evt); | |
620 } | |
621 | |
622 // | |
623 // End of code block subject to Apple, Inc. copyright | |
624 // | |
625 | |
626 void EditorClientImpl::handleKeyboardEvent(WebCore::KeyboardEvent* evt) { | |
627 if (evt->keyCode() == WebCore::VKEY_DOWN || | |
628 evt->keyCode() == WebCore::VKEY_UP) { | |
629 ASSERT(evt->target()->toNode()); | |
630 ShowFormAutofillForNode(evt->target()->toNode()); | |
631 } | |
632 | |
633 // Give the embedder a chance to handle the keyboard event. | |
634 if ((webview_->client() && | |
635 webview_->client()->handleCurrentKeyboardEvent()) || | |
636 handleEditingKeyboardEvent(evt)) | |
637 evt->setDefaultHandled(); | |
638 } | |
639 | |
640 void EditorClientImpl::handleInputMethodKeydown( | |
641 WebCore::KeyboardEvent* keyEvent) { | |
642 // We handle IME within chrome. | |
643 } | |
644 | |
645 void EditorClientImpl::textFieldDidBeginEditing(WebCore::Element*) { | |
646 } | |
647 | |
648 void EditorClientImpl::textFieldDidEndEditing(WebCore::Element* element) { | |
649 // Notification that focus was lost. Be careful with this, it's also sent | |
650 // when the page is being closed. | |
651 | |
652 // Cancel any pending DoAutofill call. | |
653 autofill_args_.clear(); | |
654 autofill_timer_.stop(); | |
655 | |
656 // Hide any showing popup. | |
657 webview_->HideAutoCompletePopup(); | |
658 | |
659 if (!webview_->client()) | |
660 return; // The page is getting closed, don't fill the password. | |
661 | |
662 // Notify any password-listener of the focus change. | |
663 WebCore::HTMLInputElement* input_element = | |
664 WebKit::toHTMLInputElement(element); | |
665 if (!input_element) | |
666 return; | |
667 | |
668 WebFrameImpl* webframe = | |
669 WebFrameImpl::FromFrame(input_element->document()->frame()); | |
670 PasswordAutocompleteListener* listener = | |
671 webframe->GetPasswordListener(input_element); | |
672 if (!listener) | |
673 return; | |
674 | |
675 listener->didBlurInputElement(input_element->value()); | |
676 } | |
677 | |
678 void EditorClientImpl::textDidChangeInTextField(WebCore::Element* element) { | |
679 ASSERT(element->hasLocalName(WebCore::HTMLNames::inputTag)); | |
680 // Note that we only show the autofill popup in this case if the caret is at | |
681 // the end. This matches FireFox and Safari but not IE. | |
682 Autofill(static_cast<WebCore::HTMLInputElement*>(element), | |
683 false, false, true); | |
684 } | |
685 | |
686 bool EditorClientImpl::ShowFormAutofillForNode(WebCore::Node* node) { | |
687 WebCore::HTMLInputElement* input_element = | |
688 WebKit::toHTMLInputElement(node); | |
689 if (input_element) | |
690 return Autofill(input_element, true, true, false); | |
691 return false; | |
692 } | |
693 | |
694 bool EditorClientImpl::Autofill(WebCore::HTMLInputElement* input_element, | |
695 bool autofill_form_only, | |
696 bool autofill_on_empty_value, | |
697 bool require_caret_at_end) { | |
698 // Cancel any pending DoAutofill call. | |
699 autofill_args_.clear(); | |
700 autofill_timer_.stop(); | |
701 | |
702 // Let's try to trigger autofill for that field, if applicable. | |
703 if (!input_element->isEnabledFormControl() || !input_element->isTextField() || | |
704 input_element->isPasswordField() || !input_element->autoComplete()) { | |
705 return false; | |
706 } | |
707 | |
708 string16 name = webkit_glue::StringToString16( | |
709 WebKit::nameOfInputElement(input_element)); | |
710 if (name.empty()) // If the field has no name, then we won't have values. | |
711 return false; | |
712 | |
713 // Don't attempt to autofill with values that are too large. | |
714 if (input_element->value().length() > kMaximumTextSizeForAutofill) | |
715 return false; | |
716 | |
717 autofill_args_ = new AutofillArgs(); | |
718 autofill_args_->input_element = input_element; | |
719 autofill_args_->autofill_form_only = autofill_form_only; | |
720 autofill_args_->autofill_on_empty_value = autofill_on_empty_value; | |
721 autofill_args_->require_caret_at_end = require_caret_at_end; | |
722 autofill_args_->backspace_or_delete_pressed = backspace_or_delete_pressed_; | |
723 | |
724 if (!require_caret_at_end) { | |
725 DoAutofill(NULL); | |
726 } else { | |
727 // We post a task for doing the autofill as the caret position is not set | |
728 // properly at this point (http://bugs.webkit.org/show_bug.cgi?id=16976) | |
729 // and we need it to determine whether or not to trigger autofill. | |
730 autofill_timer_.startOneShot(0.0); | |
731 } | |
732 return true; | |
733 } | |
734 | |
735 void EditorClientImpl::DoAutofill(WebCore::Timer<EditorClientImpl>* timer) { | |
736 OwnPtr<AutofillArgs> args(autofill_args_.release()); | |
737 WebCore::HTMLInputElement* input_element = args->input_element.get(); | |
738 | |
739 const WebCore::String& value = input_element->value(); | |
740 | |
741 // Enforce autofill_on_empty_value and caret_at_end. | |
742 bool is_caret_at_end = args->require_caret_at_end ? | |
743 input_element->selectionStart() == input_element->selectionEnd() && | |
744 input_element->selectionEnd() == static_cast<int>(value.length()) : | |
745 true; // When |require_caret_at_end| is false, just pretend we are at | |
746 // the end. | |
747 if ((!args->autofill_on_empty_value && value.isEmpty()) || !is_caret_at_end) { | |
748 webview_->HideAutoCompletePopup(); | |
749 return; | |
750 } | |
751 | |
752 // First let's see if there is a password listener for that element. | |
753 // We won't trigger form autofill in that case, as having both behavior on | |
754 // a node would be confusing. | |
755 WebFrameImpl* webframe = | |
756 WebFrameImpl::FromFrame(input_element->document()->frame()); | |
757 PasswordAutocompleteListener* listener = | |
758 webframe->GetPasswordListener(input_element); | |
759 if (listener) { | |
760 if (args->autofill_form_only) | |
761 return; | |
762 | |
763 listener->performInlineAutocomplete( | |
764 value, args->backspace_or_delete_pressed, true); | |
765 return; | |
766 } | |
767 | |
768 // Then trigger form autofill. | |
769 string16 name = webkit_glue::StringToString16( | |
770 WebKit::nameOfInputElement(input_element)); | |
771 ASSERT(static_cast<int>(name.length()) > 0); | |
772 | |
773 if (webview_->client()) { | |
774 webview_->client()->queryAutofillSuggestions( | |
775 webkit_glue::NodeToWebNode(input_element), name, | |
776 webkit_glue::StringToWebString(value)); | |
777 } | |
778 } | |
779 | |
780 void EditorClientImpl::CancelPendingAutofill() { | |
781 autofill_args_.clear(); | |
782 autofill_timer_.stop(); | |
783 } | |
784 | |
785 void EditorClientImpl::OnAutofillSuggestionAccepted( | |
786 WebCore::HTMLInputElement* text_field) { | |
787 WebFrameImpl* webframe = | |
788 WebFrameImpl::FromFrame(text_field->document()->frame()); | |
789 PasswordAutocompleteListener* listener = | |
790 webframe->GetPasswordListener(text_field); | |
791 // Password listeners need to autocomplete other fields that depend on the | |
792 // input element with autofill suggestions. | |
793 if (listener) | |
794 listener->performInlineAutocomplete(text_field->value(), false, false); | |
795 } | |
796 | |
797 bool EditorClientImpl::doTextFieldCommandFromEvent( | |
798 WebCore::Element* element, | |
799 WebCore::KeyboardEvent* event) { | |
800 // Remember if backspace was pressed for the autofill. It is not clear how to | |
801 // find if backspace was pressed from textFieldDidBeginEditing and | |
802 // textDidChangeInTextField as when these methods are called the value of the | |
803 // input element already contains the type character. | |
804 backspace_or_delete_pressed_ = (event->keyCode() == WebCore::VKEY_BACK) || | |
805 (event->keyCode() == WebCore::VKEY_DELETE); | |
806 | |
807 // The Mac code appears to use this method as a hook to implement special | |
808 // keyboard commands specific to Safari's auto-fill implementation. We | |
809 // just return false to allow the default action. | |
810 return false; | |
811 } | |
812 | |
813 void EditorClientImpl::textWillBeDeletedInTextField(WebCore::Element*) { | |
814 } | |
815 | |
816 void EditorClientImpl::textDidChangeInTextArea(WebCore::Element*) { | |
817 } | |
818 | |
819 void EditorClientImpl::ignoreWordInSpellDocument(const WebCore::String&) { | |
820 notImplemented(); | |
821 } | |
822 | |
823 void EditorClientImpl::learnWord(const WebCore::String&) { | |
824 notImplemented(); | |
825 } | |
826 | |
827 void EditorClientImpl::checkSpellingOfString(const UChar* text, int length, | |
828 int* misspellingLocation, | |
829 int* misspellingLength) { | |
830 // SpellCheckWord will write (0, 0) into the output vars, which is what our | |
831 // caller expects if the word is spelled correctly. | |
832 int spell_location = -1; | |
833 int spell_length = 0; | |
834 | |
835 // Check to see if the provided text is spelled correctly. | |
836 if (isContinuousSpellCheckingEnabled() && webview_->client()) { | |
837 webview_->client()->spellCheck( | |
838 WebString(text, length), spell_location, spell_length); | |
839 } else { | |
840 spell_location = 0; | |
841 spell_length = 0; | |
842 } | |
843 | |
844 // Note: the Mac code checks if the pointers are NULL before writing to them, | |
845 // so we do too. | |
846 if (misspellingLocation) | |
847 *misspellingLocation = spell_location; | |
848 if (misspellingLength) | |
849 *misspellingLength = spell_length; | |
850 } | |
851 | |
852 WebCore::String EditorClientImpl::getAutoCorrectSuggestionForMisspelledWord( | |
853 const WebCore::String& misspelledWord) { | |
854 if (!(isContinuousSpellCheckingEnabled() && webview_->client())) | |
855 return WebCore::String(); | |
856 | |
857 // Do not autocorrect words with capital letters in it except the | |
858 // first letter. This will remove cases changing "IMB" to "IBM". | |
859 for (size_t i = 1; i < misspelledWord.length(); i++) { | |
860 if (u_isupper(static_cast<UChar32>(misspelledWord[i]))) | |
861 return WebCore::String(); | |
862 } | |
863 | |
864 return webkit_glue::WebStringToString(webview_->client()->autoCorrectWord( | |
865 webkit_glue::StringToWebString(misspelledWord))); | |
866 } | |
867 | |
868 void EditorClientImpl::checkGrammarOfString(const UChar*, int length, | |
869 WTF::Vector<WebCore::GrammarDetail>&, int* badGrammarLocation, | |
870 int* badGrammarLength) { | |
871 notImplemented(); | |
872 if (badGrammarLocation) | |
873 *badGrammarLocation = 0; | |
874 if (badGrammarLength) | |
875 *badGrammarLength = 0; | |
876 } | |
877 | |
878 void EditorClientImpl::updateSpellingUIWithGrammarString(const WebCore::String&, | |
879 const WebCore::GrammarDetail& detail) { | |
880 notImplemented(); | |
881 } | |
882 | |
883 void EditorClientImpl::updateSpellingUIWithMisspelledWord( | |
884 const WebCore::String& misspelled_word) { | |
885 if (webview_->client()) { | |
886 webview_->client()->updateSpellingUIWithMisspelledWord( | |
887 webkit_glue::StringToWebString(misspelled_word)); | |
888 } | |
889 } | |
890 | |
891 void EditorClientImpl::showSpellingUI(bool show) { | |
892 if (webview_->client()) | |
893 webview_->client()->showSpellingUI(show); | |
894 } | |
895 | |
896 bool EditorClientImpl::spellingUIIsShowing() { | |
897 if (webview_->client()) | |
898 return webview_->client()->isShowingSpellingUI(); | |
899 return false; | |
900 } | |
901 | |
902 void EditorClientImpl::getGuessesForWord(const WebCore::String&, | |
903 WTF::Vector<WebCore::String>& guesses) { | |
904 notImplemented(); | |
905 } | |
906 | |
907 void EditorClientImpl::setInputMethodState(bool enabled) { | |
908 if (webview_->client()) | |
909 webview_->client()->setInputMethodEnabled(enabled); | |
910 } | |
OLD | NEW |