1// Copyright (c) 2011 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 CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_WIN_H_
6#define CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_WIN_H_
7#pragma once
8
9#include <atlbase.h>
10#include <atlapp.h>
11#include <atlcrack.h>
12#include <atlmisc.h>
13
14#include <vector>
15
16#include "base/memory/scoped_ptr.h"
17#include "base/memory/scoped_vector.h"
18#include "base/task.h"
19#include "base/win/scoped_comptr.h"
20#include "chrome/browser/accessibility/browser_accessibility_manager.h"
21#include "content/browser/renderer_host/render_widget_host_view.h"
22#include "content/common/notification_observer.h"
23#include "content/common/notification_registrar.h"
24#include "ui/base/win/ime_input.h"
25#include "ui/gfx/native_widget_types.h"
26#include "webkit/glue/webcursor.h"
27
28class BackingStore;
29class RenderWidgetHost;
30
31namespace gfx {
32class Size;
33class Rect;
34}
35
36namespace IPC {
37class Message;
38}
39
40namespace ui {
41class ViewProp;
42}
43
44typedef CWinTraits<WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 0>
45    RenderWidgetHostHWNDTraits;
46
47extern const wchar_t kRenderWidgetHostHWNDClass[];
48
49///////////////////////////////////////////////////////////////////////////////
50// RenderWidgetHostViewWin
51//
52//  An object representing the "View" of a rendered web page. This object is
53//  responsible for displaying the content of the web page, receiving windows
54//  messages, and containing plugins HWNDs. It is the implementation of the
55//  RenderWidgetHostView that the cross-platform RenderWidgetHost object uses
56//  to display the data.
57//
58//  Comment excerpted from render_widget_host.h:
59//
60//    "The lifetime of the RenderWidgetHostHWND is tied to the render process.
61//     If the render process dies, the RenderWidgetHostHWND goes away and all
62//     references to it must become NULL."
63//
64class RenderWidgetHostViewWin
65    : public CWindowImpl<RenderWidgetHostViewWin,
66                         CWindow,
67                         RenderWidgetHostHWNDTraits>,
68      public RenderWidgetHostView,
69      public NotificationObserver,
70      public BrowserAccessibilityDelegate {
71 public:
72  // The view will associate itself with the given widget.
73  explicit RenderWidgetHostViewWin(RenderWidgetHost* widget);
74  virtual ~RenderWidgetHostViewWin();
75
76  void CreateWnd(HWND parent);
77
78  DECLARE_WND_CLASS_EX(kRenderWidgetHostHWNDClass, CS_DBLCLKS, 0);
79
80  BEGIN_MSG_MAP(RenderWidgetHostHWND)
81    MSG_WM_CREATE(OnCreate)
82    MSG_WM_ACTIVATE(OnActivate)
83    MSG_WM_DESTROY(OnDestroy)
84    MSG_WM_PAINT(OnPaint)
85    MSG_WM_NCPAINT(OnNCPaint)
86    MSG_WM_ERASEBKGND(OnEraseBkgnd)
87    MSG_WM_SETCURSOR(OnSetCursor)
88    MSG_WM_SETFOCUS(OnSetFocus)
89    MSG_WM_KILLFOCUS(OnKillFocus)
90    MSG_WM_CAPTURECHANGED(OnCaptureChanged)
91    MSG_WM_CANCELMODE(OnCancelMode)
92    MSG_WM_INPUTLANGCHANGE(OnInputLangChange)
93    MSG_WM_THEMECHANGED(OnThemeChanged)
94    MSG_WM_NOTIFY(OnNotify)
95    MESSAGE_HANDLER(WM_IME_SETCONTEXT, OnImeSetContext)
96    MESSAGE_HANDLER(WM_IME_STARTCOMPOSITION, OnImeStartComposition)
97    MESSAGE_HANDLER(WM_IME_COMPOSITION, OnImeComposition)
98    MESSAGE_HANDLER(WM_IME_ENDCOMPOSITION, OnImeEndComposition)
99    MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseEvent)
100    MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseEvent)
101    MESSAGE_HANDLER(WM_LBUTTONDOWN, OnMouseEvent)
102    MESSAGE_HANDLER(WM_MBUTTONDOWN, OnMouseEvent)
103    MESSAGE_HANDLER(WM_RBUTTONDOWN, OnMouseEvent)
104    MESSAGE_HANDLER(WM_LBUTTONUP, OnMouseEvent)
105    MESSAGE_HANDLER(WM_MBUTTONUP, OnMouseEvent)
106    MESSAGE_HANDLER(WM_RBUTTONUP, OnMouseEvent)
107    MESSAGE_HANDLER(WM_LBUTTONDBLCLK, OnMouseEvent)
108    MESSAGE_HANDLER(WM_MBUTTONDBLCLK, OnMouseEvent)
109    MESSAGE_HANDLER(WM_RBUTTONDBLCLK, OnMouseEvent)
110    MESSAGE_HANDLER(WM_SYSKEYDOWN, OnKeyEvent)
111    MESSAGE_HANDLER(WM_SYSKEYUP, OnKeyEvent)
112    MESSAGE_HANDLER(WM_KEYDOWN, OnKeyEvent)
113    MESSAGE_HANDLER(WM_KEYUP, OnKeyEvent)
114    MESSAGE_HANDLER(WM_MOUSEWHEEL, OnWheelEvent)
115    MESSAGE_HANDLER(WM_MOUSEHWHEEL, OnWheelEvent)
116    MESSAGE_HANDLER(WM_HSCROLL, OnWheelEvent)
117    MESSAGE_HANDLER(WM_VSCROLL, OnWheelEvent)
118    MESSAGE_HANDLER(WM_CHAR, OnKeyEvent)
119    MESSAGE_HANDLER(WM_SYSCHAR, OnKeyEvent)
120    MESSAGE_HANDLER(WM_IME_CHAR, OnKeyEvent)
121    MESSAGE_HANDLER(WM_MOUSEACTIVATE, OnMouseActivate)
122    MESSAGE_HANDLER(WM_GETOBJECT, OnGetObject)
123    MESSAGE_HANDLER(WM_PARENTNOTIFY, OnParentNotify)
124  END_MSG_MAP()
125
126  // Implementation of RenderWidgetHostView:
127  virtual void InitAsPopup(RenderWidgetHostView* parent_host_view,
128                           const gfx::Rect& pos);
129  virtual void InitAsFullscreen();
130  virtual RenderWidgetHost* GetRenderWidgetHost() const;
131  virtual void DidBecomeSelected();
132  virtual void WasHidden();
133  virtual void SetSize(const gfx::Size& size);
134  virtual void SetBounds(const gfx::Rect& rect);
135  virtual gfx::NativeView GetNativeView();
136  virtual void MovePluginWindows(
137      const std::vector<webkit::npapi::WebPluginGeometry>& moves);
138  virtual void Focus();
139  virtual void Blur();
140  virtual bool HasFocus();
141  virtual void Show();
142  virtual void Hide();
143  virtual bool IsShowing();
144  virtual gfx::Rect GetViewBounds() const;
145  virtual void UpdateCursor(const WebCursor& cursor);
146  virtual void SetIsLoading(bool is_loading);
147  virtual void ImeUpdateTextInputState(WebKit::WebTextInputType type,
148                                       const gfx::Rect& caret_rect);
149  virtual void ImeCancelComposition();
150  virtual void DidUpdateBackingStore(
151      const gfx::Rect& scroll_rect, int scroll_dx, int scroll_dy,
152      const std::vector<gfx::Rect>& copy_rects);
153  virtual void RenderViewGone(base::TerminationStatus status,
154                              int error_code);
155  virtual void WillWmDestroy();  // called by TabContents before DestroyWindow
156  virtual void WillDestroyRenderWidget(RenderWidgetHost* rwh);
157  virtual void Destroy();
158  virtual void SetTooltipText(const std::wstring& tooltip_text);
159  virtual BackingStore* AllocBackingStore(const gfx::Size& size);
160  virtual void SetBackground(const SkBitmap& background);
161  virtual bool ContainsNativeView(gfx::NativeView native_view) const;
162  virtual void SetVisuallyDeemphasized(const SkColor* color, bool animate);
163
164  virtual gfx::PluginWindowHandle GetCompositingSurface();
165  virtual void ShowCompositorHostWindow(bool show);
166
167  virtual void OnAccessibilityNotifications(
168      const std::vector<ViewHostMsg_AccessibilityNotification_Params>& params);
169
170  // Implementation of NotificationObserver:
171  virtual void Observe(NotificationType type,
172                       const NotificationSource& source,
173                       const NotificationDetails& details);
174
175  // Implementation of BrowserAccessibilityDelegate:
176  virtual void SetAccessibilityFocus(int acc_obj_id);
177  virtual void AccessibilityDoDefaultAction(int acc_obj_id);
178
179 protected:
180  // Windows Message Handlers
181  LRESULT OnCreate(CREATESTRUCT* create_struct);
182  void OnActivate(UINT, BOOL, HWND);
183  void OnDestroy();
184  void OnPaint(HDC unused_dc);
185  void OnNCPaint(HRGN update_region);
186  LRESULT OnEraseBkgnd(HDC dc);
187  LRESULT OnSetCursor(HWND window, UINT hittest_code, UINT mouse_message_id);
188  void OnSetFocus(HWND window);
189  void OnKillFocus(HWND window);
190  void OnCaptureChanged(HWND window);
191  void OnCancelMode();
192  void OnInputLangChange(DWORD character_set, HKL input_language_id);
193  void OnThemeChanged();
194  LRESULT OnNotify(int w_param, NMHDR* header);
195  LRESULT OnImeSetContext(
196      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
197  LRESULT OnImeStartComposition(
198      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
199  LRESULT OnImeComposition(
200      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
201  LRESULT OnImeEndComposition(
202      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
203  LRESULT OnMouseEvent(
204      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
205  LRESULT OnKeyEvent(
206      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
207  LRESULT OnWheelEvent(
208      UINT message, WPARAM wparam, LPARAM lparam, BOOL& handled);
209  LRESULT OnMouseActivate(UINT message,
210                          WPARAM wparam,
211                          LPARAM lparam,
212                          BOOL& handled);
213  // Handle MSAA requests for accessibility information.
214  LRESULT OnGetObject(UINT message, WPARAM wparam, LPARAM lparam,
215                      BOOL& handled);
216  // Handle vertical scrolling
217  LRESULT OnVScroll(int code, short position, HWND scrollbar_control);
218  // Handle horizontal scrolling
219  LRESULT OnHScroll(int code, short position, HWND scrollbar_control);
220
221  LRESULT OnParentNotify(UINT message, WPARAM wparam, LPARAM lparam,
222                         BOOL& handled);
223
224  void OnFinalMessage(HWND window);
225
226 private:
227  // Updates the display cursor to the current cursor if the cursor is over this
228  // render view.
229  void UpdateCursorIfOverSelf();
230
231  // Tells Windows that we want to hear about mouse exit messages.
232  void TrackMouseLeave(bool start_tracking);
233
234  // Sends a message to the RenderView in the renderer process.
235  bool Send(IPC::Message* message);
236
237  // Set the tooltip region to the size of the window, creating the tooltip
238  // hwnd if it has not been created yet.
239  void EnsureTooltip();
240
241  // Tooltips become invalid when the root ancestor changes. When the View
242  // becomes hidden, this method is called to reset the tooltip.
243  void ResetTooltip();
244
245  // Sends the specified mouse event to the renderer.
246  void ForwardMouseEventToRenderer(UINT message, WPARAM wparam, LPARAM lparam);
247
248  // Synthesize mouse wheel event.
249  LRESULT SynthesizeMouseWheel(bool is_vertical, int scroll_code,
250                               short scroll_position);
251
252  // Shuts down the render_widget_host_.  This is a separate function so we can
253  // invoke it from the message loop.
254  void ShutdownHost();
255
256  // Redraws the window synchronously, and any child windows (i.e. plugins)
257  // asynchronously.
258  void Redraw();
259
260  // Draw our background over the given HDC in the given |rect|. The background
261  // will be tiled such that it lines up with existing tiles starting from the
262  // origin of |dc|.
263  void DrawBackground(const RECT& rect, CPaintDC* dc);
264
265  // Create an intermediate window between the given HWND and its parent.
266  HWND ReparentWindow(HWND window);
267
268  // Clean up the compositor window, if needed.
269  void CleanupCompositorWindow();
270
271  // Whether the window should be activated.
272  bool IsActivatable() const;
273
274  // The associated Model.
275  RenderWidgetHost* render_widget_host_;
276
277  // When we are doing accelerated compositing
278  HWND compositor_host_window_;
279
280  // true if the compositor host window must be hidden after the
281  // software renderered view is updated.
282  bool hide_compositor_window_at_next_paint_;
283
284  // The cursor for the page. This is passed up from the renderer.
285  WebCursor current_cursor_;
286
287  // Indicates if the page is loading.
288  bool is_loading_;
289
290  // true if we are currently tracking WM_MOUSEEXIT messages.
291  bool track_mouse_leave_;
292
293  // Wrapper class for IME input.
294  // (See "ui/base/win/ime_input.h" for its details.)
295  ui::ImeInput ime_input_;
296
297  // Represents whether or not this browser process is receiving status
298  // messages about the focused edit control from a renderer process.
299  bool ime_notification_;
300
301  // true if Enter was hit when render widget host was in focus.
302  bool capture_enter_key_;
303
304  // true if the View is not visible.
305  bool is_hidden_;
306
307  // True if we're in the midst of a paint operation and should respond to
308  // DidPaintRect() notifications by merely invalidating.  See comments on
309  // render_widget_host_view.h:DidPaintRect().
310  bool about_to_validate_and_paint_;
311
312  // true if the View should be closed when its HWND is deactivated (used to
313  // support SELECT popups which are closed when they are deactivated).
314  bool close_on_deactivate_;
315
316  // Whether Destroy() has been called.  Used to detect a crasher
317  // (http://crbug.com/24248) where render_view_host_ has been deleted when
318  // OnFinalMessage is called.
319  bool being_destroyed_;
320
321  // Tooltips
322  // The text to be shown in the tooltip, supplied by the renderer.
323  std::wstring tooltip_text_;
324  // The tooltip control hwnd
325  HWND tooltip_hwnd_;
326  // Whether or not a tooltip is currently visible. We use this to track
327  // whether or not we want to force-close the tooltip when we receive mouse
328  // move notifications from the renderer. See comment in OnMsgSetTooltipText.
329  bool tooltip_showing_;
330
331  // Factory used to safely scope delayed calls to ShutdownHost().
332  ScopedRunnableMethodFactory<RenderWidgetHostViewWin> shutdown_factory_;
333
334  // Our parent HWND.  We keep a reference to it as we SetParent(NULL) when
335  // hidden to prevent getting messages (Paint, Resize...), and we reattach
336  // when shown again.
337  HWND parent_hwnd_;
338
339  // Instance of accessibility information for the root of the MSAA
340  // tree representation of the WebKit render tree.
341  scoped_ptr<BrowserAccessibilityManager> browser_accessibility_manager_;
342
343  // The time at which this view started displaying white pixels as a result of
344  // not having anything to paint (empty backing store from renderer). This
345  // value returns true for is_null() if we are not recording whiteout times.
346  base::TimeTicks whiteout_start_time_;
347
348  // The time it took after this view was selected for it to be fully painted.
349  base::TimeTicks tab_switch_paint_time_;
350
351  // A color we use to shade the entire render view. If 100% transparent, we do
352  // not shade the render view.
353  SkColor overlay_color_;
354
355  // Registrar so we can listen to RENDERER_PROCESS_TERMINATED events.
356  NotificationRegistrar registrar_;
357
358  // Stores the current text input type received by ImeUpdateTextInputState()
359  // method.
360  WebKit::WebTextInputType text_input_type_;
361
362  ScopedVector<ui::ViewProp> props_;
363
364  scoped_ptr<ui::ViewProp> accessibility_prop_;
365
366  DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewWin);
367};
368
369#endif  // CHROME_BROWSER_RENDERER_HOST_RENDER_WIDGET_HOST_VIEW_WIN_H_
370