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 "content/browser/renderer_host/text_input_manager.h" |
| 6 |
| 7 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 8 |
| 9 namespace content { |
| 10 |
| 11 namespace { |
| 12 |
| 13 bool AreDifferentTextInputStates(const content::TextInputState& old_state, |
| 14 const content::TextInputState& new_state) { |
| 15 #if defined(USE_AURA) |
| 16 return old_state.type != new_state.type || old_state.mode != new_state.mode || |
| 17 old_state.flags != new_state.flags || |
| 18 old_state.can_compose_inline != new_state.can_compose_inline; |
| 19 #else |
| 20 // TODO(ekaramad): Implement this logic for other platforms. |
| 21 NOTREACHED(); |
| 22 #endif |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 TextInputManager::TextInputManager() : active_view_(nullptr) {} |
| 28 |
| 29 TextInputManager::~TextInputManager() { |
| 30 // If there is an acitve view, we should unregister it first so that the |
| 31 // the tab's top-level RWHV will be notified about |TextInputState.type| |
| 32 // resetting to none (i.e., we do not have an active RWHV anymore). |
| 33 if (active_view_) |
| 34 Unregister(active_view_); |
| 35 |
| 36 // Unregister all the remaining views. |
| 37 std::vector<RenderWidgetHostViewBase*> views; |
| 38 for (auto pair : text_input_state_map_) |
| 39 views.push_back(pair.first); |
| 40 |
| 41 for (auto view : views) |
| 42 Unregister(view); |
| 43 |
| 44 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 45 OnDestroyingTextInputManager(this)); |
| 46 } |
| 47 |
| 48 const TextInputState* TextInputManager::GetTextInputState() { |
| 49 return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr; |
| 50 } |
| 51 |
| 52 RenderWidgetHostViewBase* TextInputManager::GetActiveView() const { |
| 53 return active_view_; |
| 54 } |
| 55 |
| 56 void TextInputManager::UpdateTextInputState( |
| 57 RenderWidgetHostViewBase* view, |
| 58 const TextInputState& text_input_state) { |
| 59 DCHECK(IsRegistered(view)); |
| 60 |
| 61 // Since |view| is registgered, then we already have a previous value for its |
| 62 // TextInputState. |
| 63 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], |
| 64 text_input_state); |
| 65 |
| 66 text_input_state_map_[view] = text_input_state; |
| 67 |
| 68 // |active_view_| is only updated when the state for |view| is not none. |
| 69 if (text_input_state.type != ui::TEXT_INPUT_TYPE_NONE) |
| 70 active_view_ = view; |
| 71 |
| 72 // If the state for |active_view_| is none, then we no longer have an |
| 73 // |active_view_|. |
| 74 if (active_view_ == view && text_input_state.type == ui::TEXT_INPUT_TYPE_NONE) |
| 75 active_view_ = nullptr; |
| 76 |
| 77 NotifyObserversAboutInputStateUpdate(view, changed); |
| 78 } |
| 79 |
| 80 void TextInputManager::Register(RenderWidgetHostViewBase* view) { |
| 81 DCHECK(!IsRegistered(view)); |
| 82 |
| 83 text_input_state_map_[view] = TextInputState(); |
| 84 } |
| 85 |
| 86 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { |
| 87 DCHECK(IsRegistered(view)); |
| 88 |
| 89 text_input_state_map_.erase(view); |
| 90 if (active_view_ == view) { |
| 91 active_view_ = nullptr; |
| 92 NotifyObserversAboutInputStateUpdate(view, true); |
| 93 } |
| 94 view->DidUnregisterFromTextInputManager(this); |
| 95 } |
| 96 |
| 97 bool TextInputManager::IsRegistered(RenderWidgetHostViewBase* view) const { |
| 98 return text_input_state_map_.count(view) == 1; |
| 99 } |
| 100 |
| 101 void TextInputManager::AddObserver(Observer* observer) { |
| 102 observer_list_.AddObserver(observer); |
| 103 } |
| 104 |
| 105 void TextInputManager::RemoveObserver(Observer* observer) { |
| 106 observer_list_.RemoveObserver(observer); |
| 107 } |
| 108 |
| 109 void TextInputManager::NotifyObserversAboutInputStateUpdate( |
| 110 RenderWidgetHostViewBase* updated_view, |
| 111 bool did_update_state) { |
| 112 FOR_EACH_OBSERVER( |
| 113 Observer, observer_list_, |
| 114 OnUpdateTextInputStateCalled(this, updated_view, did_update_state)); |
| 115 } |
| 116 |
| 117 } // namespace content |
OLD | NEW |