 Chromium Code Reviews
 Chromium Code Reviews Issue 698253004:
  Reland: Implement Aura side of unified touch text selection for contents  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 698253004:
  Reland: Implement Aura side of unified touch text selection for contents  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: content/browser/renderer_host/render_widget_host_view_aura.cc | 
| diff --git a/content/browser/renderer_host/render_widget_host_view_aura.cc b/content/browser/renderer_host/render_widget_host_view_aura.cc | 
| index fd85fcc8745cdf59c93e87322518bda129aff961..d1191d295ecb5d8adafae5687978510bc6bd872c 100644 | 
| --- a/content/browser/renderer_host/render_widget_host_view_aura.cc | 
| +++ b/content/browser/renderer_host/render_widget_host_view_aura.cc | 
| @@ -30,6 +30,7 @@ | 
| #include "content/browser/renderer_host/compositor_resize_lock_aura.h" | 
| #include "content/browser/renderer_host/dip_util.h" | 
| #include "content/browser/renderer_host/input/synthetic_gesture_target_aura.h" | 
| +#include "content/browser/renderer_host/input/ui_touch_selection_helper.h" | 
| #include "content/browser/renderer_host/input/web_input_event_util.h" | 
| #include "content/browser/renderer_host/overscroll_controller.h" | 
| #include "content/browser/renderer_host/render_view_host_delegate.h" | 
| @@ -71,6 +72,7 @@ | 
| #include "ui/events/blink/blink_event_util.h" | 
| #include "ui/events/event.h" | 
| #include "ui/events/event_utils.h" | 
| +#include "ui/events/gesture_detection/gesture_configuration.h" | 
| #include "ui/events/gestures/gesture_recognizer.h" | 
| #include "ui/gfx/canvas.h" | 
| #include "ui/gfx/display.h" | 
| @@ -78,6 +80,7 @@ | 
| #include "ui/gfx/geometry/size_conversions.h" | 
| #include "ui/gfx/screen.h" | 
| #include "ui/gfx/skia_util.h" | 
| +#include "ui/touch_selection/touch_selection_controller.h" | 
| #include "ui/wm/public/activation_client.h" | 
| #include "ui/wm/public/scoped_tooltip_disabler.h" | 
| #include "ui/wm/public/tooltip_client.h" | 
| @@ -442,7 +445,6 @@ RenderWidgetHostViewAura::RenderWidgetHostViewAura(RenderWidgetHost* host, | 
| showing_context_menu_(false), | 
| #endif | 
| has_snapped_to_boundary_(false), | 
| - touch_editing_client_(NULL), | 
| is_guest_view_hack_(is_guest_view_hack), | 
| weak_ptr_factory_(this) { | 
| if (!is_guest_view_hack_) | 
| @@ -868,6 +870,8 @@ void RenderWidgetHostViewAura::Focus() { | 
| void RenderWidgetHostViewAura::Blur() { | 
| window_->Blur(); | 
| + if (selection_controller_) | 
| + selection_controller_->HideAndDisallowShowingAutomatically(); | 
| 
sadrul
2015/04/29 19:55:03
Why is this needed? Should |selection_controller_|
 
mohsen
2015/04/30 18:29:34
My understanding was that this is called on some s
 | 
| } | 
| bool RenderWidgetHostViewAura::HasFocus() const { | 
| @@ -935,8 +939,6 @@ void RenderWidgetHostViewAura::TextInputTypeChanged( | 
| text_input_flags_ = flags; | 
| if (GetInputMethod()) | 
| GetInputMethod()->OnTextInputTypeChanged(this); | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->OnTextInputTypeChanged(text_input_type_); | 
| } | 
| } | 
| @@ -1017,47 +1019,7 @@ gfx::Size RenderWidgetHostViewAura::GetRequestedRendererSize() const { | 
| void RenderWidgetHostViewAura::SelectionBoundsChanged( | 
| const ViewHostMsg_SelectionBounds_Params& params) { | 
| - ui::SelectionBound anchor_bound, focus_bound; | 
| - anchor_bound.SetEdge(params.anchor_rect.origin(), | 
| - params.anchor_rect.bottom_left()); | 
| - focus_bound.SetEdge(params.focus_rect.origin(), | 
| - params.focus_rect.bottom_left()); | 
| - | 
| - if (params.anchor_rect == params.focus_rect) { | 
| - anchor_bound.set_type(ui::SelectionBound::CENTER); | 
| - focus_bound.set_type(ui::SelectionBound::CENTER); | 
| - } else { | 
| - // Whether text is LTR at the anchor handle. | 
| - bool anchor_LTR = params.anchor_dir == blink::WebTextDirectionLeftToRight; | 
| - // Whether text is LTR at the focus handle. | 
| - bool focus_LTR = params.focus_dir == blink::WebTextDirectionLeftToRight; | 
| - | 
| - if ((params.is_anchor_first && anchor_LTR) || | 
| - (!params.is_anchor_first && !anchor_LTR)) { | 
| - anchor_bound.set_type(ui::SelectionBound::LEFT); | 
| - } else { | 
| - anchor_bound.set_type(ui::SelectionBound::RIGHT); | 
| - } | 
| - if ((params.is_anchor_first && focus_LTR) || | 
| - (!params.is_anchor_first && !focus_LTR)) { | 
| - focus_bound.set_type(ui::SelectionBound::RIGHT); | 
| - } else { | 
| - focus_bound.set_type(ui::SelectionBound::LEFT); | 
| - } | 
| - } | 
| - | 
| - if (anchor_bound == selection_anchor_ && focus_bound == selection_focus_) | 
| - return; | 
| - | 
| - selection_anchor_ = anchor_bound; | 
| - selection_focus_ = focus_bound; | 
| - if (GetInputMethod()) | 
| - GetInputMethod()->OnCaretBoundsChanged(this); | 
| - | 
| - if (touch_editing_client_) { | 
| - touch_editing_client_->OnSelectionOrCursorChanged( | 
| - anchor_bound, focus_bound); | 
| - } | 
| + NOTREACHED() << "Selection bounds should be routed through the compositor."; | 
| 
sadrul
2015/04/29 19:55:03
As discussed offline, it would be a good idea to k
 
mohsen
2015/04/30 18:29:34
Sure.
 | 
| } | 
| void RenderWidgetHostViewAura::CopyFromCompositingSurface( | 
| @@ -1147,6 +1109,10 @@ void RenderWidgetHostViewAura::OnSwapCompositorFrame( | 
| frame->delegated_frame_data.Pass(), | 
| frame->metadata.device_scale_factor, | 
| frame->metadata.latency_info); | 
| + SelectionUpdated(frame->metadata.selection.is_editable, | 
| + frame->metadata.selection.is_empty_text_form_control, | 
| + ConvertSelectionBound(frame->metadata.selection.start), | 
| + ConvertSelectionBound(frame->metadata.selection.end)); | 
| return; | 
| } | 
| @@ -1159,8 +1125,8 @@ void RenderWidgetHostViewAura::OnSwapCompositorFrame( | 
| } | 
| void RenderWidgetHostViewAura::DidStopFlinging() { | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->DidStopFlinging(); | 
| + if (selection_controller_client_) | 
| + selection_controller_client_->OnScrollCompleted(); | 
| } | 
| #if defined(OS_WIN) | 
| @@ -1240,9 +1206,6 @@ void RenderWidgetHostViewAura::WheelEventAck( | 
| void RenderWidgetHostViewAura::GestureEventAck( | 
| const blink::WebGestureEvent& event, | 
| InputEventAckState ack_result) { | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->GestureEventAck(event.type); | 
| - | 
| if (overscroll_controller_) { | 
| overscroll_controller_->ReceivedEventACK( | 
| event, (INPUT_EVENT_ACK_STATE_CONSUMED == ack_result)); | 
| @@ -1633,9 +1596,8 @@ gfx::Rect RenderWidgetHostViewAura::ConvertRectFromScreen( | 
| } | 
| gfx::Rect RenderWidgetHostViewAura::GetCaretBounds() const { | 
| - gfx::Rect rect = | 
| - ui::RectBetweenSelectionBounds(selection_anchor_, selection_focus_); | 
| - return ConvertRectToScreen(rect); | 
| + return ConvertRectToScreen(ui::RectBetweenSelectionBounds(selection_start_, | 
| + selection_end_)); | 
| } | 
| bool RenderWidgetHostViewAura::GetCompositionCharacterBounds( | 
| @@ -1838,8 +1800,6 @@ bool RenderWidgetHostViewAura::CanFocus() { | 
| void RenderWidgetHostViewAura::OnCaptureLost() { | 
| host_->LostCapture(); | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->EndTouchEditing(false); | 
| } | 
| void RenderWidgetHostViewAura::OnPaint(const ui::PaintContext& context) { | 
| @@ -1922,8 +1882,6 @@ void RenderWidgetHostViewAura::GetHitTestMask(gfx::Path* mask) const { | 
| void RenderWidgetHostViewAura::OnKeyEvent(ui::KeyEvent* event) { | 
| TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnKeyEvent"); | 
| - if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event)) | 
| - return; | 
| if (popup_child_host_view_ && popup_child_host_view_->NeedsInputGrab()) { | 
| popup_child_host_view_->OnKeyEvent(event); | 
| @@ -1970,9 +1928,6 @@ void RenderWidgetHostViewAura::OnKeyEvent(ui::KeyEvent* event) { | 
| void RenderWidgetHostViewAura::OnMouseEvent(ui::MouseEvent* event) { | 
| TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnMouseEvent"); | 
| - if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event)) | 
| - return; | 
| - | 
| if (mouse_locked_) { | 
| aura::client::CursorClient* cursor_client = | 
| aura::client::GetCursorClient(window_->GetRootWindow()); | 
| @@ -2136,8 +2091,6 @@ void RenderWidgetHostViewAura::OnMouseEvent(ui::MouseEvent* event) { | 
| void RenderWidgetHostViewAura::OnScrollEvent(ui::ScrollEvent* event) { | 
| TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnScrollEvent"); | 
| - if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event)) | 
| - return; | 
| if (event->type() == ui::ET_SCROLL) { | 
| #if !defined(OS_WIN) | 
| @@ -2166,8 +2119,6 @@ void RenderWidgetHostViewAura::OnScrollEvent(ui::ScrollEvent* event) { | 
| void RenderWidgetHostViewAura::OnTouchEvent(ui::TouchEvent* event) { | 
| TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnTouchEvent"); | 
| - if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event)) | 
| - return; | 
| // Update the touch event first. | 
| if (!pointer_state_.OnTouch(*event)) { | 
| @@ -2175,6 +2126,13 @@ void RenderWidgetHostViewAura::OnTouchEvent(ui::TouchEvent* event) { | 
| return; | 
| } | 
| + if (selection_controller_ && | 
| + selection_controller_->WillHandleTouchEvent(pointer_state_)) { | 
| + event->SetHandled(); | 
| + pointer_state_.CleanupRemovedTouchPoints(*event); | 
| 
sadrul
2015/04/29 19:55:03
Should there be some Scoped thingy to make sure th
 
mohsen
2015/04/30 18:29:34
I thought of a similar thing, but thought it would
 | 
| + return; | 
| + } | 
| + | 
| blink::WebTouchEvent touch_event = ui::CreateWebTouchEventFromMotionEvent( | 
| pointer_state_, event->may_cause_scrolling()); | 
| pointer_state_.CleanupRemovedTouchPoints(*event); | 
| @@ -2187,8 +2145,44 @@ void RenderWidgetHostViewAura::OnTouchEvent(ui::TouchEvent* event) { | 
| host_->ForwardTouchEventWithLatencyInfo(touch_event, *event->latency()); | 
| } | 
| +void RenderWidgetHostViewAura::HandleGestureForTouchSelection( | 
| + ui::GestureEvent* event) { | 
| + if (!selection_controller_) | 
| + return; | 
| + | 
| + switch (event->type()) { | 
| + case ui::ET_GESTURE_LONG_PRESS: | 
| + selection_controller_->OnLongPressEvent(); | 
| + break; | 
| + case ui::ET_GESTURE_TAP: | 
| + if (RectBetweenSelectionBounds(selection_controller_->start(), | 
| + selection_controller_->end()) | 
| + .Contains(event->location())) { | 
| + if (!selection_controller_->is_insertion_active() && | 
| + !selection_controller_->is_selection_active()) { | 
| + selection_controller_->AllowShowingFromCurrentSelection(); | 
| + event->SetHandled(); | 
| + } else if (text_input_type_ == ui::TEXT_INPUT_TYPE_NONE) { | 
| + event->SetHandled(); | 
| + } | 
| + } | 
| + if (!event->handled()) | 
| + selection_controller_->OnTapEvent(); | 
| + break; | 
| + case ui::ET_GESTURE_SCROLL_BEGIN: | 
| + selection_controller_client_->OnScrollStarted(); | 
| + break; | 
| + case ui::ET_GESTURE_SCROLL_END: | 
| + selection_controller_client_->OnScrollCompleted(); | 
| + break; | 
| + default: | 
| + break; | 
| + } | 
| +} | 
| + | 
| void RenderWidgetHostViewAura::OnGestureEvent(ui::GestureEvent* event) { | 
| TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnGestureEvent"); | 
| + | 
| if ((event->type() == ui::ET_GESTURE_PINCH_BEGIN || | 
| event->type() == ui::ET_GESTURE_PINCH_UPDATE || | 
| event->type() == ui::ET_GESTURE_PINCH_END) && !pinch_zoom_enabled_) { | 
| @@ -2196,7 +2190,8 @@ void RenderWidgetHostViewAura::OnGestureEvent(ui::GestureEvent* event) { | 
| return; | 
| } | 
| - if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event)) | 
| + HandleGestureForTouchSelection(event); | 
| + if (event->handled()) | 
| return; | 
| // Confirm existing composition text on TAP gesture, to make sure the input | 
| @@ -2293,8 +2288,8 @@ void RenderWidgetHostViewAura::OnWindowFocused(aura::Window* gained_focus, | 
| DetachFromInputMethod(); | 
| host_->SetInputMethodActive(false); | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->EndTouchEditing(false); | 
| + if (selection_controller_) | 
| + selection_controller_->HideAndDisallowShowingAutomatically(); | 
| if (overscroll_controller_) | 
| overscroll_controller_->Cancel(); | 
| @@ -2351,9 +2346,8 @@ void RenderWidgetHostViewAura::OnHostMoved(const aura::WindowTreeHost* host, | 
| // RenderWidgetHostViewAura, private: | 
| RenderWidgetHostViewAura::~RenderWidgetHostViewAura() { | 
| - if (touch_editing_client_) | 
| - touch_editing_client_->OnViewDestroyed(); | 
| - | 
| + selection_controller_.reset(); | 
| + selection_controller_client_.reset(); | 
| delegated_frame_host_.reset(); | 
| window_observer_.reset(); | 
| if (window_->GetHost()) | 
| @@ -2504,6 +2498,23 @@ void RenderWidgetHostViewAura::SetOverscrollControllerEnabled(bool enabled) { | 
| overscroll_controller_.reset(new OverscrollController()); | 
| } | 
| +void RenderWidgetHostViewAura::InitSelectionController( | 
| + scoped_ptr<ui::TouchSelectionControllerClient> | 
| + selection_controller_client) { | 
| + DCHECK(!selection_controller_client_); | 
| + selection_controller_client_ = selection_controller_client.Pass(); | 
| + const int tap_timeout_ms = | 
| + ui::GestureConfiguration::GetInstance()->show_press_delay_in_ms(); | 
| + const int touch_slop = ui::GestureConfiguration::GetInstance() | 
| + ->max_touch_move_in_pixels_for_click(); | 
| + const bool show_on_tap_for_empty_editable = true; | 
| + selection_controller_.reset(new ui::TouchSelectionController( | 
| + selection_controller_client_.get(), | 
| + base::TimeDelta::FromMilliseconds(tap_timeout_ms), | 
| + touch_slop, | 
| + show_on_tap_for_empty_editable)); | 
| +} | 
| + | 
| void RenderWidgetHostViewAura::SnapToPhysicalPixelBoundary() { | 
| // The top left corner of our view in window coordinates might not land on a | 
| // device pixel boundary if we have a non-integer device scale. In that case, | 
| @@ -2542,10 +2553,6 @@ void RenderWidgetHostViewAura::InternalSetBounds(const gfx::Rect& rect) { | 
| window_->SetBounds(rect); | 
| host_->WasResized(); | 
| delegated_frame_host_->WasResized(); | 
| - if (touch_editing_client_) { | 
| - touch_editing_client_->OnSelectionOrCursorChanged(selection_anchor_, | 
| - selection_focus_); | 
| - } | 
| #if defined(OS_WIN) | 
| // Create the legacy dummy window which corresponds to the bounds of the | 
| // webcontents. This will be passed as the container window for windowless | 
| @@ -2689,6 +2696,24 @@ SkColorType RenderWidgetHostViewAura::PreferredReadbackFormat() { | 
| return kN32_SkColorType; | 
| } | 
| +void RenderWidgetHostViewAura::SelectionUpdated(bool is_editable, | 
| + bool is_empty_text_form_control, | 
| + const ui::SelectionBound& start, | 
| + const ui::SelectionBound& end) { | 
| + if (selection_start_ != start || selection_end_ != end) { | 
| + selection_start_ = start; | 
| + selection_end_ = end; | 
| + if (GetInputMethod()) | 
| + GetInputMethod()->OnCaretBoundsChanged(this); | 
| + } | 
| + | 
| + if (selection_controller_) { | 
| + selection_controller_->OnSelectionEditable(is_editable); | 
| + selection_controller_->OnSelectionEmpty(is_empty_text_form_control); | 
| + selection_controller_->OnSelectionBoundsUpdated(start, end); | 
| + } | 
| +} | 
| + | 
| //////////////////////////////////////////////////////////////////////////////// | 
| // DelegatedFrameHost, public: |