• 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_MANAGER_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_MANAGER_H_
7 #pragma once
8 
9 #include <memory>
10 #include <set>
11 
12 #include "include/cef_command_line.h"
13 #include "include/cef_request_context_handler.h"
14 #include "tests/cefclient/browser/image_cache.h"
15 #include "tests/cefclient/browser/root_window.h"
16 #include "tests/cefclient/browser/temp_window.h"
17 
18 namespace client {
19 
20 // Used to create/manage RootWindow instances. The methods of this class can be
21 // called from any browser process thread unless otherwise indicated.
22 class RootWindowManager : public RootWindow::Delegate {
23  public:
24   // If |terminate_when_all_windows_closed| is true quit the main message loop
25   // after all windows have closed.
26   explicit RootWindowManager(bool terminate_when_all_windows_closed);
27 
28   // Create a new top-level native window. This method can be called from
29   // anywhere.
30   scoped_refptr<RootWindow> CreateRootWindow(
31       std::unique_ptr<RootWindowConfig> config);
32 
33   // Create a new native popup window.
34   // If |with_controls| is true the window will show controls.
35   // If |with_osr| is true the window will use off-screen rendering.
36   // This method is called from ClientHandler::CreatePopupWindow() to
37   // create a new popup or DevTools window. Must be called on the UI thread.
38   scoped_refptr<RootWindow> CreateRootWindowAsPopup(
39       bool with_controls,
40       bool with_osr,
41       const CefPopupFeatures& popupFeatures,
42       CefWindowInfo& windowInfo,
43       CefRefPtr<CefClient>& client,
44       CefBrowserSettings& settings);
45 
46   // Create a new top-level native window to host |extension|.
47   // If |with_controls| is true the window will show controls.
48   // If |with_osr| is true the window will use off-screen rendering.
49   // This method can be called from anywhere.
50   scoped_refptr<RootWindow> CreateRootWindowAsExtension(
51       CefRefPtr<CefExtension> extension,
52       const CefRect& source_bounds,
53       CefRefPtr<CefWindow> parent_window,
54       base::OnceClosure close_callback,
55       bool with_controls,
56       bool with_osr);
57 
58   // Returns true if a window hosting |extension| currently exists. Must be
59   // called on the main thread.
60   bool HasRootWindowAsExtension(CefRefPtr<CefExtension> extension);
61 
62   // Returns the RootWindow associated with the specified browser ID. Must be
63   // called on the main thread.
64   scoped_refptr<RootWindow> GetWindowForBrowser(int browser_id) const;
65 
66   // Returns the currently active/foreground RootWindow. May return nullptr.
67   // Must be called on the main thread.
68   scoped_refptr<RootWindow> GetActiveRootWindow() const;
69 
70   // Returns the currently active/foreground browser. May return nullptr. Safe
71   // to call from any thread.
72   CefRefPtr<CefBrowser> GetActiveBrowser() const;
73 
74   // Close all existing windows. If |force| is true onunload handlers will not
75   // be executed.
76   void CloseAllWindows(bool force);
77 
78   // Manage the set of loaded extensions. RootWindows will be notified via the
79   // OnExtensionsChanged method.
80   void AddExtension(CefRefPtr<CefExtension> extension);
81 
request_context_per_browser()82   bool request_context_per_browser() const {
83     return request_context_per_browser_;
84   }
85 
86  private:
87   // Allow deletion via std::unique_ptr only.
88   friend std::default_delete<RootWindowManager>;
89 
90   ~RootWindowManager();
91 
92   void OnRootWindowCreated(scoped_refptr<RootWindow> root_window);
93   void NotifyExtensionsChanged();
94 
95   // RootWindow::Delegate methods.
96   CefRefPtr<CefRequestContext> GetRequestContext(
97       RootWindow* root_window) override;
98   scoped_refptr<ImageCache> GetImageCache() override;
99   void OnTest(RootWindow* root_window, int test_id) override;
100   void OnExit(RootWindow* root_window) override;
101   void OnRootWindowDestroyed(RootWindow* root_window) override;
102   void OnRootWindowActivated(RootWindow* root_window) override;
103   void OnBrowserCreated(RootWindow* root_window,
104                         CefRefPtr<CefBrowser> browser) override;
105   void CreateExtensionWindow(CefRefPtr<CefExtension> extension,
106                              const CefRect& source_bounds,
107                              CefRefPtr<CefWindow> parent_window,
108                              base::OnceClosure close_callback,
109                              bool with_osr) override;
110 
111   void CleanupOnUIThread();
112 
113   const bool terminate_when_all_windows_closed_;
114   bool request_context_per_browser_;
115   bool request_context_shared_cache_;
116 
117   // Existing root windows. Only accessed on the main thread.
118   typedef std::set<scoped_refptr<RootWindow>> RootWindowSet;
119   RootWindowSet root_windows_;
120 
121   // The currently active/foreground RootWindow. Only accessed on the main
122   // thread.
123   scoped_refptr<RootWindow> active_root_window_;
124 
125   // The currently active/foreground browser. Access is protected by
126   // |active_browser_lock_;
127   mutable base::Lock active_browser_lock_;
128   CefRefPtr<CefBrowser> active_browser_;
129 
130   // Singleton window used as the temporary parent for popup browsers.
131   std::unique_ptr<TempWindow> temp_window_;
132 
133   CefRefPtr<CefRequestContext> shared_request_context_;
134 
135   // Loaded extensions. Only accessed on the main thread.
136   ExtensionSet extensions_;
137 
138   scoped_refptr<ImageCache> image_cache_;
139 
140   DISALLOW_COPY_AND_ASSIGN(RootWindowManager);
141 };
142 
143 }  // namespace client
144 
145 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_ROOT_WINDOW_MANAGER_H_
146