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:
|
2024-01-29 19:38:25 +01:00
|
|
|
SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view,
|
2024-04-17 18:01:26 +02:00
|
|
|
cef_runtime_style_t runtime_style,
|
2024-01-29 19:38:25 +01:00
|
|
|
cef_show_state_t initial_show_state)
|
2024-04-17 18:01:26 +02:00
|
|
|
: browser_view_(browser_view),
|
|
|
|
runtime_style_(runtime_style),
|
|
|
|
initial_show_state_(initial_show_state) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
void OnWindowCreated(CefRefPtr<CefWindow> window) override {
|
2016-01-19 21:09:01 +01:00
|
|
|
// Add the browser view and show the window.
|
|
|
|
window->AddChildView(browser_view_);
|
|
|
|
|
2024-01-29 19:38:25 +01:00
|
|
|
if (initial_show_state_ != CEF_SHOW_STATE_HIDDEN) {
|
|
|
|
window->Show();
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
bool CanClose(CefRefPtr<CefWindow> window) override {
|
2016-01-19 21:09:01 +01:00
|
|
|
// Allow the window to close if the browser says it's OK.
|
|
|
|
CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (browser) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return browser->GetHost()->TryCloseBrowser();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:43:06 +02:00
|
|
|
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
|
2019-04-23 19:00:14 +02:00
|
|
|
return CefSize(800, 600);
|
|
|
|
}
|
|
|
|
|
2024-01-29 19:38:25 +01:00
|
|
|
cef_show_state_t GetInitialShowState(CefRefPtr<CefWindow> window) override {
|
|
|
|
return initial_show_state_;
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
cef_runtime_style_t GetWindowRuntimeStyle() override {
|
|
|
|
return runtime_style_;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefBrowserView> browser_view_;
|
2024-04-17 18:01:26 +02:00
|
|
|
const cef_runtime_style_t runtime_style_;
|
2024-01-29 19:38:25 +01:00
|
|
|
const cef_show_state_t initial_show_state_;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
|
|
|
|
};
|
|
|
|
|
2020-01-28 02:35:02 +01:00
|
|
|
class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
|
|
|
|
public:
|
2024-04-17 18:01:26 +02:00
|
|
|
explicit SimpleBrowserViewDelegate(cef_runtime_style_t runtime_style)
|
|
|
|
: runtime_style_(runtime_style) {}
|
2020-01-28 02:35:02 +01:00
|
|
|
|
|
|
|
bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
|
|
|
|
CefRefPtr<CefBrowserView> popup_browser_view,
|
2021-06-17 21:43:06 +02:00
|
|
|
bool is_devtools) override {
|
2020-01-28 02:35:02 +01:00
|
|
|
// Create a new top-level Window for the popup. It will show itself after
|
|
|
|
// creation.
|
2024-04-17 18:01:26 +02:00
|
|
|
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(
|
|
|
|
popup_browser_view, runtime_style_, CEF_SHOW_STATE_NORMAL));
|
2020-01-28 02:35:02 +01:00
|
|
|
|
|
|
|
// We created the Window.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
cef_runtime_style_t GetBrowserRuntimeStyle() override {
|
|
|
|
return runtime_style_;
|
|
|
|
}
|
|
|
|
|
2020-01-28 02:35:02 +01:00
|
|
|
private:
|
2024-04-17 18:01:26 +02:00
|
|
|
const cef_runtime_style_t runtime_style_;
|
|
|
|
|
2020-01-28 02:35:02 +01:00
|
|
|
IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
|
|
|
|
};
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
} // namespace
|
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
SimpleApp::SimpleApp() = default;
|
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
|
|
|
|
2024-06-26 02:12:37 +02:00
|
|
|
// Check if Alloy style will be used.
|
2024-04-17 18:01:26 +02:00
|
|
|
cef_runtime_style_t runtime_style = CEF_RUNTIME_STYLE_DEFAULT;
|
2024-06-26 02:12:37 +02:00
|
|
|
bool use_alloy_style = command_line->HasSwitch("use-alloy-style");
|
|
|
|
if (use_alloy_style) {
|
|
|
|
runtime_style = CEF_RUNTIME_STYLE_ALLOY;
|
2024-04-17 18:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-11-26 23:02:14 +01:00
|
|
|
// SimpleHandler implements browser-level callbacks.
|
2024-04-17 18:01:26 +02:00
|
|
|
CefRefPtr<SimpleHandler> handler(new SimpleHandler(use_alloy_style));
|
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");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (url.empty()) {
|
2024-05-13 23:36:09 +02:00
|
|
|
url = "https://www.google.com";
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-11-26 23:02:14 +01:00
|
|
|
|
2024-06-26 02:12:37 +02:00
|
|
|
// Views is enabled by default (add `--use-native` to disable).
|
2024-05-09 18:18:57 +02:00
|
|
|
const bool use_views = !command_line->HasSwitch("use-native");
|
|
|
|
|
|
|
|
// If using Views create the browser using the Views framework, otherwise
|
|
|
|
// create the browser using the native platform framework.
|
|
|
|
if (use_views) {
|
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,
|
2024-04-17 18:01:26 +02:00
|
|
|
new SimpleBrowserViewDelegate(runtime_style));
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2024-01-29 19:38:25 +01:00
|
|
|
// Optionally configure the initial show state.
|
|
|
|
cef_show_state_t initial_show_state = CEF_SHOW_STATE_NORMAL;
|
|
|
|
const std::string& show_state_value =
|
|
|
|
command_line->GetSwitchValue("initial-show-state");
|
|
|
|
if (show_state_value == "minimized") {
|
|
|
|
initial_show_state = CEF_SHOW_STATE_MINIMIZED;
|
|
|
|
} else if (show_state_value == "maximized") {
|
|
|
|
initial_show_state = CEF_SHOW_STATE_MAXIMIZED;
|
|
|
|
}
|
|
|
|
#if defined(OS_MAC)
|
|
|
|
// Hidden show state is only supported on MacOS.
|
|
|
|
else if (show_state_value == "hidden") {
|
|
|
|
initial_show_state = CEF_SHOW_STATE_HIDDEN;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
// Create the Window. It will show itself after creation.
|
2024-04-17 18:01:26 +02:00
|
|
|
CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(
|
|
|
|
browser_view, runtime_style, initial_show_state));
|
2016-01-19 21:09:01 +01:00
|
|
|
} 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().
|
2021-06-17 22:08:01 +02:00
|
|
|
window_info.SetAsPopup(nullptr, "cefsimple");
|
2016-01-19 21:09:01 +01:00
|
|
|
#endif
|
|
|
|
|
2024-06-26 02:12:37 +02:00
|
|
|
// Alloy style will create a basic native window. Chrome style will create a
|
|
|
|
// fully styled Chrome UI window.
|
2024-04-17 18:01:26 +02:00
|
|
|
window_info.runtime_style = runtime_style;
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
// 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
|
|
|
}
|
2021-02-15 22:40:07 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefClient> SimpleApp::GetDefaultClient() {
|
2024-06-26 02:12:37 +02:00
|
|
|
// Called when a new browser window is created via Chrome style UI.
|
2021-02-15 22:40:07 +01:00
|
|
|
return SimpleHandler::GetInstance();
|
|
|
|
}
|