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

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

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

Powered by Google App Engine
This is Rietveld 408576698