| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ | 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ | 6 #define CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ |
| 7 | 7 |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 13 #include "content/common/text_input_state.h" | 13 #include "content/common/text_input_state.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 14 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/selection_bound.h" | 15 #include "ui/gfx/selection_bound.h" |
| 16 | 16 |
| 17 struct ViewHostMsg_SelectionBounds_Params; | 17 struct ViewHostMsg_SelectionBounds_Params; |
| 18 | 18 |
| 19 namespace gfx { |
| 20 class Range; |
| 21 } |
| 22 |
| 19 namespace content { | 23 namespace content { |
| 24 |
| 20 class RenderWidgetHostImpl; | 25 class RenderWidgetHostImpl; |
| 21 class RenderWidgetHostView; | 26 class RenderWidgetHostView; |
| 22 class RenderWidgetHostViewBase; | 27 class RenderWidgetHostViewBase; |
| 23 class WebContents; | 28 class WebContents; |
| 24 | 29 |
| 25 // A class which receives updates of TextInputState from multiple sources and | 30 // A class which receives updates of TextInputState from multiple sources and |
| 26 // decides what the new TextInputState is. It also notifies the observers when | 31 // decides what the new TextInputState is. It also notifies the observers when |
| 27 // text input state is updated. | 32 // text input state is updated. |
| 28 class CONTENT_EXPORT TextInputManager { | 33 class CONTENT_EXPORT TextInputManager { |
| 29 public: | 34 public: |
| 30 // The tab's top-level RWHV should be an observer of TextInputManager to get | 35 // The tab's top-level RWHV should be an observer of TextInputManager to get |
| 31 // notifications about changes in TextInputState or other IME related state | 36 // notifications about changes in TextInputState or other IME related state |
| 32 // for child frames. | 37 // for child frames. |
| 33 class CONTENT_EXPORT Observer { | 38 class CONTENT_EXPORT Observer { |
| 34 public: | 39 public: |
| 35 // Called when a view has called UpdateTextInputState on TextInputManager. | 40 // Called when a view has called UpdateTextInputState on TextInputManager. |
| 36 // If the call has led to a change in TextInputState, |did_update_state| is | 41 // If the call has led to a change in TextInputState, |did_update_state| is |
| 37 // true. In some plaforms, we need this update even when the state has not | 42 // true. In some plaforms, we need this update even when the state has not |
| 38 // changed (e.g., Aura for updating IME). | 43 // changed (e.g., Aura for updating IME). |
| 39 virtual void OnUpdateTextInputStateCalled( | 44 virtual void OnUpdateTextInputStateCalled( |
| 40 TextInputManager* text_input_manager, | 45 TextInputManager* text_input_manager, |
| 41 RenderWidgetHostViewBase* updated_view, | 46 RenderWidgetHostViewBase* updated_view, |
| 42 bool did_update_state) {} | 47 bool did_update_state) {} |
| 43 // Called when |updated_view| has called ImeCancelComposition on | 48 // Called when |updated_view| has called ImeCancelComposition on |
| 44 // TextInputManager. | 49 // TextInputManager. |
| 45 virtual void OnImeCancelComposition( | 50 virtual void OnImeCancelComposition( |
| 46 TextInputManager* text_input_manager, | 51 TextInputManager* text_input_manager, |
| 47 RenderWidgetHostViewBase* updated_view) {} | 52 RenderWidgetHostViewBase* updated_view) {} |
| 48 // Called when the selection bounds for the |updated_view| has changed. | 53 // Called when |updated_view| has changed its SelectionRegion. |
| 49 virtual void OnSelectionBoundsChanged( | 54 virtual void OnSelectionBoundsChanged( |
| 50 TextInputManager* text_input_manager, | 55 TextInputManager* text_input_manager, |
| 51 RenderWidgetHostViewBase* updated_view) {} | 56 RenderWidgetHostViewBase* updated_view) {} |
| 57 // Called when |updated_view| has changed its CompositionRangeInfo. |
| 58 virtual void OnImeCompositionRangeChanged( |
| 59 TextInputManager* text_input_manager, |
| 60 RenderWidgetHostViewBase* updated_view) {} |
| 52 }; | 61 }; |
| 53 | 62 |
| 54 TextInputManager(); | 63 TextInputManager(); |
| 55 ~TextInputManager(); | 64 ~TextInputManager(); |
| 56 | 65 |
| 57 // --------------------------------------------------------------------------- | |
| 58 // The following methods can be used to obtain information about IME-related | |
| 59 // state for the active RenderWidget. | |
| 60 | |
| 61 // Returns the currently active widget, i.e., the RWH which is associated with | 66 // Returns the currently active widget, i.e., the RWH which is associated with |
| 62 // |active_view_|. | 67 // |active_view_|. |
| 63 RenderWidgetHostImpl* GetActiveWidget() const; | 68 RenderWidgetHostImpl* GetActiveWidget() const; |
| 64 | 69 |
| 65 // Returns the TextInputState corresponding to |active_view_|. | 70 // --------------------------------------------------------------------------- |
| 66 // Users of this method should not hold on to the pointer as it might become | 71 // The following methods can be used to obtain information about IME-related |
| 67 // dangling if the TextInputManager or |active_view_| might go away. | 72 // state for the active RenderWidgetHost. If the active widget is nullptr, the |
| 73 // methods below will return nullptr as well. |
| 74 // Users of these methods should not hold on to the pointers as they become |
| 75 // dangling if the TextInputManager or |active_view_| are destroyed. |
| 76 |
| 77 // Returns the currently stored TextInputState. An state of nullptr can be |
| 78 // interpreted as a ui::TextInputType of ui::TEXT_INPUT_TYPE_NONE. |
| 68 const TextInputState* GetTextInputState(); | 79 const TextInputState* GetTextInputState(); |
| 69 | 80 |
| 70 // Returns the rect between selection bounds for the |active_view_|. | 81 // Returns the rect between selection bounds. |
| 71 gfx::Rect GetSelectionBoundsRect(); | 82 gfx::Rect GetSelectionBoundsRect(); |
| 72 | 83 |
| 84 // Returns a vector of rects representing the character bounds. |
| 85 const std::vector<gfx::Rect>* GetCompositionCharacterBounds(); |
| 86 |
| 73 // --------------------------------------------------------------------------- | 87 // --------------------------------------------------------------------------- |
| 74 // The following methods are called by RWHVs on the tab to update their IME- | 88 // The following methods are called by RWHVs on the tab to update their IME- |
| 75 // related state. | 89 // related state. |
| 76 | 90 |
| 77 // Updates the TextInputState for |view|. | 91 // Updates the TextInputState for |view|. |
| 78 void UpdateTextInputState(RenderWidgetHostViewBase* view, | 92 void UpdateTextInputState(RenderWidgetHostViewBase* view, |
| 79 const TextInputState& state); | 93 const TextInputState& state); |
| 80 | 94 |
| 81 // The current IME composition has been cancelled on the renderer side for | 95 // The current IME composition has been cancelled on the renderer side for |
| 82 // the widget corresponding to |view|. | 96 // the widget corresponding to |view|. |
| 83 void ImeCancelComposition(RenderWidgetHostViewBase* view); | 97 void ImeCancelComposition(RenderWidgetHostViewBase* view); |
| 84 | 98 |
| 85 // Updates the selection bounds for the |view|. In Aura, selection bounds are | 99 // Updates the selection bounds for the |view|. In Aura, selection bounds are |
| 86 // used to provide the InputMethod with the position of the caret, e.g., in | 100 // used to provide the InputMethod with the position of the caret, e.g., in |
| 87 // setting the position of the ui::ImeWindow. | 101 // setting the position of the ui::ImeWindow. |
| 88 void SelectionBoundsChanged(RenderWidgetHostViewBase* view, | 102 void SelectionBoundsChanged(RenderWidgetHostViewBase* view, |
| 89 const ViewHostMsg_SelectionBounds_Params& params); | 103 const ViewHostMsg_SelectionBounds_Params& params); |
| 90 | 104 |
| 105 // Called when the composition range and/or character bounds have changed. |
| 106 void ImeCompositionRangeChanged( |
| 107 RenderWidgetHostViewBase* view, |
| 108 const gfx::Range& range, |
| 109 const std::vector<gfx::Rect>& character_bounds); |
| 110 |
| 91 // Registers the given |view| for tracking its TextInputState. This is called | 111 // Registers the given |view| for tracking its TextInputState. This is called |
| 92 // by any view which has updates in its TextInputState (whether tab's RWHV or | 112 // by any view which has updates in its TextInputState (whether tab's RWHV or |
| 93 // that of a child frame). The |view| must unregister itself before being | 113 // that of a child frame). The |view| must unregister itself before being |
| 94 // destroyed (i.e., call TextInputManager::Unregister). | 114 // destroyed (i.e., call TextInputManager::Unregister). |
| 95 void Register(RenderWidgetHostViewBase* view); | 115 void Register(RenderWidgetHostViewBase* view); |
| 96 | 116 |
| 97 // Clears the TextInputState from the |view|. If |view == active_view_|, this | 117 // Clears the TextInputState from the |view|. If |view == active_view_|, this |
| 98 // call will lead to a TextInputState update since the TextInputState.type | 118 // call will lead to a TextInputState update since the TextInputState.type |
| 99 // should be reset to none. | 119 // should be reset to none. |
| 100 void Unregister(RenderWidgetHostViewBase* view); | 120 void Unregister(RenderWidgetHostViewBase* view); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 121 struct SelectionRegion { | 141 struct SelectionRegion { |
| 122 SelectionRegion(); | 142 SelectionRegion(); |
| 123 SelectionRegion(const SelectionRegion& other); | 143 SelectionRegion(const SelectionRegion& other); |
| 124 | 144 |
| 125 // The begining of the selection region. | 145 // The begining of the selection region. |
| 126 gfx::SelectionBound anchor; | 146 gfx::SelectionBound anchor; |
| 127 // The end of the selection region (caret position). | 147 // The end of the selection region (caret position). |
| 128 gfx::SelectionBound focus; | 148 gfx::SelectionBound focus; |
| 129 }; | 149 }; |
| 130 | 150 |
| 151 // Ccomposition range information. |
| 152 struct CompositionRangeInfo { |
| 153 CompositionRangeInfo(); |
| 154 CompositionRangeInfo(const CompositionRangeInfo& other); |
| 155 ~CompositionRangeInfo(); |
| 156 |
| 157 std::vector<gfx::Rect> character_bounds; |
| 158 }; |
| 159 |
| 131 // This class is used to create maps which hold specific IME state for a | 160 // This class is used to create maps which hold specific IME state for a |
| 132 // view. | 161 // view. |
| 133 template <class Value> | 162 template <class Value> |
| 134 class ViewMap : public std::unordered_map<RenderWidgetHostViewBase*, Value> { | 163 class ViewMap : public std::unordered_map<RenderWidgetHostViewBase*, Value> { |
| 135 }; | 164 }; |
| 136 | 165 |
| 137 void NotifyObserversAboutInputStateUpdate(RenderWidgetHostViewBase* view, | 166 void NotifyObserversAboutInputStateUpdate(RenderWidgetHostViewBase* view, |
| 138 bool did_update_state); | 167 bool did_update_state); |
| 139 | 168 |
| 140 // The view with active text input state, i.e., a focused <input> element. | 169 // The view with active text input state, i.e., a focused <input> element. |
| 141 // It will be nullptr if no such view exists. Note that the active view | 170 // It will be nullptr if no such view exists. Note that the active view |
| 142 // cannot have a |TextInputState.type| of ui::TEXT_INPUT_TYPE_NONE. | 171 // cannot have a |TextInputState.type| of ui::TEXT_INPUT_TYPE_NONE. |
| 143 RenderWidgetHostViewBase* active_view_; | 172 RenderWidgetHostViewBase* active_view_; |
| 144 | 173 |
| 145 ViewMap<TextInputState> text_input_state_map_; | 174 ViewMap<TextInputState> text_input_state_map_; |
| 146 | 175 |
| 147 // Text selection bounds information for registered views. | 176 // Text selection bounds information for registered views. |
| 148 ViewMap<SelectionRegion> selection_region_map_; | 177 ViewMap<SelectionRegion> selection_region_map_; |
| 178 ViewMap<CompositionRangeInfo> composition_range_info_map_; |
| 149 | 179 |
| 150 base::ObserverList<Observer> observer_list_; | 180 base::ObserverList<Observer> observer_list_; |
| 151 | 181 |
| 152 DISALLOW_COPY_AND_ASSIGN(TextInputManager); | 182 DISALLOW_COPY_AND_ASSIGN(TextInputManager); |
| 153 }; | 183 }; |
| 154 } | 184 } |
| 155 | 185 |
| 156 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ | 186 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ |
| OLD | NEW |