[Windows] windows & macos should share same cpp code base

This commit is contained in:
Prome
2022-09-12 18:31:24 +08:00
parent aba962527b
commit f844155227
272 changed files with 15 additions and 491 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// 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.

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// 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.
@@ -23,8 +23,10 @@ public:
CefRefPtr<CefCommandLine> command_line) override {
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
command_line->AppendSwitch("use-mock-keychain");
command_line->AppendSwitch("single-process");
#ifdef __APPLE__
command_line->AppendSwitch("use-mock-keychain");
command_line->AppendSwitch("single-process");
#endif
}
// CefBrowserProcessHandler methods:

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// 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.

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// 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.

View File

@@ -23,11 +23,10 @@ set(PLUGIN_NAME "webview_cef_plugin")
list(APPEND PLUGIN_SOURCES
"webview_cef_plugin.cpp"
"webview_cef_plugin.h"
"simple_app.cc"
"simple_app.h"
"simple_handler.cc"
"simple_handler.h"
"simple_handler_win.cc"
"${CMAKE_CURRENT_LIST_DIR}/../common/simple_app.cc"
"${CMAKE_CURRENT_LIST_DIR}/../common/simple_app.h"
"${CMAKE_CURRENT_LIST_DIR}/../common/simple_handler.cc"
"${CMAKE_CURRENT_LIST_DIR}/../common/simple_handler.h"
)
# Define the plugin library target. Its name must not be changed (see comment
@@ -54,6 +53,9 @@ target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
# dependencies here.
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/third/cef)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin
debug ${CMAKE_CURRENT_SOURCE_DIR}/cefbins/debug/libcef.lib
debug ${CMAKE_CURRENT_SOURCE_DIR}/cefbins/debug/libcef_dll_wrapper.lib

View File

@@ -1,103 +0,0 @@
// 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 "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"
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)
: browser_view_(browser_view) {}
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 {
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);
}
private:
CefRefPtr<CefBrowserView> browser_view_;
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
};
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);
};
} // namespace
SimpleApp::SimpleApp(CefRefPtr<SimpleHandler> handler) {
m_handler = handler;
}
void SimpleApp::OnContextInitialized() {
CEF_REQUIRE_UI_THREAD();
// Specify CEF browser settings here.
CefBrowserSettings browser_settings;
browser_settings.windowless_frame_rate = 60;
std::string url = "https://www.bilibili.com/";
CefWindowInfo window_info;
window_info.SetAsWindowless(nullptr);
// Create the first browser window.
CefBrowserHost::CreateBrowser(window_info, m_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();
}

View File

@@ -1,42 +0,0 @@
// 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.
#ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_
#define CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_
#include "include/cef_app.h"
#include <functional>
#include "simple_handler.h"
// Implement application-level callbacks for the browser process.
class SimpleApp : public CefApp, public CefBrowserProcessHandler {
public:
SimpleApp(CefRefPtr<SimpleHandler> handler);
// CefApp methods:
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override {
return this;
}
void OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) override {
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
command_line->AppendSwitch("enable-begin-frame-scheduling");
command_line->AppendSwitch("enable-system-flash");
command_line->AppendSwitch("log-severity=disable");
}
// CefBrowserProcessHandler methods:
void OnContextInitialized() override;
CefRefPtr<CefClient> GetDefaultClient() override;
private:
CefRefPtr<SimpleHandler> m_handler;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(SimpleApp);
};
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_APP_H_

View File

