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