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/dom/DocumentUserGestureToken.h" |
| 9 #include "core/editing/EditingUtilities.h" |
| 10 #include "core/editing/Editor.h" |
| 11 #include "core/editing/EphemeralRange.h" |
| 12 #include "core/editing/FrameSelection.h" |
| 13 #include "core/editing/InputMethodController.h" |
| 14 #include "core/editing/PlainTextRange.h" |
| 15 #include "core/frame/LocalFrame.h" |
| 16 #include "core/page/FocusController.h" |
| 17 #include "core/page/Page.h" |
| 18 #include "platform/UserGestureIndicator.h" |
| 19 #include "public/platform/WebString.h" |
| 20 #include "public/web/WebPlugin.h" |
| 21 #include "public/web/WebRange.h" |
| 22 #include "web/CompositionUnderlineVectorBuilder.h" |
| 23 #include "web/WebLocalFrameImpl.h" |
| 24 #include "web/WebPluginContainerImpl.h" |
| 25 |
| 26 namespace blink { |
| 27 |
| 28 WebInputMethodControllerImpl::WebInputMethodControllerImpl( |
| 29 WebLocalFrameImpl* webLocalFrame) |
| 30 : m_webLocalFrame(webLocalFrame), m_suppressNextKeypressEvent(false) {} |
| 31 |
| 32 WebInputMethodControllerImpl::~WebInputMethodControllerImpl() {} |
| 33 |
| 34 // static |
| 35 WebInputMethodControllerImpl* WebInputMethodControllerImpl::fromFrame( |
| 36 LocalFrame* frame) { |
| 37 WebLocalFrameImpl* webLocalFrameImpl = WebLocalFrameImpl::fromFrame(frame); |
| 38 return webLocalFrameImpl ? webLocalFrameImpl->inputMethodController() |
| 39 : nullptr; |
| 40 } |
| 41 |
| 42 DEFINE_TRACE(WebInputMethodControllerImpl) { |
| 43 visitor->trace(m_webLocalFrame); |
| 44 } |
| 45 |
| 46 bool WebInputMethodControllerImpl::setComposition( |
| 47 const WebString& text, |
| 48 const WebVector<WebCompositionUnderline>& underlines, |
| 49 int selectionStart, |
| 50 int selectionEnd) { |
| 51 if (WebPlugin* plugin = focusedPluginIfInputMethodSupported()) { |
| 52 return plugin->setComposition(text, underlines, selectionStart, |
| 53 selectionEnd); |
| 54 } |
| 55 |
| 56 // We should use this |editor| object only to complete the ongoing |
| 57 // composition. |
| 58 if (!frame()->editor().canEdit() && !inputMethodController().hasComposition()) |
| 59 return false; |
| 60 |
| 61 // We should verify the parent node of this IME composition node are |
| 62 // editable because JavaScript may delete a parent node of the composition |
| 63 // node. In this case, WebKit crashes while deleting texts from the parent |
| 64 // node, which doesn't exist any longer. |
| 65 const EphemeralRange range = |
| 66 inputMethodController().compositionEphemeralRange(); |
| 67 if (range.isNotNull()) { |
| 68 Node* node = range.startPosition().computeContainerNode(); |
| 69 frame()->document()->updateStyleAndLayoutTree(); |
| 70 if (!node || !hasEditableStyle(*node)) |
| 71 return false; |
| 72 } |
| 73 |
| 74 // A keypress event is canceled. If an ongoing composition exists, then the |
| 75 // keydown event should have arisen from a handled key (e.g., backspace). |
| 76 // In this case we ignore the cancellation and continue; otherwise (no |
| 77 // ongoing composition) we exit and signal success only for attempts to |
| 78 // clear the composition. |
| 79 if (m_suppressNextKeypressEvent && !inputMethodController().hasComposition()) |
| 80 return text.isEmpty(); |
| 81 |
| 82 UserGestureIndicator gestureIndicator(DocumentUserGestureToken::create( |
| 83 frame()->document(), UserGestureToken::NewGesture)); |
| 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( |
| 90 String(text), CompositionUnderlineVectorBuilder(underlines), |
| 91 selectionStart, selectionEnd); |
| 92 |
| 93 return text.isEmpty() || inputMethodController().hasComposition(); |
| 94 } |
| 95 |
| 96 bool WebInputMethodControllerImpl::finishComposingText( |
| 97 ConfirmCompositionBehavior selectionBehavior) { |
| 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( |
| 108 selectionBehavior == WebInputMethodController::KeepSelection |
| 109 ? InputMethodController::KeepSelection |
| 110 : InputMethodController::DoNotKeepSelection); |
| 111 } |
| 112 |
| 113 bool WebInputMethodControllerImpl::commitText(const WebString& text, |
| 114 int relativeCaretPosition) { |
| 115 UserGestureIndicator gestureIndicator(DocumentUserGestureToken::create( |
| 116 frame()->document(), UserGestureToken::NewGesture)); |
| 117 |
| 118 if (WebPlugin* plugin = focusedPluginIfInputMethodSupported()) |
| 119 return plugin->commitText(text, relativeCaretPosition); |
| 120 |
| 121 // TODO(xiaochengh): The use of updateStyleAndLayoutIgnorePendingStylesheets |
| 122 // needs to be audited. See http://crbug.com/590369 for more details. |
| 123 frame()->document()->updateStyleAndLayoutIgnorePendingStylesheets(); |
| 124 |
| 125 return inputMethodController().commitText(text, relativeCaretPosition); |
| 126 } |
| 127 |
| 128 LocalFrame* WebInputMethodControllerImpl::frame() const { |
| 129 return m_webLocalFrame->frame(); |
| 130 } |
| 131 |
| 132 InputMethodController& WebInputMethodControllerImpl::inputMethodController() |
| 133 const { |
| 134 return frame()->inputMethodController(); |
| 135 } |
| 136 |
| 137 WebPlugin* WebInputMethodControllerImpl::focusedPluginIfInputMethodSupported() |
| 138 const { |
| 139 WebPluginContainerImpl* container = |
| 140 WebLocalFrameImpl::currentPluginContainer(frame()); |
| 141 if (container && container->supportsInputMethod()) |
| 142 return container->plugin(); |
| 143 return nullptr; |
| 144 } |
| 145 |
| 146 } // namespace blink |
OLD | NEW |