@@ -1,225 +0,0 @@
// 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 "simple_handler.h"
#include <sstream>
#include <string>
#include <iostream>
#include "include/base/cef_callback.h"
#include "include/cef_app.h"
#include "include/cef_parser.h"
#include "include/views/cef_browser_view.h"
#include "include/views/cef_window.h"
#include "include/wrapper/cef_closure_task.h"
#include "include/wrapper/cef_helpers.h"
namespace {
SimpleHandler* g_instance = nullptr;
// Returns a data: URI with the specified contents.
std::string GetDataURI(const std::string& data, const std::string& mime_type) {
return "data:" + mime_type + ";base64," +
CefURIEncode(CefBase64Encode(data.data(), data.size()), false)
.ToString();
}
} // namespace
SimpleHandler::SimpleHandler(bool use_views)
: use_views_(use_views), is_closing_(false) {
DCHECK(!g_instance);
g_instance = this;
}
SimpleHandler::~SimpleHandler() {
g_instance = nullptr;
}
// static
SimpleHandler* SimpleHandler::GetInstance() {
return g_instance;
}
void SimpleHandler::OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
CEF_REQUIRE_UI_THREAD();
if (use_views_) {
// Set the title of the window using the Views framework.
CefRefPtr<CefBrowserView> browser_view =
CefBrowserView::GetForBrowser(browser);
if (browser_view) {
CefRefPtr<CefWindow> window = browser_view->GetWindow();
if (window)
window->SetTitle(title);
}
} else if (!IsChromeRuntimeEnabled()) {
// Set the title of the window using platform APIs.
PlatformTitleChange(browser, title);
}
}
void SimpleHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
browser_list_.push_back(browser);
}
bool SimpleHandler::DoClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Closing the main window requires special handling. See the DoClose()
// documentation in the CEF header for a detailed destription of this
// process.
if (browser_list_.size() == 1) {
// Set a flag to indicate that the window close should be allowed.
is_closing_ = true;
}
// Allow the close. For windowed browsers this will result in the OS close
// event being sent.
return false;
}
void SimpleHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Remove from the list of existing browsers.
BrowserList::iterator bit = browser_list_.begin();
for (; bit != browser_list_.end(); ++bit) {
if ((*bit)->IsSame(browser)) {
browser_list_.erase(bit);
break;
}
}
if (browser_list_.empty()) {
// All browser windows have closed. Quit the application message loop.
CefQuitMessageLoop();
}
}
void SimpleHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) {
CEF_REQUIRE_UI_THREAD();
// Allow Chrome to show the error page.
if (IsChromeRuntimeEnabled())
return;
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Display a load error message using a data: URI.
std::stringstream ss;
ss << "<html><body bgcolor=\"white\">"
"<h2>Failed to load URL "
<< std::string(failedUrl) << " with error " << std::string(errorText)
<< " (" << errorCode << ").</h2></body></html>";
frame->LoadURL(GetDataURI(ss.str(), "text/html"));
}
void SimpleHandler::CloseAllBrowsers(bool force_close) {
if (!CefCurrentlyOn(TID_UI)) {
// Execute on the UI thread.
// CefPostTask(TID_UI, base::BindOnce(&SimpleHandler::CloseAllBrowsers, this,
// force_close));
return;
}
if (browser_list_.empty())
return;
BrowserList::const_iterator it = browser_list_.begin();
for (; it != browser_list_.end(); ++it)
(*it)->GetHost()->CloseBrowser(force_close);
}
// static
bool SimpleHandler::IsChromeRuntimeEnabled() {
static int value = -1;
if (value == -1) {
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::GetGlobalCommandLine();
value = command_line->HasSwitch("enable-chrome-runtime") ? 1 : 0;
}
return value == 1;
}
void SimpleHandler::scrollUp()
{
BrowserList::const_iterator it = browser_list_.begin();
if (it != browser_list_.end()) {
CefMouseEvent ev;
ev.x = 500;
ev.y = 500;
(*it)->GetHost()->SendMouseWheelEvent(ev, 0, -100);
}
}
void SimpleHandler::scrollDown()
{
BrowserList::const_iterator it = browser_list_.begin();
if (it != browser_list_.end()) {
CefMouseEvent ev;
ev.x = 500;
ev.y = 500;
(*it)->GetHost()->SendMouseWheelEvent(ev, 0, 100);
}
}
void SimpleHandler::changeSize(int w, int h)
{
// this->width = w;
// this->height = h;
BrowserList::const_iterator it = browser_list_.begin();
if (it != browser_list_.end()) {
(*it)->GetHost()->WasResized();
}
}
void SimpleHandler::cursorClick(int x, int y, bool up)
{
BrowserList::const_iterator it = browser_list_.begin();
if (it != browser_list_.end()) {
CefMouseEvent ev;
ev.x = x;
ev.y = y;
(*it)->GetHost()->SendMouseClickEvent(ev, CefBrowserHost::MouseButtonType::MBT_LEFT, up, 1);
}
}
void SimpleHandler::loadUrl(std::string url)
{
BrowserList::const_iterator it = browser_list_.begin();
if (it != browser_list_.end()) {
(*it)->GetMainFrame()->LoadURL(url);
}
}
void SimpleHandler::GetViewRect(CefRefPtr<CefBrowser> browser, CefRect &rect) {
rect.x = rect.y = 0;
rect.width = width;
rect.height = height;
return;
}
void SimpleHandler::OnPaint(CefRefPtr<CefBrowser> browser, CefRenderHandler::PaintElementType type,
const CefRenderHandler::RectList &dirtyRects, const void *buffer, int w, int h) {
onPaintCallback(buffer, w, h);
}
void SimpleHandler::PlatformTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
}

View File

@@ -1,96 +0,0 @@
// 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.
#ifndef CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
#define CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_
#include "include/cef_client.h"
#include <functional>
#include <list>
class SimpleHandler : public CefClient,
public CefDisplayHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRenderHandler{
public:
std::function<void(const void*, int32_t width, int32_t height)> onPaintCallback;
explicit SimpleHandler(bool use_views);
~SimpleHandler();
// Provide access to the single global instance of this object.
static SimpleHandler* GetInstance();
// CefClient methods:
virtual CefRefPtr<CefDisplayHandler> GetDisplayHandler() override {
return this;
}
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override {
return this;
}
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() override { return this; }
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() override { return this; } //新加的
// CefDisplayHandler methods:
virtual void OnTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) override;
// CefLifeSpanHandler methods:
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
virtual bool DoClose(CefRefPtr<CefBrowser> browser) override;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
// CefLoadHandler methods:
virtual void OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) override;
virtual void GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect) override;
virtual void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList& dirtyRects, const void* buffer, int width, int height) override;
// Request that all existing browser windows close.
void CloseAllBrowsers(bool force_close);
bool IsClosing() const { return is_closing_; }
// Returns true if the Chrome runtime is enabled.
static bool IsChromeRuntimeEnabled();
void scrollUp();
void scrollDown();
void changeSize(int width, int height);
void cursorClick(int x, int y, bool up);
void loadUrl(std::string url);
private:
uint32_t width = 1920;
uint32_t height = 1080;
// Platform-specific implementation.
void PlatformTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title);
// True if the application is using the Views framework.
const bool use_views_;
// List of existing browser windows. Only accessed on the CEF UI thread.
typedef std::list<CefRefPtr<CefBrowser>> BrowserList;
BrowserList browser_list_;
bool is_closing_;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(SimpleHandler);
};
#endif // CEF_TESTS_CEFSIMPLE_SIMPLE_HANDLER_H_

View File

@@ -1,14 +0,0 @@
// 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 "simple_handler.h"
#include <windows.h>
#include <string>
#include "include/cef_browser.h"
void SimpleHandler::PlatformTitleChange(CefRefPtr<CefBrowser> browser,
const CefString& title) {
}

Some files were not shown because too many files have changed in this diff Show More