blob: eed20121103d96cdaaec68cdcafea5ecacc241a6 [file] [log] [blame]
ekaramadadd882292016-06-08 15:22:561// 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
ekaramad8cba78862016-06-24 19:42:317#include "content/browser/renderer_host/render_widget_host_impl.h"
ekaramadadd882292016-06-08 15:22:568#include "content/browser/renderer_host/render_widget_host_view_base.h"
ekaramadfcce0882016-07-07 02:44:519#include "content/common/view_messages.h"
ekaramadb8e23a96c2016-07-13 01:21:1510#include "ui/gfx/geometry/rect.h"
ekaramadadd882292016-06-08 15:22:5611
12namespace content {
13
14namespace {
15
16bool 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
31TextInputManager::TextInputManager() : active_view_(nullptr) {}
32
33TextInputManager::~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
ekaramad8cba78862016-06-24 19:42:3149RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const {
50 return !!active_view_ ? static_cast<RenderWidgetHostImpl*>(
51 active_view_->GetRenderWidgetHost())
52 : nullptr;
ekaramadadd882292016-06-08 15:22:5653}
54
ekaramadb8e23a96c2016-07-13 01:21:1555const TextInputState* TextInputManager::GetTextInputState() {
56 return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr;
57}
58
ekaramadfcce0882016-07-07 02:44:5159gfx::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
ekaramadb8e23a96c2016-07-13 01:21:1568const std::vector<gfx::Rect>*
69TextInputManager::GetCompositionCharacterBounds() {
70 return !!active_view_
71 ? &composition_range_info_map_[active_view_].character_bounds
72 : nullptr;
73}
74
ekaramadadd882292016-06-08 15:22:5675void 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
ekaramad50ee2032016-06-29 02:18:2599void TextInputManager::ImeCancelComposition(RenderWidgetHostViewBase* view) {
100 DCHECK(IsRegistered(view));
101 FOR_EACH_OBSERVER(Observer, observer_list_,
102 OnImeCancelComposition(this, view));
103}
104
ekaramadfcce0882016-07-07 02:44:51105void 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
ekaramadb8e23a96c2016-07-13 01:21:15159void 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
ekaramadadd882292016-06-08 15:22:56177void TextInputManager::Register(RenderWidgetHostViewBase* view) {
178 DCHECK(!IsRegistered(view));
179
180 text_input_state_map_[view] = TextInputState();
181}
182
183void 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
194bool TextInputManager::IsRegistered(RenderWidgetHostViewBase* view) const {
195 return text_input_state_map_.count(view) == 1;
196}
197
198void TextInputManager::AddObserver(Observer* observer) {
199 observer_list_.AddObserver(observer);
200}
201
202void TextInputManager::RemoveObserver(Observer* observer) {
203 observer_list_.RemoveObserver(observer);
204}
205
ekaramad936536d2016-06-29 05:44:44206size_t TextInputManager::GetRegisteredViewsCountForTesting() {
207 return text_input_state_map_.size();
208}
209
210ui::TextInputType TextInputManager::GetTextInputTypeForViewForTesting(
211 RenderWidgetHostViewBase* view) {
212 DCHECK(IsRegistered(view));
213 return text_input_state_map_[view].type;
214}
215
ekaramadadd882292016-06-08 15:22:56216void 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
ekaramadfcce0882016-07-07 02:44:51224TextInputManager::SelectionRegion::SelectionRegion() {}
225
226TextInputManager::SelectionRegion::SelectionRegion(
227 const SelectionRegion& other) = default;
228
ekaramadb8e23a96c2016-07-13 01:21:15229TextInputManager::CompositionRangeInfo::CompositionRangeInfo() {}
230
231TextInputManager::CompositionRangeInfo::CompositionRangeInfo(
232 const CompositionRangeInfo& other) = default;
233
234TextInputManager::CompositionRangeInfo::~CompositionRangeInfo() {}
235
ekaramadadd882292016-06-08 15:22:56236} // namespace content