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