| 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/CompositionUnderlineVectorBuilder.h" | |
| 10 #include "core/editing/EditingUtilities.h" | |
| 11 #include "core/editing/Editor.h" | |
| 12 #include "core/editing/EphemeralRange.h" | |
| 13 #include "core/editing/FrameSelection.h" | |
| 14 #include "core/editing/InputMethodController.h" | |
| 15 #include "core/editing/PlainTextRange.h" | |
| 16 #include "core/exported/WebPluginContainerBase.h" | |
| 17 #include "core/frame/LocalFrame.h" | |
| 18 #include "core/page/FocusController.h" | |
| 19 #include "core/page/Page.h" | |
| 20 #include "platform/UserGestureIndicator.h" | |
| 21 #include "public/platform/WebString.h" | |
| 22 #include "public/web/WebPlugin.h" | |
| 23 #include "public/web/WebRange.h" | |
| 24 #include "web/WebLocalFrameImpl.h" | |
| 25 | |
| 26 namespace blink { | |
| 27 | |
| 28 WebInputMethodControllerImpl::WebInputMethodControllerImpl( | |
| 29 WebLocalFrameImpl& web_frame) | |
| 30 : web_frame_(&web_frame) {} | |
| 31 | |
| 32 WebInputMethodControllerImpl::~WebInputMethodControllerImpl() {} | |
| 33 | |
| 34 // static | |
| 35 WebInputMethodControllerImpl* WebInputMethodControllerImpl::FromFrame( | |
| 36 LocalFrame* frame) { | |
| 37 WebLocalFrameImpl* web_frame_impl = WebLocalFrameImpl::FromFrame(frame); | |
| 38 return web_frame_impl ? web_frame_impl->GetInputMethodController() : nullptr; | |
| 39 } | |
| 40 | |
| 41 DEFINE_TRACE(WebInputMethodControllerImpl) { | |
| 42 visitor->Trace(web_frame_); | |
| 43 } | |
| 44 | |
| 45 bool WebInputMethodControllerImpl::SetComposition( | |
| 46 const WebString& text, | |
| 47 const WebVector<WebCompositionUnderline>& underlines, | |
| 48 const WebRange& replacement_range, | |
| 49 int selection_start, | |
| 50 int selection_end) { | |
| 51 if (WebPlugin* plugin = FocusedPluginIfInputMethodSupported()) { | |
| 52 return plugin->SetComposition(text, underlines, replacement_range, | |
| 53 selection_start, selection_end); | |
| 54 } | |
| 55 | |
| 56 // We should use this |editor| object only to complete the ongoing | |
| 57 // composition. | |
| 58 if (!GetFrame()->GetEditor().CanEdit() && | |
| 59 !GetInputMethodController().HasComposition()) | |
| 60 return false; | |
| 61 | |
| 62 // Select the range to be replaced with the composition later. | |
| 63 if (!replacement_range.IsNull()) | |
| 64 web_frame_->SelectRange(replacement_range); | |
| 65 | |
| 66 // We should verify the parent node of this IME composition node are | |
| 67 // editable because JavaScript may delete a parent node of the composition | |
| 68 // node. In this case, WebKit crashes while deleting texts from the parent | |
| 69 // node, which doesn't exist any longer. | |
| 70 const EphemeralRange range = | |
| 71 GetInputMethodController().CompositionEphemeralRange(); | |
| 72 if (range.IsNotNull()) { | |
| 73 Node* node = range.StartPosition().ComputeContainerNode(); | |
| 74 GetFrame()->GetDocument()->UpdateStyleAndLayoutTree(); | |
| 75 if (!node || !HasEditableStyle(*node)) | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 UserGestureIndicator gesture_indicator(DocumentUserGestureToken::Create( | |
| 80 GetFrame()->GetDocument(), UserGestureToken::kNewGesture)); | |
| 81 | |
| 82 // When the range of composition underlines overlap with the range between | |
| 83 // selectionStart and selectionEnd, WebKit somehow won't paint the selection | |
| 84 // at all (see InlineTextBox::paint() function in InlineTextBox.cpp). | |
| 85 // But the selection range actually takes effect. | |
| 86 GetInputMethodController().SetComposition( | |
| 87 String(text), CompositionUnderlineVectorBuilder::Build(underlines), | |
| 88 selection_start, selection_end); | |
| 89 | |
| 90 return text.IsEmpty() || GetInputMethodController().HasComposition(); | |
| 91 } | |
| 92 | |
| 93 bool WebInputMethodControllerImpl::FinishComposingText( | |
| 94 ConfirmCompositionBehavior selection_behavior) { | |
| 95 // TODO(ekaramad): Here and in other IME calls we should expect the | |
| 96 // call to be made when our frame is focused. This, however, is not the case | |
| 97 // all the time. For instance, resetInputMethod call on RenderViewImpl could | |
| 98 // be after losing the focus on frame. But since we return the core frame | |
| 99 // in WebViewImpl::focusedLocalFrameInWidget(), we will reach here with | |
| 100 // |m_webLocalFrame| not focused on page. | |
| 101 | |
| 102 if (WebPlugin* plugin = FocusedPluginIfInputMethodSupported()) | |
| 103 return plugin->FinishComposingText(selection_behavior); | |
| 104 | |
| 105 // TODO(editing-dev): The use of updateStyleAndLayoutIgnorePendingStylesheets | |
| 106 // needs to be audited. See http://crbug.com/590369 for more details. | |
| 107 GetFrame()->GetDocument()->UpdateStyleAndLayoutIgnorePendingStylesheets(); | |
| 108 | |
| 109 return GetInputMethodController().FinishComposingText( | |
| 110 selection_behavior == WebInputMethodController::kKeepSelection | |
| 111 ? InputMethodController::kKeepSelection | |
| 112 : InputMethodController::kDoNotKeepSelection); | |
| 113 } | |
| 114 | |
| 115 bool WebInputMethodControllerImpl::CommitText( | |
| 116 const WebString& text, | |
| 117 const WebVector<WebCompositionUnderline>& underlines, | |
| 118 const WebRange& replacement_range, | |
| 119 int relative_caret_position) { | |
| 120 UserGestureIndicator gesture_indicator(DocumentUserGestureToken::Create( | |
| 121 GetFrame()->GetDocument(), UserGestureToken::kNewGesture)); | |
| 122 | |
| 123 if (WebPlugin* plugin = FocusedPluginIfInputMethodSupported()) { | |
| 124 return plugin->CommitText(text, underlines, replacement_range, | |
| 125 relative_caret_position); | |
| 126 } | |
| 127 | |
| 128 // Select the range to be replaced with the composition later. | |
| 129 if (!replacement_range.IsNull()) | |
| 130 web_frame_->SelectRange(replacement_range); | |
| 131 | |
| 132 // TODO(editing-dev): The use of updateStyleAndLayoutIgnorePendingStylesheets | |
| 133 // needs to be audited. See http://crbug.com/590369 for more details. | |
| 134 GetFrame()->GetDocument()->UpdateStyleAndLayoutIgnorePendingStylesheets(); | |
| 135 | |
| 136 return GetInputMethodController().CommitText( | |
| 137 text, CompositionUnderlineVectorBuilder::Build(underlines), | |
| 138 relative_caret_position); | |
| 139 } | |
| 140 | |
| 141 WebTextInputInfo WebInputMethodControllerImpl::TextInputInfo() { | |
| 142 return GetFrame()->GetInputMethodController().TextInputInfo(); | |
| 143 } | |
| 144 | |
| 145 WebTextInputType WebInputMethodControllerImpl::TextInputType() { | |
| 146 return GetFrame()->GetInputMethodController().TextInputType(); | |
| 147 } | |
| 148 | |
| 149 LocalFrame* WebInputMethodControllerImpl::GetFrame() const { | |
| 150 return web_frame_->GetFrame(); | |
| 151 } | |
| 152 | |
| 153 InputMethodController& WebInputMethodControllerImpl::GetInputMethodController() | |
| 154 const { | |
| 155 return GetFrame()->GetInputMethodController(); | |
| 156 } | |
| 157 | |
| 158 WebPlugin* WebInputMethodControllerImpl::FocusedPluginIfInputMethodSupported() | |
| 159 const { | |
| 160 WebPluginContainerBase* container = GetFrame()->GetWebPluginContainerBase(); | |
| 161 if (container && container->SupportsInputMethod()) { | |
| 162 return container->Plugin(); | |
| 163 } | |
| 164 return nullptr; | |
| 165 } | |
| 166 | |
| 167 } // namespace blink | |
| OLD | NEW |