1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef EditorClientImpl_h
32#define EditorClientImpl_h
33
34#include "EditorClient.h"
35#include "TextCheckerClient.h"
36#include "Timer.h"
37#include <wtf/Deque.h>
38
39namespace WebCore {
40class HTMLInputElement;
41class SpellChecker;
42}
43
44namespace WebKit {
45class WebViewImpl;
46
47class EditorClientImpl : public WebCore::EditorClient, public WebCore::TextCheckerClient {
48public:
49    EditorClientImpl(WebViewImpl* webView);
50
51    virtual ~EditorClientImpl();
52    virtual void pageDestroyed();
53
54    virtual bool shouldShowDeleteInterface(WebCore::HTMLElement*);
55    virtual bool smartInsertDeleteEnabled();
56    virtual bool isSelectTrailingWhitespaceEnabled();
57    virtual bool isContinuousSpellCheckingEnabled();
58    virtual void toggleContinuousSpellChecking();
59    virtual bool isGrammarCheckingEnabled();
60    virtual void toggleGrammarChecking();
61    virtual int spellCheckerDocumentTag();
62    virtual bool shouldBeginEditing(WebCore::Range*);
63    virtual bool shouldEndEditing(WebCore::Range*);
64    virtual bool shouldInsertNode(WebCore::Node*, WebCore::Range*, WebCore::EditorInsertAction);
65    virtual bool shouldInsertText(const WTF::String&, WebCore::Range*, WebCore::EditorInsertAction);
66    virtual bool shouldDeleteRange(WebCore::Range*);
67    virtual bool shouldChangeSelectedRange(WebCore::Range* fromRange,
68                                           WebCore::Range* toRange,
69                                           WebCore::EAffinity,
70                                           bool stillSelecting);
71    virtual bool shouldApplyStyle(WebCore::CSSStyleDeclaration*, WebCore::Range*);
72    virtual bool shouldMoveRangeAfterDelete(WebCore::Range*, WebCore::Range*);
73    virtual void didBeginEditing();
74    virtual void respondToChangedContents();
75    virtual void respondToChangedSelection();
76    virtual void didEndEditing();
77    virtual void didWriteSelectionToPasteboard();
78    virtual void didSetSelectionTypesForPasteboard();
79    virtual void registerCommandForUndo(PassRefPtr<WebCore::EditCommand>);
80    virtual void registerCommandForRedo(PassRefPtr<WebCore::EditCommand>);
81    virtual void clearUndoRedoOperations();
82    virtual bool canCopyCut(bool defaultValue) const;
83    virtual bool canPaste(bool defaultValue) const;
84    virtual bool canUndo() const;
85    virtual bool canRedo() const;
86    virtual void undo();
87    virtual void redo();
88    virtual const char* interpretKeyEvent(const WebCore::KeyboardEvent*);
89    virtual bool handleEditingKeyboardEvent(WebCore::KeyboardEvent*);
90    virtual void handleKeyboardEvent(WebCore::KeyboardEvent*);
91    virtual void handleInputMethodKeydown(WebCore::KeyboardEvent*);
92    virtual void textFieldDidBeginEditing(WebCore::Element*);
93    virtual void textFieldDidEndEditing(WebCore::Element*);
94    virtual void textDidChangeInTextField(WebCore::Element*);
95    virtual bool doTextFieldCommandFromEvent(WebCore::Element*, WebCore::KeyboardEvent*);
96    virtual void textWillBeDeletedInTextField(WebCore::Element*);
97    virtual void textDidChangeInTextArea(WebCore::Element*);
98    virtual void ignoreWordInSpellDocument(const WTF::String&);
99    virtual void learnWord(const WTF::String&);
100    virtual void checkSpellingOfString(const UChar*, int length,
101                                       int* misspellingLocation,
102                                       int* misspellingLength);
103    virtual void checkGrammarOfString(const UChar*, int length,
104                                      WTF::Vector<WebCore::GrammarDetail>&,
105                                      int* badGrammarLocation,
106                                      int* badGrammarLength);
107    virtual WTF::String getAutoCorrectSuggestionForMisspelledWord(const WTF::String&);
108    virtual void updateSpellingUIWithGrammarString(const WTF::String&, const WebCore::GrammarDetail&);
109    virtual void updateSpellingUIWithMisspelledWord(const WTF::String&);
110    virtual void showSpellingUI(bool show);
111    virtual bool spellingUIIsShowing();
112    virtual void getGuessesForWord(const WTF::String& word,
113                                   const WTF::String& context,
114                                   WTF::Vector<WTF::String>& guesses);
115    virtual void willSetInputMethodState();
116    virtual void setInputMethodState(bool enabled);
117    virtual void requestCheckingOfString(WebCore::SpellChecker*, int, WebCore::TextCheckingTypeMask, const WTF::String&);
118
119    virtual WebCore::TextCheckerClient* textChecker() { return this; }
120
121    // Shows the form autofill popup for |node| if it is an HTMLInputElement and
122    // it is empty.  This is called when you press the up or down arrow in a
123    // text-field or when clicking an already focused text-field.
124    // Returns true if the autofill popup has been scheduled to be shown, false
125    // otherwise.
126    virtual bool showFormAutofillForNode(WebCore::Node*);
127
128private:
129    void modifySelection(WebCore::Frame*, WebCore::KeyboardEvent*);
130
131    // Triggers autofill for an input element if applicable.  This can be form
132    // autofill (via a popup-menu) or password autofill depending on the
133    // input element.  If |formAutofillOnly| is true, password autofill is not
134    // triggered.
135    // |autofillOnEmptyValue| indicates whether the autofill should be shown
136    // when the text-field is empty.
137    // If |requiresCaretAtEnd| is true, the autofill popup is only shown if the
138    // caret is located at the end of the entered text.
139    // Returns true if the autofill popup has been scheduled to be shown, false
140    // otherwise.
141    bool autofill(WebCore::HTMLInputElement*,
142                  bool formAutofillOnly, bool autofillOnEmptyValue,
143                  bool requiresCaretAtEnd);
144
145    // Called to process the autofill described by m_autofillArgs.
146    // This method is invoked asynchronously if the caret position is not
147    // reflecting the last text change yet, and we need it to decide whether or
148    // not to show the autofill popup.
149    void doAutofill(WebCore::Timer<EditorClientImpl>*);
150
151    void cancelPendingAutofill();
152
153    // Returns whether or not the focused control needs spell-checking.
154    // Currently, this function just retrieves the focused node and determines
155    // whether or not it is a <textarea> element or an element whose
156    // contenteditable attribute is true.
157    // FIXME: Bug 740540: This code just implements the default behavior
158    // proposed in this issue. We should also retrieve "spellcheck" attributes
159    // for text fields and create a flag to over-write the default behavior.
160    bool shouldSpellcheckByDefault();
161
162    WebViewImpl* m_webView;
163    bool m_inRedo;
164
165    typedef Deque<RefPtr<WebCore::EditCommand> > EditCommandStack;
166    EditCommandStack m_undoStack;
167    EditCommandStack m_redoStack;
168
169    // Whether the last entered key was a backspace.
170    bool m_backspaceOrDeletePressed;
171
172    // This flag is set to false if spell check for this editor is manually
173    // turned off. The default setting is SpellCheckAutomatic.
174    enum {
175        SpellCheckAutomatic,
176        SpellCheckForcedOn,
177        SpellCheckForcedOff
178    };
179    int m_spellCheckThisFieldStatus;
180
181    // Used to delay autofill processing.
182    WebCore::Timer<EditorClientImpl> m_autofillTimer;
183
184    struct AutofillArgs {
185        RefPtr<WebCore::HTMLInputElement> inputElement;
186        bool autofillFormOnly;
187        bool autofillOnEmptyValue;
188        bool requireCaretAtEnd;
189        bool backspaceOrDeletePressed;
190    };
191    OwnPtr<AutofillArgs> m_autofillArgs;
192};
193
194} // namespace WebKit
195
196#endif
197