1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_IMPL_H_ 6 #define CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_IMPL_H_ 7 8 #include <deque> 9 #include <list> 10 #include <map> 11 #include <queue> 12 #include <string> 13 #include <utility> 14 #include <vector> 15 16 #include "base/callback.h" 17 #include "base/gtest_prod_util.h" 18 #include "base/memory/scoped_ptr.h" 19 #include "base/memory/weak_ptr.h" 20 #include "base/observer_list.h" 21 #include "base/process/kill.h" 22 #include "base/strings/string16.h" 23 #include "base/time/time.h" 24 #include "base/timer/timer.h" 25 #include "build/build_config.h" 26 #include "cc/resources/shared_bitmap.h" 27 #include "content/browser/accessibility/browser_accessibility_manager.h" 28 #include "content/browser/renderer_host/event_with_latency_info.h" 29 #include "content/browser/renderer_host/input/input_ack_handler.h" 30 #include "content/browser/renderer_host/input/input_router_client.h" 31 #include "content/browser/renderer_host/input/synthetic_gesture.h" 32 #include "content/browser/renderer_host/input/touch_emulator_client.h" 33 #include "content/common/input/input_event_ack_state.h" 34 #include "content/common/input/synthetic_gesture_packet.h" 35 #include "content/common/view_message_enums.h" 36 #include "content/public/browser/render_widget_host.h" 37 #include "content/public/common/page_zoom.h" 38 #include "ipc/ipc_listener.h" 39 #include "ui/base/ime/text_input_mode.h" 40 #include "ui/base/ime/text_input_type.h" 41 #include "ui/events/latency_info.h" 42 #include "ui/gfx/native_widget_types.h" 43 44 struct AcceleratedSurfaceMsg_BufferPresented_Params; 45 struct ViewHostMsg_BeginSmoothScroll_Params; 46 struct ViewHostMsg_CompositorSurfaceBuffersSwapped_Params; 47 struct ViewHostMsg_SelectionBounds_Params; 48 struct ViewHostMsg_TextInputState_Params; 49 struct ViewHostMsg_UpdateRect_Params; 50 51 namespace base { 52 class TimeTicks; 53 } 54 55 namespace cc { 56 class CompositorFrame; 57 class CompositorFrameAck; 58 } 59 60 namespace gfx { 61 class Range; 62 } 63 64 namespace ui { 65 class KeyEvent; 66 } 67 68 namespace blink { 69 class WebInputEvent; 70 class WebMouseEvent; 71 struct WebCompositionUnderline; 72 struct WebScreenInfo; 73 } 74 75 #if defined(OS_ANDROID) 76 namespace blink { 77 class WebLayer; 78 } 79 #endif 80 81 namespace content { 82 class InputRouter; 83 class MockRenderWidgetHost; 84 class RenderWidgetHostDelegate; 85 class RenderWidgetHostViewBase; 86 class SyntheticGestureController; 87 class TimeoutMonitor; 88 class TouchEmulator; 89 class WebCursor; 90 struct EditCommand; 91 92 // This implements the RenderWidgetHost interface that is exposed to 93 // embedders of content, and adds things only visible to content. 94 class CONTENT_EXPORT RenderWidgetHostImpl 95 : virtual public RenderWidgetHost, 96 public InputRouterClient, 97 public InputAckHandler, 98 public TouchEmulatorClient, 99 public IPC::Listener, 100 public BrowserAccessibilityDelegate { 101 public: 102 // routing_id can be MSG_ROUTING_NONE, in which case the next available 103 // routing id is taken from the RenderProcessHost. 104 // If this object outlives |delegate|, DetachDelegate() must be called when 105 // |delegate| goes away. 106 RenderWidgetHostImpl(RenderWidgetHostDelegate* delegate, 107 RenderProcessHost* process, 108 int routing_id, 109 bool hidden); 110 virtual ~RenderWidgetHostImpl(); 111 112 // Similar to RenderWidgetHost::FromID, but returning the Impl object. 113 static RenderWidgetHostImpl* FromID(int32 process_id, int32 routing_id); 114 115 // Returns all RenderWidgetHosts including swapped out ones for 116 // internal use. The public interface 117 // RendgerWidgetHost::GetRenderWidgetHosts only returns active ones. 118 static scoped_ptr<RenderWidgetHostIterator> GetAllRenderWidgetHosts(); 119 120 // Use RenderWidgetHostImpl::From(rwh) to downcast a 121 // RenderWidgetHost to a RenderWidgetHostImpl. Internally, this 122 // uses RenderWidgetHost::AsRenderWidgetHostImpl(). 123 static RenderWidgetHostImpl* From(RenderWidgetHost* rwh); 124 set_hung_renderer_delay_ms(const base::TimeDelta & timeout)125 void set_hung_renderer_delay_ms(const base::TimeDelta& timeout) { 126 hung_renderer_delay_ms_ = timeout.InMilliseconds(); 127 } 128 129 // RenderWidgetHost implementation. 130 virtual void UpdateTextDirection(blink::WebTextDirection direction) OVERRIDE; 131 virtual void NotifyTextDirection() OVERRIDE; 132 virtual void Focus() OVERRIDE; 133 virtual void Blur() OVERRIDE; 134 virtual void SetActive(bool active) OVERRIDE; 135 virtual void CopyFromBackingStore( 136 const gfx::Rect& src_rect, 137 const gfx::Size& accelerated_dst_size, 138 const base::Callback<void(bool, const SkBitmap&)>& callback, 139 const SkBitmap::Config& bitmap_config) OVERRIDE; 140 virtual bool CanCopyFromBackingStore() OVERRIDE; 141 #if defined(OS_ANDROID) 142 virtual void LockBackingStore() OVERRIDE; 143 virtual void UnlockBackingStore() OVERRIDE; 144 #endif 145 virtual void EnableFullAccessibilityMode() OVERRIDE; 146 virtual bool IsFullAccessibilityModeForTesting() OVERRIDE; 147 virtual void EnableTreeOnlyAccessibilityMode() OVERRIDE; 148 virtual bool IsTreeOnlyAccessibilityModeForTesting() OVERRIDE; 149 virtual void ForwardMouseEvent( 150 const blink::WebMouseEvent& mouse_event) OVERRIDE; 151 virtual void ForwardWheelEvent( 152 const blink::WebMouseWheelEvent& wheel_event) OVERRIDE; 153 virtual void ForwardKeyboardEvent( 154 const NativeWebKeyboardEvent& key_event) OVERRIDE; 155 virtual const gfx::Vector2d& GetLastScrollOffset() const OVERRIDE; 156 virtual RenderProcessHost* GetProcess() const OVERRIDE; 157 virtual int GetRoutingID() const OVERRIDE; 158 virtual RenderWidgetHostView* GetView() const OVERRIDE; 159 virtual bool IsLoading() const OVERRIDE; 160 virtual bool IsRenderView() const OVERRIDE; 161 virtual void ResizeRectChanged(const gfx::Rect& new_rect) OVERRIDE; 162 virtual void RestartHangMonitorTimeout() OVERRIDE; 163 virtual void SetIgnoreInputEvents(bool ignore_input_events) OVERRIDE; 164 virtual void Stop() OVERRIDE; 165 virtual void WasResized() OVERRIDE; 166 virtual void AddKeyPressEventCallback( 167 const KeyPressEventCallback& callback) OVERRIDE; 168 virtual void RemoveKeyPressEventCallback( 169 const KeyPressEventCallback& callback) OVERRIDE; 170 virtual void AddMouseEventCallback( 171 const MouseEventCallback& callback) OVERRIDE; 172 virtual void RemoveMouseEventCallback( 173 const MouseEventCallback& callback) OVERRIDE; 174 virtual void GetWebScreenInfo(blink::WebScreenInfo* result) OVERRIDE; 175 176 virtual SkBitmap::Config PreferredReadbackFormat() OVERRIDE; 177 178 // BrowserAccessibilityDelegate 179 virtual void AccessibilitySetFocus(int acc_obj_id) OVERRIDE; 180 virtual void AccessibilityDoDefaultAction(int acc_obj_id) OVERRIDE; 181 virtual void AccessibilityShowMenu(int acc_obj_id) OVERRIDE; 182 virtual void AccessibilityScrollToMakeVisible( 183 int acc_obj_id, gfx::Rect subfocus) OVERRIDE; 184 virtual void AccessibilityScrollToPoint( 185 int acc_obj_id, gfx::Point point) OVERRIDE; 186 virtual void AccessibilitySetTextSelection( 187 int acc_obj_id, int start_offset, int end_offset) OVERRIDE; 188 virtual bool AccessibilityViewHasFocus() const OVERRIDE; 189 virtual gfx::Rect AccessibilityGetViewBounds() const OVERRIDE; 190 virtual gfx::Point AccessibilityOriginInScreen(const gfx::Rect& bounds) 191 const OVERRIDE; 192 virtual void AccessibilityHitTest(const gfx::Point& point) OVERRIDE; 193 virtual void AccessibilityFatalError() OVERRIDE; 194 195 const NativeWebKeyboardEvent* GetLastKeyboardEvent() const; 196 197 // Notification that the screen info has changed. 198 void NotifyScreenInfoChanged(); 199 200 // Invalidates the cached screen info so that next resize request 201 // will carry the up to date screen info. Unlike 202 // |NotifyScreenInfoChanged|, this doesn't send a message to the renderer. 203 void InvalidateScreenInfo(); 204 205 // Sets the View of this RenderWidgetHost. 206 void SetView(RenderWidgetHostViewBase* view); 207 surface_id()208 int surface_id() const { return surface_id_; } 209 empty()210 bool empty() const { return current_size_.IsEmpty(); } 211 212 // Called when a renderer object already been created for this host, and we 213 // just need to be attached to it. Used for window.open, <select> dropdown 214 // menus, and other times when the renderer initiates creating an object. 215 virtual void Init(); 216 217 // Tells the renderer to die and then calls Destroy(). 218 virtual void Shutdown(); 219 220 // IPC::Listener 221 virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; 222 223 // Sends a message to the corresponding object in the renderer. 224 virtual bool Send(IPC::Message* msg) OVERRIDE; 225 226 // Called to notify the RenderWidget that it has been hidden or restored from 227 // having been hidden. 228 void WasHidden(); 229 void WasShown(); 230 231 // Returns true if the RenderWidget is hidden. is_hidden()232 bool is_hidden() const { return is_hidden_; } 233 234 // Called to notify the RenderWidget that its associated native window 235 // got/lost focused. 236 virtual void GotFocus(); 237 virtual void LostCapture(); 238 239 // Called to notify the RenderWidget that it has lost the mouse lock. 240 virtual void LostMouseLock(); 241 242 // Noifies the RenderWidget of the current mouse cursor visibility state. 243 void SendCursorVisibilityState(bool is_visible); 244 245 // Notifies the RenderWidgetHost that the View was destroyed. 246 void ViewDestroyed(); 247 248 // Indicates if the page has finished loading. 249 void SetIsLoading(bool is_loading); 250 251 // Pause for a moment to wait for pending repaint or resize messages sent to 252 // the renderer to arrive. If pending resize messages are for an old window 253 // size, then also pump through a new resize message if there is time. 254 void PauseForPendingResizeOrRepaints(); 255 256 // Whether pausing may be useful. 257 bool CanPauseForPendingResizeOrRepaints(); 258 259 // Wait for a surface matching the size of the widget's view, possibly 260 // blocking until the renderer sends a new frame. 261 void WaitForSurface(); 262 263 // GPU accelerated version of GetBackingStore function. This will 264 // trigger a re-composite to the view. It may fail if a resize is pending, or 265 // if a composite has already been requested and not acked yet. 266 bool ScheduleComposite(); 267 268 // Starts a hang monitor timeout. If there's already a hang monitor timeout 269 // the new one will only fire if it has a shorter delay than the time 270 // left on the existing timeouts. 271 void StartHangMonitorTimeout(base::TimeDelta delay); 272 273 // Stops all existing hang monitor timeouts and assumes the renderer is 274 // responsive. 275 void StopHangMonitorTimeout(); 276 277 // Forwards the given message to the renderer. These are called by the view 278 // when it has received a message. 279 void ForwardGestureEventWithLatencyInfo( 280 const blink::WebGestureEvent& gesture_event, 281 const ui::LatencyInfo& ui_latency); 282 void ForwardTouchEventWithLatencyInfo( 283 const blink::WebTouchEvent& touch_event, 284 const ui::LatencyInfo& ui_latency); 285 void ForwardMouseEventWithLatencyInfo( 286 const blink::WebMouseEvent& mouse_event, 287 const ui::LatencyInfo& ui_latency); 288 void ForwardWheelEventWithLatencyInfo( 289 const blink::WebMouseWheelEvent& wheel_event, 290 const ui::LatencyInfo& ui_latency); 291 292 // TouchEmulatorClient overrides. 293 virtual void ForwardGestureEvent( 294 const blink::WebGestureEvent& gesture_event) OVERRIDE; 295 virtual void ForwardTouchEvent( 296 const blink::WebTouchEvent& touch_event) OVERRIDE; 297 virtual void SetCursor(const WebCursor& cursor) OVERRIDE; 298 virtual void ShowContextMenuAtPoint(const gfx::Point& point) OVERRIDE; 299 300 // Queues a synthetic gesture for testing purposes. Invokes the on_complete 301 // callback when the gesture is finished running. 302 void QueueSyntheticGesture( 303 scoped_ptr<SyntheticGesture> synthetic_gesture, 304 const base::Callback<void(SyntheticGesture::Result)>& on_complete); 305 306 void CancelUpdateTextDirection(); 307 308 // Called when a mouse click/gesture tap activates the renderer. 309 virtual void OnPointerEventActivate(); 310 311 // Notifies the renderer whether or not the input method attached to this 312 // process is activated. 313 // When the input method is activated, a renderer process sends IPC messages 314 // to notify the status of its composition node. (This message is mainly used 315 // for notifying the position of the input cursor so that the browser can 316 // display input method windows under the cursor.) 317 void SetInputMethodActive(bool activate); 318 319 // Notifies the renderer changes of IME candidate window state. 320 void CandidateWindowShown(); 321 void CandidateWindowUpdated(); 322 void CandidateWindowHidden(); 323 324 // Update the composition node of the renderer (or WebKit). 325 // WebKit has a special node (a composition node) for input method to change 326 // its text without affecting any other DOM nodes. When the input method 327 // (attached to the browser) updates its text, the browser sends IPC messages 328 // to update the composition node of the renderer. 329 // (Read the comments of each function for its detail.) 330 331 // Sets the text of the composition node. 332 // This function can also update the cursor position and mark the specified 333 // range in the composition node. 334 // A browser should call this function: 335 // * when it receives a WM_IME_COMPOSITION message with a GCS_COMPSTR flag 336 // (on Windows); 337 // * when it receives a "preedit_changed" signal of GtkIMContext (on Linux); 338 // * when markedText of NSTextInput is called (on Mac). 339 void ImeSetComposition( 340 const base::string16& text, 341 const std::vector<blink::WebCompositionUnderline>& underlines, 342 int selection_start, 343 int selection_end); 344 345 // Finishes an ongoing composition with the specified text. 346 // A browser should call this function: 347 // * when it receives a WM_IME_COMPOSITION message with a GCS_RESULTSTR flag 348 // (on Windows); 349 // * when it receives a "commit" signal of GtkIMContext (on Linux); 350 // * when insertText of NSTextInput is called (on Mac). 351 void ImeConfirmComposition(const base::string16& text, 352 const gfx::Range& replacement_range, 353 bool keep_selection); 354 355 // Cancels an ongoing composition. 356 void ImeCancelComposition(); 357 358 // This is for derived classes to give us access to the resizer rect. 359 // And to also expose it to the RenderWidgetHostView. 360 virtual gfx::Rect GetRootWindowResizerRect() const; 361 ignore_input_events()362 bool ignore_input_events() const { 363 return ignore_input_events_; 364 } 365 input_method_active()366 bool input_method_active() const { 367 return input_method_active_; 368 } 369 370 // Whether forwarded WebInputEvents should be ignored. True if either 371 // |ignore_input_events_| or |process_->IgnoreInputEvents()| is true. 372 bool IgnoreInputEvents() const; 373 374 // Event queries delegated to the |input_router_|. 375 bool ShouldForwardTouchEvent() const; 376 has_touch_handler()377 bool has_touch_handler() const { return has_touch_handler_; } 378 379 // Notification that the user has made some kind of input that could 380 // perform an action. See OnUserGesture for more details. 381 void StartUserGesture(); 382 383 // Set the RenderView background transparency. 384 void SetBackgroundOpaque(bool opaque); 385 386 // Notifies the renderer that the next key event is bound to one or more 387 // pre-defined edit commands 388 void SetEditCommandsForNextKeyEvent( 389 const std::vector<EditCommand>& commands); 390 391 // Gets the accessibility mode. accessibility_mode()392 AccessibilityMode accessibility_mode() const { 393 return accessibility_mode_; 394 } 395 396 // Adds the given accessibility mode to the current accessibility mode bitmap. 397 void AddAccessibilityMode(AccessibilityMode mode); 398 399 // Removes the given accessibility mode from the current accessibility mode 400 // bitmap, managing the bits that are shared with other modes such that a 401 // bit will only be turned off when all modes that depend on it have been 402 // removed. 403 void RemoveAccessibilityMode(AccessibilityMode mode); 404 405 // Resets the accessibility mode to the default setting in 406 // BrowserStateAccessibilityImpl. 407 void ResetAccessibilityMode(); 408 409 #if defined(OS_WIN) 410 void SetParentNativeViewAccessible( 411 gfx::NativeViewAccessible accessible_parent); 412 gfx::NativeViewAccessible GetParentNativeViewAccessible() const; 413 #endif 414 415 // Executes the edit command on the RenderView. 416 void ExecuteEditCommand(const std::string& command, 417 const std::string& value); 418 419 // Tells the renderer to scroll the currently focused node into rect only if 420 // the currently focused node is a Text node (textfield, text area or content 421 // editable divs). 422 void ScrollFocusedEditableNodeIntoRect(const gfx::Rect& rect); 423 424 // Requests the renderer to move the caret selection towards the point. 425 void MoveCaret(const gfx::Point& point); 426 427 // Called when the reponse to a pending mouse lock request has arrived. 428 // Returns true if |allowed| is true and the mouse has been successfully 429 // locked. 430 bool GotResponseToLockMouseRequest(bool allowed); 431 432 // Tells the RenderWidget about the latest vsync parameters. 433 // Note: Make sure the timebase was obtained using 434 // base::TimeTicks::HighResNow. Using the non-high res timer will result in 435 // incorrect synchronization across processes. 436 virtual void UpdateVSyncParameters(base::TimeTicks timebase, 437 base::TimeDelta interval); 438 439 // Called by the view in response to AcceleratedSurfaceBuffersSwapped or 440 // AcceleratedSurfacePostSubBuffer. 441 static void AcknowledgeBufferPresent( 442 int32 route_id, 443 int gpu_host_id, 444 const AcceleratedSurfaceMsg_BufferPresented_Params& params); 445 446 // Called by the view in response to OnSwapCompositorFrame. 447 static void SendSwapCompositorFrameAck( 448 int32 route_id, 449 uint32 output_surface_id, 450 int renderer_host_id, 451 const cc::CompositorFrameAck& ack); 452 453 // Called by the view to return resources to the compositor. 454 static void SendReclaimCompositorResources(int32 route_id, 455 uint32 output_surface_id, 456 int renderer_host_id, 457 const cc::CompositorFrameAck& ack); 458 set_allow_privileged_mouse_lock(bool allow)459 void set_allow_privileged_mouse_lock(bool allow) { 460 allow_privileged_mouse_lock_ = allow; 461 } 462 463 // Resets state variables related to tracking pending size and painting. 464 // 465 // We need to reset these flags when we want to repaint the contents of 466 // browser plugin in this RWH. Resetting these flags will ensure we ignore 467 // any previous pending acks that are not relevant upon repaint. 468 void ResetSizeAndRepaintPendingFlags(); 469 470 void DetachDelegate(); 471 472 // Update the renderer's cache of the screen rect of the view and window. 473 void SendScreenRects(); 474 475 // Suppreses future char events until a keydown. See 476 // suppress_next_char_events_. 477 void SuppressNextCharEvents(); 478 479 // Called by RenderWidgetHostView in response to OnSetNeedsFlushInput. 480 void FlushInput(); 481 482 // InputRouterClient 483 virtual void SetNeedsFlush() OVERRIDE; 484 485 // Indicates whether the renderer drives the RenderWidgetHosts's size or the 486 // other way around. should_auto_resize()487 bool should_auto_resize() { return should_auto_resize_; } 488 489 void ComputeTouchLatency(const ui::LatencyInfo& latency_info); 490 void FrameSwapped(const ui::LatencyInfo& latency_info); 491 void DidReceiveRendererFrame(); 492 493 // Returns the ID that uniquely describes this component to the latency 494 // subsystem. 495 int64 GetLatencyComponentId(); 496 497 static void CompositorFrameDrawn( 498 const std::vector<ui::LatencyInfo>& latency_info); 499 500 // Don't check whether we expected a resize ack during layout tests. 501 static void DisableResizeAckCheckForTesting(); 502 503 void WindowSnapshotAsyncCallback( 504 int routing_id, 505 int snapshot_id, 506 gfx::Size snapshot_size, 507 scoped_refptr<base::RefCountedBytes> png_data); 508 509 // LatencyComponents generated in the renderer must have component IDs 510 // provided to them by the browser process. This function adds the correct 511 // component ID where necessary. 512 void AddLatencyInfoComponentIds(ui::LatencyInfo* latency_info); 513 input_router()514 InputRouter* input_router() { return input_router_.get(); } 515 516 protected: 517 virtual RenderWidgetHostImpl* AsRenderWidgetHostImpl() OVERRIDE; 518 519 // Create a LatencyInfo struct with INPUT_EVENT_LATENCY_RWH_COMPONENT 520 // component if it is not already in |original|. And if |original| is 521 // not NULL, it is also merged into the resulting LatencyInfo. 522 ui::LatencyInfo CreateRWHLatencyInfoIfNotExist( 523 const ui::LatencyInfo* original, blink::WebInputEvent::Type type); 524 525 // Called when we receive a notification indicating that the renderer 526 // process has gone. This will reset our state so that our state will be 527 // consistent if a new renderer is created. 528 void RendererExited(base::TerminationStatus status, int exit_code); 529 530 // Retrieves an id the renderer can use to refer to its view. 531 // This is used for various IPC messages, including plugins. 532 gfx::NativeViewId GetNativeViewId() const; 533 534 // Retrieves an id for the surface that the renderer can draw to 535 // when accelerated compositing is enabled. 536 gfx::GLSurfaceHandle GetCompositingSurface(); 537 538 // --------------------------------------------------------------------------- 539 // The following methods are overridden by RenderViewHost to send upwards to 540 // its delegate. 541 542 // Called when a mousewheel event was not processed by the renderer. UnhandledWheelEvent(const blink::WebMouseWheelEvent & event)543 virtual void UnhandledWheelEvent(const blink::WebMouseWheelEvent& event) {} 544 545 // Notification that the user has made some kind of input that could 546 // perform an action. The gestures that count are 1) any mouse down 547 // event and 2) enter or space key presses. OnUserGesture()548 virtual void OnUserGesture() {} 549 550 // Callbacks for notification when the renderer becomes unresponsive to user 551 // input events, and subsequently responsive again. NotifyRendererUnresponsive()552 virtual void NotifyRendererUnresponsive() {} NotifyRendererResponsive()553 virtual void NotifyRendererResponsive() {} 554 555 // Called when auto-resize resulted in the renderer size changing. OnRenderAutoResized(const gfx::Size & new_size)556 virtual void OnRenderAutoResized(const gfx::Size& new_size) {} 557 558 // --------------------------------------------------------------------------- 559 560 // RenderViewHost overrides this method to impose further restrictions on when 561 // to allow mouse lock. 562 // Once the request is approved or rejected, GotResponseToLockMouseRequest() 563 // will be called. 564 virtual void RequestToLockMouse(bool user_gesture, 565 bool last_unlocked_by_target); 566 567 void RejectMouseLockOrUnlockIfNecessary(); 568 bool IsMouseLocked() const; 569 570 // RenderViewHost overrides this method to report when in fullscreen mode. 571 virtual bool IsFullscreen() const; 572 573 // Indicates if the render widget host should track the render widget's size 574 // as opposed to visa versa. 575 void SetShouldAutoResize(bool enable); 576 577 // Expose increment/decrement of the in-flight event count, so 578 // RenderViewHostImpl can account for in-flight beforeunload/unload events. increment_in_flight_event_count()579 int increment_in_flight_event_count() { return ++in_flight_event_count_; } decrement_in_flight_event_count()580 int decrement_in_flight_event_count() { return --in_flight_event_count_; } 581 582 // The View associated with the RenderViewHost. The lifetime of this object 583 // is associated with the lifetime of the Render process. If the Renderer 584 // crashes, its View is destroyed and this pointer becomes NULL, even though 585 // render_view_host_ lives on to load another URL (creating a new View while 586 // doing so). 587 RenderWidgetHostViewBase* view_; 588 589 // true if a renderer has once been valid. We use this flag to display a sad 590 // tab only when we lose our renderer and not if a paint occurs during 591 // initialization. 592 bool renderer_initialized_; 593 594 // This value indicates how long to wait before we consider a renderer hung. 595 int hung_renderer_delay_ms_; 596 597 private: 598 friend class MockRenderWidgetHost; 599 600 // Tell this object to destroy itself. 601 void Destroy(); 602 603 // Called by |hang_timeout_monitor_| on delayed response from the renderer. 604 void RendererIsUnresponsive(); 605 606 // Called if we know the renderer is responsive. When we currently think the 607 // renderer is unresponsive, this will clear that state and call 608 // NotifyRendererResponsive. 609 void RendererIsResponsive(); 610 611 // IPC message handlers 612 void OnRenderViewReady(); 613 void OnRenderProcessGone(int status, int error_code); 614 void OnClose(); 615 void OnUpdateScreenRectsAck(); 616 void OnRequestMove(const gfx::Rect& pos); 617 void OnSetTooltipText(const base::string16& tooltip_text, 618 blink::WebTextDirection text_direction_hint); 619 #if defined(OS_MACOSX) 620 void OnCompositorSurfaceBuffersSwapped( 621 const ViewHostMsg_CompositorSurfaceBuffersSwapped_Params& params); 622 #endif 623 bool OnSwapCompositorFrame(const IPC::Message& message); 624 void OnFlingingStopped(); 625 void OnUpdateRect(const ViewHostMsg_UpdateRect_Params& params); 626 void OnQueueSyntheticGesture(const SyntheticGesturePacket& gesture_packet); 627 virtual void OnFocus(); 628 virtual void OnBlur(); 629 void OnSetCursor(const WebCursor& cursor); 630 void OnSetTouchEventEmulationEnabled(bool enabled, bool allow_pinch); 631 void OnTextInputStateChanged( 632 const ViewHostMsg_TextInputState_Params& params); 633 634 #if defined(OS_MACOSX) || defined(USE_AURA) 635 void OnImeCompositionRangeChanged( 636 const gfx::Range& range, 637 const std::vector<gfx::Rect>& character_bounds); 638 #endif 639 void OnImeCancelComposition(); 640 void OnLockMouse(bool user_gesture, 641 bool last_unlocked_by_target, 642 bool privileged); 643 void OnUnlockMouse(); 644 void OnShowDisambiguationPopup(const gfx::Rect& rect, 645 const gfx::Size& size, 646 const cc::SharedBitmapId& id); 647 #if defined(OS_WIN) 648 void OnWindowlessPluginDummyWindowCreated( 649 gfx::NativeViewId dummy_activation_window); 650 void OnWindowlessPluginDummyWindowDestroyed( 651 gfx::NativeViewId dummy_activation_window); 652 #endif 653 void OnSelectionChanged(const base::string16& text, 654 size_t offset, 655 const gfx::Range& range); 656 void OnSelectionBoundsChanged( 657 const ViewHostMsg_SelectionBounds_Params& params); 658 void OnSnapshot(bool success, const SkBitmap& bitmap); 659 660 // Called (either immediately or asynchronously) after we're done with our 661 // BackingStore and can send an ACK to the renderer so it can paint onto it 662 // again. 663 void DidUpdateBackingStore(const ViewHostMsg_UpdateRect_Params& params, 664 const base::TimeTicks& paint_start); 665 666 // Give key press listeners a chance to handle this key press. This allow 667 // widgets that don't have focus to still handle key presses. 668 bool KeyPressListenersHandleEvent(const NativeWebKeyboardEvent& event); 669 670 // InputRouterClient 671 virtual InputEventAckState FilterInputEvent( 672 const blink::WebInputEvent& event, 673 const ui::LatencyInfo& latency_info) OVERRIDE; 674 virtual void IncrementInFlightEventCount() OVERRIDE; 675 virtual void DecrementInFlightEventCount() OVERRIDE; 676 virtual void OnHasTouchEventHandlers(bool has_handlers) OVERRIDE; 677 virtual void DidFlush() OVERRIDE; 678 virtual void DidOverscroll(const DidOverscrollParams& params) OVERRIDE; 679 680 // InputAckHandler 681 virtual void OnKeyboardEventAck(const NativeWebKeyboardEvent& event, 682 InputEventAckState ack_result) OVERRIDE; 683 virtual void OnWheelEventAck(const MouseWheelEventWithLatencyInfo& event, 684 InputEventAckState ack_result) OVERRIDE; 685 virtual void OnTouchEventAck(const TouchEventWithLatencyInfo& event, 686 InputEventAckState ack_result) OVERRIDE; 687 virtual void OnGestureEventAck(const GestureEventWithLatencyInfo& event, 688 InputEventAckState ack_result) OVERRIDE; 689 virtual void OnUnexpectedEventAck(UnexpectedEventAckType type) OVERRIDE; 690 691 void OnSyntheticGestureCompleted(SyntheticGesture::Result result); 692 693 // Called when there is a new auto resize (using a post to avoid a stack 694 // which may get in recursive loops). 695 void DelayedAutoResized(); 696 697 void WindowSnapshotReachedScreen(int snapshot_id); 698 699 // Send a message to the renderer process to change the accessibility mode. 700 void SetAccessibilityMode(AccessibilityMode AccessibilityMode); 701 702 // Our delegate, which wants to know mainly about keyboard events. 703 // It will remain non-NULL until DetachDelegate() is called. 704 RenderWidgetHostDelegate* delegate_; 705 706 // Created during construction but initialized during Init*(). Therefore, it 707 // is guaranteed never to be NULL, but its channel may be NULL if the 708 // renderer crashed, so you must always check that. 709 RenderProcessHost* process_; 710 711 // The ID of the corresponding object in the Renderer Instance. 712 int routing_id_; 713 714 // The ID of the surface corresponding to this render widget. 715 int surface_id_; 716 717 // Indicates whether a page is loading or not. 718 bool is_loading_; 719 720 // Indicates whether a page is hidden or not. It has to stay in sync with the 721 // most recent call to process_->WidgetRestored() / WidgetHidden(). 722 bool is_hidden_; 723 724 // Indicates whether a page is fullscreen or not. 725 bool is_fullscreen_; 726 727 // Set if we are waiting for a repaint ack for the view. 728 bool repaint_ack_pending_; 729 730 // True when waiting for RESIZE_ACK. 731 bool resize_ack_pending_; 732 733 // Cached copy of the screen info so that it doesn't need to be updated every 734 // time the window is resized. 735 scoped_ptr<blink::WebScreenInfo> screen_info_; 736 737 // Set if screen_info_ may have changed and should be recomputed and force a 738 // resize message. 739 bool screen_info_out_of_date_; 740 741 // The current size of the RenderWidget. 742 gfx::Size current_size_; 743 744 // The size of the view's backing surface in non-DPI-adjusted pixels. 745 gfx::Size physical_backing_size_; 746 747 // The height of the physical backing surface that is overdrawn opaquely in 748 // the browser, for example by an on-screen-keyboard (in DPI-adjusted pixels). 749 float overdraw_bottom_height_; 750 751 // The size of the visible viewport, which may be smaller than the view if the 752 // view is partially occluded (e.g. by a virtual keyboard). The size is in 753 // DPI-adjusted pixels. 754 gfx::Size visible_viewport_size_; 755 756 // The size we last sent as requested size to the renderer. |current_size_| 757 // is only updated once the resize message has been ack'd. This on the other 758 // hand is updated when the resize message is sent. This is very similar to 759 // |resize_ack_pending_|, but the latter is not set if the new size has width 760 // or height zero, which is why we need this too. 761 gfx::Size last_requested_size_; 762 763 // The next auto resize to send. 764 gfx::Size new_auto_size_; 765 766 // True if the render widget host should track the render widget's size as 767 // opposed to visa versa. 768 bool should_auto_resize_; 769 770 bool waiting_for_screen_rects_ack_; 771 gfx::Rect last_view_screen_rect_; 772 gfx::Rect last_window_screen_rect_; 773 774 AccessibilityMode accessibility_mode_; 775 776 // Keyboard event listeners. 777 std::vector<KeyPressEventCallback> key_press_event_callbacks_; 778 779 // Mouse event callbacks. 780 std::vector<MouseEventCallback> mouse_event_callbacks_; 781 782 // If true, then we should repaint when restoring even if we have a 783 // backingstore. This flag is set to true if we receive a paint message 784 // while is_hidden_ to true. Even though we tell the render widget to hide 785 // itself, a paint message could already be in flight at that point. 786 bool needs_repainting_on_restore_; 787 788 // This is true if the renderer is currently unresponsive. 789 bool is_unresponsive_; 790 791 // The following value indicates a time in the future when we would consider 792 // the renderer hung if it does not generate an appropriate response message. 793 base::Time time_when_considered_hung_; 794 795 // This value denotes the number of input events yet to be acknowledged 796 // by the renderer. 797 int in_flight_event_count_; 798 799 // This timer runs to check if time_when_considered_hung_ has past. 800 base::OneShotTimer<RenderWidgetHostImpl> hung_renderer_timer_; 801 802 // Flag to detect recursive calls to GetBackingStore(). 803 bool in_get_backing_store_; 804 805 // Used for UMA histogram logging to measure the time for a repaint view 806 // operation to finish. 807 base::TimeTicks repaint_start_time_; 808 809 // Set to true if we shouldn't send input events from the render widget. 810 bool ignore_input_events_; 811 812 // Indicates whether IME is active. 813 bool input_method_active_; 814 815 // Set when we update the text direction of the selected input element. 816 bool text_direction_updated_; 817 blink::WebTextDirection text_direction_; 818 819 // Set when we cancel updating the text direction. 820 // This flag also ignores succeeding update requests until we call 821 // NotifyTextDirection(). 822 bool text_direction_canceled_; 823 824 // Indicates if the next sequence of Char events should be suppressed or not. 825 // System may translate a RawKeyDown event into zero or more Char events, 826 // usually we send them to the renderer directly in sequence. However, If a 827 // RawKeyDown event was not handled by the renderer but was handled by 828 // our UnhandledKeyboardEvent() method, e.g. as an accelerator key, then we 829 // shall not send the following sequence of Char events, which was generated 830 // by this RawKeyDown event, to the renderer. Otherwise the renderer may 831 // handle the Char events and cause unexpected behavior. 832 // For example, pressing alt-2 may let the browser switch to the second tab, 833 // but the Char event generated by alt-2 may also activate a HTML element 834 // if its accesskey happens to be "2", then the user may get confused when 835 // switching back to the original tab, because the content may already be 836 // changed. 837 bool suppress_next_char_events_; 838 839 // The last scroll offset of the render widget. 840 gfx::Vector2d last_scroll_offset_; 841 842 bool pending_mouse_lock_request_; 843 bool allow_privileged_mouse_lock_; 844 845 // Keeps track of whether the webpage has any touch event handler. If it does, 846 // then touch events are sent to the renderer. Otherwise, the touch events are 847 // not sent to the renderer. 848 bool has_touch_handler_; 849 850 base::WeakPtrFactory<RenderWidgetHostImpl> weak_factory_; 851 852 scoped_ptr<SyntheticGestureController> synthetic_gesture_controller_; 853 854 scoped_ptr<TouchEmulator> touch_emulator_; 855 856 // Receives and handles all input events. 857 scoped_ptr<InputRouter> input_router_; 858 859 scoped_ptr<TimeoutMonitor> hang_monitor_timeout_; 860 861 #if defined(OS_WIN) 862 std::list<HWND> dummy_windows_for_activation_; 863 #endif 864 865 int64 last_input_number_; 866 867 DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostImpl); 868 }; 869 870 } // namespace content 871 872 #endif // CONTENT_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_IMPL_H_ 873