ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 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 "content/browser/renderer_host/text_input_manager.h" |
| 6 | |
ekaramad | 8cba7886 | 2016-06-24 19:42:31 | [diff] [blame] | 7 | #include "content/browser/renderer_host/render_widget_host_impl.h" |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 8 | #include "content/browser/renderer_host/render_widget_host_view_base.h" |
ekaramad | fcce088 | 2016-07-07 02:44:51 | [diff] [blame] | 9 | #include "content/common/view_messages.h" |
ekaramad | b8e23a96c | 2016-07-13 01:21:15 | [diff] [blame] | 10 | #include "ui/gfx/geometry/rect.h" |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 11 | |
| 12 | namespace content { |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | bool AreDifferentTextInputStates(const content::TextInputState& old_state, |
| 17 | const content::TextInputState& new_state) { |
| 18 | #if defined(USE_AURA) |
| 19 | return old_state.type != new_state.type || old_state.mode != new_state.mode || |
| 20 | old_state.flags != new_state.flags || |
| 21 | old_state.can_compose_inline != new_state.can_compose_inline; |
| 22 | #else |
| 23 | // TODO(ekaramad): Implement the logic for other platforms (crbug.com/578168). |
| 24 | NOTREACHED(); |
| 25 | return true; |
| 26 | #endif |
| 27 | } |
| 28 | |
| 29 | } // namespace |
| 30 | |
| 31 | TextInputManager::TextInputManager() : active_view_(nullptr) {} |
| 32 | |
| 33 | TextInputManager::~TextInputManager() { |
| 34 | // If there is an active view, we should unregister it first so that the |
| 35 | // the tab's top-level RWHV will be notified about |TextInputState.type| |
| 36 | // resetting to none (i.e., we do not have an active RWHV anymore). |
| 37 | if (active_view_) |
| 38 | Unregister(active_view_); |
| 39 | |
| 40 | // Unregister all the remaining views. |
| 41 | std::vector<RenderWidgetHostViewBase*> views; |
| 42 | for (auto pair : text_input_state_map_) |
| 43 | views.push_back(pair.first); |
| 44 | |
| 45 | for (auto view : views) |
| 46 | Unregister(view); |
| 47 | } |
| 48 | |
ekaramad | 8cba7886 | 2016-06-24 19:42:31 | [diff] [blame] | 49 | RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const { |
| 50 | return !!active_view_ ? static_cast<RenderWidgetHostImpl*>( |
| 51 | active_view_->GetRenderWidgetHost()) |
| 52 | : nullptr; |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 53 | } |
| 54 | |
ekaramad | b8e23a96c | 2016-07-13 01:21:15 | [diff] [blame] | 55 | const TextInputState* TextInputManager::GetTextInputState() { |
| 56 | return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr; |
| 57 | } |
| 58 | |
ekaramad | fcce088 | 2016-07-07 02:44:51 | [diff] [blame] | 59 | gfx::Rect TextInputManager::GetSelectionBoundsRect() { |
| 60 | if (!active_view_) |
| 61 | return gfx::Rect(); |
| 62 | |
| 63 | return gfx::RectBetweenSelectionBounds( |
| 64 | selection_region_map_[active_view_].anchor, |
| 65 | selection_region_map_[active_view_].focus); |
| 66 | } |
| 67 | |
ekaramad | b8e23a96c | 2016-07-13 01:21:15 | [diff] [blame] | 68 | const std::vector<gfx::Rect>* |
| 69 | TextInputManager::GetCompositionCharacterBounds() { |
| 70 | return !!active_view_ |
| 71 | ? &composition_range_info_map_[active_view_].character_bounds |
| 72 | : nullptr; |
| 73 | } |
| 74 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 75 | void TextInputManager::UpdateTextInputState( |
| 76 | RenderWidgetHostViewBase* view, |
| 77 | const TextInputState& text_input_state) { |
| 78 | DCHECK(IsRegistered(view)); |
| 79 | |
| 80 | // Since |view| is registgered, we already have a previous value for its |
| 81 | // TextInputState. |
| 82 | bool changed = AreDifferentTextInputStates(text_input_state_map_[view], |
| 83 | text_input_state); |
| 84 | |
| 85 | text_input_state_map_[view] = text_input_state; |
| 86 | |
| 87 | // |active_view_| is only updated when the state for |view| is not none. |
| 88 | if (text_input_state.type != ui::TEXT_INPUT_TYPE_NONE) |
| 89 | active_view_ = view; |
| 90 | |
| 91 | // If the state for |active_view_| is none, then we no longer have an |
| 92 | // |active_view_|. |
| 93 | if (active_view_ == view && text_input_state.type == ui::TEXT_INPUT_TYPE_NONE) |
| 94 | active_view_ = nullptr; |
| 95 | |
| 96 | NotifyObserversAboutInputStateUpdate(view, changed); |
| 97 | } |
| 98 | |
ekaramad | 50ee203 | 2016-06-29 02:18:25 | [diff] [blame] | 99 | void TextInputManager::ImeCancelComposition(RenderWidgetHostViewBase* view) { |
| 100 | DCHECK(IsRegistered(view)); |
| 101 | FOR_EACH_OBSERVER(Observer, observer_list_, |
| 102 | OnImeCancelComposition(this, view)); |
| 103 | } |
| 104 | |
ekaramad | fcce088 | 2016-07-07 02:44:51 | [diff] [blame] | 105 | void TextInputManager::SelectionBoundsChanged( |
| 106 | RenderWidgetHostViewBase* view, |
| 107 | const ViewHostMsg_SelectionBounds_Params& params) { |
| 108 | DCHECK(IsRegistered(view)); |
| 109 | |
| 110 | // TODO(ekaramad): Implement the logic for other platforms (crbug.com/578168). |
| 111 | #if defined(USE_AURA) |
| 112 | gfx::SelectionBound anchor_bound, focus_bound; |
| 113 | // Converting the points to the |view|'s root coordinate space (for child |
| 114 | // frame views). |
| 115 | anchor_bound.SetEdge(gfx::PointF(view->TransformPointToRootCoordSpace( |
| 116 | params.anchor_rect.origin())), |
| 117 | gfx::PointF(view->TransformPointToRootCoordSpace( |
| 118 | params.anchor_rect.bottom_left()))); |
| 119 | focus_bound.SetEdge(gfx::PointF(view->TransformPointToRootCoordSpace( |
| 120 | params.focus_rect.origin())), |
| 121 | gfx::PointF(view->TransformPointToRootCoordSpace( |
| 122 | params.focus_rect.bottom_left()))); |
| 123 | |
| 124 | if (params.anchor_rect == params.focus_rect) { |
| 125 | anchor_bound.set_type(gfx::SelectionBound::CENTER); |
| 126 | focus_bound.set_type(gfx::SelectionBound::CENTER); |
| 127 | } else { |
| 128 | // Whether text is LTR at the anchor handle. |
| 129 | bool anchor_LTR = params.anchor_dir == blink::WebTextDirectionLeftToRight; |
| 130 | // Whether text is LTR at the focus handle. |
| 131 | bool focus_LTR = params.focus_dir == blink::WebTextDirectionLeftToRight; |
| 132 | |
| 133 | if ((params.is_anchor_first && anchor_LTR) || |
| 134 | (!params.is_anchor_first && !anchor_LTR)) { |
| 135 | anchor_bound.set_type(gfx::SelectionBound::LEFT); |
| 136 | } else { |
| 137 | anchor_bound.set_type(gfx::SelectionBound::RIGHT); |
| 138 | } |
| 139 | if ((params.is_anchor_first && focus_LTR) || |
| 140 | (!params.is_anchor_first && !focus_LTR)) { |
| 141 | focus_bound.set_type(gfx::SelectionBound::RIGHT); |
| 142 | } else { |
| 143 | focus_bound.set_type(gfx::SelectionBound::LEFT); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (anchor_bound == selection_region_map_[view].anchor && |
| 148 | focus_bound == selection_region_map_[view].focus) |
| 149 | return; |
| 150 | |
| 151 | selection_region_map_[view].anchor = anchor_bound; |
| 152 | selection_region_map_[view].focus = focus_bound; |
| 153 | |
| 154 | FOR_EACH_OBSERVER(Observer, observer_list_, |
| 155 | OnSelectionBoundsChanged(this, view)); |
| 156 | #endif |
| 157 | } |
| 158 | |
ekaramad | b8e23a96c | 2016-07-13 01:21:15 | [diff] [blame] | 159 | void TextInputManager::ImeCompositionRangeChanged( |
| 160 | RenderWidgetHostViewBase* view, |
| 161 | const gfx::Range& range, |
| 162 | const std::vector<gfx::Rect>& character_bounds) { |
| 163 | DCHECK(IsRegistered(view)); |
| 164 | composition_range_info_map_[view].character_bounds.clear(); |
| 165 | |
| 166 | // The values for the bounds should be converted to root view's coordinates |
| 167 | // before being stored. |
| 168 | for (auto rect : character_bounds) { |
| 169 | composition_range_info_map_[view].character_bounds.emplace_back(gfx::Rect( |
| 170 | view->TransformPointToRootCoordSpace(rect.origin()), rect.size())); |
| 171 | } |
| 172 | |
| 173 | FOR_EACH_OBSERVER(Observer, observer_list_, |
| 174 | OnImeCompositionRangeChanged(this, view)); |
| 175 | } |
| 176 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 177 | void TextInputManager::Register(RenderWidgetHostViewBase* view) { |
| 178 | DCHECK(!IsRegistered(view)); |
| 179 | |
| 180 | text_input_state_map_[view] = TextInputState(); |
| 181 | } |
| 182 | |
| 183 | void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { |
| 184 | DCHECK(IsRegistered(view)); |
| 185 | |
| 186 | text_input_state_map_.erase(view); |
| 187 | if (active_view_ == view) { |
| 188 | active_view_ = nullptr; |
| 189 | NotifyObserversAboutInputStateUpdate(view, true); |
| 190 | } |
| 191 | view->DidUnregisterFromTextInputManager(this); |
| 192 | } |
| 193 | |
| 194 | bool TextInputManager::IsRegistered(RenderWidgetHostViewBase* view) const { |
| 195 | return text_input_state_map_.count(view) == 1; |
| 196 | } |
| 197 | |
| 198 | void TextInputManager::AddObserver(Observer* observer) { |
| 199 | observer_list_.AddObserver(observer); |
| 200 | } |
| 201 | |
| 202 | void TextInputManager::RemoveObserver(Observer* observer) { |
| 203 | observer_list_.RemoveObserver(observer); |
| 204 | } |
| 205 | |
ekaramad | 936536d | 2016-06-29 05:44:44 | [diff] [blame] | 206 | size_t TextInputManager::GetRegisteredViewsCountForTesting() { |
| 207 | return text_input_state_map_.size(); |
| 208 | } |
| 209 | |
| 210 | ui::TextInputType TextInputManager::GetTextInputTypeForViewForTesting( |
| 211 | RenderWidgetHostViewBase* view) { |
| 212 | DCHECK(IsRegistered(view)); |
| 213 | return text_input_state_map_[view].type; |
| 214 | } |
| 215 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 216 | void TextInputManager::NotifyObserversAboutInputStateUpdate( |
| 217 | RenderWidgetHostViewBase* updated_view, |
| 218 | bool did_update_state) { |
| 219 | FOR_EACH_OBSERVER( |
| 220 | Observer, observer_list_, |
| 221 | OnUpdateTextInputStateCalled(this, updated_view, did_update_state)); |
| 222 | } |
| 223 | |
ekaramad | fcce088 | 2016-07-07 02:44:51 | [diff] [blame] | 224 | TextInputManager::SelectionRegion::SelectionRegion() {} |
| 225 | |
| 226 | TextInputManager::SelectionRegion::SelectionRegion( |
| 227 | const SelectionRegion& other) = default; |
| 228 | |
ekaramad | b8e23a96c | 2016-07-13 01:21:15 | [diff] [blame] | 229 | TextInputManager::CompositionRangeInfo::CompositionRangeInfo() {} |
| 230 | |
| 231 | TextInputManager::CompositionRangeInfo::CompositionRangeInfo( |
| 232 | const CompositionRangeInfo& other) = default; |
| 233 | |
| 234 | TextInputManager::CompositionRangeInfo::~CompositionRangeInfo() {} |
| 235 | |
ekaramad | add88229 | 2016-06-08 15:22:56 | [diff] [blame] | 236 | } // namespace content |