• 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_ROOT_WINDOW_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_H_
7 #pragma once
8 
9 #include <set>
10 #include <string>
11 
12 #include "include/base/cef_callback_forward.h"
13 #include "include/base/cef_ref_counted.h"
14 #include "include/cef_browser.h"
15 #include "include/views/cef_window.h"
16 #include "tests/cefclient/browser/client_types.h"
17 #include "tests/cefclient/browser/image_cache.h"
18 #include "tests/shared/browser/main_message_loop.h"
19 
20 #if defined(OS_MAC) && __OBJC__
21 @class NSWindow;
22 #endif  // defined(OS_MAC) && __OBJC__
23 
24 namespace client {
25 
26 // Used to configure how a RootWindow is created.
27 struct RootWindowConfig {
28   RootWindowConfig();
29 
30   // If true the window will always display above other windows.
31   bool always_on_top;
32 
33   // If true the window will show controls.
34   bool with_controls;
35 
36   // If true the window will use off-screen rendering.
37   bool with_osr;
38 
39   // If true the window is hosting an extension app.
40   bool with_extension;
41 
42   // If true the window will be created initially hidden.
43   bool initially_hidden;
44 
45   // Requested window position. If |bounds| and |source_bounds| are empty the
46   // default window size and location will be used.
47   CefRect bounds;
48 
49   // Position of the UI element that triggered the window creation. If |bounds|
50   // is empty and |source_bounds| is non-empty the new window will be positioned
51   // relative to |source_bounds|. This is currently only implemented for Views-
52   // based windows when |initially_hidden| is also true.
53   CefRect source_bounds;
54 
55   // Parent window. Only used for Views-based windows.
56   CefRefPtr<CefWindow> parent_window;
57 
58   // Callback to be executed when the window is closed. Will be executed on the
59   // main thread. This is currently only implemented for Views-based windows.
60   base::Closure close_callback;
61 
62   // Initial URL to load.
63   std::string url;
64 };
65 
66 typedef std::set<CefRefPtr<CefExtension>> ExtensionSet;
67 
68 // Represents a top-level native window in the browser process. While references
69 // to this object are thread-safe the methods must be called on the main thread
70 // unless otherwise indicated.
71 class RootWindow
72     : public base::RefCountedThreadSafe<RootWindow, DeleteOnMainThread> {
73  public:
74   // This interface is implemented by the owner of the RootWindow. The methods
75   // of this class will be called on the main thread.
76   class Delegate {
77    public:
78     // Called to retrieve the CefRequestContext for browser. Only called for
79     // non-popup browsers. May return NULL.
80     virtual CefRefPtr<CefRequestContext> GetRequestContext(
81         RootWindow* root_window) = 0;
82 
83     // Returns the ImageCache.
84     virtual scoped_refptr<ImageCache> GetImageCache() = 0;
85 
86     // Called to execute a test. See resource.h for |test_id| values.
87     virtual void OnTest(RootWindow* root_window, int test_id) = 0;
88 
89     // Called to exit the application.
90     virtual void OnExit(RootWindow* root_window) = 0;
91 
92     // Called when the RootWindow has been destroyed.
93     virtual void OnRootWindowDestroyed(RootWindow* root_window) = 0;
94 
95     // Called when the RootWindow is activated (becomes the foreground window).
96     virtual void OnRootWindowActivated(RootWindow* root_window) = 0;
97 
98     // Called when the browser is created for the RootWindow.
99     virtual void OnBrowserCreated(RootWindow* root_window,
100                                   CefRefPtr<CefBrowser> browser) = 0;
101 
102     // Create a window for |extension|. |source_bounds| are the bounds of the
103     // UI element, like a button, that triggered the extension.
104     virtual void CreateExtensionWindow(CefRefPtr<CefExtension> extension,
105                                        const CefRect& source_bounds,
106                                        CefRefPtr<CefWindow> parent_window,
107                                        const base::Closure& close_callback,
108                                        bool with_osr) = 0;
109 
110    protected:
~Delegate()111     virtual ~Delegate() {}
112   };
113 
114   // Create a new RootWindow object. This method may be called on any thread.
115   // Use RootWindowManager::CreateRootWindow() or CreateRootWindowAsPopup()
116   // instead of calling this method directly. |use_views| will be true if the
117   // Views framework should be used.
118   static scoped_refptr<RootWindow> Create(bool use_views);
119 
120   // Returns the RootWindow associated with the specified |browser_id|. Must be
121   // called on the main thread.
122   static scoped_refptr<RootWindow> GetForBrowser(int browser_id);
123 
124 #if defined(OS_MAC) && __OBJC__
125   // Returns the RootWindow associated with the specified |window|. Must be
126   // called on the main thread.
127   static scoped_refptr<RootWindow> GetForNSWindow(NSWindow* window);
128 #endif
129 
130   // Initialize as a normal window. This will create and show a native window
131   // hosting a single browser instance. This method may be called on any thread.
132   // |delegate| must be non-NULL and outlive this object.
133   // Use RootWindowManager::CreateRootWindow() instead of calling this method
134   // directly.
135   virtual void Init(RootWindow::Delegate* delegate,
136                     const RootWindowConfig& config,
137                     const CefBrowserSettings& settings) = 0;
138 
139   // Initialize as a popup window. This is used to attach a new native window to
140   // a single browser instance that will be created later. The native window
141   // will be created and shown once the browser is available. This method may be
142   // called on any thread. |delegate| must be non-NULL and outlive this object.
143   // Use RootWindowManager::CreateRootWindowAsPopup() instead of calling this
144   // method directly. Called on the UI thread.
145   virtual void InitAsPopup(RootWindow::Delegate* delegate,
146                            bool with_controls,
147                            bool with_osr,
148                            const CefPopupFeatures& popupFeatures,
149                            CefWindowInfo& windowInfo,
150                            CefRefPtr<CefClient>& client,
151                            CefBrowserSettings& settings) = 0;
152 
153   enum ShowMode {
154     ShowNormal,
155     ShowMinimized,
156     ShowMaximized,
157     ShowNoActivate,
158   };
159 
160   // Show the window.
161   virtual void Show(ShowMode mode) = 0;
162 
163   // Hide the window.
164   virtual void Hide() = 0;
165 
166   // Set the window bounds in screen coordinates.
167   virtual void SetBounds(int x, int y, size_t width, size_t height) = 0;
168 
169   // Close the window. If |force| is true onunload handlers will not be
170   // executed.
171   virtual void Close(bool force) = 0;
172 
173   // Set the device scale factor. Only used in combination with off-screen
174   // rendering.
175   virtual void SetDeviceScaleFactor(float device_scale_factor) = 0;
176 
177   // Returns the device scale factor. Only used in combination with off-screen
178   // rendering.
179   virtual float GetDeviceScaleFactor() const = 0;
180 
181   // Returns the browser that this window contains, if any.
182   virtual CefRefPtr<CefBrowser> GetBrowser() const = 0;
183 
184   // Returns the native handle for this window, if any.
185   virtual ClientWindowHandle GetWindowHandle() const = 0;
186 
187   // Returns true if this window is using windowless rendering (osr).
188   virtual bool WithWindowlessRendering() const = 0;
189 
190   // Returns true if this window is hosting an extension app.
191   virtual bool WithExtension() const = 0;
192 
193   // Called when the set of loaded extensions changes. The default
194   // implementation will create a single window instance for each extension.
195   virtual void OnExtensionsChanged(const ExtensionSet& extensions);
196 
197  protected:
198   // Allow deletion via scoped_refptr only.
199   friend struct DeleteOnMainThread;
200   friend class base::RefCountedThreadSafe<RootWindow, DeleteOnMainThread>;
201 
202   RootWindow();
203   virtual ~RootWindow();
204 
205   Delegate* delegate_;
206 };
207 
208 }  // namespace client
209 
210 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_H_
211