Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: third_party/WebKit/Source/web/WebInputMethodControllerImpl.cpp

Issue 2333813002: Introduce WebInputMethodController to blink (Closed)
Patch Set: Moved ConfirmCompositionBehavior to WebInputMethodController Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2016 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 #include "web/WebInputMethodControllerImpl.h"
32
33 #include "core/InputTypeNames.h"
34 #include "core/editing/EditingUtilities.h"
35 #include "core/editing/Editor.h"
36 #include "core/editing/EphemeralRange.h"
37 #include "core/editing/FrameSelection.h"
38 #include "core/editing/InputMethodController.h"
39 #include "core/editing/PlainTextRange.h"
40 #include "core/frame/LocalFrame.h"
41 #include "core/page/FocusController.h"
42 #include "core/page/Page.h"
43 #include "platform/UserGestureIndicator.h"
44 #include "public/platform/WebString.h"
45 #include "public/web/WebPlugin.h"
46 #include "public/web/WebRange.h"
47 #include "web/CompositionUnderlineVectorBuilder.h"
48 #include "web/WebLocalFrameImpl.h"
49 #include "web/WebPluginContainerImpl.h"
50
51 namespace blink {
52
53 WebInputMethodControllerImpl::WebInputMethodControllerImpl(WebLocalFrameImpl* we bLocalFrame)
54 : m_webLocalFrame(webLocalFrame)
55 , m_suppressNextKeypressEvent(false)
56 {
57 }
58
59 WebInputMethodControllerImpl::~WebInputMethodControllerImpl()
60 {
61 }
62
63 // static
64 WebInputMethodControllerImpl* WebInputMethodControllerImpl::fromFrame(LocalFrame * frame)
65 {
66 WebLocalFrameImpl* webLocalFrameImpl = WebLocalFrameImpl::fromFrame(frame);
67 return webLocalFrameImpl ? webLocalFrameImpl->inputMethodController() : null ptr;
68 }
69
70 DEFINE_TRACE(WebInputMethodControllerImpl)
71 {
72 visitor->trace(m_webLocalFrame);
73 }
74
75 bool WebInputMethodControllerImpl::setComposition(
76 const WebString& text,
77 const WebVector<WebCompositionUnderline>& underlines,
78 int selectionStart,
79 int selectionEnd)
80 {
81 if (WebPlugin* plugin = focusedPluginIfInputMethodSupported())
82 return plugin->setComposition(text, underlines, selectionStart, selectio nEnd);
83
84 // We should use this |editor| object only to complete the ongoing
85 // composition.
86 if (!frame()->editor().canEdit() && !inputMethodController().hasComposition( ))
87 return false;
88
89 // We should verify the parent node of this IME composition node are
90 // editable because JavaScript may delete a parent node of the composition
91 // node. In this case, WebKit crashes while deleting texts from the parent
92 // node, which doesn't exist any longer.
93 const EphemeralRange range = inputMethodController().compositionEphemeralRan ge();
94 if (range.isNotNull()) {
95 Node* node = range.startPosition().computeContainerNode();
96 frame()->document()->updateStyleAndLayoutTree();
97 if (!node || !hasEditableStyle(*node))
98 return false;
99 }
100
101 // A keypress event is canceled. If an ongoing composition exists, then the
102 // keydown event should have arisen from a handled key (e.g., backspace).
103 // In this case we ignore the cancellation and continue; otherwise (no
104 // ongoing composition) we exit and signal success only for attempts to
105 // clear the composition.
106 if (m_suppressNextKeypressEvent && !inputMethodController().hasComposition() )
107 return text.isEmpty();
108
109 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
110
111 // When the range of composition underlines overlap with the range between
112 // selectionStart and selectionEnd, WebKit somehow won't paint the selection
113 // at all (see InlineTextBox::paint() function in InlineTextBox.cpp).
114 // But the selection range actually takes effect.
115 inputMethodController().setComposition(String(text),
116 CompositionUnderlineVectorBuilder(underlines),
117 selectionStart, selectionEnd);
118
119 return text.isEmpty() || inputMethodController().hasComposition();
120 }
121
122 bool WebInputMethodControllerImpl::finishComposingText(ConfirmCompositionBehavio r selectionBehavior)
123 {
124 // TODO(ekaramad): Here and in other IME calls we should expect the
125 // call to be made when our frame is focused. This, however, is not the case
126 // all the time. For instance, resetInputMethod call on RenderViewImpl could
127 // be after losing the focus on frame. But since we return the core frame
128 // in WebViewImpl::focusedLocalFrameInWidget(), we will reach here with
129 // |m_webLocalFrame| not focused on page.
130
131 if (WebPlugin* plugin = focusedPluginIfInputMethodSupported())
132 return plugin->finishComposingText(selectionBehavior);
133 return inputMethodController().finishComposingText(selectionBehavior == WebI nputMethodController::KeepSelection ? InputMethodController::KeepSelection : Inp utMethodController::DoNotKeepSelection);
134 }
135
136 bool WebInputMethodControllerImpl::commitText(const WebString& text, int relativ eCaretPosition)
137 {
138 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
139
140 if (WebPlugin* plugin = focusedPluginIfInputMethodSupported())
141 return plugin->commitText(text, relativeCaretPosition);
142
143 // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets
144 // needs to be audited. See http://crbug.com/590369 for more details.
145 frame()->document()->updateStyleAndLayoutIgnorePendingStylesheets();
146
147 return inputMethodController().commitText(text, relativeCaretPosition);
148 }
149
150 LocalFrame* WebInputMethodControllerImpl::frame() const
151 {
152 return m_webLocalFrame->frame();
153 }
154
155 InputMethodController& WebInputMethodControllerImpl::inputMethodController() con st
156 {
157 return frame()->inputMethodController();
158 }
159
160 WebPlugin* WebInputMethodControllerImpl::focusedPluginIfInputMethodSupported() c onst
161 {
162 WebPluginContainerImpl* container = WebLocalFrameImpl::currentPluginContaine r(frame());
163 if (container && container->supportsInputMethod())
164 return container->plugin();
165 return nullptr;
166 }
167
168 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698