| 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 #include "content/browser/renderer_host/text_input_manager.h" | 5 #include "content/browser/renderer_host/text_input_manager.h" |
| 6 | 6 |
| 7 #include "base/strings/string16.h" | 7 #include "base/strings/string16.h" |
| 8 #include "content/browser/renderer_host/render_widget_host_impl.h" | 8 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 9 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 9 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 10 #include "content/common/view_messages.h" | 10 #include "content/common/view_messages.h" |
| 11 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
| 12 #include "ui/gfx/range/range.h" | 12 #include "ui/gfx/range/range.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 bool AreDifferentTextInputStates(const content::TextInputState& old_state, | 18 bool ShouldUpdateTextInputState(const content::TextInputState& old_state, |
| 19 const content::TextInputState& new_state) { | 19 const content::TextInputState& new_state) { |
| 20 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
| 21 return old_state.type != new_state.type || old_state.mode != new_state.mode || | 21 return old_state.type != new_state.type || old_state.mode != new_state.mode || |
| 22 old_state.flags != new_state.flags || | 22 old_state.flags != new_state.flags || |
| 23 old_state.can_compose_inline != new_state.can_compose_inline; | 23 old_state.can_compose_inline != new_state.can_compose_inline; |
| 24 #elif defined(OS_MACOSX) | 24 #elif defined(OS_MACOSX) |
| 25 return old_state.type != new_state.type || | 25 return old_state.type != new_state.type || |
| 26 old_state.can_compose_inline != new_state.can_compose_inline; | 26 old_state.can_compose_inline != new_state.can_compose_inline; |
| 27 #elif defined(OS_ANDROID) |
| 28 // On Android, TextInputState update is sent only if there is some change in |
| 29 // the state. So the new state is always different. |
| 30 return true; |
| 27 #else | 31 #else |
| 28 // TODO(ekaramad): Implement the logic for other platforms (crbug.com/578168). | |
| 29 NOTREACHED(); | 32 NOTREACHED(); |
| 30 return true; | 33 return true; |
| 31 #endif | 34 #endif |
| 32 } | 35 } |
| 33 | 36 |
| 34 } // namespace | 37 } // namespace |
| 35 | 38 |
| 36 TextInputManager::TextInputManager() : active_view_(nullptr) {} | 39 TextInputManager::TextInputManager() : active_view_(nullptr) {} |
| 37 | 40 |
| 38 TextInputManager::~TextInputManager() { | 41 TextInputManager::~TextInputManager() { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 for (auto* view : views) | 53 for (auto* view : views) |
| 51 Unregister(view); | 54 Unregister(view); |
| 52 } | 55 } |
| 53 | 56 |
| 54 RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const { | 57 RenderWidgetHostImpl* TextInputManager::GetActiveWidget() const { |
| 55 return !!active_view_ ? static_cast<RenderWidgetHostImpl*>( | 58 return !!active_view_ ? static_cast<RenderWidgetHostImpl*>( |
| 56 active_view_->GetRenderWidgetHost()) | 59 active_view_->GetRenderWidgetHost()) |
| 57 : nullptr; | 60 : nullptr; |
| 58 } | 61 } |
| 59 | 62 |
| 60 const TextInputState* TextInputManager::GetTextInputState() const { | 63 const TextInputState* TextInputManager::GetTextInputState( |
| 61 return !!active_view_ ? &text_input_state_map_.at(active_view_) : nullptr; | 64 RenderWidgetHostViewBase* view) const { |
| 65 DCHECK(!view || IsRegistered(view)); |
| 66 if (!view) |
| 67 view = active_view_; |
| 68 return !!view ? &text_input_state_map_.at(view) : nullptr; |
| 62 } | 69 } |
| 63 | 70 |
| 64 const TextInputManager::SelectionRegion* TextInputManager::GetSelectionRegion( | 71 const TextInputManager::SelectionRegion* TextInputManager::GetSelectionRegion( |
| 65 RenderWidgetHostViewBase* view) const { | 72 RenderWidgetHostViewBase* view) const { |
| 66 DCHECK(!view || IsRegistered(view)); | 73 DCHECK(!view || IsRegistered(view)); |
| 67 if (!view) | 74 if (!view) |
| 68 view = active_view_; | 75 view = active_view_; |
| 69 return view ? &selection_region_map_.at(view) : nullptr; | 76 return view ? &selection_region_map_.at(view) : nullptr; |
| 70 } | 77 } |
| 71 | 78 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 98 // one other view has become active and we have received the corresponding | 105 // one other view has become active and we have received the corresponding |
| 99 // IPC from their RenderWidget sooner than this one. That also means we have | 106 // IPC from their RenderWidget sooner than this one. That also means we have |
| 100 // already synthesized the loss of TextInputState for the |view| before (see | 107 // already synthesized the loss of TextInputState for the |view| before (see |
| 101 // below). So we can forget about this method ever being called (no observer | 108 // below). So we can forget about this method ever being called (no observer |
| 102 // calls necessary). | 109 // calls necessary). |
| 103 return; | 110 return; |
| 104 } | 111 } |
| 105 | 112 |
| 106 // Since |view| is registered, we already have a previous value for its | 113 // Since |view| is registered, we already have a previous value for its |
| 107 // TextInputState. | 114 // TextInputState. |
| 108 bool changed = AreDifferentTextInputStates(text_input_state_map_[view], | 115 bool changed = ShouldUpdateTextInputState(text_input_state_map_[view], |
| 109 text_input_state); | 116 text_input_state); |
| 110 | 117 |
| 111 text_input_state_map_[view] = text_input_state; | 118 text_input_state_map_[view] = text_input_state; |
| 112 | 119 |
| 113 // If |view| is different from |active_view| and its |TextInputState.type| is | 120 // If |view| is different from |active_view| and its |TextInputState.type| is |
| 114 // not NONE, |active_view_| should change to |view|. | 121 // not NONE, |active_view_| should change to |view|. |
| 115 if (text_input_state.type != ui::TEXT_INPUT_TYPE_NONE && | 122 if (text_input_state.type != ui::TEXT_INPUT_TYPE_NONE && |
| 116 active_view_ != view) { | 123 active_view_ != view) { |
| 117 if (active_view_) { | 124 if (active_view_) { |
| 118 // Ideally, we should always receive an IPC from |active_view_|'s | 125 // Ideally, we should always receive an IPC from |active_view_|'s |
| 119 // RenderWidget to reset its |TextInputState.type| to NONE, before any | 126 // RenderWidget to reset its |TextInputState.type| to NONE, before any |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 "point exceeds text length)."; | 348 "point exceeds text length)."; |
| 342 return false; | 349 return false; |
| 343 } | 350 } |
| 344 | 351 |
| 345 selected_text->clear(); | 352 selected_text->clear(); |
| 346 selected_text->append(text.substr(pos, n)); | 353 selected_text->append(text.substr(pos, n)); |
| 347 return true; | 354 return true; |
| 348 } | 355 } |
| 349 | 356 |
| 350 } // namespace content | 357 } // namespace content |
| OLD | NEW |