• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 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_VIEWS_WINDOW_H_
6 #define CEF_TESTS_CEFCLIENT_BROWSER_VIEWS_WINDOW_H_
7 #pragma once
8 
9 #include <set>
10 #include <string>
11 #include <vector>
12 
13 #include "include/base/cef_callback_forward.h"
14 #include "include/cef_menu_model_delegate.h"
15 #include "include/views/cef_browser_view.h"
16 #include "include/views/cef_browser_view_delegate.h"
17 #include "include/views/cef_button_delegate.h"
18 #include "include/views/cef_label_button.h"
19 #include "include/views/cef_menu_button.h"
20 #include "include/views/cef_menu_button_delegate.h"
21 #include "include/views/cef_overlay_controller.h"
22 #include "include/views/cef_textfield.h"
23 #include "include/views/cef_textfield_delegate.h"
24 #include "include/views/cef_window.h"
25 #include "include/views/cef_window_delegate.h"
26 #include "tests/cefclient/browser/image_cache.h"
27 #include "tests/cefclient/browser/views_menu_bar.h"
28 #include "tests/cefclient/browser/views_overlay_controls.h"
29 
30 namespace client {
31 
32 typedef std::set<CefRefPtr<CefExtension>> ExtensionSet;
33 
34 // Implements a CefWindow that hosts a single CefBrowserView and optional
35 // Views-based controls. All methods must be called on the browser process UI
36 // thread.
37 class ViewsWindow : public CefBrowserViewDelegate,
38                     public CefMenuButtonDelegate,
39                     public CefMenuModelDelegate,
40                     public CefTextfieldDelegate,
41                     public CefWindowDelegate,
42                     public ViewsMenuBar::Delegate {
43  public:
44   // Delegate methods will be called on the browser process UI thread.
45   class Delegate {
46    public:
47     // Return true if the window should show controls.
48     virtual bool WithControls() = 0;
49 
50     // Return true if the window is hosting an extension.
51     virtual bool WithExtension() = 0;
52 
53     // Return true if the window should be created initially hidden.
54     virtual bool InitiallyHidden() = 0;
55 
56     // Returns the parent for this window.
57     virtual CefRefPtr<CefWindow> GetParentWindow() = 0;
58 
59     // Return the initial window bounds.
60     virtual CefRect GetWindowBounds() = 0;
61 
62     // Returns the ImageCache.
63     virtual scoped_refptr<ImageCache> GetImageCache() = 0;
64 
65     // Called when the ViewsWindow is created.
66     virtual void OnViewsWindowCreated(CefRefPtr<ViewsWindow> window) = 0;
67 
68     // Called when the ViewsWindow is destroyed. All references to |window|
69     // should be released in this callback.
70     virtual void OnViewsWindowDestroyed(CefRefPtr<ViewsWindow> window) = 0;
71 
72     // Called when the ViewsWindow is activated (becomes the foreground window).
73     virtual void OnViewsWindowActivated(CefRefPtr<ViewsWindow> window) = 0;
74 
75     // Return the Delegate for the popup window controlled by |client|.
76     virtual Delegate* GetDelegateForPopup(CefRefPtr<CefClient> client) = 0;
77 
78     // Create a window for |extension|. |source_bounds| are the bounds of the
79     // UI element, like a button, that triggered the extension.
80     virtual void CreateExtensionWindow(CefRefPtr<CefExtension> extension,
81                                        const CefRect& source_bounds,
82                                        CefRefPtr<CefWindow> parent_window,
83                                        base::OnceClosure close_callback) = 0;
84 
85     // Called to execute a test. See resource.h for |test_id| values.
86     virtual void OnTest(int test_id) = 0;
87 
88     // Called to exit the application.
89     virtual void OnExit() = 0;
90 
91    protected:
~Delegate()92     virtual ~Delegate() {}
93   };
94 
95   // Create a new top-level ViewsWindow hosting a browser with the specified
96   // configuration.
97   static CefRefPtr<ViewsWindow> Create(
98       Delegate* delegate,
99       CefRefPtr<CefClient> client,
100       const CefString& url,
101       const CefBrowserSettings& settings,
102       CefRefPtr<CefRequestContext> request_context);
103 
104   void Show();
105   void Hide();
106   void Minimize();
107   void Maximize();
108   void SetBounds(const CefRect& bounds);
109   void SetBrowserSize(const CefSize& size,
110                       bool has_position,
111                       const CefPoint& position);
112   void Close(bool force);
113   void SetAddress(const std::string& url);
114   void SetTitle(const std::string& title);
115   void SetFavicon(CefRefPtr<CefImage> image);
116   void SetFullscreen(bool fullscreen);
117   void SetAlwaysOnTop(bool on_top);
118   void SetLoadingState(bool isLoading, bool canGoBack, bool canGoForward);
119   void SetDraggableRegions(const std::vector<CefDraggableRegion>& regions);
120   void TakeFocus(bool next);
121   void OnBeforeContextMenu(CefRefPtr<CefMenuModel> model);
122   void OnExtensionsChanged(const ExtensionSet& extensions);
123 
124   // CefBrowserViewDelegate methods:
125   CefRefPtr<CefBrowserViewDelegate> GetDelegateForPopupBrowserView(
126       CefRefPtr<CefBrowserView> browser_view,
127       const CefBrowserSettings& settings,
128       CefRefPtr<CefClient> client,
129       bool is_devtools) override;
130   bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
131                                  CefRefPtr<CefBrowserView> popup_browser_view,
132                                  bool is_devtools) override;
133   ChromeToolbarType GetChromeToolbarType() override;
134 
135   // CefButtonDelegate methods:
136   void OnButtonPressed(CefRefPtr<CefButton> button) override;
137 
138   // CefMenuButtonDelegate methods:
139   void OnMenuButtonPressed(
140       CefRefPtr<CefMenuButton> menu_button,
141       const CefPoint& screen_point,
142       CefRefPtr<CefMenuButtonPressedLock> button_pressed_lock) override;
143 
144   // CefMenuModelDelegate methods:
145   void ExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
146                       int command_id,
147                       cef_event_flags_t event_flags) override;
148 
149   // CefTextfieldDelegate methods:
150   bool OnKeyEvent(CefRefPtr<CefTextfield> textfield,
151                   const CefKeyEvent& event) override;
152 
153   // CefWindowDelegate methods:
154   void OnWindowCreated(CefRefPtr<CefWindow> window) override;
155   void OnWindowDestroyed(CefRefPtr<CefWindow> window) override;
156   CefRefPtr<CefWindow> GetParentWindow(CefRefPtr<CefWindow> window,
157                                        bool* is_menu,
158                                        bool* can_activate_menu) override;
159   CefRect GetInitialBounds(CefRefPtr<CefWindow> window) override;
160   cef_show_state_t GetInitialShowState(CefRefPtr<CefWindow> window) override;
161   bool IsFrameless(CefRefPtr<CefWindow> window) override;
162   bool CanResize(CefRefPtr<CefWindow> window) override;
163   bool CanClose(CefRefPtr<CefWindow> window) override;
164   bool OnAccelerator(CefRefPtr<CefWindow> window, int command_id) override;
165   bool OnKeyEvent(CefRefPtr<CefWindow> window,
166                   const CefKeyEvent& event) override;
167 
168   // CefViewDelegate methods:
169   CefSize GetMinimumSize(CefRefPtr<CefView> view) override;
170   void OnFocus(CefRefPtr<CefView> view) override;
171   void OnBlur(CefRefPtr<CefView> view) override;
172   void OnWindowChanged(CefRefPtr<CefView> view, bool added) override;
173   void OnLayoutChanged(CefRefPtr<CefView> view,
174                        const CefRect& new_bounds) override;
175 
176   // ViewsMenuBar::Delegate methods:
177   void MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
178                              int command_id,
179                              cef_event_flags_t event_flags) override;
180 
181  private:
182   // |delegate| is guaranteed to outlive this object.
183   // |browser_view| may be nullptr, in which case SetBrowserView() will be
184   // called.
185   ViewsWindow(Delegate* delegate, CefRefPtr<CefBrowserView> browser_view);
186 
187   void SetBrowserView(CefRefPtr<CefBrowserView> browser_view);
188 
189   // Create controls.
190   void CreateMenuModel();
191   CefRefPtr<CefLabelButton> CreateBrowseButton(const std::string& label,
192                                                int id);
193   CefRefPtr<CefMenuButton> CreateMenuButton();
194   CefRefPtr<CefView> CreateLocationBar();
195 
196   // Add the BrowserView to the Window.
197   void AddBrowserView();
198 
199   // Add other controls to the Window.
200   void AddControls();
201 
202   // Add keyboard accelerators to the Window.
203   void AddAccelerators();
204 
205   // Control whether the top menu butons are focusable.
206   void SetMenuFocusable(bool focusable);
207 
208   // Enable or disable a view by |id|.
209   void EnableView(int id, bool enable);
210 
211   // Show/hide top controls on the Window.
212   void ShowTopControls(bool show);
213 
214   // Update extension controls on the Window.
215   void UpdateExtensionControls();
216 
217   void OnExtensionIconsLoaded(const ExtensionSet& extensions,
218                               const ImageCache::ImageSet& images);
219   void OnExtensionWindowClosed();
220 
221   CefRect GetInitialBounds() const;
222 
223   Delegate* delegate_;  // Not owned by this object.
224   CefRefPtr<CefBrowserView> browser_view_;
225   bool frameless_;
226   bool with_controls_;
227   bool with_overlay_controls_;
228   ChromeToolbarType chrome_toolbar_type_;
229   CefRefPtr<CefWindow> window_;
230 
231   CefRefPtr<CefMenuModel> button_menu_model_;
232   CefRefPtr<ViewsMenuBar> top_menu_bar_;
233   CefRefPtr<CefView> top_toolbar_;
234   CefRefPtr<CefMenuButton> menu_button_;
235   CefRefPtr<CefView> location_bar_;
236   bool menu_has_focus_;
237   int last_focused_view_;
238 
239   CefSize minimum_window_size_;
240   cef_show_state_t initial_show_state_ = CEF_SHOW_STATE_NORMAL;
241 
242   CefRefPtr<ViewsOverlayControls> overlay_controls_;
243 
244   // Structure representing an extension.
245   struct ExtensionInfo {
ExtensionInfoExtensionInfo246     ExtensionInfo(CefRefPtr<CefExtension> extension, CefRefPtr<CefImage> image)
247         : extension_(extension), image_(image) {}
248 
249     CefRefPtr<CefExtension> extension_;
250     CefRefPtr<CefImage> image_;
251   };
252   typedef std::vector<ExtensionInfo> ExtensionInfoSet;
253 
254   ExtensionInfoSet extensions_;
255   CefRefPtr<CefPanel> extensions_panel_;
256   CefRefPtr<CefMenuButtonPressedLock> extension_button_pressed_lock_;
257 
258   IMPLEMENT_REFCOUNTING(ViewsWindow);
259   DISALLOW_COPY_AND_ASSIGN(ViewsWindow);
260 };
261 
262 }  // namespace client
263 
264 #endif  // CEF_TESTS_CEFCLIENT_BROWSER_VIEWS_WINDOW_H_
265