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 return true; |
| 22 #endif |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 TextInputManager::TextInputManager() : active_view_(nullptr) {} |
| 28 |
| 29 TextInputManager::~TextInputManager() { |
| 30 // It is necessary and sufficient to remove the TextInputManager from |
| 31 // all the registered views' observer lists. |
| 32 std::vector<RenderWidgetHostViewBase*> tracked_views; |
| 33 for (auto pair : text_input_state_map_) |
| 34 tracked_views.push_back(pair.first); |
| 35 |
| 36 for (RenderWidgetHostViewBase* view : tracked_views) |
| 37 Unregister(view); |
| 38 |
| 39 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 40 OnTextInputManagerDestroyed(this)); |
| 41 } |
| 42 |
| 43 const TextInputState* TextInputManager::GetTextInputState() { |
| 44 return !!active_view_ ? &text_input_state_map_[active_view_] : nullptr; |
| 45 } |
| 46 |
| 47 RenderWidgetHostViewBase* TextInputManager::GetActiveView() const { |
| 48 return active_view_; |
| 49 } |
| 50 |
| 51 void TextInputManager::UpdateTextInputState( |
| 52 RenderWidgetHostViewBase* view, |
| 53 const TextInputState& text_input_state) { |
| 54 DCHECK(IsRegisteredView(view)); |
| 55 |
| 56 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], |
| 57 text_input_state); |
| 58 |
| 59 text_input_state_map_[view] = text_input_state; |
| 60 |
| 61 // Only change |active_view_| if this new text input state is not none. |
| 62 if (text_input_state.type != ui::TEXT_INPUT_TYPE_NONE) |
| 63 active_view_ = view; |
| 64 |
| 65 // If the |active_view_| loses state, then there is no active sources. |
| 66 if (active_view_ == view && text_input_state.type == ui::TEXT_INPUT_TYPE_NONE) |
| 67 active_view_ = nullptr; |
| 68 |
| 69 text_input_state_map_[view] = text_input_state; |
| 70 NotifyObserversAboutInputStateUpdate(view, changed); |
| 71 } |
| 72 |
| 73 void TextInputManager::Register(RenderWidgetHostViewBase* view) { |
| 74 DCHECK(!IsRegisteredView(view)); |
| 75 |
| 76 text_input_state_map_[view] = TextInputState(); |
| 77 view->AddObserver(this); |
| 78 } |
| 79 |
| 80 void TextInputManager::Unregister(RenderWidgetHostViewBase* view) { |
| 81 DCHECK(IsRegisteredView(view)); |
| 82 |
| 83 text_input_state_map_.erase(view); |
| 84 if (active_view_ == view) { |
| 85 active_view_ = nullptr; |
| 86 NotifyObserversAboutInputStateUpdate(view, true); |
| 87 } |
| 88 view->RemoveObserver(this); |
| 89 } |
| 90 |
| 91 bool TextInputManager::IsRegisteredView(RenderWidgetHostViewBase* view) const { |
| 92 return text_input_state_map_.count(view) == 1; |
| 93 } |
| 94 |
| 95 void TextInputManager::AddObserver(Observer* observer) { |
| 96 observer_list_.AddObserver(observer); |
| 97 } |
| 98 |
| 99 void TextInputManager::RemoveObserver(Observer* observer) { |
| 100 observer_list_.RemoveObserver(observer); |
| 101 } |
| 102 |
| 103 void TextInputManager::OnRenderWidgetHostViewBaseDestroyed( |
| 104 RenderWidgetHostViewBase* view) { |
| 105 DCHECK(IsRegisteredView(view)); |
| 106 |
| 107 Unregister(view); |
| 108 |
| 109 if (active_view_ == view) { |
| 110 DCHECK(text_input_state_map_[active_view_].type != |
| 111 ui::TEXT_INPUT_TYPE_NONE); |
| 112 active_view_ = nullptr; |
| 113 NotifyObserversAboutInputStateUpdate(view, true); |
| 114 } |
| 115 } |
| 116 |
| 117 void TextInputManager::NotifyObserversAboutInputStateUpdate( |
| 118 RenderWidgetHostViewBase* updated_view, |
| 119 bool changed) { |
| 120 FOR_EACH_OBSERVER(Observer, observer_list_, |
| 121 OnTextInputStateUpdated(this, updated_view, changed)); |
| 122 } |
| 123 |
| 124 } // namespace content |
OLD | NEW |