cef/tests/cefsimple/simple_app.cc

164 lines
5.2 KiB
C++

// 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.
#include "tests/cefsimple/simple_app.h"
#include <string>
#include "include/cef_browser.h"
#include "include/cef_command_line.h"
#include "include/views/cef_browser_view.h"
#include "include/views/cef_window.h"
#include "include/wrapper/cef_helpers.h"
#include "tests/cefsimple/simple_handler.h"
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:
SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view,
cef_show_state_t initial_show_state)
: browser_view_(browser_view), initial_show_state_(initial_show_state) {}
void OnWindowCreated(CefRefPtr<CefWindow> window) override {
// Add the browser view and show the window.
window->AddChildView(browser_view_);
if (initial_show_state_ != CEF_SHOW_STATE_HIDDEN) {
window->Show();
}
if (initial_show_state_ != CEF_SHOW_STATE_MINIMIZED &&
initial_show_state_ != CEF_SHOW_STATE_HIDDEN) {
// Give keyboard focus to the browser view.
browser_view_->RequestFocus();
}
}
void OnWindowDestroyed(CefRefPtr<CefWindow> window) override {
browser_view_ = nullptr;
}
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;
}
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
return CefSize(800, 600);
}
cef_show_state_t GetInitialShowState(CefRefPtr<CefWindow> window) override {
return initial_show_state_;
}
private:
CefRefPtr<CefBrowserView> browser_view_;
const cef_show_state_t initial_show_state_;
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
};
class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
public:
SimpleBrowserViewDelegate() = default;
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, CEF_SHOW_STATE_NORMAL));
// We created the Window.
return true;
}
private:
IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
};
} // namespace
SimpleApp::SimpleApp() = default;
void SimpleApp::OnContextInitialized() {
CEF_REQUIRE_UI_THREAD();
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();
// SimpleHandler implements browser-level callbacks.
CefRefPtr<SimpleHandler> handler(new SimpleHandler());
// 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";
}
// 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.
if (command_line->HasSwitch("use-views")) {
// Create the BrowserView.
CefRefPtr<CefBrowserView> browser_view = CefBrowserView::CreateBrowserView(
handler, url, browser_settings, nullptr, nullptr,
new SimpleBrowserViewDelegate());
// 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
// Create the Window. It will show itself after creation.
CefWindow::CreateTopLevelWindow(
new SimpleWindowDelegate(browser_view, initial_show_state));
} 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(nullptr, "cefsimple");
#endif
// Create the first browser window.
CefBrowserHost::CreateBrowser(window_info, handler, url, browser_settings,
nullptr, nullptr);
}
}
CefRefPtr<CefClient> SimpleApp::GetDefaultClient() {
// Called when a new browser window is created via the Chrome runtime UI.
return SimpleHandler::GetInstance();
}