mirror of
https://github.com/chenasraf/webview_cef.git
synced 2026-05-18 01:49:03 +00:00
isolate code between windows and macos
This commit is contained in:
@@ -15,7 +15,7 @@ EXTERNAL SOURCES:
|
||||
|
||||
SPEC CHECKSUMS:
|
||||
FlutterMacOS: 57701585bf7de1b3fc2bb61f6378d73bbdea8424
|
||||
webview_cef: 60cb104c4f5d7454500fa260cfbaeb1da3d45292
|
||||
webview_cef: 1c58878c88cb921c6060d6df87d9fe26990a16a2
|
||||
|
||||
PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c
|
||||
|
||||
|
||||
103
macos/Classes/simple_app.cc
Normal file
103
macos/Classes/simple_app.cc
Normal file
@@ -0,0 +1,103 @@
|
||||
// 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();
|
||||
}
|
||||
45
macos/Classes/simple_app.h
Normal file
45
macos/Classes/simple_app.h
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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("disable-gpu-shader-disk-cache");
|
||||
command_line->AppendSwitch("enable-begin-frame-scheduling");
|
||||
command_line->AppendSwitch("enable-system-flash");
|
||||
command_line->AppendSwitch("log-severity=disable");
|
||||
command_line->AppendSwitch("use-mock-keychain");
|
||||
command_line->AppendSwitch("off-screen-rendering-enabled");
|
||||
}
|
||||
|
||||
|
||||
// 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_
|
||||
225
macos/Classes/simple_handler.cc
Normal file
225
macos/Classes/simple_handler.cc
Normal file
@@ -0,0 +1,225 @@
|
||||
// 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) {
|
||||
int a = 500;
|
||||
int b = 500;
|
||||
rect = CefRect(0, 0, width, 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) {
|
||||
}
|
||||
|
||||
96
macos/Classes/simple_handler.h
Normal file
96
macos/Classes/simple_handler.h
Normal file
@@ -0,0 +1,96 @@
|
||||
// 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 = 1280;
|
||||
uint32_t height = 720;
|
||||
|
||||
// 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_
|
||||
@@ -22,6 +22,7 @@ Flutter webview backed by CEF (Chromium Embedded Framework)
|
||||
$dir = File.dirname(__FILE__)
|
||||
$dir = $dir + "/third/cef/**"
|
||||
s.xcconfig = { "HEADER_SEARCH_PATHS" => $dir}
|
||||
s.private_header_files = 'Classes/simple_app.h', 'Classes/simple_handler.h'
|
||||
|
||||
s.platform = :osx, '10.11'
|
||||
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }
|
||||
|
||||
Reference in New Issue
Block a user