| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/render_widget_host_input_event_router.h" | 5 #include "content/browser/renderer_host/render_widget_host_input_event_router.h" |
| 6 | 6 |
| 7 #include "cc/quads/surface_draw_quad.h" | 7 #include "cc/quads/surface_draw_quad.h" |
| 8 #include "cc/surfaces/surface_id_allocator.h" | 8 #include "cc/surfaces/surface_id_allocator.h" |
| 9 #include "cc/surfaces/surface_manager.h" | 9 #include "cc/surfaces/surface_manager.h" |
| 10 #include "content/browser/renderer_host/render_widget_host_view_base.h" | 10 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 11 #include "content/common/frame_messages.h" | 11 #include "content/common/frame_messages.h" |
| 12 #include "third_party/WebKit/public/web/WebInputEvent.h" | 12 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 13 | 13 |
| 14 namespace content { | 14 namespace content { |
| 15 | 15 |
| 16 void RenderWidgetHostInputEventRouter::OnRenderWidgetHostViewBaseDestroyed( |
| 17 RenderWidgetHostViewBase* view) { |
| 18 view->RemoveObserver(this); |
| 19 |
| 20 // Remove this view from the owner_map. |
| 21 for (auto entry : owner_map_) { |
| 22 if (entry.second == view) { |
| 23 owner_map_.erase(entry.first); |
| 24 // There will only be one instance of a particular view in the map. |
| 25 break; |
| 26 } |
| 27 } |
| 28 |
| 29 if (view == current_touch_target_) { |
| 30 current_touch_target_ = nullptr; |
| 31 active_touches_ = 0; |
| 32 } |
| 33 } |
| 34 |
| 35 void RenderWidgetHostInputEventRouter::ClearAllObserverRegistrations() { |
| 36 for (auto entry : owner_map_) |
| 37 entry.second->RemoveObserver(this); |
| 38 owner_map_.clear(); |
| 39 } |
| 40 |
| 16 RenderWidgetHostInputEventRouter::HittestDelegate::HittestDelegate( | 41 RenderWidgetHostInputEventRouter::HittestDelegate::HittestDelegate( |
| 17 const std::unordered_map<cc::SurfaceId, HittestData, cc::SurfaceIdHash>& | 42 const std::unordered_map<cc::SurfaceId, HittestData, cc::SurfaceIdHash>& |
| 18 hittest_data) | 43 hittest_data) |
| 19 : hittest_data_(hittest_data) {} | 44 : hittest_data_(hittest_data) {} |
| 20 | 45 |
| 21 bool RenderWidgetHostInputEventRouter::HittestDelegate::RejectHitTarget( | 46 bool RenderWidgetHostInputEventRouter::HittestDelegate::RejectHitTarget( |
| 22 const cc::SurfaceDrawQuad* surface_quad, | 47 const cc::SurfaceDrawQuad* surface_quad, |
| 23 const gfx::Point& point_in_quad_space) { | 48 const gfx::Point& point_in_quad_space) { |
| 24 auto it = hittest_data_.find(surface_quad->surface_id); | 49 auto it = hittest_data_.find(surface_quad->surface_id); |
| 25 if (it != hittest_data_.end() && it->second.ignored_for_hittest) | 50 if (it != hittest_data_.end() && it->second.ignored_for_hittest) |
| 26 return true; | 51 return true; |
| 27 return false; | 52 return false; |
| 28 } | 53 } |
| 29 | 54 |
| 30 bool RenderWidgetHostInputEventRouter::HittestDelegate::AcceptHitTarget( | 55 bool RenderWidgetHostInputEventRouter::HittestDelegate::AcceptHitTarget( |
| 31 const cc::SurfaceDrawQuad* surface_quad, | 56 const cc::SurfaceDrawQuad* surface_quad, |
| 32 const gfx::Point& point_in_quad_space) { | 57 const gfx::Point& point_in_quad_space) { |
| 33 auto it = hittest_data_.find(surface_quad->surface_id); | 58 auto it = hittest_data_.find(surface_quad->surface_id); |
| 34 if (it != hittest_data_.end() && !it->second.ignored_for_hittest) | 59 if (it != hittest_data_.end() && !it->second.ignored_for_hittest) |
| 35 return true; | 60 return true; |
| 36 return false; | 61 return false; |
| 37 } | 62 } |
| 38 | 63 |
| 39 RenderWidgetHostInputEventRouter::RenderWidgetHostInputEventRouter() | 64 RenderWidgetHostInputEventRouter::RenderWidgetHostInputEventRouter() |
| 40 : active_touches_(0) {} | 65 : current_touch_target_(nullptr), active_touches_(0) {} |
| 41 | 66 |
| 42 RenderWidgetHostInputEventRouter::~RenderWidgetHostInputEventRouter() { | 67 RenderWidgetHostInputEventRouter::~RenderWidgetHostInputEventRouter() { |
| 43 owner_map_.clear(); | 68 // We may be destroyed before some of the owners in the map, so we must |
| 69 // remove ourself from their observer lists. |
| 70 ClearAllObserverRegistrations(); |
| 44 } | 71 } |
| 45 | 72 |
| 46 RenderWidgetHostViewBase* RenderWidgetHostInputEventRouter::FindEventTarget( | 73 RenderWidgetHostViewBase* RenderWidgetHostInputEventRouter::FindEventTarget( |
| 47 RenderWidgetHostViewBase* root_view, | 74 RenderWidgetHostViewBase* root_view, |
| 48 const gfx::Point& point, | 75 const gfx::Point& point, |
| 49 gfx::Point* transformed_point) { | 76 gfx::Point* transformed_point) { |
| 50 // Short circuit if owner_map has only one RenderWidgetHostView, no need for | 77 // Short circuit if owner_map has only one RenderWidgetHostView, no need for |
| 51 // hit testing. | 78 // hit testing. |
| 52 if (owner_map_.size() <= 1) { | 79 if (owner_map_.size() <= 1) { |
| 53 *transformed_point = point; | 80 *transformed_point = point; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 64 // single process with only one RenderWidgetHost. | 91 // single process with only one RenderWidgetHost. |
| 65 uint32_t surface_id_namespace = | 92 uint32_t surface_id_namespace = |
| 66 root_view->SurfaceIdNamespaceAtPoint(&delegate, point, transformed_point); | 93 root_view->SurfaceIdNamespaceAtPoint(&delegate, point, transformed_point); |
| 67 const SurfaceIdNamespaceOwnerMap::iterator iter = | 94 const SurfaceIdNamespaceOwnerMap::iterator iter = |
| 68 owner_map_.find(surface_id_namespace); | 95 owner_map_.find(surface_id_namespace); |
| 69 // If the point hit a Surface whose namspace is no longer in the map, then | 96 // If the point hit a Surface whose namspace is no longer in the map, then |
| 70 // it likely means the RenderWidgetHostView has been destroyed but its | 97 // it likely means the RenderWidgetHostView has been destroyed but its |
| 71 // parent frame has not sent a new compositor frame since that happened. | 98 // parent frame has not sent a new compositor frame since that happened. |
| 72 if (iter == owner_map_.end()) | 99 if (iter == owner_map_.end()) |
| 73 return root_view; | 100 return root_view; |
| 74 RenderWidgetHostViewBase* target = iter->second.get(); | 101 |
| 75 // If we find the weak pointer is now null, it means the map entry is stale | 102 return iter->second; |
| 76 // and should be removed to free space. | |
| 77 if (!target) | |
| 78 owner_map_.erase(iter); | |
| 79 return target; | |
| 80 } | 103 } |
| 81 | 104 |
| 82 void RenderWidgetHostInputEventRouter::RouteMouseEvent( | 105 void RenderWidgetHostInputEventRouter::RouteMouseEvent( |
| 83 RenderWidgetHostViewBase* root_view, | 106 RenderWidgetHostViewBase* root_view, |
| 84 blink::WebMouseEvent* event) { | 107 blink::WebMouseEvent* event) { |
| 85 gfx::Point transformed_point; | 108 gfx::Point transformed_point; |
| 86 RenderWidgetHostViewBase* target = FindEventTarget( | 109 RenderWidgetHostViewBase* target = FindEventTarget( |
| 87 root_view, gfx::Point(event->x, event->y), &transformed_point); | 110 root_view, gfx::Point(event->x, event->y), &transformed_point); |
| 88 if (!target) | 111 if (!target) |
| 89 return; | 112 return; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 115 const ui::LatencyInfo& latency) { | 138 const ui::LatencyInfo& latency) { |
| 116 switch (event->type) { | 139 switch (event->type) { |
| 117 case blink::WebInputEvent::TouchStart: { | 140 case blink::WebInputEvent::TouchStart: { |
| 118 if (!active_touches_) { | 141 if (!active_touches_) { |
| 119 // Since this is the first touch, it defines the target for the rest | 142 // Since this is the first touch, it defines the target for the rest |
| 120 // of this sequence. | 143 // of this sequence. |
| 121 DCHECK(!current_touch_target_); | 144 DCHECK(!current_touch_target_); |
| 122 gfx::Point transformed_point; | 145 gfx::Point transformed_point; |
| 123 gfx::Point original_point(event->touches[0].position.x, | 146 gfx::Point original_point(event->touches[0].position.x, |
| 124 event->touches[0].position.y); | 147 event->touches[0].position.y); |
| 125 RenderWidgetHostViewBase* target = | 148 current_touch_target_ = |
| 126 FindEventTarget(root_view, original_point, &transformed_point); | 149 FindEventTarget(root_view, original_point, &transformed_point); |
| 127 if (!target) | 150 if (!current_touch_target_) |
| 128 return; | 151 return; |
| 129 | |
| 130 // Store the weak-ptr to the target, since it could disappear in the | |
| 131 // middle of a touch sequence. | |
| 132 current_touch_target_ = target->GetWeakPtr(); | |
| 133 } | 152 } |
| 134 ++active_touches_; | 153 ++active_touches_; |
| 135 if (current_touch_target_) | 154 if (current_touch_target_) |
| 136 current_touch_target_->ProcessTouchEvent(*event, latency); | 155 current_touch_target_->ProcessTouchEvent(*event, latency); |
| 137 break; | 156 break; |
| 138 } | 157 } |
| 139 case blink::WebInputEvent::TouchMove: | 158 case blink::WebInputEvent::TouchMove: |
| 140 if (current_touch_target_) | 159 if (current_touch_target_) |
| 141 current_touch_target_->ProcessTouchEvent(*event, latency); | 160 current_touch_target_->ProcessTouchEvent(*event, latency); |
| 142 break; | 161 break; |
| 143 case blink::WebInputEvent::TouchEnd: | 162 case blink::WebInputEvent::TouchEnd: |
| 144 case blink::WebInputEvent::TouchCancel: | 163 case blink::WebInputEvent::TouchCancel: |
| 164 if (!current_touch_target_) |
| 165 break; |
| 166 |
| 145 DCHECK(active_touches_); | 167 DCHECK(active_touches_); |
| 146 if (current_touch_target_) | 168 current_touch_target_->ProcessTouchEvent(*event, latency); |
| 147 current_touch_target_->ProcessTouchEvent(*event, latency); | |
| 148 --active_touches_; | 169 --active_touches_; |
| 149 if (!active_touches_) | 170 if (!active_touches_) |
| 150 current_touch_target_ = WeakTarget(); | 171 current_touch_target_ = nullptr; |
| 151 break; | 172 break; |
| 152 default: | 173 default: |
| 153 NOTREACHED(); | 174 NOTREACHED(); |
| 154 } | 175 } |
| 155 } | 176 } |
| 156 | 177 |
| 157 void RenderWidgetHostInputEventRouter::AddSurfaceIdNamespaceOwner( | 178 void RenderWidgetHostInputEventRouter::AddSurfaceIdNamespaceOwner( |
| 158 uint32_t id, | 179 uint32_t id, |
| 159 RenderWidgetHostViewBase* owner) { | 180 RenderWidgetHostViewBase* owner) { |
| 160 DCHECK(owner_map_.find(id) == owner_map_.end()); | 181 DCHECK(owner_map_.find(id) == owner_map_.end()); |
| 161 owner_map_.insert(std::make_pair(id, owner->GetWeakPtr())); | 182 // We want to be notified if the owner is destroyed so we can remove it from |
| 183 // our map. |
| 184 owner->AddObserver(this); |
| 185 owner_map_.insert(std::make_pair(id, owner)); |
| 162 } | 186 } |
| 163 | 187 |
| 164 void RenderWidgetHostInputEventRouter::RemoveSurfaceIdNamespaceOwner( | 188 void RenderWidgetHostInputEventRouter::RemoveSurfaceIdNamespaceOwner( |
| 165 uint32_t id) { | 189 uint32_t id) { |
| 166 owner_map_.erase(id); | 190 auto it_to_remove = owner_map_.find(id); |
| 191 if (it_to_remove != owner_map_.end()) { |
| 192 it_to_remove->second->RemoveObserver(this); |
| 193 owner_map_.erase(it_to_remove); |
| 194 } |
| 167 | 195 |
| 168 for (auto it = hittest_data_.begin(); it != hittest_data_.end();) { | 196 for (auto it = hittest_data_.begin(); it != hittest_data_.end();) { |
| 169 if (cc::SurfaceIdAllocator::NamespaceForId(it->first) == id) | 197 if (cc::SurfaceIdAllocator::NamespaceForId(it->first) == id) |
| 170 it = hittest_data_.erase(it); | 198 it = hittest_data_.erase(it); |
| 171 else | 199 else |
| 172 ++it; | 200 ++it; |
| 173 } | 201 } |
| 174 } | 202 } |
| 175 | 203 |
| 176 void RenderWidgetHostInputEventRouter::OnHittestData( | 204 void RenderWidgetHostInputEventRouter::OnHittestData( |
| 177 const FrameHostMsg_HittestData_Params& params) { | 205 const FrameHostMsg_HittestData_Params& params) { |
| 178 if (owner_map_.find(cc::SurfaceIdAllocator::NamespaceForId( | 206 if (owner_map_.find(cc::SurfaceIdAllocator::NamespaceForId( |
| 179 params.surface_id)) == owner_map_.end()) { | 207 params.surface_id)) == owner_map_.end()) { |
| 180 return; | 208 return; |
| 181 } | 209 } |
| 182 HittestData data; | 210 HittestData data; |
| 183 data.ignored_for_hittest = params.ignored_for_hittest; | 211 data.ignored_for_hittest = params.ignored_for_hittest; |
| 184 hittest_data_[params.surface_id] = data; | 212 hittest_data_[params.surface_id] = data; |
| 185 } | 213 } |
| 186 | 214 |
| 187 } // namespace content | 215 } // namespace content |
| OLD | NEW |