OSDN Git Service

Merge WebKit at r78450: Initial merge by git.
[android-x86/external-webkit.git] / Source / WebKit2 / WebProcess / WebCoreSupport / WebEditorClient.cpp
1 /*
2  * Copyright (C) 2010 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "WebEditorClient.h"
28
29 #define DISABLE_NOT_IMPLEMENTED_WARNINGS 1
30 #include "NotImplemented.h"
31
32 #include "SelectionState.h"
33 #include "WebFrameLoaderClient.h"
34 #include "WebPage.h"
35 #include "WebPageProxyMessages.h"
36 #include "WebProcess.h"
37 #include <WebCore/ArchiveResource.h>
38 #include <WebCore/DocumentFragment.h>
39 #include <WebCore/EditCommand.h>
40 #include <WebCore/FocusController.h>
41 #include <WebCore/Frame.h>
42 #include <WebCore/HTMLInputElement.h>
43 #include <WebCore/HTMLNames.h>
44 #include <WebCore/HTMLTextAreaElement.h>
45 #include <WebCore/KeyboardEvent.h>
46 #include <WebCore/Page.h>
47 #include <WebCore/UserTypingGestureIndicator.h>
48
49 using namespace WebCore;
50 using namespace HTMLNames;
51
52 namespace WebKit {
53
54 void WebEditorClient::pageDestroyed()
55 {
56     delete this;
57 }
58
59 bool WebEditorClient::shouldDeleteRange(Range* range)
60 {
61     bool result = m_page->injectedBundleEditorClient().shouldDeleteRange(m_page, range);
62     notImplemented();
63     return result;
64 }
65
66 bool WebEditorClient::shouldShowDeleteInterface(HTMLElement*)
67 {
68     notImplemented();
69     return false;
70 }
71
72 bool WebEditorClient::smartInsertDeleteEnabled()
73 {
74     // FIXME: Why isn't this Mac specific like toggleSmartInsertDeleteEnabled?
75 #if PLATFORM(MAC)
76     return m_page->isSmartInsertDeleteEnabled();
77 #else
78     return true;
79 #endif
80 }
81  
82 bool WebEditorClient::isSelectTrailingWhitespaceEnabled()
83 {
84     notImplemented();
85     return false;
86 }
87
88 bool WebEditorClient::isContinuousSpellCheckingEnabled()
89 {
90     return WebProcess::shared().textCheckerState().isContinuousSpellCheckingEnabled;
91 }
92
93 void WebEditorClient::toggleContinuousSpellChecking()
94 {
95     notImplemented();
96 }
97
98 bool WebEditorClient::isGrammarCheckingEnabled()
99 {
100     return WebProcess::shared().textCheckerState().isGrammarCheckingEnabled;
101 }
102
103 void WebEditorClient::toggleGrammarChecking()
104 {
105     notImplemented();
106 }
107
108 int WebEditorClient::spellCheckerDocumentTag()
109 {
110     notImplemented();
111     return false;
112 }
113
114     
115 bool WebEditorClient::isEditable()
116 {
117     notImplemented();
118     return false;
119 }
120
121
122 bool WebEditorClient::shouldBeginEditing(Range* range)
123 {
124     bool result = m_page->injectedBundleEditorClient().shouldBeginEditing(m_page, range);
125     notImplemented();
126     return result;
127 }
128
129 bool WebEditorClient::shouldEndEditing(Range* range)
130 {
131     bool result = m_page->injectedBundleEditorClient().shouldEndEditing(m_page, range);
132     notImplemented();
133     return result;
134 }
135
136 bool WebEditorClient::shouldInsertNode(Node* node, Range* rangeToReplace, EditorInsertAction action)
137 {
138     bool result = m_page->injectedBundleEditorClient().shouldInsertNode(m_page, node, rangeToReplace, action);
139     notImplemented();
140     return result;
141 }
142
143 bool WebEditorClient::shouldInsertText(const String& text, Range* rangeToReplace, EditorInsertAction action)
144 {
145     bool result = m_page->injectedBundleEditorClient().shouldInsertText(m_page, text.impl(), rangeToReplace, action);
146     notImplemented();
147     return result;
148 }
149
150 bool WebEditorClient::shouldChangeSelectedRange(Range* fromRange, Range* toRange, EAffinity affinity, bool stillSelecting)
151 {
152     bool result = m_page->injectedBundleEditorClient().shouldChangeSelectedRange(m_page, fromRange, toRange, affinity, stillSelecting);
153     notImplemented();
154     return result;
155 }
156     
157 bool WebEditorClient::shouldApplyStyle(CSSStyleDeclaration* style, Range* range)
158 {
159     bool result = m_page->injectedBundleEditorClient().shouldApplyStyle(m_page, style, range);
160     notImplemented();
161     return result;
162 }
163
164 bool WebEditorClient::shouldMoveRangeAfterDelete(Range*, Range*)
165 {
166     notImplemented();
167     return true;
168 }
169
170 void WebEditorClient::didBeginEditing()
171 {
172     // FIXME: What good is a notification name, if it's always the same?
173     static const String WebViewDidBeginEditingNotification = "WebViewDidBeginEditingNotification";
174     m_page->injectedBundleEditorClient().didBeginEditing(m_page, WebViewDidBeginEditingNotification.impl());
175     notImplemented();
176 }
177
178 void WebEditorClient::respondToChangedContents()
179 {
180     static const String WebViewDidChangeNotification = "WebViewDidChangeNotification";
181     m_page->injectedBundleEditorClient().didChange(m_page, WebViewDidChangeNotification.impl());
182     notImplemented();
183 }
184
185 void WebEditorClient::respondToChangedSelection()
186 {
187     static const String WebViewDidChangeSelectionNotification = "WebViewDidChangeSelectionNotification";
188     m_page->injectedBundleEditorClient().didChangeSelection(m_page, WebViewDidChangeSelectionNotification.impl());
189     Frame* frame = m_page->corePage()->focusController()->focusedFrame();
190     if (!frame)
191         return;
192
193     SelectionState selectionState;
194     selectionState.isNone = frame->selection()->isNone();
195     selectionState.isContentEditable = frame->selection()->isContentEditable();
196     selectionState.isInPasswordField = frame->selection()->isInPasswordField();
197     selectionState.hasComposition = frame->editor()->hasComposition();
198
199     WebPage::getLocationAndLengthFromRange(frame->selection()->toNormalizedRange().get(), selectionState.selectedRangeStart, selectionState.selectedRangeLength);
200
201     m_page->send(Messages::WebPageProxy::SelectionStateChanged(selectionState));
202
203 #if PLATFORM(WIN)
204     // FIXME: This should also go into the selection state.
205     if (!frame->editor()->hasComposition() || frame->editor()->ignoreCompositionSelectionChange())
206         return;
207
208     unsigned start;
209     unsigned end;
210     m_page->send(Messages::WebPageProxy::DidChangeCompositionSelection(frame->editor()->getCompositionSelection(start, end)));
211 #endif
212 }
213     
214 void WebEditorClient::didEndEditing()
215 {
216     static const String WebViewDidEndEditingNotification = "WebViewDidEndEditingNotification";
217     m_page->injectedBundleEditorClient().didEndEditing(m_page, WebViewDidEndEditingNotification.impl());
218     notImplemented();
219 }
220
221 void WebEditorClient::didWriteSelectionToPasteboard()
222 {
223     notImplemented();
224 }
225
226 void WebEditorClient::didSetSelectionTypesForPasteboard()
227 {
228     notImplemented();
229 }
230
231 void WebEditorClient::registerCommandForUndo(PassRefPtr<EditCommand> command)
232 {
233     // FIXME: Add assertion that the command being reapplied is the same command that is
234     // being passed to us.
235     if (m_page->isInRedo())
236         return;
237
238     RefPtr<WebEditCommand> webCommand = WebEditCommand::create(command);
239     m_page->addWebEditCommand(webCommand->commandID(), webCommand.get());
240     uint32_t editAction = static_cast<uint32_t>(webCommand->command()->editingAction());
241
242     m_page->send(Messages::WebPageProxy::RegisterEditCommandForUndo(webCommand->commandID(), editAction));
243 }
244
245 void WebEditorClient::registerCommandForRedo(PassRefPtr<EditCommand>)
246 {
247 }
248
249 void WebEditorClient::clearUndoRedoOperations()
250 {
251     m_page->send(Messages::WebPageProxy::ClearAllEditCommands());
252 }
253
254 bool WebEditorClient::canUndo() const
255 {
256     notImplemented();
257     return false;
258 }
259
260 bool WebEditorClient::canRedo() const
261 {
262     notImplemented();
263     return false;
264 }
265
266 void WebEditorClient::undo()
267 {
268     notImplemented();
269 }
270
271 void WebEditorClient::redo()
272 {
273     notImplemented();
274 }
275
276 #if !PLATFORM(MAC)
277 void WebEditorClient::handleKeyboardEvent(KeyboardEvent* event)
278 {
279     if (m_page->handleEditingKeyboardEvent(event))
280         event->setDefaultHandled();
281 }
282
283 void WebEditorClient::handleInputMethodKeydown(KeyboardEvent*)
284 {
285     notImplemented();
286 }
287 #endif
288
289 void WebEditorClient::textFieldDidBeginEditing(Element* element)
290 {
291     if (!element->hasTagName(inputTag))
292         return;
293
294     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
295     m_page->injectedBundleFormClient().textFieldDidBeginEditing(m_page, static_cast<HTMLInputElement*>(element), webFrame);
296 }
297
298 void WebEditorClient::textFieldDidEndEditing(Element* element)
299 {
300     if (!element->hasTagName(inputTag))
301         return;
302
303     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
304     m_page->injectedBundleFormClient().textFieldDidEndEditing(m_page, static_cast<HTMLInputElement*>(element), webFrame);
305 }
306
307 void WebEditorClient::textDidChangeInTextField(Element* element)
308 {
309     if (!element->hasTagName(inputTag))
310         return;
311
312     if (!UserTypingGestureIndicator::processingUserTypingGesture() || UserTypingGestureIndicator::focusedElementAtGestureStart() != element)
313         return;
314
315     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
316     m_page->injectedBundleFormClient().textDidChangeInTextField(m_page, static_cast<HTMLInputElement*>(element), webFrame);
317 }
318
319 void WebEditorClient::textDidChangeInTextArea(Element* element)
320 {
321     if (!element->hasTagName(textareaTag))
322         return;
323
324     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
325     m_page->injectedBundleFormClient().textDidChangeInTextArea(m_page, static_cast<HTMLTextAreaElement*>(element), webFrame);
326 }
327
328 static bool getActionTypeForKeyEvent(KeyboardEvent* event, WKInputFieldActionType& type)
329 {
330     String key = event->keyIdentifier();
331     if (key == "Up")
332         type = WKInputFieldActionTypeMoveUp;
333     else if (key == "Down")
334         type = WKInputFieldActionTypeMoveDown;
335     else if (key == "U+001B")
336         type = WKInputFieldActionTypeCancel;
337     else if (key == "U+0009") {
338         if (event->shiftKey())
339             type = WKInputFieldActionTypeInsertBacktab;
340         else
341             type = WKInputFieldActionTypeInsertTab;
342     } else if (key == "Enter")
343         type = WKInputFieldActionTypeInsertNewline;
344     else
345         return false;
346
347     return true;
348 }
349
350 bool WebEditorClient::doTextFieldCommandFromEvent(Element* element, KeyboardEvent* event)
351 {
352     if (!element->hasTagName(inputTag))
353         return false;
354
355     WKInputFieldActionType actionType = static_cast<WKInputFieldActionType>(0);
356     if (!getActionTypeForKeyEvent(event, actionType))
357         return false;
358
359     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
360     return m_page->injectedBundleFormClient().shouldPerformActionInTextField(m_page, static_cast<HTMLInputElement*>(element), actionType, webFrame);
361 }
362
363 void WebEditorClient::textWillBeDeletedInTextField(Element* element)
364 {
365     if (!element->hasTagName(inputTag))
366         return;
367
368     WebFrame* webFrame =  static_cast<WebFrameLoaderClient*>(element->document()->frame()->loader()->client())->webFrame();
369     m_page->injectedBundleFormClient().shouldPerformActionInTextField(m_page, static_cast<HTMLInputElement*>(element), WKInputFieldActionTypeInsertDelete, webFrame);
370 }
371
372 void WebEditorClient::ignoreWordInSpellDocument(const String& word)
373 {
374     m_page->send(Messages::WebPageProxy::IgnoreWord(word));
375 }
376
377 void WebEditorClient::learnWord(const String& word)
378 {
379     m_page->send(Messages::WebPageProxy::LearnWord(word));
380 }
381
382 void WebEditorClient::checkSpellingOfString(const UChar*, int, int*, int*)
383 {
384     notImplemented();
385 }
386
387 String WebEditorClient::getAutoCorrectSuggestionForMisspelledWord(const String&)
388 {
389     notImplemented();
390     return String();
391 }
392
393 void WebEditorClient::checkGrammarOfString(const UChar*, int, Vector<GrammarDetail>&, int*, int*)
394 {
395     notImplemented();
396 }
397
398 void WebEditorClient::updateSpellingUIWithGrammarString(const String&, const GrammarDetail&)
399 {
400     notImplemented();
401 }
402
403 void WebEditorClient::updateSpellingUIWithMisspelledWord(const String& misspelledWord)
404 {
405     m_page->send(Messages::WebPageProxy::UpdateSpellingUIWithMisspelledWord(misspelledWord));
406 }
407
408 void WebEditorClient::showSpellingUI(bool)
409 {
410     notImplemented();
411 }
412
413 bool WebEditorClient::spellingUIIsShowing()
414 {
415     notImplemented();
416     return false;
417 }
418
419 void WebEditorClient::getGuessesForWord(const String& word, const String& context, Vector<String>& guesses)
420 {
421     m_page->sendSync(Messages::WebPageProxy::GetGuessesForWord(word, context), Messages::WebPageProxy::GetGuessesForWord::Reply(guesses));
422 }
423
424 void WebEditorClient::willSetInputMethodState()
425 {
426     notImplemented();
427 }
428
429 void WebEditorClient::setInputMethodState(bool)
430 {
431     notImplemented();
432 }
433
434 void WebEditorClient::requestCheckingOfString(WebCore::SpellChecker*, int, const WTF::String&)
435 {
436     notImplemented();
437 }
438
439 } // namespace WebKit