2013-11-26 23:02:14 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefsimple/simple_app.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "include/cef_browser.h"
|
|
|
|
#include "include/cef_command_line.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "include/views/cef_browser_view.h"
|
|
|
|
#include "include/views/cef_window.h"
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/wrapper/cef_helpers.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "tests/cefsimple/simple_handler.h"
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// When using the Views framework this object provides the delegate
|
|
|
|
// implementation for the CefWindow that hosts the Views-based browser.
|
|
|
|
class SimpleWindowDelegate : public CefWindowDelegate {
|
|
|
|
public:
|
|
|
|
explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view)
|
2017-05-17 11:29:28 +02:00
|
|
|
: browser_view_(browser_view) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
void OnWindowCreated(CefRefPtr<CefWindow> window) OVERRIDE {
|
|
|
|
// Add the browser view and show the window.
|
|
|
|
window->AddChildView(browser_view_);
|
|
|
|
window->Show();
|
|
|
|
|
|
|
|
// Give keyboard focus to the browser view.
|
|
|
|
browser_view_->RequestFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnWindowDestroyed(CefRefPtr<CefWindow> window) OVERRIDE {
|
2020-01-15 15:26:01 +01:00
|
|
|
browser_view_ = nullptr;
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CanClose(CefRefPtr<CefWindow> window) OVERRIDE {
|
|
|
|
// Allow the window to close if the browser says it's OK.
|
|
|
|
CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
|
|
|
|
if (browser)
|
|
|
|
return browser->GetHost()->TryCloseBrowser();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-23 19:00:14 +02:00
|
|
|
CefSize GetPreferredSize(CefRefPtr<CefView> view) OVERRIDE {
|
|
|
|
return CefSize(800, 600);
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefBrowserView> browser_view_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
|
|
|
|
};
|
|
|
|
|
2020-01-28 02:35:02 +01:00
|
|
|
class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
|
|
|
|
public:
|
|
|
|
SimpleBrowserViewDelegate() {}
|
|
|
|
|
|
|
|
bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
|
|
|
|
CefRefPtr<CefBrowserView> popup_browser_view,
|
|
|
|
bool is_devtools) OVERRIDE {
|
|
|
|
// Create a new top-level Window for the popup. It will show itself after
|
|
|
|
// creation.
|
|
|
|
CefWindow::CreateTopLevelWindow(
|
|
|
|
new SimpleWindowDelegate(popup_browser_view));
|
|
|
|
|
|
|
|
// We created the Window.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
|
|
|
|
};
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
} // namespace
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
SimpleApp::SimpleApp() {}
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
void SimpleApp::OnContextInitialized() {
|
2014-07-11 22:10:05 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD();
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
CefRefPtr<CefCommandLine> command_line =
|
|
|
|
CefCommandLine::GetGlobalCommandLine();
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2020-06-25 04:34:12 +02:00
|
|
|
const bool enable_chrome_runtime =
|
|
|
|
command_line->HasSwitch("enable-chrome-runtime");
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
#if defined(OS_WIN) || defined(OS_LINUX)
|
|
|
|
// Create the browser using the Views framework if "--use-views" is specified
|
|
|
|
// via the command-line. Otherwise, create the browser using the native
|
|
|
|
// platform framework. The Views framework is currently only supported on
|
|
|
|
// Windows and Linux.
|
|
|
|
const bool use_views = command_line->HasSwitch("use-views");
|
|
|
|
#else
|
|
|
|
const bool use_views = false;
|
2013-11-26 23:02:14 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// SimpleHandler implements browser-level callbacks.
|
2016-01-19 21:09:01 +01:00
|
|
|
CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_views));
|
2013-11-26 23:02:14 +01:00
|
|
|
|
|
|
|
// Specify CEF browser settings here.
|
|
|
|
CefBrowserSettings browser_settings;
|
|
|
|
|
|
|
|
std::string url;
|
|
|
|
|
|
|
|
// Check if a "--url=" value was provided via the command-line. If so, use
|
|
|
|
// that instead of the default URL.
|
|
|
|
url = command_line->GetSwitchValue("url");
|
|
|
|
if (url.empty())
|
|
|
|
url = "http://www.google.com";
|
|
|
|
|
2020-07-04 04:51:17 +02:00
|
|
|
if (use_views && !enable_chrome_runtime) {
|
2016-01-19 21:09:01 +01:00
|
|
|
// Create the BrowserView.
|
|
|
|
CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
|
2020-01-28 02:35:02 +01:00
|
|
|
handler, url, browser_settings, nullptr, nullptr,
|
|
|
|
new SimpleBrowserViewDelegate());
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Create the Window. It will show itself after creation.
|
|
|
|
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view));
|
|
|
|
} else {
|
|
|
|
// Information used when creating the native window.
|
|
|
|
CefWindowInfo window_info;
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
// On Windows we need to specify certain flags that will be passed to
|
|
|
|
// CreateWindowEx().
|
|
|
|
window_info.SetAsPopup(NULL, "cefsimple");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create the first browser window.
|
|
|
|
CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
|
2020-01-15 15:26:01 +01:00
|
|
|
nullptr, nullptr);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
2013-11-26 23:02:14 +01:00
|
|
|
}
|