1 // Copyright (c) 2013 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 #include "tests/cefsimple/simple_app.h"
6
7 #include <string>
8
9 #include "include/cef_browser.h"
10 #include "include/cef_command_line.h"
11 #include "include/views/cef_browser_view.h"
12 #include "include/views/cef_window.h"
13 #include "include/wrapper/cef_helpers.h"
14 #include "tests/cefsimple/simple_handler.h"
15
16 namespace {
17
18 // When using the Views framework this object provides the delegate
19 // implementation for the CefWindow that hosts the Views-based browser.
20 class SimpleWindowDelegate : public CefWindowDelegate {
21 public:
SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view)22 explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view)
23 : browser_view_(browser_view) {}
24
OnWindowCreated(CefRefPtr<CefWindow> window)25 void OnWindowCreated(CefRefPtr<CefWindow> window) override {
26 // Add the browser view and show the window.
27 window->AddChildView(browser_view_);
28 window->Show();
29
30 // Give keyboard focus to the browser view.
31 browser_view_->RequestFocus();
32 }
33
OnWindowDestroyed(CefRefPtr<CefWindow> window)34 void OnWindowDestroyed(CefRefPtr<CefWindow> window) override {
35 browser_view_ = nullptr;
36 }
37
CanClose(CefRefPtr<CefWindow> window)38 bool CanClose(CefRefPtr<CefWindow> window) override {
39 // Allow the window to close if the browser says it's OK.
40 CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
41 if (browser)
42 return browser->GetHost()->TryCloseBrowser();
43 return true;
44 }
45
GetPreferredSize(CefRefPtr<CefView> view)46 CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
47 return CefSize(800, 600);
48 }
49
50 private:
51 CefRefPtr<CefBrowserView> browser_view_;
52
53 IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
54 DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
55 };
56
57 class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
58 public:
SimpleBrowserViewDelegate()59 SimpleBrowserViewDelegate() {}
60
OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,CefRefPtr<CefBrowserView> popup_browser_view,bool is_devtools)61 bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
62 CefRefPtr<CefBrowserView> popup_browser_view,
63 bool is_devtools) override {
64 // Create a new top-level Window for the popup. It will show itself after
65 // creation.
66 CefWindow::CreateTopLevelWindow(
67 new SimpleWindowDelegate(popup_browser_view));
68
69 // We created the Window.
70 return true;
71 }
72
73 private:
74 IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
75 DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
76 };
77
78 } // namespace
79
SimpleApp()80 SimpleApp::SimpleApp() {}
81
OnContextInitialized()82 void SimpleApp::OnContextInitialized() {
83 CEF_REQUIRE_UI_THREAD();
84
85 CefRefPtr<CefCommandLine> command_line =
86 CefCommandLine::GetGlobalCommandLine();
87
88 // Create the browser using the Views framework if "--use-views" is specified
89 // via the command-line. Otherwise, create the browser using the native
90 // platform framework.
91 const bool use_views = command_line->HasSwitch("use-views");
92
93 // SimpleHandler implements browser-level callbacks.
94 CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_views));
95
96 // Specify CEF browser settings here.
97 CefBrowserSettings browser_settings;
98
99 std::string url;
100
101 // Check if a "--url=" value was provided via the command-line. If so, use
102 // that instead of the default URL.
103 url = command_line->GetSwitchValue("url");
104 if (url.empty())
105 url = "http://www.google.com";
106
107 if (use_views) {
108 // Create the BrowserView.
109 CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
110 handler, url, browser_settings, nullptr, nullptr,
111 new SimpleBrowserViewDelegate());
112
113 // Create the Window. It will show itself after creation.
114 CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view));
115 } else {
116 // Information used when creating the native window.
117 CefWindowInfo window_info;
118
119 #if defined(OS_WIN)
120 // On Windows we need to specify certain flags that will be passed to
121 // CreateWindowEx().
122 window_info.SetAsPopup(nullptr, "cefsimple");
123 #endif
124
125 // Create the first browser window.
126 CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
127 nullptr, nullptr);
128 }
129 }
130
GetDefaultClient()131 CefRefPtr<CefClient> SimpleApp::GetDefaultClient() {
132 // Called when a new browser window is created via the Chrome runtime UI.
133 return SimpleHandler::GetInstance();
134 }
135