| 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 | 10 |
| 10 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 11 #include "content/common/content_export.h" | 12 #include "content/common/content_export.h" |
| 12 #include "content/common/text_input_state.h" | 13 #include "content/common/text_input_state.h" |
| 14 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/selection_bound.h" |
| 16 |
| 17 struct ViewHostMsg_SelectionBounds_Params; |
| 13 | 18 |
| 14 namespace content { | 19 namespace content { |
| 15 | |
| 16 class RenderWidgetHostImpl; | 20 class RenderWidgetHostImpl; |
| 17 class RenderWidgetHostView; | 21 class RenderWidgetHostView; |
| 18 class RenderWidgetHostViewBase; | 22 class RenderWidgetHostViewBase; |
| 19 class WebContents; | 23 class WebContents; |
| 20 | 24 |
| 21 // A class which receives updates of TextInputState from multiple sources and | 25 // A class which receives updates of TextInputState from multiple sources and |
| 22 // decides what the new TextInputState is. It also notifies the observers when | 26 // decides what the new TextInputState is. It also notifies the observers when |
| 23 // text input state is updated. | 27 // text input state is updated. |
| 24 class CONTENT_EXPORT TextInputManager { | 28 class CONTENT_EXPORT TextInputManager { |
| 25 public: | 29 public: |
| 26 // The tab's top-level RWHV should be an observer of TextInputManager to get | 30 // The tab's top-level RWHV should be an observer of TextInputManager to get |
| 27 // notifications about changes in TextInputState or other IME related state | 31 // notifications about changes in TextInputState or other IME related state |
| 28 // for child frames. | 32 // for child frames. |
| 29 class CONTENT_EXPORT Observer { | 33 class CONTENT_EXPORT Observer { |
| 30 public: | 34 public: |
| 31 // Called when a view has called UpdateTextInputState on TextInputManager. | 35 // Called when a view has called UpdateTextInputState on TextInputManager. |
| 32 // If the call has led to a change in TextInputState, |did_update_state| is | 36 // If the call has led to a change in TextInputState, |did_update_state| is |
| 33 // true. In some plaforms, we need this update even when the state has not | 37 // true. In some plaforms, we need this update even when the state has not |
| 34 // changed (e.g., Aura for updating IME). | 38 // changed (e.g., Aura for updating IME). |
| 35 virtual void OnUpdateTextInputStateCalled( | 39 virtual void OnUpdateTextInputStateCalled( |
| 36 TextInputManager* text_input_manager, | 40 TextInputManager* text_input_manager, |
| 37 RenderWidgetHostViewBase* updated_view, | 41 RenderWidgetHostViewBase* updated_view, |
| 38 bool did_update_state) {} | 42 bool did_update_state) {} |
| 39 // Called when |updated_view| has called ImeCancelComposition on | 43 // Called when |updated_view| has called ImeCancelComposition on |
| 40 // TextInputManager. | 44 // TextInputManager. |
| 41 virtual void OnImeCancelComposition( | 45 virtual void OnImeCancelComposition( |
| 42 TextInputManager* text_input_manager, | 46 TextInputManager* text_input_manager, |
| 43 RenderWidgetHostViewBase* updated_view) {} | 47 RenderWidgetHostViewBase* updated_view) {} |
| 48 // Called when the selection bounds for the |updated_view| has changed. |
| 49 virtual void OnSelectionBoundsChanged( |
| 50 TextInputManager* text_input_manager, |
| 51 RenderWidgetHostViewBase* updated_view) {} |
| 44 }; | 52 }; |
| 45 | 53 |
| 46 TextInputManager(); | 54 TextInputManager(); |
| 47 ~TextInputManager(); | 55 ~TextInputManager(); |
| 48 | 56 |
| 49 // --------------------------------------------------------------------------- | 57 // --------------------------------------------------------------------------- |
| 50 // The following methods can be used to obtain information about IME-related | 58 // The following methods can be used to obtain information about IME-related |
| 51 // state for the active RenderWidget. | 59 // state for the active RenderWidget. |
| 52 | 60 |
| 53 // Returns the currently active widget, i.e., the RWH which is associated with | 61 // Returns the currently active widget, i.e., the RWH which is associated with |
| 54 // |active_view_|. | 62 // |active_view_|. |
| 55 RenderWidgetHostImpl* GetActiveWidget() const; | 63 RenderWidgetHostImpl* GetActiveWidget() const; |
| 56 | 64 |
| 57 // Returns the TextInputState corresponding to |active_view_|. | 65 // Returns the TextInputState corresponding to |active_view_|. |
| 58 // Users of this method should not hold on to the pointer as it might become | 66 // Users of this method should not hold on to the pointer as it might become |
| 59 // dangling if the TextInputManager or |active_view_| might go away. | 67 // dangling if the TextInputManager or |active_view_| might go away. |
| 60 const TextInputState* GetTextInputState(); | 68 const TextInputState* GetTextInputState(); |
| 61 | 69 |
| 70 // Returns the rect between selection bounds for the |active_view_|. |
| 71 gfx::Rect GetSelectionBoundsRect(); |
| 72 |
| 62 // --------------------------------------------------------------------------- | 73 // --------------------------------------------------------------------------- |
| 63 // The following methods are called by RWHVs on the tab to update their IME- | 74 // The following methods are called by RWHVs on the tab to update their IME- |
| 64 // related state. | 75 // related state. |
| 65 | 76 |
| 66 // Updates the TextInputState for |view|. | 77 // Updates the TextInputState for |view|. |
| 67 void UpdateTextInputState(RenderWidgetHostViewBase* view, | 78 void UpdateTextInputState(RenderWidgetHostViewBase* view, |
| 68 const TextInputState& state); | 79 const TextInputState& state); |
| 69 | 80 |
| 70 // The current IME composition has been cancelled on the renderer side for | 81 // The current IME composition has been cancelled on the renderer side for |
| 71 // the widget corresponding to |view|. | 82 // the widget corresponding to |view|. |
| 72 void ImeCancelComposition(RenderWidgetHostViewBase* view); | 83 void ImeCancelComposition(RenderWidgetHostViewBase* view); |
| 73 | 84 |
| 85 // 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 |
| 87 // setting the position of the ui::ImeWindow. |
| 88 void SelectionBoundsChanged(RenderWidgetHostViewBase* view, |
| 89 const ViewHostMsg_SelectionBounds_Params& params); |
| 90 |
| 74 // Registers the given |view| for tracking its TextInputState. This is called | 91 // Registers the given |view| for tracking its TextInputState. This is called |
| 75 // by any view which has updates in its TextInputState (whether tab's RWHV or | 92 // by any view which has updates in its TextInputState (whether tab's RWHV or |
| 76 // that of a child frame). The |view| must unregister itself before being | 93 // that of a child frame). The |view| must unregister itself before being |
| 77 // destroyed (i.e., call TextInputManager::Unregister). | 94 // destroyed (i.e., call TextInputManager::Unregister). |
| 78 void Register(RenderWidgetHostViewBase* view); | 95 void Register(RenderWidgetHostViewBase* view); |
| 79 | 96 |
| 80 // Clears the TextInputState from the |view|. If |view == active_view_|, this | 97 // Clears the TextInputState from the |view|. If |view == active_view_|, this |
| 81 // call will lead to a TextInputState update since the TextInputState.type | 98 // call will lead to a TextInputState update since the TextInputState.type |
| 82 // should be reset to none. | 99 // should be reset to none. |
| 83 void Unregister(RenderWidgetHostViewBase* view); | 100 void Unregister(RenderWidgetHostViewBase* view); |
| 84 | 101 |
| 85 // Returns true if |view| is already registered. | 102 // Returns true if |view| is already registered. |
| 86 bool IsRegistered(RenderWidgetHostViewBase* view) const; | 103 bool IsRegistered(RenderWidgetHostViewBase* view) const; |
| 87 | 104 |
| 88 // Add and remove observers for notifications regarding updates in the | 105 // Add and remove observers for notifications regarding updates in the |
| 89 // TextInputState. Clients must be sure to remove themselves before they go | 106 // TextInputState. Clients must be sure to remove themselves before they go |
| 90 // away. | 107 // away. |
| 91 // Only the tab's RWHV should observer TextInputManager. In tests, however, | 108 // Only the tab's RWHV should observer TextInputManager. In tests, however, |
| 92 // in addition to the tab's RWHV, one or more test observers might observe | 109 // in addition to the tab's RWHV, one or more test observers might observe |
| 93 // TextInputManager. | 110 // TextInputManager. |
| 94 void AddObserver(Observer* observer); | 111 void AddObserver(Observer* observer); |
| 95 void RemoveObserver(Observer* observer); | 112 void RemoveObserver(Observer* observer); |
| 96 | 113 |
| 97 RenderWidgetHostViewBase* active_view_for_testing() { return active_view_; } | 114 RenderWidgetHostViewBase* active_view_for_testing() { return active_view_; } |
| 98 size_t GetRegisteredViewsCountForTesting(); | 115 size_t GetRegisteredViewsCountForTesting(); |
| 99 ui::TextInputType GetTextInputTypeForViewForTesting( | 116 ui::TextInputType GetTextInputTypeForViewForTesting( |
| 100 RenderWidgetHostViewBase* view); | 117 RenderWidgetHostViewBase* view); |
| 101 | 118 |
| 102 private: | 119 private: |
| 120 // Text selection bounds. |
| 121 struct SelectionRegion { |
| 122 SelectionRegion(); |
| 123 SelectionRegion(const SelectionRegion& other); |
| 124 |
| 125 // The begining of the selection region. |
| 126 gfx::SelectionBound anchor; |
| 127 // The end of the selection region (caret position). |
| 128 gfx::SelectionBound focus; |
| 129 }; |
| 130 |
| 131 // This class is used to create maps which hold specific IME state for a |
| 132 // view. |
| 133 template <class Value> |
| 134 class ViewMap : public std::unordered_map<RenderWidgetHostViewBase*, Value> { |
| 135 }; |
| 136 |
| 103 void NotifyObserversAboutInputStateUpdate(RenderWidgetHostViewBase* view, | 137 void NotifyObserversAboutInputStateUpdate(RenderWidgetHostViewBase* view, |
| 104 bool did_update_state); | 138 bool did_update_state); |
| 105 | 139 |
| 106 // The view with active text input state, i.e., a focused <input> element. | 140 // The view with active text input state, i.e., a focused <input> element. |
| 107 // It will be nullptr if no such view exists. Note that the active view | 141 // It will be nullptr if no such view exists. Note that the active view |
| 108 // cannot have a |TextInputState.type| of ui::TEXT_INPUT_TYPE_NONE. | 142 // cannot have a |TextInputState.type| of ui::TEXT_INPUT_TYPE_NONE. |
| 109 RenderWidgetHostViewBase* active_view_; | 143 RenderWidgetHostViewBase* active_view_; |
| 110 | 144 |
| 111 std::unordered_map<RenderWidgetHostViewBase*, TextInputState> | 145 ViewMap<TextInputState> text_input_state_map_; |
| 112 text_input_state_map_; | 146 |
| 147 // Text selection bounds information for registered views. |
| 148 ViewMap<SelectionRegion> selection_region_map_; |
| 113 | 149 |
| 114 base::ObserverList<Observer> observer_list_; | 150 base::ObserverList<Observer> observer_list_; |
| 115 | 151 |
| 116 DISALLOW_COPY_AND_ASSIGN(TextInputManager); | 152 DISALLOW_COPY_AND_ASSIGN(TextInputManager); |
| 117 }; | 153 }; |
| 118 } | 154 } |
| 119 | 155 |
| 120 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ | 156 #endif // CONTENT_BROWSER_RENDERER_HOST_TEXT_INPUT_MANAGER_H__ |
| OLD | NEW |