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