• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #ifndef CEF_TESTS_CEFCLIENT_BROWSER_OSR_WINDOW_WIN_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_OSR_WINDOW_WIN_H_
7 #pragma once
8 
9 #include "include/base/cef_callback.h"
10 #include "include/base/cef_ref_counted.h"
11 #include "include/wrapper/cef_closure_task.h"
12 #include "include/wrapper/cef_helpers.h"
13 #include "tests/cefclient/browser/client_handler_osr.h"
14 #include "tests/cefclient/browser/osr_accessibility_node.h"
15 #include "tests/cefclient/browser/osr_dragdrop_win.h"
16 #include "tests/cefclient/browser/osr_render_handler_win.h"
17 #include "tests/cefclient/browser/osr_renderer_settings.h"
18 
19 namespace client {
20 
21 class OsrAccessibilityHelper;
22 class OsrImeHandlerWin;
23 
24 // Represents the native parent window for an off-screen browser. This object
25 // must live on the CEF UI thread in order to handle CefRenderHandler callbacks.
26 // The methods of this class are thread-safe unless otherwise indicated.
27 class OsrWindowWin
28     : public base::RefCountedThreadSafe<OsrWindowWin, CefDeleteOnUIThread>,
29       public ClientHandlerOsr::OsrDelegate
30 #if defined(CEF_USE_ATL)
31     ,
32       public OsrDragEvents
33 #endif
34 {
35  public:
36   // This interface is implemented by the owner of the OsrWindowWin. The
37   // methods of this class will be called on the main thread.
38   class Delegate {
39    public:
40     // Called after the native window has been created.
41     virtual void OnOsrNativeWindowCreated(HWND hwnd) = 0;
42 
43    protected:
~Delegate()44     virtual ~Delegate() {}
45   };
46 
47   // |delegate| must outlive this object.
48   OsrWindowWin(Delegate* delegate, const OsrRendererSettings& settings);
49 
50   // Create a new browser and native window.
51   void CreateBrowser(HWND parent_hwnd,
52                      const RECT& rect,
53                      CefRefPtr<CefClient> handler,
54                      const CefBrowserSettings& settings,
55                      CefRefPtr<CefDictionaryValue> extra_info,
56                      CefRefPtr<CefRequestContext> request_context,
57                      const std::string& startup_url);
58 
59   // Show the popup window with correct parent and bounds in parent coordinates.
60   void ShowPopup(HWND parent_hwnd, int x, int y, size_t width, size_t height);
61 
62   void Show();
63   void Hide();
64   void SetBounds(int x, int y, size_t width, size_t height);
65   void SetFocus();
66   void SetDeviceScaleFactor(float device_scale_factor);
67 
settings()68   const OsrRendererSettings& settings() const { return settings_; }
69 
70  private:
71   // Only allow deletion via scoped_refptr.
72   friend struct CefDeleteOnThread<TID_UI>;
73   friend class base::RefCountedThreadSafe<OsrWindowWin, CefDeleteOnUIThread>;
74 
75   ~OsrWindowWin();
76 
77   // Manage native window lifespan.
78   void Create(HWND parent_hwnd, const RECT& rect);
79   void Destroy();
80 
81   void NotifyNativeWindowCreated(HWND hwnd);
82 
83   static void RegisterOsrClass(HINSTANCE hInstance, HBRUSH background_brush);
84   static LRESULT CALLBACK OsrWndProc(HWND hWnd,
85                                      UINT message,
86                                      WPARAM wParam,
87                                      LPARAM lParam);
88 
89   // WndProc message handlers.
90   void OnMouseEvent(UINT message, WPARAM wParam, LPARAM lParam);
91   void OnSize();
92   void OnFocus(bool setFocus);
93   void OnCaptureLost();
94   void OnKeyEvent(UINT message, WPARAM wParam, LPARAM lParam);
95   void OnPaint();
96   bool OnEraseBkgnd();
97   bool OnTouchEvent(UINT message, WPARAM wParam, LPARAM lParam);
98 
99   void OnIMESetContext(UINT message, WPARAM wParam, LPARAM lParam);
100   void OnIMEStartComposition();
101   void OnIMEComposition(UINT message, WPARAM wParam, LPARAM lParam);
102   void OnIMECancelCompositionEvent();
103 
104   // Manage popup bounds.
105   bool IsOverPopupWidget(int x, int y) const;
106   int GetPopupXOffset() const;
107   int GetPopupYOffset() const;
108   void ApplyPopupOffset(int& x, int& y) const;
109 
110   // ClientHandlerOsr::OsrDelegate methods.
111   void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
112   void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
113   bool GetRootScreenRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override;
114   void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override;
115   bool GetScreenPoint(CefRefPtr<CefBrowser> browser,
116                       int viewX,
117                       int viewY,
118                       int& screenX,
119                       int& screenY) override;
120   bool GetScreenInfo(CefRefPtr<CefBrowser> browser,
121                      CefScreenInfo& screen_info) override;
122   void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show) override;
123   void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect) override;
124   void OnPaint(CefRefPtr<CefBrowser> browser,
125                CefRenderHandler::PaintElementType type,
126                const CefRenderHandler::RectList& dirtyRects,
127                const void* buffer,
128                int width,
129                int height) override;
130   void OnAcceleratedPaint(CefRefPtr<CefBrowser> browser,
131                           CefRenderHandler::PaintElementType type,
132                           const CefRenderHandler::RectList& dirtyRects,
133                           void* share_handle) override;
134   void OnCursorChange(CefRefPtr<CefBrowser> browser,
135                       CefCursorHandle cursor,
136                       cef_cursor_type_t type,
137                       const CefCursorInfo& custom_cursor_info) override;
138   bool StartDragging(CefRefPtr<CefBrowser> browser,
139                      CefRefPtr<CefDragData> drag_data,
140                      CefRenderHandler::DragOperationsMask allowed_ops,
141                      int x,
142                      int y) override;
143   void UpdateDragCursor(CefRefPtr<CefBrowser> browser,
144                         CefRenderHandler::DragOperation operation) override;
145   void OnImeCompositionRangeChanged(
146       CefRefPtr<CefBrowser> browser,
147       const CefRange& selection_range,
148       const CefRenderHandler::RectList& character_bounds) override;
149 
150   void UpdateAccessibilityTree(CefRefPtr<CefValue> value) override;
151 
152   void UpdateAccessibilityLocation(CefRefPtr<CefValue> value) override;
153 
154 #if defined(CEF_USE_ATL)
155   // OsrDragEvents methods.
156   CefBrowserHost::DragOperationsMask OnDragEnter(
157       CefRefPtr<CefDragData> drag_data,
158       CefMouseEvent ev,
159       CefBrowserHost::DragOperationsMask effect) override;
160   CefBrowserHost::DragOperationsMask OnDragOver(
161       CefMouseEvent ev,
162       CefBrowserHost::DragOperationsMask effect) override;
163   void OnDragLeave() override;
164   CefBrowserHost::DragOperationsMask OnDrop(
165       CefMouseEvent ev,
166       CefBrowserHost::DragOperationsMask effect) override;
167 #endif  // defined(CEF_USE_ATL)
168 
169   void EnsureRenderHandler();
170 
171   // Only accessed on the main thread.
172   Delegate* delegate_;
173 
174   const OsrRendererSettings settings_;
175   HWND hwnd_;
176   std::unique_ptr<OsrRenderHandlerWin> render_handler_;
177 
178   // Class that encapsulates IMM32 APIs and controls IMEs attached to a window.
179   std::unique_ptr<OsrImeHandlerWin> ime_handler_;
180 
181   RECT client_rect_;
182   float device_scale_factor_;
183 
184   CefRefPtr<CefBrowser> browser_;
185 
186 #if defined(CEF_USE_ATL)
187   CComPtr<DropTargetWin> drop_target_;
188   CefRenderHandler::DragOperation current_drag_op_;
189 
190   // Class that abstracts the accessibility information received from the
191   // renderer.
192   std::unique_ptr<OsrAccessibilityHelper> accessibility_handler_;
193   IAccessible* accessibility_root_;
194 #endif
195 
196   bool hidden_;
197 
198   // Mouse state tracking.
199   POINT last_mouse_pos_;
200   POINT current_mouse_pos_;
201   bool mouse_rotation_;
202   bool mouse_tracking_;
203   int last_click_x_;
204   int last_click_y_;
205   CefBrowserHost::MouseButtonType last_click_button_;
206   int last_click_count_;
207   double last_click_time_;
208   bool last_mouse_down_on_view_;
209 
210   DISALLOW_COPY_AND_ASSIGN(OsrWindowWin);
211 };
212 
213 }  // namespace client
214 
215 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_OSR_WINDOW_WIN_H_
216