From 03b324fdc250f611453b418b82caef16b2ecda60 Mon Sep 17 00:00:00 2001 From: Sheen Tian Date: Wed, 9 Oct 2024 20:55:00 +0800 Subject: [PATCH] feat: reform --- CMakeLists.txt | 4 +- CMakePresets.json | 130 +++++++++ gen-linux-arm.sh | 1 + gen-linux-arm64.sh | 1 + gen-linux-x64.sh | 1 + gen-linux-x86.sh | 1 + gen-mac-arm64.sh | 1 + gen-mac-x64.sh | 1 + gen-win-arm64.bat | 1 + gen-win-x64.bat | 1 + gen-win-x86.bat | 1 + include/CefViewBrowserApp.h | 45 +-- include/CefViewBrowserAppDelegate.h | 6 +- include/CefViewBrowserClient.h | 75 +++-- include/CefViewBrowserClientDelegate.h | 58 +++- include/CefViewCoreGlobal.h | 38 +-- include/CefViewCoreProtocol.h | 32 ++- include/CefViewCoreProtocol.h.in | 135 +++++++++ include/CefViewWingProcessName.h.in | 19 -- linux-arm-gen.sh | 1 - linux-arm64-gen.sh | 1 - linux-x86-gen.sh | 1 - linux-x86_64-gen.sh | 1 - mac-arm64-gen.sh | 1 - mac-x86_64-gen.sh | 1 - src/CMakeLists.txt | 6 +- .../CefBrowserApp/CefViewBrowserApp.cpp | 84 ++++-- .../CefBrowserApp/CefViewBrowserClient.cpp | 24 +- ...efViewBrowserClient_ContextMenuHandler.cpp | 8 +- .../CefViewBrowserClient_DialogHandler.cpp | 44 ++- .../CefViewBrowserClient_DisplayHandler.cpp | 8 +- .../CefViewBrowserClient_DownloadHandler.cpp | 6 +- .../CefViewBrowserClient_DragHandler.cpp | 8 +- .../CefViewBrowserClient_FindHandler.cpp | 20 ++ .../CefViewBrowserClient_FocusHandler.cpp | 8 +- .../CefViewBrowserClient_JSDialogHandler.cpp | 8 +- .../CefViewBrowserClient_KeyboardHandler.cpp | 8 +- .../CefViewBrowserClient_LifeSpanHandler.cpp | 21 +- .../CefViewBrowserClient_LoadHandler.cpp | 9 +- .../CefViewBrowserClient_RenderHandler.cpp | 8 +- .../CefViewBrowserClient_RequestHandler.cpp | 8 +- ...ewBrowserClient_ResourceRequestHandler.cpp | 11 +- .../CefViewQueryHandler.cpp | 51 ++-- .../CefViewQueryHandler.h | 72 ++--- .../CefViewSchemeHandler.cpp | 91 ++++++ .../CefViewSchemeHandler.h | 65 +++++ .../CefViewSchemeHandlerFactory.cpp | 35 +++ .../CefViewSchemeHandlerFactory.h | 52 ++++ .../CefViewDefaultSchemeHandler.cpp | 119 -------- .../CefViewDefaultSchemeHandler.h | 65 ----- .../{CefRenderApp => App}/CefViewAppBase.cpp | 40 ++- src/CefWing/App/CefViewAppBase.h | 67 +++++ src/CefWing/App/CefViewOtherApp.cpp | 6 + .../{CefRenderApp => App}/CefViewOtherApp.h | 9 +- src/CefWing/App/CefViewRenderApp.cpp | 212 ++++++++++++++ .../CefViewRenderApp.h} | 150 ++++++---- .../CefViewBridgeObject.cpp} | 87 +++--- .../CefViewBridgeObject.h} | 27 +- src/CefWing/CefRenderApp/CefViewAppBase.h | 44 --- src/CefWing/CefRenderApp/CefViewOtherApp.cpp | 12 - src/CefWing/CefRenderApp/CefViewRenderApp.cpp | 133 --------- src/CefWing/CefRenderApp/CefViewRenderApp.h | 262 ------------------ .../CefViewDefaultRenderDelegate.cpp | 163 ----------- src/CefWing/linux/main.cpp | 26 +- src/CefWing/mac/main.mm | 23 +- src/CefWing/win/main.cpp | 26 +- win-arm64-gen.bat | 1 - win-x86-gen.bat | 1 - win-x86_64-gen.bat | 1 - 69 files changed, 1401 insertions(+), 1285 deletions(-) create mode 100644 CMakePresets.json create mode 100644 gen-linux-arm.sh create mode 100644 gen-linux-arm64.sh create mode 100644 gen-linux-x64.sh create mode 100644 gen-linux-x86.sh create mode 100644 gen-mac-arm64.sh create mode 100644 gen-mac-x64.sh create mode 100644 gen-win-arm64.bat create mode 100644 gen-win-x64.bat create mode 100644 gen-win-x86.bat create mode 100644 include/CefViewCoreProtocol.h.in delete mode 100644 include/CefViewWingProcessName.h.in delete mode 100755 linux-arm-gen.sh delete mode 100755 linux-arm64-gen.sh delete mode 100755 linux-x86-gen.sh delete mode 100755 linux-x86_64-gen.sh delete mode 100755 mac-arm64-gen.sh delete mode 100755 mac-x86_64-gen.sh create mode 100644 src/CefView/CefBrowserApp/CefViewBrowserClient_FindHandler.cpp rename src/CefView/CefBrowserApp/{ => CefViewQueryHandler}/CefViewQueryHandler.cpp (97%) rename {include => src/CefView/CefBrowserApp/CefViewQueryHandler}/CefViewQueryHandler.h (88%) create mode 100644 src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.cpp create mode 100644 src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.h create mode 100644 src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.cpp create mode 100644 src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.h delete mode 100644 src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.cpp delete mode 100644 src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.h rename src/CefWing/{CefRenderApp => App}/CefViewAppBase.cpp (61%) create mode 100644 src/CefWing/App/CefViewAppBase.h create mode 100644 src/CefWing/App/CefViewOtherApp.cpp rename src/CefWing/{CefRenderApp => App}/CefViewOtherApp.h (75%) create mode 100644 src/CefWing/App/CefViewRenderApp.cpp rename src/CefWing/{CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.h => App/CefViewRenderApp.h} (50%) rename src/CefWing/{CefRenderApp/RenderDelegates/CefViewClient.cpp => Bridge/CefViewBridgeObject.cpp} (81%) rename src/CefWing/{CefRenderApp/RenderDelegates/CefViewClient.h => Bridge/CefViewBridgeObject.h} (91%) delete mode 100644 src/CefWing/CefRenderApp/CefViewAppBase.h delete mode 100644 src/CefWing/CefRenderApp/CefViewOtherApp.cpp delete mode 100644 src/CefWing/CefRenderApp/CefViewRenderApp.cpp delete mode 100644 src/CefWing/CefRenderApp/CefViewRenderApp.h delete mode 100644 src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.cpp delete mode 100644 win-arm64-gen.bat delete mode 100644 win-x86-gen.bat delete mode 100644 win-x86_64-gen.bat diff --git a/CMakeLists.txt b/CMakeLists.txt index be58945..b813e0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,8 +86,8 @@ if(NOT CEFVIEW_WING_NAME) endif() configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/include/CefViewWingProcessName.h.in" - "${CMAKE_CURRENT_SOURCE_DIR}/include/CefViewWingProcessName.h" + "${CMAKE_CURRENT_SOURCE_DIR}/include/CefViewCoreProtocol.h.in" + "${CMAKE_CURRENT_SOURCE_DIR}/include/CefViewCoreProtocol.h" @ONLY ) diff --git a/CMakePresets.json b/CMakePresets.json new file mode 100644 index 0000000..ccf03d0 --- /dev/null +++ b/CMakePresets.json @@ -0,0 +1,130 @@ +{ + "version": 6, + "cmakeMinimumRequired": { + "major": 3, + "minor": 20, + "patch": 0 + }, + "include": [], + "configurePresets": [ + { + "name": "default", + "hidden": true + }, + { + "name": "win", + "hidden": true, + "inherits": "default" + }, + { + "name": "win-x86", + "inherits": "win", + "displayName": "Windows x86 (Debug & Release)", + "binaryDir": "${sourceDir}/.build/win.x86", + "architecture": "Win32", + "cacheVariables": { + "PROJECT_ARCH": "x86", + "USE_SANDBOX": "ON" + } + }, + { + "name": "win-x64", + "inherits": "win", + "displayName": "Windows x64 (Debug & Release)", + "binaryDir": "${sourceDir}/.build/win.x64", + "architecture": "x64", + "cacheVariables": { + "PROJECT_ARCH": "x86_64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "win-arm64", + "inherits": "win", + "displayName": "Windows arm64 (Debug & Release)", + "binaryDir": "${sourceDir}/.build/win.arm64", + "architecture": "ARM64", + "cacheVariables": { + "PROJECT_ARCH": "arm64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "osx", + "hidden": true, + "generator": "Xcode", + "inherits": "default" + }, + { + "name": "osx-x64", + "inherits": "osx", + "displayName": "macOS x64 (Debug & Release)", + "binaryDir": "${sourceDir}/.build/osx.x64", + "architecture": "x64", + "cacheVariables": { + "PROJECT_ARCH": "x86_64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "osx-arm64", + "inherits": "osx", + "displayName": "macOS arm64 (Debug & Release)", + "binaryDir": "${sourceDir}/.build/osx.arm64", + "architecture": "arm64", + "cacheVariables": { + "PROJECT_ARCH": "arm64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "linux", + "hidden": true, + "inherits": "default" + }, + { + "name": "linux-x64", + "inherits": "linux", + "displayName": "Linux x64", + "binaryDir": "${sourceDir}/.build/linux.x64", + "architecture": "x64", + "cacheVariables": { + "PROJECT_ARCH": "x86_64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "linux-arm64", + "inherits": "linux", + "displayName": "Linux arm64", + "binaryDir": "${sourceDir}/.build/linux.arm64", + "architecture": "arm64", + "cacheVariables": { + "PROJECT_ARCH": "arm64", + "USE_SANDBOX": "ON" + } + }, + { + "name": "linux-x86", + "inherits": "linux", + "displayName": "Linux x86", + "binaryDir": "${sourceDir}/.build/linux.x86", + "architecture": "x86", + "cacheVariables": { + "PROJECT_ARCH": "x86", + "USE_SANDBOX": "ON" + } + }, + { + "name": "linux-arm", + "inherits": "linux", + "displayName": "Linux arm", + "binaryDir": "${sourceDir}/.build/linux.arm", + "architecture": "arm", + "cacheVariables": { + "PROJECT_ARCH": "arm", + "USE_SANDBOX": "ON" + } + } + ] +} \ No newline at end of file diff --git a/gen-linux-arm.sh b/gen-linux-arm.sh new file mode 100644 index 0000000..0c3df8c --- /dev/null +++ b/gen-linux-arm.sh @@ -0,0 +1 @@ +cmake --preset=linux-arm %* \ No newline at end of file diff --git a/gen-linux-arm64.sh b/gen-linux-arm64.sh new file mode 100644 index 0000000..b9fa0e6 --- /dev/null +++ b/gen-linux-arm64.sh @@ -0,0 +1 @@ +cmake --preset=linux-arm64 %* \ No newline at end of file diff --git a/gen-linux-x64.sh b/gen-linux-x64.sh new file mode 100644 index 0000000..b920be5 --- /dev/null +++ b/gen-linux-x64.sh @@ -0,0 +1 @@ +cmake --preset=linux-x64 %* \ No newline at end of file diff --git a/gen-linux-x86.sh b/gen-linux-x86.sh new file mode 100644 index 0000000..a9a07a3 --- /dev/null +++ b/gen-linux-x86.sh @@ -0,0 +1 @@ +cmake --preset=linux-x86 %* \ No newline at end of file diff --git a/gen-mac-arm64.sh b/gen-mac-arm64.sh new file mode 100644 index 0000000..3c8c266 --- /dev/null +++ b/gen-mac-arm64.sh @@ -0,0 +1 @@ +cmake --preset=osx-arm64 %* \ No newline at end of file diff --git a/gen-mac-x64.sh b/gen-mac-x64.sh new file mode 100644 index 0000000..7cce468 --- /dev/null +++ b/gen-mac-x64.sh @@ -0,0 +1 @@ +cmake --preset=osx-x64 %* \ No newline at end of file diff --git a/gen-win-arm64.bat b/gen-win-arm64.bat new file mode 100644 index 0000000..c13996c --- /dev/null +++ b/gen-win-arm64.bat @@ -0,0 +1 @@ +cmake --preset=win-arm64 %* \ No newline at end of file diff --git a/gen-win-x64.bat b/gen-win-x64.bat new file mode 100644 index 0000000..9d4ca9a --- /dev/null +++ b/gen-win-x64.bat @@ -0,0 +1 @@ +cmake --preset=win-x64 %* \ No newline at end of file diff --git a/gen-win-x86.bat b/gen-win-x86.bat new file mode 100644 index 0000000..433038f --- /dev/null +++ b/gen-win-x86.bat @@ -0,0 +1 @@ +cmake --preset=win-x86 %* \ No newline at end of file diff --git a/include/CefViewBrowserApp.h b/include/CefViewBrowserApp.h index 7bb7ac4..96bc5bc 100644 --- a/include/CefViewBrowserApp.h +++ b/include/CefViewBrowserApp.h @@ -1,6 +1,6 @@ // // CefViewBrowserApp.h -// CefView +// CefViewCore // // Created by Sheen Tian on 2020/6/11. // @@ -9,17 +9,14 @@ #define CefViewBrowserApp_h #pragma once -#pragma region std_headers -#include +#pragma region stl_headers #include -#include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#include +#pragma endregion +#include #include +#include class CefViewBrowserApp : public CefApp @@ -29,32 +26,30 @@ class CefViewBrowserApp private: // The name of the bridge object + std::string builtin_scheme_name_; std::string bridge_object_name_; - std::unordered_set client_set_; + std::unordered_map client_handler_map_; // The app delegate CefViewBrowserAppDelegateInterface::WeakPtr app_delegate_; public: - CefViewBrowserApp(const std::string& bridge_name, CefViewBrowserAppDelegateInterface::RefPtr delegate); + CefViewBrowserApp(const std::string& scheme_name, + const std::string& bridge_name, + CefViewBrowserAppDelegateInterface::RefPtr delegate); ~CefViewBrowserApp(); - void CheckInClient(void* ctx); + void CheckInClient(void* ctx, const CefViewBrowserClientDelegateInterface::RefPtr& handler); void CheckOutClient(void* ctx); + CefViewBrowserClientDelegateInterface::RefPtr GetClientHandler(void* ctx); + bool IsSafeToExit(); private: - // Registers custom schemes handler factories - static void RegisterCustomSchemesHandlerFactories(); - - // Registers custom schemes. Implemented in client_app_delegates. - static void RegisterCustomSchemes(CefRawPtr registrar); - #pragma region CefApp - ////////////////////////////////////////////////////////////////////////// // CefApp methods: virtual void OnBeforeCommandLineProcessing(const CefString& process_type, @@ -62,12 +57,16 @@ class CefViewBrowserApp virtual void OnRegisterCustomSchemes(CefRawPtr registrar) override; + virtual CefRefPtr GetResourceBundleHandler() override; + virtual CefRefPtr GetBrowserProcessHandler() override; -#pragma endregion CefApp + virtual CefRefPtr GetRenderProcessHandler() override; -#pragma region CefBrowserProcessHandler +#pragma endregion +#pragma region CefBrowserProcessHandler + ////////////////////////////////////////////////////////////////////////// // CefBrowserProcessHandler methods: virtual void OnContextInitialized() override; @@ -75,7 +74,9 @@ class CefViewBrowserApp virtual void OnScheduleMessagePumpWork(int64_t delay_ms) override; -#pragma endregion CefBrowserProcessHandler + CefRefPtr GetDefaultClient() override; + +#pragma endregion }; #endif diff --git a/include/CefViewBrowserAppDelegate.h b/include/CefViewBrowserAppDelegate.h index e172076..1d81897 100644 --- a/include/CefViewBrowserAppDelegate.h +++ b/include/CefViewBrowserAppDelegate.h @@ -1,6 +1,6 @@ // // CefViewBrowserAppDelegate.h -// CefView +// CefViewCore // // Created by Sheen Tian on 2020/6/11. // @@ -9,10 +9,12 @@ #define CefViewBrowserAppDelegate_h #pragma once +#pragma region stl_headers #include #include +#pragma endregion -#include +#include /// /// diff --git a/include/CefViewBrowserClient.h b/include/CefViewBrowserClient.h index 01156b5..ca27d02 100644 --- a/include/CefViewBrowserClient.h +++ b/include/CefViewBrowserClient.h @@ -1,6 +1,6 @@ // -// CefViewBrowserHandler.h -// CefView +// CefViewBrowserClient.h +// CefViewCore // // Created by Sheen Tian on 2020/6/11. // @@ -9,27 +9,23 @@ #define CefViewBrowserHandler_h #pragma once -#pragma region std_headers -#include -#include -#include -#include +#pragma region stl_headers #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#include -#include -#pragma endregion cef_headers +#pragma endregion #include #include #include -#include +/// +/// Forward declaration +/// +class CefViewQueryHandler; + +/// +/// +/// class CefViewBrowserClient : public CefClient , public CefContextMenuHandler @@ -37,6 +33,7 @@ class CefViewBrowserClient , public CefDisplayHandler , public CefDownloadHandler , public CefDragHandler + , public CefFindHandler , public CefFocusHandler , public CefJSDialogHandler , public CefKeyboardHandler @@ -49,22 +46,31 @@ class CefViewBrowserClient IMPLEMENT_REFCOUNTING(CefViewBrowserClient); private: + /// + /// + /// + CefRefPtr app_; + + /// + /// + /// + CefViewBrowserClientDelegateInterface::WeakPtr client_delegate_; + + // flags bool is_closing_; bool close_by_native_; bool initial_navigation_; - std::unordered_map> browser_map_; - - CefRefPtr app_; - CefViewBrowserClientDelegateInterface::WeakPtr client_delegate_; + // resource manager + CefRefPtr resource_manager_; // message router CefMessageRouterConfig message_router_config_; - CefRefPtr cefquery_handler_; CefRefPtr message_router_; + CefRefPtr message_router_handler_; - // resource manager - CefRefPtr resource_manager_; + // browser map + std::unordered_map> browser_map_; public: /// @@ -249,17 +255,17 @@ class CefViewBrowserClient // CefDownloadHandler #pragma region CefDownloadHandler virtual CefRefPtr GetDownloadHandler() override; - #if CEF_VERSION_MAJOR < 125 +#if CEF_VERSION_MAJOR < 125 virtual void OnBeforeDownload(CefRefPtr browser, - CefRefPtr download_item, - const CefString& suggested_name, - CefRefPtr callback) override; - #else + CefRefPtr download_item, + const CefString& suggested_name, + CefRefPtr callback) override; +#else virtual bool OnBeforeDownload(CefRefPtr browser, CefRefPtr download_item, const CefString& suggested_name, CefRefPtr callback) override; - #endif +#endif void OnDownloadUpdated(CefRefPtr browser, CefRefPtr download_item, @@ -278,6 +284,17 @@ class CefViewBrowserClient const std::vector& regions) override; #pragma endregion + // CefFindHandler methods +#pragma region CefDragHandler + virtual CefRefPtr GetFindHandler() override; + virtual void OnFindResult(CefRefPtr browser, + int identifier, + int count, + const CefRect& selectionRect, + int activeMatchOrdinal, + bool finalUpdate) override; +#pragma endregion + // CefFocusHandler methods #pragma region CefFocusHandler virtual CefRefPtr GetFocusHandler() override; diff --git a/include/CefViewBrowserClientDelegate.h b/include/CefViewBrowserClientDelegate.h index e7f7792..a2c571b 100644 --- a/include/CefViewBrowserClientDelegate.h +++ b/include/CefViewBrowserClientDelegate.h @@ -1,6 +1,6 @@ // // CefViewBrowserHandlerDelegate.h -// CefView +// CefViewCore // // Created by Sheen Tian on 2020/6/11. // @@ -9,11 +9,10 @@ #define CefViewBrowserHandlerDelegate_h #pragma once +#pragma region stl_headers #include #include - -#include -#include +#pragma endregion #include @@ -23,27 +22,70 @@ class CefViewBrowserClientDelegateInterface { public: + /// + /// + /// typedef std::shared_ptr RefPtr; + + /// + /// + /// typedef std::weak_ptr WeakPtr; + /// + /// + /// virtual ~CefViewBrowserClientDelegateInterface() {} - virtual void processUrlRequest(const std::string& url) = 0; - + /// + /// + /// + /// + /// + /// + virtual void processUrlRequest(CefRefPtr& browser, const CefFrameId& frameId, const std::string& url) = 0; + + /// + /// + /// + /// + /// + /// + /// virtual void processQueryRequest(CefRefPtr& browser, const CefFrameId& frameId, const std::string& query, const int64_t query_id) = 0; + /// + /// + /// + /// + /// + /// virtual void focusedEditableNodeChanged(CefRefPtr& browser, const CefFrameId& frameId, bool focusOnEditableNode) = 0; + /// + /// + /// + /// + /// + /// + /// virtual void invokeMethodNotify(CefRefPtr& browser, const CefFrameId& frameId, const std::string& method, const CefRefPtr& arguments) = 0; + /// + /// + /// + /// + /// + /// + /// virtual void reportJSResult(CefRefPtr& browser, const CefFrameId& frameId, const std::string& context, @@ -216,14 +258,14 @@ class CefViewBrowserClientDelegateInterface void* shared_handle) { } - #else +#else virtual void onAcceleratedPaint(CefRefPtr browser, CefRenderHandler::PaintElementType type, const CefRenderHandler::RectList& dirtyRects, const CefAcceleratedPaintInfo& info) { } - #endif +#endif virtual bool startDragging(CefRefPtr browser, CefRefPtr drag_data, CefRenderHandler::DragOperationsMask allowed_ops, diff --git a/include/CefViewCoreGlobal.h b/include/CefViewCoreGlobal.h index c290e89..0557270 100644 --- a/include/CefViewCoreGlobal.h +++ b/include/CefViewCoreGlobal.h @@ -1,17 +1,28 @@ -/* - * File: CefViewCoreGlobal.h - * Project: include - * Created: 30 May 2023 - * Author: Sheen Tian - */ +// +// CefViewCoreGlobal.h +// CefViewCore +// +// Created by Sheen Tian on 2023/5/30. +// + #ifndef CefViewCoreGlobal_h #define CefViewCoreGlobal_h #pragma once +#pragma region stl_headers +#include +#include +#pragma endregion + #pragma region cef_headers #include +#include #include -#pragma endregion cef_headers +#include +#include +#include +#include +#pragma endregion #if CEF_VERSION_MAJOR < 122 using CefFrameId = int64_t; @@ -28,15 +39,6 @@ struct std::hash return std::hash()(k.ToString()); } }; +#endif // CEF_VERSION_MAJOR < 122 -// namespace std { -// static std::string -// to_string(const CefFrameId& k) -//{ -// return k.ToString(); -// } -// } // namespace std - -#endif - -#endif +#endif // CefViewCoreGlobal_h diff --git a/include/CefViewCoreProtocol.h b/include/CefViewCoreProtocol.h index 9e4f8f6..e97b18a 100644 --- a/include/CefViewCoreProtocol.h +++ b/include/CefViewCoreProtocol.h @@ -1,27 +1,42 @@ -#ifndef CefViewCoreProtocol_h +// +// CefViewCoreProtocol.h +// CefViewCore +// +// Created by Sheen Tian on 2020/6/11. +// This file was generated during CMake configuring. +// Do not edit this file directly by manual. +// Edit the CefViewCoreProtocol.h.in and then re-config project with CMake. +// + +#ifndef CefViewCoreProtocol_h #define CefViewCoreProtocol_h #pragma once /// -/// +/// CefView Helper Process Name /// -#define kCefViewSchema "CefView" +#define kCefViewRenderProcessName "CefViewWing" /// /// /// -#define kCefViewQueryFuntionName "CefViewQuery" +#define kCefViewQueryFuntionName "cefViewQuery" /// /// /// -#define kCefViewQueryCancelFunctionName "CefViewQueryCancel" +#define kCefViewQueryCancelFunctionName "cefViewQueryCancel" /// /// /// #define kCefViewBridgeObjectNameKey "bridge-obj-name" +/// +/// +/// +#define kCefViewBuiltinSchemeNameKey "builtin-scheme-name" + /// /// /// @@ -32,10 +47,15 @@ /// #define kCefViewDefaultBridgeObjectName "CefViewClient" +/// +/// +/// +#define kCefViewDefaultBuiltinSchemaName "CefView" + /// /// CEFVIEWClient.invokeMethod("method_name", ...) /// -#define kCefViewInvokeMethodFunctionName "invokeMethod" +#define kCefViewInvokeMethodFunctionName "invoke" /// /// CEFVIEWClient.addEventListener(type, listener) diff --git a/include/CefViewCoreProtocol.h.in b/include/CefViewCoreProtocol.h.in new file mode 100644 index 0000000..0a92555 --- /dev/null +++ b/include/CefViewCoreProtocol.h.in @@ -0,0 +1,135 @@ +// +// CefViewCoreProtocol.h +// CefViewCore +// +// Created by Sheen Tian on 2020/6/11. +// This file was generated during CMake configuring. +// Do not edit this file directly by manual. +// Edit the CefViewCoreProtocol.h.in and then re-config project with CMake. +// + +#ifndef CefViewCoreProtocol_h +#define CefViewCoreProtocol_h +#pragma once + +/// +/// CefView Helper Process Name +/// +#define kCefViewRenderProcessName "@CEFVIEW_WING_NAME@" + +/// +/// +/// +#define kCefViewQueryFuntionName "cefViewQuery" + +/// +/// +/// +#define kCefViewQueryCancelFunctionName "cefViewQueryCancel" + +/// +/// +/// +#define kCefViewBridgeObjectNameKey "bridge-obj-name" + +/// +/// +/// +#define kCefViewBuiltinSchemeNameKey "builtin-scheme-name" + +/// +/// +/// +#define kCefViewWindowsJobNameKey "windows-job-name" + +/// +/// +/// +#define kCefViewDefaultBridgeObjectName "CefViewClient" + +/// +/// +/// +#define kCefViewDefaultBuiltinSchemaName "CefView" + +/// +/// CEFVIEWClient.invokeMethod("method_name", ...) +/// +#define kCefViewInvokeMethodFunctionName "invoke" + +/// +/// CEFVIEWClient.addEventListener(type, listener) +/// +#define kCefViewAddEventListenerFunctionName "addEventListener" + +/// +/// CEFVIEWClient.removeEventListener(type, listener) +/// +#define kCefViewRemoveEventListenerFunctionName "removeEventListener" + +/// +/// this message is sent from render process to browser process +/// and is processed in the Qt UI thread +/// +/// +#define kCefViewClientRenderFocusedNodeChangedMessage "CefViewClientRender.FocusedNodeChanged" + +/// +/// this message is sent from render process to browser process +/// and is processed in the Qt UI thread +/// +/// +#define kCefViewClientRenderReportJSResultMessage "CefViewClientRender.ReportJSResult" + +/// +/// this message is sent from render process to browser process +/// and is processed in the Qt UI thread +/// +/// format +/// msg.name +/// msg.arg[0]: function name +/// msg.arg[1~...]: function parameters +/// +#define kCefViewClientRenderInvokeMethodMessage "CefViewClientRender.InvokeMethod" + +/// +/// this message is sent from browser process to render process +/// and is processed in the CefRenderer_Main thread +/// +/// format: +/// msg.name: +/// msg.arg[0]: function name +/// msg.arg[1~...]: function parameters +/// +#define kCefViewClientBrowserTriggerEventMessage "CefViewClientBrowser.TriggerEvent" + +/// +/// window.__cefview_report_js_result__(context, result) +/// context must be double +/// +#define kCefViewReportJSResultFunctionName "__cefview_report_js_result__" + +/// +/// +/// +#define kCefViewResourceDirectoryName "resources" + +/// +/// +/// +#define kCefViewLocalesDirectoryName "locales" + +/// +/// +/// +#if defined(OS_WINDOWS) +#define kCefViewDefaultUserAgent "CefView/1.0 (Windows; en-us)" +#elif defined(OS_MACOS) +#define kCefViewDefaultUserAgent "CefView/1.0 (macOS; en-us)" +#elif defined(OS_LINUX) +#define kCefViewDefaultUserAgent "CefView/1.0 (Linux; en-us)" +#else +#define kCefViewDefaultUserAgent "CefView/1.0 (Unknown; en-us)" +#endif + +#endif diff --git a/include/CefViewWingProcessName.h.in b/include/CefViewWingProcessName.h.in deleted file mode 100644 index f0bddfe..0000000 --- a/include/CefViewWingProcessName.h.in +++ /dev/null @@ -1,19 +0,0 @@ -/* - * File: CefViewWingProcessName.h.in - * Project: include - * Created: 23rd November 2023 - * Author: Sheen Tian - * This file was generated by CMake, do not modify this manually - */ -#ifndef CefViewWingProcessName_h -#define CefViewWingProcessName_h -#pragma once - -/* clang-format off */ -/// -/// CefView Helper Process Name -/// -#define kCefViewRenderProcessName "@CEFVIEW_WING_NAME@" -/* clang-format on */ - -#endif diff --git a/linux-arm-gen.sh b/linux-arm-gen.sh deleted file mode 100755 index 2f5b71d..0000000 --- a/linux-arm-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Unix Makefiles" -S . -B .build/linux.arm -DPROJECT_ARCH=arm -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/linux-arm64-gen.sh b/linux-arm64-gen.sh deleted file mode 100755 index 7b82ff8..0000000 --- a/linux-arm64-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Unix Makefiles" -S . -B .build/linux.arm64 -DPROJECT_ARCH=arm64 -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/linux-x86-gen.sh b/linux-x86-gen.sh deleted file mode 100755 index fabb7fa..0000000 --- a/linux-x86-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Unix Makefiles" -S . -B .build/linux.x86 -DPROJECT_ARCH=x86 -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/linux-x86_64-gen.sh b/linux-x86_64-gen.sh deleted file mode 100755 index 7f7dfd7..0000000 --- a/linux-x86_64-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Unix Makefiles" -S . -B .build/linux.x86_64 -DPROJECT_ARCH=x86_64 -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/mac-arm64-gen.sh b/mac-arm64-gen.sh deleted file mode 100755 index 9ec2f73..0000000 --- a/mac-arm64-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Xcode" -S . -B .build/mac.arm64 -DPROJECT_ARCH=arm64 -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/mac-x86_64-gen.sh b/mac-x86_64-gen.sh deleted file mode 100755 index fc9bdda..0000000 --- a/mac-x86_64-gen.sh +++ /dev/null @@ -1 +0,0 @@ -cmake -G "Xcode" -S . -B .build/mac.x86_64 -DPROJECT_ARCH=x86_64 -DUSE_SANDBOX=ON $* \ No newline at end of file diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cc6dd6c..36fbd52 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -146,8 +146,10 @@ endif() # OS_MACOS # ############################################################################################### # soruce code for all platforms file(GLOB_RECURSE CefViewWing_SRC_FILES - "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/CefRenderApp/*.h" - "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/CefRenderApp/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/App/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/App/*.cpp" + "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/Bridge/*.h" + "${CMAKE_CURRENT_SOURCE_DIR}/CefWing/Bridge/*.cpp" ) source_group( TREE "${CMAKE_CURRENT_SOURCE_DIR}/CefWing" diff --git a/src/CefView/CefBrowserApp/CefViewBrowserApp.cpp b/src/CefView/CefBrowserApp/CefViewBrowserApp.cpp index e941f17..3f6bc7d 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserApp.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserApp.cpp @@ -1,25 +1,24 @@ #include -#pragma region std_headers +#pragma region stl_headers #include -#pragma endregion std_headers +#include +#pragma endregion -#pragma region cef_headers -#include -#include -#include -#pragma endregion cef_headers - -#include "Common/CefViewCoreLog.h" -#include "SchemeHandlers/CefViewDefaultSchemeHandler.h" +#include #include -CefViewBrowserApp::CefViewBrowserApp(const std::string& bridge_name, +#include "CefViewSchemeHandler/CefViewSchemeHandlerFactory.h" + +CefViewBrowserApp::CefViewBrowserApp(const std::string& scheme_name, + const std::string& bridge_name, CefViewBrowserAppDelegateInterface::RefPtr delegate) - : bridge_object_name_(bridge_name) + : builtin_scheme_name_(scheme_name.empty() ? kCefViewDefaultBuiltinSchemaName : scheme_name) + , bridge_object_name_(bridge_name.empty() ? kCefViewDefaultBridgeObjectName : bridge_name) , app_delegate_(delegate) -{} +{ +} CefViewBrowserApp::~CefViewBrowserApp() { @@ -27,33 +26,30 @@ CefViewBrowserApp::~CefViewBrowserApp() } void -CefViewBrowserApp::CheckInClient(void* ctx) +CefViewBrowserApp::CheckInClient(void* ctx, const CefViewBrowserClientDelegateInterface::RefPtr& handler) { - client_set_.insert(ctx); + client_handler_map_[ctx] = handler; } void CefViewBrowserApp::CheckOutClient(void* ctx) { - client_set_.erase(ctx); + client_handler_map_.erase(ctx); } -bool -CefViewBrowserApp::IsSafeToExit() +CefViewBrowserClientDelegateInterface::RefPtr +CefViewBrowserApp::GetClientHandler(void* ctx) { - return client_set_.empty(); + if (client_handler_map_.count(ctx)) { + return client_handler_map_[ctx].lock(); + } + return nullptr; } -void -CefViewBrowserApp::RegisterCustomSchemesHandlerFactories() -{ - CefViewDefaultSchemeHandler::RegisterSchemeHandlerFactory(); -} - -void -CefViewBrowserApp::RegisterCustomSchemes(CefRawPtr registrar) +bool +CefViewBrowserApp::IsSafeToExit() { - CefViewDefaultSchemeHandler::RegisterScheme(registrar); + return client_handler_map_.empty(); } ////////////////////////////////////////////////////////////////////////// @@ -67,7 +63,18 @@ CefViewBrowserApp::OnBeforeCommandLineProcessing(const CefString& process_type, void CefViewBrowserApp::OnRegisterCustomSchemes(CefRawPtr registrar) -{} +{ + if (registrar) { + // register custom scheme + registrar->AddCustomScheme(builtin_scheme_name_, 0); + } +} + +CefRefPtr +CefViewBrowserApp::GetResourceBundleHandler() +{ + return nullptr; +} CefRefPtr CefViewBrowserApp::GetBrowserProcessHandler() @@ -75,11 +82,20 @@ CefViewBrowserApp::GetBrowserProcessHandler() return this; } +CefRefPtr +CefViewBrowserApp::GetRenderProcessHandler() +{ + return nullptr; +} + ////////////////////////////////////////////////////////////////////////// void CefViewBrowserApp::OnContextInitialized() { CEF_REQUIRE_UI_THREAD(); + + // register custom scheme and handler + CefRegisterSchemeHandlerFactory(builtin_scheme_name_, "", new CefViewSchemeHandlerFactory(this)); } void @@ -92,6 +108,10 @@ CefViewBrowserApp::OnBeforeChildProcessLaunch(CefRefPtr command_ if (bridge_object_name_.empty()) bridge_object_name_ = kCefViewDefaultBridgeObjectName; command_line->AppendSwitchWithValue(kCefViewBridgeObjectNameKey, bridge_object_name_); + + if (builtin_scheme_name_.empty()) + builtin_scheme_name_ = kCefViewBuiltinSchemeNameKey; + command_line->AppendSwitchWithValue(kCefViewBuiltinSchemeNameKey, bridge_object_name_); } void @@ -102,3 +122,9 @@ CefViewBrowserApp::OnScheduleMessagePumpWork(int64_t delay_ms) if (delegate) delegate->onScheduleMessageLoopWork(delay_ms); } + +CefRefPtr +CefViewBrowserApp::GetDefaultClient() +{ + return nullptr; +} diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient.cpp index 49107a2..38b5256 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient.cpp @@ -1,17 +1,16 @@ -#include +#include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers +#pragma endregion -#pragma region cef_headers -#include -#pragma endregion cef_headers +#include #include -#include + +#include "CefViewQueryHandler/CefViewQueryHandler.h" #if CEF_VERSION_MAJOR < 122 const CefFrameId CefViewBrowserClient::MAIN_FRAME = 0; @@ -28,11 +27,11 @@ CefViewBrowserClient::CefViewBrowserClient(CefRefPtr app, , initial_navigation_(true) , app_(app) , client_delegate_(delegate) - , cefquery_handler_(nullptr) , message_router_(nullptr) + , message_router_handler_(nullptr) , resource_manager_(new CefResourceManager()) { - app_->CheckInClient(this); + app_->CheckInClient(this, delegate); // Create the browser-side router for query handling. message_router_config_.js_query_function = kCefViewQueryFuntionName; @@ -42,6 +41,7 @@ CefViewBrowserClient::CefViewBrowserClient(CefRefPtr app, CefViewBrowserClient::~CefViewBrowserClient() { log_debug("CefViewBrowserClient::~CefViewBrowserClient()"); + app_->CheckOutClient(this); } @@ -111,7 +111,7 @@ CefViewBrowserClient::TriggerEvent(CefRefPtr browser, #else auto frame = browser->GetFrame(id); #endif - + frame->SendProcessMessage(PID_RENDERER, m); } @@ -124,8 +124,8 @@ CefViewBrowserClient::TriggerEvent(CefRefPtr browser, bool CefViewBrowserClient::ResponseQuery(const int64_t query, bool success, const CefString& response, int error) { - if (cefquery_handler_) - return cefquery_handler_->Response(query, success, response, error); + if (message_router_handler_) + return message_router_handler_->Response(query, success, response, error); return false; } diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_ContextMenuHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_ContextMenuHandler.cpp index 037a40f..a03c442 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_ContextMenuHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_ContextMenuHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_DialogHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_DialogHandler.cpp index 3243273..86bc04c 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_DialogHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_DialogHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include @@ -21,30 +17,30 @@ CefViewBrowserClient::GetDialogHandler() #if CEF_VERSION_MAJOR < 102 bool CefViewBrowserClient::OnFileDialog(CefRefPtr browser, - FileDialogMode mode, - const CefString& title, - const CefString& default_file_path, - const std::vector& accept_filters, - int selected_accept_filter, - CefRefPtr callback) + FileDialogMode mode, + const CefString& title, + const CefString& default_file_path, + const std::vector& accept_filters, + int selected_accept_filter, + CefRefPtr callback) #elif CEF_VERSION_MAJOR < 126 bool CefViewBrowserClient::OnFileDialog(CefRefPtr browser, - FileDialogMode mode, - const CefString& title, - const CefString& default_file_path, - const std::vector& accept_filters, - CefRefPtr callback) + FileDialogMode mode, + const CefString& title, + const CefString& default_file_path, + const std::vector& accept_filters, + CefRefPtr callback) #else bool CefViewBrowserClient::OnFileDialog(CefRefPtr browser, - FileDialogMode mode, - const CefString& title, - const CefString& default_file_path, - const std::vector& accept_filters, - const std::vector& accept_extensions, - const std::vector& accept_descriptions, - CefRefPtr callback) + FileDialogMode mode, + const CefString& title, + const CefString& default_file_path, + const std::vector& accept_filters, + const std::vector& accept_extensions, + const std::vector& accept_descriptions, + CefRefPtr callback) #endif { auto delegate = client_delegate_.lock(); diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_DisplayHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_DisplayHandler.cpp index 85d818b..b4a7431 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_DisplayHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_DisplayHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_DownloadHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_DownloadHandler.cpp index f442a51..66e162f 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_DownloadHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_DownloadHandler.cpp @@ -1,10 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers +#pragma endregion #include @@ -40,7 +40,7 @@ CefViewBrowserClient::OnBeforeDownload(CefRefPtr browser, if (delegate) delegate->onBeforeDownload(browser, download_item, suggested_name, callback); - return true; + return false; } #endif diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_DragHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_DragHandler.cpp index 32388db..c663d29 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_DragHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_DragHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_FindHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_FindHandler.cpp new file mode 100644 index 0000000..4f3d788 --- /dev/null +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_FindHandler.cpp @@ -0,0 +1,20 @@ +#include + +#include + +CefRefPtr +CefViewBrowserClient::GetFindHandler() +{ + return this; +} + +void +CefViewBrowserClient::OnFindResult(CefRefPtr browser, + int identifier, + int count, + const CefRect& selectionRect, + int activeMatchOrdinal, + bool finalUpdate) +{ + CEF_REQUIRE_UI_THREAD(); +} diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_FocusHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_FocusHandler.cpp index 28ec1f9..d0b045e 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_FocusHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_FocusHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_JSDialogHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_JSDialogHandler.cpp index 36d3798..2180857 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_JSDialogHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_JSDialogHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_KeyboardHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_KeyboardHandler.cpp index 3f4746a..a0f0e71 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_KeyboardHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_KeyboardHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_LifeSpanHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_LifeSpanHandler.cpp index 177f26d..8045135 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_LifeSpanHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_LifeSpanHandler.cpp @@ -1,17 +1,15 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include +#include "CefViewQueryHandler/CefViewQueryHandler.h" + CefRefPtr CefViewBrowserClient::GetLifeSpanHandler() { @@ -73,8 +71,8 @@ CefViewBrowserClient::OnAfterCreated(CefRefPtr browser) if (browser_map_.empty()) { message_router_ = CefMessageRouterBrowserSide::Create(message_router_config_); - cefquery_handler_ = new CefViewQueryHandler(client_delegate_); - message_router_->AddHandler(cefquery_handler_.get(), false); + message_router_handler_ = new CefViewQueryHandler(client_delegate_); + message_router_->AddHandler(message_router_handler_.get(), false); } auto delegate = client_delegate_.lock(); @@ -96,8 +94,7 @@ CefViewBrowserClient::DoClose(CefRefPtr browser) } auto delegate = client_delegate_.lock(); - if (delegate) - { + if (delegate) { if (close_by_native_) { // close by native ignoreClose = delegate->doClose(browser); @@ -128,8 +125,8 @@ CefViewBrowserClient::OnBeforeClose(CefRefPtr browser) browser_map_.erase(browser->GetIdentifier()); if (browser_map_.empty()) { - message_router_->RemoveHandler(cefquery_handler_.get()); + message_router_->RemoveHandler(message_router_handler_.get()); message_router_ = nullptr; - cefquery_handler_ = nullptr; + message_router_handler_ = nullptr; } } diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_LoadHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_LoadHandler.cpp index f2cfc17..84773ab 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_LoadHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_LoadHandler.cpp @@ -1,15 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_RenderHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_RenderHandler.cpp index 3959a78..9369033 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_RenderHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_RenderHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_RequestHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_RequestHandler.cpp index 256fb32..741c40d 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_RequestHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_RequestHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include diff --git a/src/CefView/CefBrowserApp/CefViewBrowserClient_ResourceRequestHandler.cpp b/src/CefView/CefBrowserApp/CefViewBrowserClient_ResourceRequestHandler.cpp index 6d6adaf..5228cfe 100644 --- a/src/CefView/CefBrowserApp/CefViewBrowserClient_ResourceRequestHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewBrowserClient_ResourceRequestHandler.cpp @@ -1,14 +1,10 @@ #include -#pragma region std_headers +#pragma region stl_headers #include #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers +#pragma endregion #include @@ -53,4 +49,5 @@ CefViewBrowserClient::OnProtocolExecution(CefRefPtr browser, CefRefPtr frame, CefRefPtr request, bool& allow_os_execution) -{} +{ +} diff --git a/src/CefView/CefBrowserApp/CefViewQueryHandler.cpp b/src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.cpp similarity index 97% rename from src/CefView/CefBrowserApp/CefViewQueryHandler.cpp rename to src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.cpp index db46a39..4bdc5a5 100644 --- a/src/CefView/CefBrowserApp/CefViewQueryHandler.cpp +++ b/src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.cpp @@ -1,11 +1,35 @@ -#include +#include "CefViewQueryHandler.h" CefViewQueryHandler::CefViewQueryHandler(CefViewBrowserClientDelegateInterface::WeakPtr delegate) : handler_delegate_(delegate) -{} +{ +} CefViewQueryHandler::~CefViewQueryHandler() {} +bool +CefViewQueryHandler::Response(int64_t query, bool success, const CefString& response, int error) +{ + CefRefPtr cb; + mtxCallbackMap_.lock(); + auto it = mapCallback_.find(query); + if (it != mapCallback_.end()) { + cb = it->second; + mapCallback_.erase(it); + } + mtxCallbackMap_.unlock(); + + if (!cb) + return false; + + if (success) + cb->Success(response); + else + cb->Failure(error, response); + + return true; +} + bool CefViewQueryHandler::OnQuery(CefRefPtr browser, CefRefPtr frame, @@ -38,26 +62,3 @@ CefViewQueryHandler::OnQueryCanceled(CefRefPtr browser, CefRefPtr cb; - mtxCallbackMap_.lock(); - auto it = mapCallback_.find(query); - if (it != mapCallback_.end()) { - cb = it->second; - mapCallback_.erase(it); - } - mtxCallbackMap_.unlock(); - - if (!cb) - return false; - - if (success) - cb->Success(response); - else - cb->Failure(error, response); - - return true; -} diff --git a/include/CefViewQueryHandler.h b/src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.h similarity index 88% rename from include/CefViewQueryHandler.h rename to src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.h index c16558c..f397a70 100644 --- a/include/CefViewQueryHandler.h +++ b/src/CefView/CefBrowserApp/CefViewQueryHandler/CefViewQueryHandler.h @@ -9,18 +9,12 @@ #define CefViewQueryHandler_h #pragma once -#pragma region std_headers +#pragma region stl_headers #include #include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#include - -#pragma endregion cef_headers +#pragma endregion +#include #include /// @@ -32,11 +26,45 @@ class CefViewQueryHandler { IMPLEMENT_REFCOUNTING(CefViewQueryHandler); +private: + /// + /// + /// + CefViewBrowserClientDelegateInterface::WeakPtr handler_delegate_; + + /// + /// + /// + std::map> mapCallback_; + + /// + /// + /// + std::mutex mtxCallbackMap_; + public: + /// + /// + /// + /// CefViewQueryHandler(CefViewBrowserClientDelegateInterface::WeakPtr delegate); + /// + /// + /// ~CefViewQueryHandler(); + /// + /// + /// + /// + /// + /// + /// + /// + bool Response(int64_t query, bool success, const CefString& response, int error); + +protected: /// /// /// @@ -61,31 +89,5 @@ class CefViewQueryHandler /// /// virtual void OnQueryCanceled(CefRefPtr browser, CefRefPtr frame, int64_t query_id) override; - - /// - /// - /// - /// - /// - /// - /// - /// - bool Response(int64_t query, bool success, const CefString& response, int error); - -private: - /// - /// - /// - CefViewBrowserClientDelegateInterface::WeakPtr handler_delegate_; - - /// - /// - /// - std::map> mapCallback_; - - /// - /// - /// - std::mutex mtxCallbackMap_; }; #endif diff --git a/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.cpp b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.cpp new file mode 100644 index 0000000..224e9fb --- /dev/null +++ b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.cpp @@ -0,0 +1,91 @@ +#include "CefViewSchemeHandler.h" + +CefViewSchemeHandler::CefViewSchemeHandler(CefRefPtr browser, + CefRefPtr frame, + CefViewBrowserClientDelegateInterface::RefPtr delegate) + : browser_(browser) + , frame_(frame) + , handler_delegate_(delegate) + , offset_(0) +{ +} + +CefViewSchemeHandler::~CefViewSchemeHandler() {} + +bool +CefViewSchemeHandler::Open(CefRefPtr request, bool& handle_request, CefRefPtr callback) +{ + // DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO)); + + handle_request = true; + + auto browserDelegate = handler_delegate_.lock(); + if (browserDelegate) { + CefString cefStrUrl = request->GetURL(); + CefFrameId frameId = frame_->GetIdentifier(); + browserDelegate->processUrlRequest(browser_, frameId, cefStrUrl.ToString()); + } + + data_ = "ok"; + mime_type_ = "text/html"; + + return true; +} + +void +CefViewSchemeHandler::GetResponseHeaders(CefRefPtr response, + int64_t& response_length, + CefString& redirectUrl) +{ + CEF_REQUIRE_IO_THREAD(); + + DCHECK(!data_.empty()); + response->SetMimeType(mime_type_); + response->SetStatus(200); + // Set the resulting response length + response_length = data_.length(); +} + +bool +CefViewSchemeHandler::Skip(int64_t bytes_to_skip, int64_t& bytes_skipped, CefRefPtr callback) +{ + // CEF_REQUIRE_IO_THREAD(); + + bytes_skipped = 0; + + offset_ += bytes_to_skip; + if (offset_ <= (int)data_.length()) { + bytes_skipped = bytes_to_skip; + } else { + offset_ = (int)data_.length(); + bytes_skipped = data_.length() - offset_; + } + + return true; +} + +bool +CefViewSchemeHandler::Read(void* data_out, + int bytes_to_read, + int& bytes_read, + CefRefPtr callback) +{ + // DCHECK(!CefCurrentlyOn(TID_UI) && !CefCurrentlyOn(TID_IO)); + + bytes_read = 0; + if (offset_ < (int)data_.length()) { + // Copy the next block of data into the buffer. + int transfer_size = std::min(bytes_to_read, static_cast(data_.length() - offset_)); + memcpy(data_out, data_.c_str() + offset_, transfer_size); + offset_ += transfer_size; + bytes_read = transfer_size; + } + + return bytes_read > 0; +} + +void +CefViewSchemeHandler::Cancel() +{ + // CEF_REQUIRE_IO_THREAD(); +} diff --git a/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.h b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.h new file mode 100644 index 0000000..41d586c --- /dev/null +++ b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandler.h @@ -0,0 +1,65 @@ +// +// CefViewSchemeHandler.h +// CefView +// +// Created by Sheen Tian on 2024/10/9. +// + +#ifndef CefViewSchemeHandler_h +#define CefViewSchemeHandler_h + +#pragma once + +#include +#include + +class CefViewSchemeHandler : public CefResourceHandler +{ + IMPLEMENT_REFCOUNTING(CefViewSchemeHandler); + +private: + /// + /// + /// + CefRefPtr browser_; + + /// + /// + /// + CefRefPtr frame_; + + /// + /// + /// + CefViewBrowserClientDelegateInterface::WeakPtr handler_delegate_; + + int offset_; + std::string data_; + std::string mime_type_; + +public: + CefViewSchemeHandler(CefRefPtr browser, + CefRefPtr frame, + CefViewBrowserClientDelegateInterface::RefPtr delegate); + + ~CefViewSchemeHandler(); + + virtual bool Open(CefRefPtr request, bool& handle_request, CefRefPtr callback) override; + + virtual void GetResponseHeaders(CefRefPtr response, + int64_t& response_length, + CefString& redirectUrl) override; + + virtual bool Skip(int64_t bytes_to_skip, + int64_t& bytes_skipped, + CefRefPtr callback) override; + + virtual bool Read(void* data_out, + int bytes_to_read, + int& bytes_read, + CefRefPtr callback) override; + + virtual void Cancel() override; +}; + +#endif diff --git a/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.cpp b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.cpp new file mode 100644 index 0000000..39f5ddf --- /dev/null +++ b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.cpp @@ -0,0 +1,35 @@ +#include "CefViewSchemeHandlerFactory.h" + +#include +#include + +#include "CefViewSchemeHandler.h" + +CefViewSchemeHandlerFactory::CefViewSchemeHandlerFactory(CefRefPtr app) + : app_(app) +{ +} + +CefViewSchemeHandlerFactory::~CefViewSchemeHandlerFactory() {} + +CefRefPtr +CefViewSchemeHandlerFactory::Create(CefRefPtr browser, + CefRefPtr frame, + const CefString& scheme_name, + CefRefPtr request) +{ + if (!app_) { + return nullptr; + } + + auto client = browser->GetHost()->GetClient(); + if (!client) { + return nullptr; + } + + auto handler = app_->GetClientHandler(client.get()); + if (!handler) { + return nullptr; + } + return new CefViewSchemeHandler(browser, frame, handler); +} diff --git a/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.h b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.h new file mode 100644 index 0000000..6e2e2b2 --- /dev/null +++ b/src/CefView/CefBrowserApp/CefViewSchemeHandler/CefViewSchemeHandlerFactory.h @@ -0,0 +1,52 @@ +// +// CefViewSchemeHandlerFactory.h +// CefView +// +// Created by Sheen Tian on 2024/10/9. +// + +#ifndef CefViewSchemeHandlerFactory_h +#define CefViewSchemeHandlerFactory_h + +#pragma once + +#include +#include + +class CefViewSchemeHandlerFactory : public CefSchemeHandlerFactory +{ + IMPLEMENT_REFCOUNTING(CefViewSchemeHandlerFactory); + +private: + /// + /// + /// + CefRefPtr app_; + +public: + /// + /// + /// + /// + CefViewSchemeHandlerFactory(CefRefPtr app); + + /// + /// + /// + ~CefViewSchemeHandlerFactory(); + + /// + /// + /// + /// + /// + /// + /// + /// + virtual CefRefPtr Create(CefRefPtr browser, + CefRefPtr frame, + const CefString& scheme_name, + CefRefPtr request) override; +}; + +#endif diff --git a/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.cpp b/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.cpp deleted file mode 100644 index 9aca1e4..0000000 --- a/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "CefViewDefaultSchemeHandler.h" - -#pragma region std_headers -#include -#include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#include -#include -#include -#include -#include -#include -#pragma endregion cef_headers - -#include - -namespace CefViewDefaultSchemeHandler { -////////////////////////////////////////////////////////////////////////// -// handler -SchemeHandler::SchemeHandler(CefViewBrowserClientDelegateInterface::RefPtr delegate) - : browser_delegate_(delegate) - , offset_(0) -{} - -bool -SchemeHandler::Open(CefRefPtr request, bool& handle_request, CefRefPtr callback) -{ - handle_request = true; - - auto browserDelegate = browser_delegate_.lock(); - if (browserDelegate) { - CefString cefStrUrl = request->GetURL(); - browserDelegate->processUrlRequest(cefStrUrl.ToString()); - } - - data_ = "ok"; - mime_type_ = "text/html"; - - return true; -} - -void -SchemeHandler::GetResponseHeaders(CefRefPtr response, int64_t& response_length, CefString& redirectUrl) -{ - CEF_REQUIRE_IO_THREAD(); - DCHECK(!data_.empty()); - response->SetMimeType(mime_type_); - response->SetStatus(200); - // Set the resulting response length - response_length = data_.length(); -} - -bool -SchemeHandler::Skip(int64_t bytes_to_skip, int64_t& bytes_skipped, CefRefPtr callback) -{ - bytes_skipped = 0; - - offset_ += bytes_to_skip; - if (offset_ <= (int)data_.length()) { - bytes_skipped = bytes_to_skip; - } else { - offset_ = (int)data_.length(); - bytes_skipped = data_.length() - offset_; - } - - return true; -} - -bool -SchemeHandler::Read(void* data_out, int bytes_to_read, int& bytes_read, CefRefPtr callback) -{ - bytes_read = 0; - if (offset_ < (int)data_.length()) { - // Copy the next block of data into the buffer. - int transfer_size = std::min(bytes_to_read, static_cast(data_.length() - offset_)); - memcpy(data_out, data_.c_str() + offset_, transfer_size); - offset_ += transfer_size; - bytes_read = transfer_size; - } - - return bytes_read > 0; -} - -void -SchemeHandler::Cancel() -{} - -////////////////////////////////////////////////////////////////////////// -// handler factory -// - -CefRefPtr -SchemeHandlerFactory::Create(CefRefPtr browser, - CefRefPtr frame, - const CefString& scheme_name, - CefRefPtr request) -{ - CefViewBrowserClientDelegateInterface::RefPtr browserDelegate; - return new SchemeHandler(browserDelegate); -} - -////////////////////////////////////////////////////////////////////////// -bool -RegisterScheme(CefRawPtr registrar) -{ - // register default scheme_name - return registrar->AddCustomScheme(kCefViewSchema, 0); -} - -bool -RegisterSchemeHandlerFactory() -{ - return CefRegisterSchemeHandlerFactory(kCefViewSchema, "", new SchemeHandlerFactory()); -} -} // namespace CefViewDefaultSchemeHandler diff --git a/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.h b/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.h deleted file mode 100644 index 2c12875..0000000 --- a/src/CefView/CefBrowserApp/SchemeHandlers/CefViewDefaultSchemeHandler.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#pragma region std_headers -#include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#include -#pragma endregion cef_headers - -#include -#include - -namespace CefViewDefaultSchemeHandler { -bool -RegisterSchemeHandlerFactory(); - -bool -RegisterScheme(CefRawPtr registrar); - -class SchemeHandler : public CefResourceHandler -{ -public: - SchemeHandler(CefViewBrowserClientDelegateInterface::RefPtr delegate); - - virtual bool Open(CefRefPtr request, bool& handle_request, CefRefPtr callback) override; - - virtual void GetResponseHeaders(CefRefPtr response, - int64_t& response_length, - CefString& redirectUrl) override; - - virtual bool Skip(int64_t bytes_to_skip, int64_t& bytes_skipped, CefRefPtr callback) override; - - virtual bool Read(void* data_out, - int bytes_to_read, - int& bytes_read, - CefRefPtr callback) override; - - virtual void Cancel() override; - -private: - CefViewBrowserClientDelegateInterface::WeakPtr browser_delegate_; - std::string data_; - std::string mime_type_; - int offset_; - -private: - IMPLEMENT_REFCOUNTING(SchemeHandler); -}; - -class SchemeHandlerFactory : public CefSchemeHandlerFactory -{ - - // Return a new scheme handler instance to handle the request. - virtual CefRefPtr Create(CefRefPtr browser, - CefRefPtr frame, - const CefString& scheme_name, - CefRefPtr request) override; - -private: - IMPLEMENT_REFCOUNTING(SchemeHandlerFactory); -}; -} // namespace CefViewDefaultSchemeHandler diff --git a/src/CefWing/CefRenderApp/CefViewAppBase.cpp b/src/CefWing/App/CefViewAppBase.cpp similarity index 61% rename from src/CefWing/CefRenderApp/CefViewAppBase.cpp rename to src/CefWing/App/CefViewAppBase.cpp index 36b1577..3d5f449 100644 --- a/src/CefWing/CefRenderApp/CefViewAppBase.cpp +++ b/src/CefWing/App/CefViewAppBase.cpp @@ -1,17 +1,6 @@ -// -// CefWingAppBase.cpp -// CeViewfWing -// -// Created by Sheen Tian on 2020/6/17. -// - -#pragma region project_heasers -#include "CefViewAppBase.h" -#pragma endregion project_heasers - -#pragma region mac_headers +#include "CefViewAppBase.h" + #include -#pragma endregion mac_headers #include @@ -20,7 +9,10 @@ const char kProcessType[] = "type"; const char kZygoteProcess[] = "zygote"; const char kRendererProcess[] = "renderer"; -CefViewAppBase::CefViewAppBase() {} +CefViewAppBase::CefViewAppBase(const std::string& scheme_name) + : builtin_scheme_name_(scheme_name) +{ +} // static CefViewAppBase::ProcessType @@ -52,3 +44,23 @@ CefViewAppBase::GetBridgeObjectName(CefRefPtr command_line) logI("bridge object name: %s", name.c_str()); return name; } + +std::string +CefViewAppBase::GetBuiltinSchemeName(CefRefPtr command_line) +{ + if (!command_line->HasSwitch(kCefViewBuiltinSchemeNameKey)) + return ""; + + const std::string& name = command_line->GetSwitchValue(kCefViewBuiltinSchemeNameKey); + logI("built-in scheme name: %s", name.c_str()); + return name; +} + +void +CefViewAppBase::OnRegisterCustomSchemes(CefRawPtr registrar) +{ + if (registrar) { + registrar->AddCustomScheme(builtin_scheme_name_.empty() ? kCefViewDefaultBuiltinSchemaName : builtin_scheme_name_, + 0); + } +} diff --git a/src/CefWing/App/CefViewAppBase.h b/src/CefWing/App/CefViewAppBase.h new file mode 100644 index 0000000..a7e9728 --- /dev/null +++ b/src/CefWing/App/CefViewAppBase.h @@ -0,0 +1,67 @@ +// +// CefWingAppBase.hpp +// CefViewWing +// +// Created by Sheen Tian on 2020/6/17. +// + +#ifndef CefAppBase_h +#define CefAppBase_h + +#pragma region stl_headers +#include +#include +#pragma endregion + +#pragma region cef_headers +#include +#pragma endregion + +class CefViewAppBase : public CefApp +{ + /// + /// + /// + std::string builtin_scheme_name_; + +public: + CefViewAppBase(const std::string& scheme_name); + + enum ProcessType + { + UnkownProcess, + ZygoteProcess, + RendererProcess, + OtherProcess, + }; + + /// + /// Gets the current process type + /// + /// The command line + /// The process type + static ProcessType GetProcessType(CefRefPtr command_line); + + /// + /// Gets the bridge object name from command line + /// + /// The command line + /// The bridge object name + static std::string GetBridgeObjectName(CefRefPtr command_line); + + /// + /// Gets the built-in scheme name + /// + /// The command line + /// The built-in scheme name + static std::string GetBuiltinSchemeName(CefRefPtr command_line); + +private: + /// + /// + /// + /// + virtual void OnRegisterCustomSchemes(CefRawPtr registrar) override; +}; + +#endif diff --git a/src/CefWing/App/CefViewOtherApp.cpp b/src/CefWing/App/CefViewOtherApp.cpp new file mode 100644 index 0000000..63d3fe4 --- /dev/null +++ b/src/CefWing/App/CefViewOtherApp.cpp @@ -0,0 +1,6 @@ +#include "CefViewOtherApp.h" + +CefViewOtherApp::CefViewOtherApp(const std::string& scheme_name) + : CefViewAppBase(scheme_name) +{ +} diff --git a/src/CefWing/CefRenderApp/CefViewOtherApp.h b/src/CefWing/App/CefViewOtherApp.h similarity index 75% rename from src/CefWing/CefRenderApp/CefViewOtherApp.h rename to src/CefWing/App/CefViewOtherApp.h index e1f7cea..bc6822c 100644 --- a/src/CefWing/CefRenderApp/CefViewOtherApp.h +++ b/src/CefWing/App/CefViewOtherApp.h @@ -9,17 +9,14 @@ #define CefOtherApp_h #pragma once -#pragma region project_heasers #include "CefViewAppBase.h" -#pragma endregion project_heasers class CefViewOtherApp : public CefViewAppBase { -public: - CefViewOtherApp(); - -private: IMPLEMENT_REFCOUNTING(CefViewOtherApp); + +public: + CefViewOtherApp(const std::string& scheme_name); }; #endif diff --git a/src/CefWing/App/CefViewRenderApp.cpp b/src/CefWing/App/CefViewRenderApp.cpp new file mode 100644 index 0000000..67aa482 --- /dev/null +++ b/src/CefWing/App/CefViewRenderApp.cpp @@ -0,0 +1,212 @@ +#include "CefViewRenderApp.h" + +#pragma region stl_headers +#include +#pragma endregion + +#pragma region cef_headers +#include +#include +#include +#pragma endregion + +#include + +#include + +CefViewRenderApp::CefViewRenderApp(const std::string& scheme_name, const std::string& bridge_name) + : CefViewAppBase(scheme_name) + , bridge_object_name_(bridge_name) + , last_node_is_editable_(false) +{ +} + +CefViewRenderApp::~CefViewRenderApp() {} + +////////////////////////////////////////////////////////////////////////// +CefRefPtr +CefViewRenderApp::GetRenderProcessHandler() +{ + return this; +} + +void +CefViewRenderApp::OnWebKitInitialized() +{ + CEF_REQUIRE_RENDERER_THREAD(); + + CefMessageRouterConfig config; + config.js_query_function = kCefViewQueryFuntionName; + config.js_cancel_function = kCefViewQueryCancelFunctionName; + message_router_ = CefMessageRouterRendererSide::Create(config); +} + +void +CefViewRenderApp::OnBrowserCreated(CefRefPtr browser, CefRefPtr extra_info) +{ + CEF_REQUIRE_RENDERER_THREAD(); +} + +void +CefViewRenderApp::OnBrowserDestroyed(CefRefPtr browser) +{ + CEF_REQUIRE_RENDERER_THREAD(); +} + +CefRefPtr +CefViewRenderApp::GetLoadHandler() +{ + CEF_REQUIRE_RENDERER_THREAD(); + + return nullptr; +} + +void +CefViewRenderApp::OnContextCreated(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr context) +{ + CEF_REQUIRE_RENDERER_THREAD(); + + // [Javascript Context] + // V8 context for this frame has been initialized already, + // but the script of the page hasn't been executed now + message_router_->OnContextCreated(browser, frame, context); + + // log this event + frame->ExecuteJavaScript("console.info('[JSRuntime]:frame context created')", frame->GetURL(), 0); + + // binding bridge object and functions + auto frameId = frame->GetIdentifier(); + auto it = frame_id_to_bridge_obj_map_.find(frameId); + if (it == frame_id_to_bridge_obj_map_.end()) { + // create and insert the bridge Object into this frame.window object + CefRefPtr objWindow = context->GetGlobal(); + CefRefPtr objClient = new CefViewBridgeObject(browser, frame, objWindow, bridge_object_name_); + if (!objClient) { + log_error("Failed to create the client object"); + return; + } + frame_id_to_bridge_obj_map_[frameId] = objClient; + } +} + +void +CefViewRenderApp::OnContextReleased(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr context) +{ + CEF_REQUIRE_RENDERER_THREAD(); + + message_router_->OnContextReleased(browser, frame, context); + + auto frameId = frame->GetIdentifier(); + auto it = frame_id_to_bridge_obj_map_.find(frameId); + if (it != frame_id_to_bridge_obj_map_.end()) { + frame_id_to_bridge_obj_map_.erase(it); + } +} + +void +CefViewRenderApp::OnUncaughtException(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr context, + CefRefPtr exception, + CefRefPtr stackTrace) +{ + CEF_REQUIRE_RENDERER_THREAD(); +} + +void +CefViewRenderApp::OnFocusedNodeChanged(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr node) +{ + CEF_REQUIRE_RENDERER_THREAD(); + + bool is_editable = (node.get() && node->IsEditable()); + if (is_editable != last_node_is_editable_) { + // Notify the browser of the change in focused element type. + last_node_is_editable_ = is_editable; + CefRefPtr message = CefProcessMessage::Create(kCefViewClientRenderFocusedNodeChangedMessage); + message->GetArgumentList()->SetBool(0, is_editable); + frame->SendProcessMessage(PID_BROWSER, message); + } +} + +bool +CefViewRenderApp::OnProcessMessageReceived(CefRefPtr browser, + CefRefPtr frame, + CefProcessId source_process, + CefRefPtr message) +{ + CEF_REQUIRE_RENDERER_THREAD(); + + bool handled = false; + + if (message_router_->OnProcessMessageReceived(browser, frame, source_process, message)) { + handled = true; + } + + if (OnTriggerEventNotifyMessage(browser, frame, source_process, message)) { + handled = true; + } + + return handled; +} + +bool +CefViewRenderApp::OnTriggerEventNotifyMessage(CefRefPtr browser, + CefRefPtr frame, + CefProcessId source_process, + CefRefPtr message) +{ + if (message->GetName() == kCefViewClientBrowserTriggerEventMessage) { + CefRefPtr args = message->GetArgumentList()->Copy(); + //** arguments(CefValueList) + //** +------------+ + //** | event name | + //** | event arg1 | + //** | event arg2 | + //** | event arg3 | + //** | event arg4 | + //** | ... | + //** | ... | + //** | ... | + //** | ... | + //** +------------+ + if (!args || args->GetSize() <= 0) { + log_error("Invalid message arguments, event name is required"); + return true; + } + + if (CefValueType::VTYPE_STRING != args->GetType(0)) { + log_error("Invalid message arguments, invalid type for event name"); + return true; + } + + auto name = args->GetString(0); + args->Remove(0); + ExecuteEventListener(browser, frame, name, args); + + return true; + } + + return false; +} + +void +CefViewRenderApp::ExecuteEventListener(CefRefPtr browser, + CefRefPtr frame, + const CefString& name, + CefRefPtr args) +{ + if (browser && frame) { + auto frameId = frame->GetIdentifier(); + auto it = frame_id_to_bridge_obj_map_.find(frameId); + if (it != frame_id_to_bridge_obj_map_.end()) { + const CefRefPtr& objClient = it->second; + objClient->ExecuteEventListener(name, args); + } + } +} diff --git a/src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.h b/src/CefWing/App/CefViewRenderApp.h similarity index 50% rename from src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.h rename to src/CefWing/App/CefViewRenderApp.h index bad4da7..b9bda11 100644 --- a/src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.h +++ b/src/CefWing/App/CefViewRenderApp.h @@ -1,92 +1,157 @@ -#pragma once -#pragma region std_headers +#ifndef CefRenderApp_h +#define CefRenderApp_h +#pragma once + +#pragma region stl_headers +#include #include #pragma endregion #pragma region cef_headers #include -#pragma endregion cef_headers +#pragma endregion -#pragma region project_headers #include -#include "../CefViewRenderApp.h" -#include "CefViewClient.h" -#pragma endregion project_headers - -namespace CefViewDefaultRenderDelegate { -/// -/// -/// -void -CreateBrowserDelegate(CefViewRenderApp::RenderDelegateSet& delegates, const std::string& bridge_name); +#include "CefViewAppBase.h" +#include "../Bridge/CefViewBridgeObject.h" /// /// /// -class RenderDelegate : public CefViewRenderApp::RenderDelegate +class CefViewRenderApp + : public CefViewAppBase + , public CefRenderProcessHandler { + // Include the default reference counting implementation. + IMPLEMENT_REFCOUNTING(CefViewRenderApp); + +private: + std::string bridge_object_name_; + /// /// /// - typedef std::unordered_map> FrameID2CefClientMap; + CefRefPtr message_router_; + + /// + /// + /// + typedef std::unordered_map> FrameID2BridgeObjMap; + FrameID2BridgeObjMap frame_id_to_bridge_obj_map_; + + /// + /// + /// + bool last_node_is_editable_; public: /// /// /// - RenderDelegate(const std::string& objName); + CefViewRenderApp(const std::string& scheme_name, const std::string& bridge_name); + + /// + /// + /// + ~CefViewRenderApp(); + +private: +#pragma region CefApp + /// + /// + /// + /// + virtual CefRefPtr GetRenderProcessHandler() override; + +#pragma endregion + +#pragma region CefRenderProcessHandler + + /// + /// + /// + virtual void OnWebKitInitialized() override; + + /// + /// + /// + /// + /// + virtual void OnBrowserCreated(CefRefPtr browser, CefRefPtr extra_info) override; + + /// + /// + /// + /// + virtual void OnBrowserDestroyed(CefRefPtr browser) override; /// /// /// - /// - virtual void OnWebKitInitialized(CefRefPtr app) override; + /// + virtual CefRefPtr GetLoadHandler() override; /// /// /// - /// /// /// /// - virtual void OnContextCreated(CefRefPtr app, - CefRefPtr browser, + virtual void OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) override; /// /// /// - /// /// /// /// - virtual void OnContextReleased(CefRefPtr app, - CefRefPtr browser, + virtual void OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) override; - virtual void OnFocusedNodeChanged(CefRefPtr app, - CefRefPtr browser, + /// + /// + /// + /// + /// + /// + /// + /// + virtual void OnUncaughtException(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr context, + CefRefPtr exception, + CefRefPtr stackTrace) override; + + /// + /// + /// + /// + /// + /// + virtual void OnFocusedNodeChanged(CefRefPtr browser, CefRefPtr frame, CefRefPtr node) override; /// /// /// - /// /// + /// /// /// /// - virtual bool OnProcessMessageReceived(CefRefPtr app, - CefRefPtr browser, + virtual bool OnProcessMessageReceived(CefRefPtr browser, CefRefPtr frame, CefProcessId source_process, CefRefPtr message) override; -protected: +#pragma endregion + +private: /// /// /// @@ -111,27 +176,6 @@ class RenderDelegate : public CefViewRenderApp::RenderDelegate CefRefPtr frame, const CefString& name, CefRefPtr args); - -private: - std::string bridge_object_name_; - - /// - /// - /// - CefRefPtr render_message_router_; - - /// - /// - /// - FrameID2CefClientMap frame_id_to_client_map_; - - /// - /// - /// - bool last_node_is_editable_; - -private: - IMPLEMENT_REFCOUNTING(RenderDelegate); }; -} // namespace CefViewDefaultRenderDelegate +#endif diff --git a/src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.cpp b/src/CefWing/Bridge/CefViewBridgeObject.cpp similarity index 81% rename from src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.cpp rename to src/CefWing/Bridge/CefViewBridgeObject.cpp index 5965ea2..c3aa7f5 100644 --- a/src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.cpp +++ b/src/CefWing/Bridge/CefViewBridgeObject.cpp @@ -1,19 +1,20 @@ -#pragma region projet_headers -#include "Common/CefViewCoreLog.h" -#include "CefViewClient.h" -#pragma endregion projet_headers +#include "CefViewBridgeObject.h" -CefViewClient::V8Handler::V8Handler(CefViewClient* client) - : client_(client) +#include + +#include + +CefViewBridgeObject::V8Handler::V8Handler(CefViewBridgeObject* object) + : object_(object) { } bool -CefViewClient::V8Handler::Execute(const CefString& function, - CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) +CefViewBridgeObject::V8Handler::Execute(const CefString& function, + CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) { if (function == kCefViewInvokeMethodFunctionName) ExecuteNativeMethod(object, arguments, retval, exception); @@ -30,20 +31,20 @@ CefViewClient::V8Handler::Execute(const CefString& function, } void -CefViewClient::V8Handler::ExecuteNativeMethod(CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) +CefViewBridgeObject::V8Handler::ExecuteNativeMethod(CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) { - client_->AsyncExecuteNativeMethod(arguments); + object_->AsyncExecuteNativeMethod(arguments); retval = CefV8Value::CreateUndefined(); } void -CefViewClient::V8Handler::ExecuteAddEventListener(CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) +CefViewBridgeObject::V8Handler::ExecuteAddEventListener(CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) { bool bRet = false; @@ -54,7 +55,7 @@ CefViewClient::V8Handler::ExecuteAddEventListener(CefRefPtr object, EventListener listener; listener.callback_ = arguments[1]; listener.context_ = CefV8Context::GetCurrentContext(); - client_->AddEventListener(eventName, listener); + object_->AddEventListener(eventName, listener); bRet = true; } else exception = "Invalid arguments; argument 2 must be a function"; @@ -67,10 +68,10 @@ CefViewClient::V8Handler::ExecuteAddEventListener(CefRefPtr object, } void -CefViewClient::V8Handler::ExecuteRemoveEventListener(CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) +CefViewBridgeObject::V8Handler::ExecuteRemoveEventListener(CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) { bool bRet = false; @@ -81,7 +82,7 @@ CefViewClient::V8Handler::ExecuteRemoveEventListener(CefRefPtr objec EventListener listener; listener.callback_ = arguments[1]; listener.context_ = CefV8Context::GetCurrentContext(); - client_->RemoveEventListener(eventName, listener); + object_->RemoveEventListener(eventName, listener); bRet = true; } else exception = "Invalid arguments; argument 2 must be a function"; @@ -94,14 +95,14 @@ CefViewClient::V8Handler::ExecuteRemoveEventListener(CefRefPtr objec } void -CefViewClient::V8Handler::ExecuteReportJSResult(CefRefPtr object, - const CefV8ValueList& arguments, - CefRefPtr& retval, - CefString& exception) +CefViewBridgeObject::V8Handler::ExecuteReportJSResult(CefRefPtr object, + const CefV8ValueList& arguments, + CefRefPtr& retval, + CefString& exception) { if (arguments.size() == 2) { if (arguments[0]->IsString()) { - client_->AsyncExecuteReportJSResult(arguments); + object_->AsyncExecuteReportJSResult(arguments); } else exception = "Invalid argument; argument 1 must be a double"; } else @@ -112,10 +113,10 @@ CefViewClient::V8Handler::ExecuteReportJSResult(CefRefPtr object, ////////////////////////////////////////////////////////////////////////// -CefViewClient::CefViewClient(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr global, - const std::string& name) +CefViewBridgeObject::CefViewBridgeObject(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr global, + const std::string& name) : name_(name.empty() ? kCefViewDefaultBridgeObjectName : name) , bridgeObject_(nullptr) , reportJSResultFunction_(nullptr) @@ -177,7 +178,7 @@ CefViewClient::CefViewClient(CefRefPtr browser, } CefRefPtr -CefViewClient::CefValueToV8Value(CefValue* cefValue) +CefViewBridgeObject::CefValueToV8Value(CefValue* cefValue) { CefRefPtr v8Value = CefV8Value::CreateNull(); if (!cefValue) { @@ -241,7 +242,7 @@ CefViewClient::CefValueToV8Value(CefValue* cefValue) } CefRefPtr -CefViewClient::V8ValueToCefValue(CefV8Value* v8Value) +CefViewBridgeObject::V8ValueToCefValue(CefV8Value* v8Value) { CefRefPtr cefValue = CefValue::Create(); if (!v8Value) { @@ -270,7 +271,7 @@ CefViewClient::V8ValueToCefValue(CefV8Value* v8Value) * logD("isUnint: %d", isUnint); // false * auto isInt = v->IsInt(); * logD("isInt: %d", isInt); // false - * + * * so we need to keep the testing order, IsInt/IsUint - IsDouble * since there is no Uint type in JavaScript, we just ignore it. * Please refer to this test souce: @@ -315,7 +316,7 @@ CefViewClient::V8ValueToCefValue(CefV8Value* v8Value) } void -CefViewClient::AsyncExecuteNativeMethod(const CefV8ValueList& arguments) +CefViewBridgeObject::AsyncExecuteNativeMethod(const CefV8ValueList& arguments) { CefRefPtr msg = CefProcessMessage::Create(kCefViewClientRenderInvokeMethodMessage); @@ -345,7 +346,7 @@ CefViewClient::AsyncExecuteNativeMethod(const CefV8ValueList& arguments) } void -CefViewClient::AsyncExecuteReportJSResult(const CefV8ValueList& arguments) +CefViewBridgeObject::AsyncExecuteReportJSResult(const CefV8ValueList& arguments) { CefRefPtr msg = CefProcessMessage::Create(kCefViewClientRenderReportJSResultMessage); @@ -368,7 +369,7 @@ CefViewClient::AsyncExecuteReportJSResult(const CefV8ValueList& arguments) } void -CefViewClient::AddEventListener(const CefString& name, const EventListener& listener) +CefViewBridgeObject::AddEventListener(const CefString& name, const EventListener& listener) { auto itListenerList = eventListenerListMap_.find(name); if (itListenerList == eventListenerListMap_.end()) { @@ -392,7 +393,7 @@ CefViewClient::AddEventListener(const CefString& name, const EventListener& list } void -CefViewClient::RemoveEventListener(const CefString& name, const EventListener& listener) +CefViewBridgeObject::RemoveEventListener(const CefString& name, const EventListener& listener) { auto itListenerList = eventListenerListMap_.find(name); if (itListenerList != eventListenerListMap_.end()) { @@ -407,7 +408,7 @@ CefViewClient::RemoveEventListener(const CefString& name, const EventListener& l } void -CefViewClient::ExecuteEventListener(const CefString eventName, CefRefPtr args) +CefViewBridgeObject::ExecuteEventListener(const CefString eventName, CefRefPtr args) { // find the listeners auto itListenerList = eventListenerListMap_.find(eventName); diff --git a/src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.h b/src/CefWing/Bridge/CefViewBridgeObject.h similarity index 91% rename from src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.h rename to src/CefWing/Bridge/CefViewBridgeObject.h index a201fa7..615288a 100644 --- a/src/CefWing/CefRenderApp/RenderDelegates/CefViewClient.h +++ b/src/CefWing/Bridge/CefViewBridgeObject.h @@ -1,20 +1,20 @@ #pragma once -#pragma region std_headers +#pragma region stl_headers #include #include #pragma endregion #pragma region cef_headers #include -#pragma endregion cef_headers - -#include +#pragma endregion /// /// /// -class CefViewClient : public CefBaseRefCounted +class CefViewBridgeObject : public CefBaseRefCounted { + IMPLEMENT_REFCOUNTING(CefViewBridgeObject); + /// /// /// @@ -44,7 +44,7 @@ class CefViewClient : public CefBaseRefCounted /// /// /// - V8Handler(CefViewClient* client); + V8Handler(CefViewBridgeObject* object); /// /// @@ -114,7 +114,7 @@ class CefViewClient : public CefBaseRefCounted /// /// /// - CefViewClient* client_; + CefViewBridgeObject* object_; private: IMPLEMENT_REFCOUNTING(V8Handler); @@ -128,10 +128,10 @@ class CefViewClient : public CefBaseRefCounted /// /// /// - CefViewClient(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr global, - const std::string& name); + CefViewBridgeObject(CefRefPtr browser, + CefRefPtr frame, + CefRefPtr global, + const std::string& name); /// /// @@ -216,8 +216,5 @@ class CefViewClient : public CefBaseRefCounted /// /// /// - CefViewClient::EventListenerListMap eventListenerListMap_; - -private: - IMPLEMENT_REFCOUNTING(CefViewClient); + CefViewBridgeObject::EventListenerListMap eventListenerListMap_; }; diff --git a/src/CefWing/CefRenderApp/CefViewAppBase.h b/src/CefWing/CefRenderApp/CefViewAppBase.h deleted file mode 100644 index 678514c..0000000 --- a/src/CefWing/CefRenderApp/CefViewAppBase.h +++ /dev/null @@ -1,44 +0,0 @@ -// -// CefWingAppBase.hpp -// CefViewWing -// -// Created by Sheen Tian on 2020/6/17. -// - -#ifndef CefAppBase_h -#define CefAppBase_h - -#pragma region std_headers -#include -#include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#pragma endregion cef_headers - -class CefViewAppBase : public CefApp -{ -public: - CefViewAppBase(); - - enum ProcessType - { - UnkownProcess, - ZygoteProcess, - RendererProcess, - OtherProcess, - }; - - // Determine the process type based on command-line arguments. - static ProcessType GetProcessType(CefRefPtr command_line); - - // Determine the bridge object name. - static std::string GetBridgeObjectName(CefRefPtr command_line); - -protected: - // Schemes that will be registered with the global cookie manager. - std::vector cookieable_schemes_; -}; - -#endif diff --git a/src/CefWing/CefRenderApp/CefViewOtherApp.cpp b/src/CefWing/CefRenderApp/CefViewOtherApp.cpp deleted file mode 100644 index 9e6c26d..0000000 --- a/src/CefWing/CefRenderApp/CefViewOtherApp.cpp +++ /dev/null @@ -1,12 +0,0 @@ -// -// CefOtherApp.cpp -// CefViewWing -// -// Created by Sheen Tian on 2020/6/17. -// - -#pragma region project_heasers -#include "CefViewOtherApp.h" -#pragma endregion project_heasers - -CefViewOtherApp::CefViewOtherApp() {} diff --git a/src/CefWing/CefRenderApp/CefViewRenderApp.cpp b/src/CefWing/CefRenderApp/CefViewRenderApp.cpp deleted file mode 100644 index 95b9a5b..0000000 --- a/src/CefWing/CefRenderApp/CefViewRenderApp.cpp +++ /dev/null @@ -1,133 +0,0 @@ -#pragma region std_headers -#include -#pragma endregion std_headers - -#pragma region cef_headers -#include -#include -#include -#pragma endregion cef_headers - -#pragma region project_headers -#include "CefViewRenderApp.h" -#include "RenderDelegates/CefViewDefaultRenderDelegate.h" -#pragma endregion project_headers - -CefViewRenderApp::CefViewRenderApp(const std::string& bridge_name) - : bridge_object_name_(bridge_name) -{ - CefViewDefaultRenderDelegate::CreateBrowserDelegate(render_delegates_, bridge_object_name_); -} - -CefViewRenderApp::~CefViewRenderApp() {} - -////////////////////////////////////////////////////////////////////////// -CefRefPtr -CefViewRenderApp::GetRenderProcessHandler() -{ - return this; -} - -void -CefViewRenderApp::OnWebKitInitialized() -{ - CEF_REQUIRE_RENDERER_THREAD(); - - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnWebKitInitialized(this); -} - -void -CefViewRenderApp::OnBrowserCreated(CefRefPtr browser, CefRefPtr extra_info) -{ - CEF_REQUIRE_RENDERER_THREAD(); - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnBrowserCreated(this, browser, extra_info); -} - -void -CefViewRenderApp::OnBrowserDestroyed(CefRefPtr browser) -{ - CEF_REQUIRE_RENDERER_THREAD(); - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnBrowserDestroyed(this, browser); -} - -CefRefPtr -CefViewRenderApp::GetLoadHandler() -{ - CefRefPtr load_handler; - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end() && !load_handler.get(); ++it) - load_handler = (*it)->GetLoadHandler(this); - - return load_handler; -} - -void -CefViewRenderApp::OnContextCreated(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) -{ - CEF_REQUIRE_RENDERER_THREAD(); - - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnContextCreated(this, browser, frame, context); -} - -void -CefViewRenderApp::OnContextReleased(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) -{ - CEF_REQUIRE_RENDERER_THREAD(); - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnContextReleased(this, browser, frame, context); -} - -void -CefViewRenderApp::OnUncaughtException(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context, - CefRefPtr exception, - CefRefPtr stackTrace) -{ - CEF_REQUIRE_RENDERER_THREAD(); - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnUncaughtException(this, browser, frame, context, exception, stackTrace); -} - -void -CefViewRenderApp::OnFocusedNodeChanged(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr node) -{ - CEF_REQUIRE_RENDERER_THREAD(); - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end(); ++it) - (*it)->OnFocusedNodeChanged(this, browser, frame, node); -} - -bool -CefViewRenderApp::OnProcessMessageReceived(CefRefPtr browser, - CefRefPtr frame, - CefProcessId source_process, - CefRefPtr message) -{ - CEF_REQUIRE_RENDERER_THREAD(); - DCHECK_EQ(source_process, PID_BROWSER); - - bool handled = false; - - RenderDelegateSet::iterator it = render_delegates_.begin(); - for (; it != render_delegates_.end() && !handled; ++it) - handled = (*it)->OnProcessMessageReceived(this, browser, frame, source_process, message); - - return handled; -} diff --git a/src/CefWing/CefRenderApp/CefViewRenderApp.h b/src/CefWing/CefRenderApp/CefViewRenderApp.h deleted file mode 100644 index bd96546..0000000 --- a/src/CefWing/CefRenderApp/CefViewRenderApp.h +++ /dev/null @@ -1,262 +0,0 @@ -#ifndef CefRenderApp_h -#define CefRenderApp_h -#pragma once - -#pragma region std_headers -#include -#pragma endregion std_headers - -#pragma region project_heasers -#include "CefViewAppBase.h" -#pragma endregion project_heasers - -/// -/// -/// -class CefViewRenderApp - : public CefViewAppBase - , public CefRenderProcessHandler -{ -public: - /// - /// - /// - CefViewRenderApp(const std::string& bridge_name); - - /// - /// - /// - ~CefViewRenderApp(); - - /// - /// - /// - class RenderDelegate : public virtual CefBaseRefCounted - { - public: - /// - /// - /// - /// - virtual void OnWebKitInitialized(CefRefPtr app) {} - - /// - /// - /// - /// - /// - virtual void OnBrowserCreated(CefRefPtr app, - CefRefPtr browser, - CefRefPtr extra_info) - {} - - /// - /// - /// - /// - /// - virtual void OnBrowserDestroyed(CefRefPtr app, CefRefPtr browser) {} - - /// - /// - /// - /// - /// - virtual CefRefPtr GetLoadHandler(CefRefPtr app) { return nullptr; } - - /// - /// - /// - /// - /// - /// - /// - virtual void OnContextCreated(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) - {} - - /// - /// - /// - /// - /// - /// - /// - virtual void OnContextReleased(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) - {} - - /// - /// - /// - /// - /// - /// - /// - /// - /// - virtual void OnUncaughtException(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context, - CefRefPtr exception, - CefRefPtr stackTrace) - {} - - /// - /// - /// - /// - /// - /// - /// - virtual void OnFocusedNodeChanged(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr node) - {} - - // Called when a process message is received. Return true if the message was - // handled and should not be passed on to other handlers. RenderDelegates - // should check for unique message names to avoid interfering with each - // other. - /// - /// - /// - /// - /// - /// - /// - /// - virtual bool OnProcessMessageReceived(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefProcessId source_process, - CefRefPtr message) - { - return false; - } - }; - - /// - /// - /// - typedef std::set> RenderDelegateSet; - -private: - -#pragma region CefApp - /// - /// - /// - /// - virtual CefRefPtr GetRenderProcessHandler() override; - -#pragma endregion CefApp - -#pragma region CefRenderProcessHandler - - /// - /// - /// - virtual void OnWebKitInitialized() override; - - /// - /// - /// - /// - /// - virtual void OnBrowserCreated(CefRefPtr browser, CefRefPtr extra_info) override; - - /// - /// - /// - /// - virtual void OnBrowserDestroyed(CefRefPtr browser) override; - - /// - /// - /// - /// - virtual CefRefPtr GetLoadHandler() override; - - /// - /// - /// - /// - /// - /// - virtual void OnContextCreated(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) override; - - /// - /// - /// - /// - /// - /// - virtual void OnContextReleased(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) override; - - /// - /// - /// - /// - /// - /// - /// - /// - virtual void OnUncaughtException(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context, - CefRefPtr exception, - CefRefPtr stackTrace) override; - - /// - /// - /// - /// - /// - /// - virtual void OnFocusedNodeChanged(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr node) override; - - /// - /// - /// - /// - /// - /// - /// - /// - virtual bool OnProcessMessageReceived(CefRefPtr browser, - CefRefPtr frame, - CefProcessId source_process, - CefRefPtr message) override; - -#pragma endregion CefRenderProcessHandler - -private: - /// - /// - /// - std::string bridge_object_name_; - - // Set of supported RenderDelegates. Only used in the renderer process. - /// - /// - /// - RenderDelegateSet render_delegates_; - - // Include the default reference counting implementation. - IMPLEMENT_REFCOUNTING(CefViewRenderApp); -}; - -#endif diff --git a/src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.cpp b/src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.cpp deleted file mode 100644 index 375bee0..0000000 --- a/src/CefWing/CefRenderApp/RenderDelegates/CefViewDefaultRenderDelegate.cpp +++ /dev/null @@ -1,163 +0,0 @@ -#pragma region project_headers -#include "Common/CefViewCoreLog.h" -#include "CefViewDefaultRenderDelegate.h" -#include "CefViewClient.h" -#include "CefViewCoreProtocol.h" -#pragma endregion project_headers - -namespace CefViewDefaultRenderDelegate { -void -CreateBrowserDelegate(CefViewRenderApp::RenderDelegateSet& delegates, const std::string& bridge_name) -{ - delegates.insert(new RenderDelegate(bridge_name)); -} - -RenderDelegate::RenderDelegate(const std::string& bridge_name) - : bridge_object_name_(bridge_name) -{ -} - -void -RenderDelegate::OnWebKitInitialized(CefRefPtr app) -{ - CefMessageRouterConfig config; - config.js_query_function = kCefViewQueryFuntionName; - config.js_cancel_function = kCefViewQueryCancelFunctionName; - render_message_router_ = CefMessageRouterRendererSide::Create(config); -} - -void -RenderDelegate::OnContextCreated(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) -{ - // [Javascript Context] - // V8 context for this frame has been initialized already, - // but the script of the page hasn't been executed now - render_message_router_->OnContextCreated(browser, frame, context); - - // log this event - frame->ExecuteJavaScript("console.info('[JSRuntime]:frame context created')", frame->GetURL(), 0); - - // binding bridge object and functions - auto frameId = frame->GetIdentifier(); - auto it = frame_id_to_client_map_.find(frameId); - if (it == frame_id_to_client_map_.end()) { - // create and insert the bridge Object into this frame.window object - CefRefPtr objWindow = context->GetGlobal(); - CefRefPtr objClient = new CefViewClient(browser, frame, objWindow, bridge_object_name_); - if (!objClient) { - log_error("Failed to create the client object"); - return; - } - frame_id_to_client_map_[frameId] = objClient; - } -} - -void -RenderDelegate::OnContextReleased(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) -{ - render_message_router_->OnContextReleased(browser, frame, context); - - auto frameId = frame->GetIdentifier(); - auto it = frame_id_to_client_map_.find(frameId); - if (it != frame_id_to_client_map_.end()) { - frame_id_to_client_map_.erase(it); - } -} - -void -RenderDelegate::OnFocusedNodeChanged(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefRefPtr node) -{ - bool is_editable = (node.get() && node->IsEditable()); - if (is_editable != last_node_is_editable_) { - // Notify the browser of the change in focused element type. - last_node_is_editable_ = is_editable; - CefRefPtr message = CefProcessMessage::Create(kCefViewClientRenderFocusedNodeChangedMessage); - message->GetArgumentList()->SetBool(0, is_editable); - frame->SendProcessMessage(PID_BROWSER, message); - } -} - -bool -RenderDelegate::OnProcessMessageReceived(CefRefPtr app, - CefRefPtr browser, - CefRefPtr frame, - CefProcessId source_process, - CefRefPtr message) -{ - if (render_message_router_->OnProcessMessageReceived(browser, frame, source_process, message)) { - return true; - } - - if (OnTriggerEventNotifyMessage(browser, frame, source_process, message)) { - return true; - } - - return false; -} - -bool -RenderDelegate::OnTriggerEventNotifyMessage(CefRefPtr browser, - CefRefPtr frame, - CefProcessId source_process, - CefRefPtr message) -{ - if (message->GetName() == kCefViewClientBrowserTriggerEventMessage) { - CefRefPtr args = message->GetArgumentList()->Copy(); - //** arguments(CefValueList) - //** +------------+ - //** | event name | - //** | event arg1 | - //** | event arg2 | - //** | event arg3 | - //** | event arg4 | - //** | ... | - //** | ... | - //** | ... | - //** | ... | - //** +------------+ - if (!args || args->GetSize() <= 0) { - log_error("Invalid message arguments, event name is required"); - return true; - } - - if (CefValueType::VTYPE_STRING != args->GetType(0)) { - log_error("Invalid message arguments, invalid type for event name"); - return true; - } - - auto name = args->GetString(0); - args->Remove(0); - ExecuteEventListener(browser, frame, name, args); - - return true; - } - - return false; -} - -void -RenderDelegate::ExecuteEventListener(CefRefPtr browser, - CefRefPtr frame, - const CefString& name, - CefRefPtr args) -{ - if (browser && frame) { - auto frameId = frame->GetIdentifier(); - auto it = frame_id_to_client_map_.find(frameId); - if (it != frame_id_to_client_map_.end()) { - const CefRefPtr& objClient = it->second; - objClient->ExecuteEventListener(name, args); - } - } -} - -} // namespace CefViewDefaultRenderDelegate diff --git a/src/CefWing/linux/main.cpp b/src/CefWing/linux/main.cpp index 85c7b88..8581421 100644 --- a/src/CefWing/linux/main.cpp +++ b/src/CefWing/linux/main.cpp @@ -7,18 +7,13 @@ #pragma region cef_headers #include -#pragma endregion cef_headers +#pragma endregion -#pragma region win_headers - -#pragma endregion win_headers - -#pragma region project_heasers -#include "../CefRenderApp/CefViewAppBase.h" -#include "../CefRenderApp/CefViewOtherApp.h" -#include "../CefRenderApp/CefViewRenderApp.h" #include -#pragma endregion project_heasers + +#include "../App/CefViewAppBase.h" +#include "../App/CefViewOtherApp.h" +#include "../App/CefViewRenderApp.h" int CefViewWingMain(int argc, char* argv[]) @@ -29,12 +24,17 @@ CefViewWingMain(int argc, char* argv[]) CefRefPtr app; CefRefPtr command_line = CefCommandLine::CreateCommandLine(); command_line->InitFromArgv(argc, argv); + + // get parameter from command line + auto process_type = CefViewAppBase::GetProcessType(command_line); + auto builtin_scheme_name = CefViewAppBase::GetBuiltinSchemeName(command_line); + auto bridge_object_name = CefViewAppBase::GetBridgeObjectName(command_line); + auto process_type = CefViewAppBase::GetProcessType(command_line); if (process_type == CefViewAppBase::RendererProcess || process_type == CefViewAppBase::ZygoteProcess) { - auto bridge_name = CefViewAppBase::GetBridgeObjectName(command_line); - app = new CefViewRenderApp(bridge_name); + app = new CefViewRenderApp(builtin_scheme_name, bridge_object_name); } else if (process_type == CefViewAppBase::OtherProcess) { - app = new CefViewOtherApp(); + app = new CefViewOtherApp(builtin_scheme_name); } else { logI("Parse process unknown, exit"); return 1; diff --git a/src/CefWing/mac/main.mm b/src/CefWing/mac/main.mm index c438a5d..b535996 100644 --- a/src/CefWing/mac/main.mm +++ b/src/CefWing/mac/main.mm @@ -11,20 +11,17 @@ #if defined(CEF_USE_SANDBOX) #include #endif -#pragma endregion cef_headers +#pragma endregion -#pragma region mac_headers #include #include #include -#pragma endregion mac_headers -#pragma region project_heasers -#include "../CefRenderApp/CefViewAppBase.h" -#include "../CefRenderApp/CefViewOtherApp.h" -#include "../CefRenderApp/CefViewRenderApp.h" #include -#pragma endregion project_heasers + +#include "../App/CefViewAppBase.h" +#include "../App/CefViewOtherApp.h" +#include "../App/CefViewRenderApp.h" const char kFrameworkPath[] = "Chromium Embedded Framework.framework/Chromium Embedded Framework"; const char kPathFromHelperExe[] = "../../.."; @@ -83,12 +80,16 @@ int CefViewWingMain(int argc, char *argv[]) { CefRefPtr app; CefRefPtr command_line = CefCommandLine::CreateCommandLine(); command_line->InitFromArgv(argc, argv); + + // get parameter from command line auto process_type = CefViewAppBase::GetProcessType(command_line); + auto builtin_scheme_name = CefViewAppBase::GetBuiltinSchemeName(command_line); + auto bridge_object_name = CefViewAppBase::GetBridgeObjectName(command_line); + if (process_type == CefViewAppBase::RendererProcess) { - auto bridge_name = CefViewAppBase::GetBridgeObjectName(command_line); - app = new CefViewRenderApp(bridge_name); + app = new CefViewRenderApp(builtin_scheme_name, bridge_object_name); } else if (process_type == CefViewAppBase::OtherProcess) { - app = new CefViewOtherApp(); + app = new CefViewOtherApp(builtin_scheme_name); } else { logI("Parse process unknown, exit"); return 1; diff --git a/src/CefWing/win/main.cpp b/src/CefWing/win/main.cpp index b358e65..6f97019 100644 --- a/src/CefWing/win/main.cpp +++ b/src/CefWing/win/main.cpp @@ -11,20 +11,15 @@ #if defined(CEF_USE_SANDBOX) #include #endif -#pragma endregion cef_headers +#pragma endregion -#pragma region win_headers - -#pragma endregion win_headers +#include -#pragma region project_heasers #include -#include -#include "../CefRenderApp/CefViewAppBase.h" -#include "../CefRenderApp/CefViewOtherApp.h" -#include "../CefRenderApp/CefViewRenderApp.h" -#pragma endregion project_heasers +#include "../App/CefViewAppBase.h" +#include "../App/CefViewOtherApp.h" +#include "../App/CefViewRenderApp.h" int CefViewWingMain(HINSTANCE hInstance) @@ -47,12 +42,17 @@ CefViewWingMain(HINSTANCE hInstance) CefRefPtr app; CefRefPtr command_line = CefCommandLine::CreateCommandLine(); command_line->InitFromString(::GetCommandLineW()); + + // get parameter from command line auto process_type = CefViewAppBase::GetProcessType(command_line); + auto builtin_scheme_name = CefViewAppBase::GetBuiltinSchemeName(command_line); + auto bridge_object_name = CefViewAppBase::GetBridgeObjectName(command_line); + + // create application if (process_type == CefViewAppBase::RendererProcess) { - auto bridge_name = CefViewAppBase::GetBridgeObjectName(command_line); - app = new CefViewRenderApp(bridge_name); + app = new CefViewRenderApp(builtin_scheme_name, bridge_object_name); } else if (process_type == CefViewAppBase::OtherProcess) { - app = new CefViewOtherApp(); + app = new CefViewOtherApp(builtin_scheme_name); } else { logI("Parse process unknown, exit"); return 1; diff --git a/win-arm64-gen.bat b/win-arm64-gen.bat deleted file mode 100644 index 007f1a6..0000000 --- a/win-arm64-gen.bat +++ /dev/null @@ -1 +0,0 @@ -cmake -S . -B .build\win.arm64 -A ARM64 -DPROJECT_ARCH=arm64 -DUSE_SANDBOX=ON %* \ No newline at end of file diff --git a/win-x86-gen.bat b/win-x86-gen.bat deleted file mode 100644 index 8163a4d..0000000 --- a/win-x86-gen.bat +++ /dev/null @@ -1 +0,0 @@ -cmake -S . -B .build\win.x86 -A Win32 -DPROJECT_ARCH=x86 -DUSE_SANDBOX=ON %* \ No newline at end of file diff --git a/win-x86_64-gen.bat b/win-x86_64-gen.bat deleted file mode 100644 index d640edc..0000000 --- a/win-x86_64-gen.bat +++ /dev/null @@ -1 +0,0 @@ -cmake -S . -B .build\win.x86_64 -A x64 -DPROJECT_ARCH=x86_64 -DUSE_SANDBOX=ON %* \ No newline at end of file