diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..70f8824f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,75 @@ +{ + "cmake.configureOnOpen": false, + "files.associations": { + "algorithm": "cpp", + "any": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "coroutine": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "format": "cpp", + "forward_list": "cpp", + "functional": "cpp", + "initializer_list": "cpp", + "iomanip": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "list": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "new": "cpp", + "optional": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "set": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "stop_token": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "thread": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "variant": "cpp", + "vector": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xtree": "cpp", + "xutility": "cpp" + } +} \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/CMakeLists.txt b/flutter_inappwebview_windows/windows/CMakeLists.txt index 475fb4eb..20c412dd 100644 --- a/flutter_inappwebview_windows/windows/CMakeLists.txt +++ b/flutter_inappwebview_windows/windows/CMakeLists.txt @@ -22,7 +22,6 @@ cmake_policy(VERSION 3.14...3.25) set(PLUGIN_NAME "flutter_inappwebview_windows_plugin") set(NUGET_URL https://dist.nuget.org/win-x86-commandline/latest/nuget.exe) -set(NUGET_SHA256 852b71cc8c8c2d40d09ea49d321ff56fd2397b9d6ea9f96e532530307bbbafd3) find_program(NUGET nuget) if(NOT NUGET) @@ -32,11 +31,6 @@ if(NOT NUGET) message(NOTICE "Attempting to download nuget.") file(DOWNLOAD ${NUGET_URL} ${NUGET}) endif() - - file(SHA256 ${NUGET} NUGET_DL_HASH) - if (NOT NUGET_DL_HASH STREQUAL NUGET_SHA256) - message(FATAL_ERROR "Integrity check for ${NUGET} failed.") - endif() endif() add_custom_target(${PROJECT_NAME}_DEPENDENCIES_DOWNLOAD ALL) @@ -67,10 +61,14 @@ list(APPEND PLUGIN_SOURCES "types/web_resource_request.h" "types/web_resource_response.cpp" "types/web_resource_response.h" + "in_app_webview/in_app_webview_settings.cpp" + "in_app_webview/in_app_webview_settings.h" "in_app_webview/in_app_webview.cpp" "in_app_webview/in_app_webview.h" "in_app_webview/webview_channel_delegate.cpp" "in_app_webview/webview_channel_delegate.h" + "in_app_browser/in_app_browser_settings.cpp" + "in_app_browser/in_app_browser_settings.h" "in_app_browser/in_app_browser_manager.cpp" "in_app_browser/in_app_browser_manager.h" "in_app_browser/in_app_browser.cpp" diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.cpp b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.cpp index afa81412..219ca230 100644 --- a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.cpp +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.cpp @@ -6,13 +6,15 @@ namespace flutter_inappwebview_plugin { - InAppBrowser::InAppBrowser(FlutterInappwebviewWindowsPlugin* plugin, const InAppBrowserCreationParams& params) + InAppBrowser::InAppBrowser(const FlutterInappwebviewWindowsPlugin* plugin, const InAppBrowserCreationParams& params) : plugin(plugin), m_hInstance(GetModuleHandle(nullptr)), id(params.id), initialUrlRequest(params.urlRequest), + settings(params.initialSettings), channelDelegate(std::make_unique(id, plugin->registrar->messenger())) { + WNDCLASS wndClass = {}; wndClass.lpszClassName = InAppBrowser::CLASS_NAME; wndClass.hInstance = m_hInstance; @@ -39,7 +41,12 @@ namespace flutter_inappwebview_plugin ShowWindow(m_hWnd, SW_SHOW); - webView = std::make_unique(plugin, id, m_hWnd, InAppBrowser::METHOD_CHANNEL_NAME_PREFIX + id, [this]() -> void + InAppWebViewCreationParams webViewParams = { + id, + params.initialWebViewSettings + }; + + webView = std::make_unique(plugin, webViewParams, m_hWnd, InAppBrowser::METHOD_CHANNEL_NAME_PREFIX + id, [this]() -> void { if (channelDelegate) { channelDelegate->onBrowserCreated(); diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.h b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.h index 900fecde..9e91cf14 100644 --- a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.h +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser.h @@ -8,15 +8,19 @@ #include "../flutter_inappwebview_windows_plugin.h" #include "../in_app_webview/in_app_webview.h" +#include "../in_app_webview/in_app_webview_settings.h" #include "../types/url_request.h" #include "in_app_browser_channel_delegate.h" +#include "in_app_browser_settings.h" namespace flutter_inappwebview_plugin { struct InAppBrowserCreationParams { - std::string id; - std::optional urlRequest; + const std::string id; + const std::optional urlRequest; + const std::shared_ptr initialSettings; + const std::shared_ptr initialWebViewSettings; }; class InAppBrowser { @@ -29,17 +33,18 @@ namespace flutter_inappwebview_plugin WPARAM wparam, LPARAM lparam) noexcept; - FlutterInappwebviewWindowsPlugin* plugin; - std::string id; - std::optional initialUrlRequest; + const FlutterInappwebviewWindowsPlugin* plugin; + const std::string id; + const std::optional initialUrlRequest; std::unique_ptr webView; std::unique_ptr channelDelegate; + const std::shared_ptr settings; - InAppBrowser(FlutterInappwebviewWindowsPlugin* plugin, const InAppBrowserCreationParams& params); + InAppBrowser(const FlutterInappwebviewWindowsPlugin* plugin, const InAppBrowserCreationParams& params); ~InAppBrowser(); private: - HINSTANCE m_hInstance; + const HINSTANCE m_hInstance; HWND m_hWnd; bool destroyed_ = false; static InAppBrowser* GetThisFromHandle(HWND window) noexcept; diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.cpp b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.cpp index 1e08d7ba..b07181f4 100644 --- a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.cpp +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.cpp @@ -1,13 +1,15 @@ #include #include +#include "../in_app_webview/in_app_webview_settings.h" #include "../types/url_request.h" #include "../utils/flutter.h" #include "in_app_browser_manager.h" +#include "in_app_browser_settings.h" namespace flutter_inappwebview_plugin { - InAppBrowserManager::InAppBrowserManager(FlutterInappwebviewWindowsPlugin* plugin) + InAppBrowserManager::InAppBrowserManager(const FlutterInappwebviewWindowsPlugin* plugin) : plugin(plugin), ChannelDelegate(plugin->registrar->messenger(), InAppBrowserManager::METHOD_CHANNEL_NAME) {} @@ -30,10 +32,17 @@ namespace flutter_inappwebview_plugin auto urlRequestMap = get_optional_fl_map_value(*arguments, "urlRequest"); std::optional urlRequest = urlRequestMap.has_value() ? std::make_optional(urlRequestMap.value()) : std::optional{}; + auto settingsMap = get_fl_map_value(*arguments, "settings"); + auto initialSettings = std::make_unique(settingsMap); + auto initialWebViewSettings = std::make_unique(settingsMap); + InAppBrowserCreationParams params = { id, - urlRequest + urlRequest, + std::move(initialSettings), + std::move(initialWebViewSettings) }; + auto inAppBrowser = std::make_unique(plugin, params); browsers.insert({ id, std::move(inAppBrowser) }); } diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.h b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.h index 91f813e2..31180fee 100644 --- a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.h +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_manager.h @@ -17,10 +17,10 @@ namespace flutter_inappwebview_plugin public: static inline const std::string METHOD_CHANNEL_NAME = "com.pichillilorenzo/flutter_inappbrowser"; - FlutterInappwebviewWindowsPlugin* plugin; + const FlutterInappwebviewWindowsPlugin* plugin; std::map> browsers; - InAppBrowserManager(FlutterInappwebviewWindowsPlugin* plugin); + InAppBrowserManager(const FlutterInappwebviewWindowsPlugin* plugin); ~InAppBrowserManager(); void HandleMethodCall( diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.cpp b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.cpp new file mode 100644 index 00000000..4b5d9594 --- /dev/null +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.cpp @@ -0,0 +1,45 @@ +#include "../utils/flutter.h" +#include "in_app_browser_settings.h" + +namespace flutter_inappwebview_plugin +{ + namespace + { + InAppBrowserWindowType inAppBrowserWindowTypeFromString(const std::string& s) + { + if (s.compare("CHILD") == 0) { + return child; + } + else if (s.compare("TABBED") == 0) { + return tabbed; + } + return window; + } + + std::string inAppBrowserWindowTypeToString(const InAppBrowserWindowType& t) + { + switch (t) { + case child: + return "CHILD"; + case tabbed: + return "TABBED"; + default: + return "WINDOW"; + } + } + } + + InAppBrowserSettings::InAppBrowserSettings() {}; + + InAppBrowserSettings::InAppBrowserSettings(const flutter::EncodableMap& encodableMap) + { + hidden = get_fl_map_value(encodableMap, "hidden", hidden); + windowType = inAppBrowserWindowTypeFromString(get_fl_map_value(encodableMap, "windowType", inAppBrowserWindowTypeToString(window))); + windowAlphaValue = get_fl_map_value(encodableMap, "windowAlphaValue", windowAlphaValue); + } + + InAppBrowserSettings::~InAppBrowserSettings() + { + debugLog("dealloc InAppBrowserSettings"); + }; +} diff --git a/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.h b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.h new file mode 100644 index 00000000..c65bbe14 --- /dev/null +++ b/flutter_inappwebview_windows/windows/in_app_browser/in_app_browser_settings.h @@ -0,0 +1,27 @@ +#ifndef FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_BROWSER_SETTINGS_H_ +#define FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_BROWSER_SETTINGS_H_ + +#include +#include + +namespace flutter_inappwebview_plugin +{ + enum InAppBrowserWindowType { + window, + child, + tabbed + }; + + class InAppBrowserSettings + { + public: + bool hidden = false; + InAppBrowserWindowType windowType = window; + double windowAlphaValue = 1.0; + + InAppBrowserSettings(); + InAppBrowserSettings(const flutter::EncodableMap& encodableMap); + ~InAppBrowserSettings(); + }; +} +#endif //FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_BROWSER_SETTINGS_H_ \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.cpp b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.cpp index 2902c704..5f1f33dd 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.cpp +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.cpp @@ -1,5 +1,6 @@ #pragma comment(lib, "Shlwapi.lib") +#include #include #include #include @@ -14,14 +15,14 @@ namespace flutter_inappwebview_plugin { using namespace Microsoft::WRL; - InAppWebView::InAppWebView(FlutterInappwebviewWindowsPlugin* plugin, const std::variant& id, const HWND parentWindow, const std::function completionHandler) - : plugin(plugin), id(id), channelDelegate(std::make_unique(this, plugin->registrar->messenger())) + InAppWebView::InAppWebView(const FlutterInappwebviewWindowsPlugin* plugin, const InAppWebViewCreationParams& params, const HWND parentWindow, const std::function completionHandler) + : plugin(plugin), id(params.id), settings(params.initialSettings), channelDelegate(std::make_unique(this, plugin->registrar->messenger())) { createWebView(parentWindow, completionHandler); } - InAppWebView::InAppWebView(FlutterInappwebviewWindowsPlugin* plugin, const std::variant& id, const HWND parentWindow, const std::string& channelName, const std::function completionHandler) - : plugin(plugin), id(id), channelDelegate(std::make_unique(this, plugin->registrar->messenger(), channelName)) + InAppWebView::InAppWebView(const FlutterInappwebviewWindowsPlugin* plugin, const InAppWebViewCreationParams& params, const HWND parentWindow, const std::string& channelName, const std::function completionHandler) + : plugin(plugin), id(params.id), settings(params.initialSettings), channelDelegate(std::make_unique(this, plugin->registrar->messenger(), channelName)) { createWebView(parentWindow, completionHandler); } @@ -42,6 +43,20 @@ namespace flutter_inappwebview_plugin webViewController->get_CoreWebView2(webView.put()); } + wil::com_ptr webView2Settings; + if (SUCCEEDED(webView->get_Settings(&webView2Settings))) { + webView2Settings->put_IsScriptEnabled(settings->javaScriptEnabled); + webView2Settings->put_IsZoomControlEnabled(settings->supportZoom); + webView2Settings->put_IsStatusBarEnabled(true); + + wil::com_ptr webView2Settings2; + if (SUCCEEDED(webView2Settings->QueryInterface(IID_PPV_ARGS(&webView2Settings2)))) { + if (!settings->userAgent.empty()) { + webView2Settings2->put_UserAgent(ansi_to_wide(settings->userAgent).c_str()); + } + } + } + // Resize WebView to fit the bounds of the parent window RECT bounds; GetClientRect(parentWindow, &bounds); diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.h b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.h index 2043a469..d8fa450e 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.h +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview.h @@ -8,12 +8,18 @@ #include "../flutter_inappwebview_windows_plugin.h" #include "../types/navigation_action.h" #include "../types/url_request.h" +#include "in_app_webview_settings.h" #include "webview_channel_delegate.h" namespace flutter_inappwebview_plugin { using namespace Microsoft::WRL; + struct InAppWebViewCreationParams { + const std::variant id; + const std::shared_ptr initialSettings; + }; + class InAppWebView { public: @@ -26,9 +32,10 @@ namespace flutter_inappwebview_plugin wil::com_ptr webView; const std::unique_ptr channelDelegate; std::map> navigationActions = {}; + const std::shared_ptr settings; - InAppWebView(FlutterInappwebviewWindowsPlugin* plugin, const std::variant& id, const HWND parentWindow, const std::function completionHandler); - InAppWebView(FlutterInappwebviewWindowsPlugin* plugin, const std::variant& id, const HWND parentWindow, const std::string& channelName, const std::function completionHandler); + InAppWebView(const FlutterInappwebviewWindowsPlugin* plugin, const InAppWebViewCreationParams& params, const HWND parentWindow, const std::function completionHandler); + InAppWebView(const FlutterInappwebviewWindowsPlugin* plugin, const InAppWebViewCreationParams& params, const HWND parentWindow, const std::string& channelName, const std::function completionHandler); ~InAppWebView(); std::optional getUrl() const; diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.cpp b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.cpp new file mode 100644 index 00000000..c66561dc --- /dev/null +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.cpp @@ -0,0 +1,24 @@ +#include "../utils/flutter.h" +#include "in_app_webview_settings.h" + +namespace flutter_inappwebview_plugin +{ + InAppWebViewSettings::InAppWebViewSettings() {}; + + InAppWebViewSettings::InAppWebViewSettings(const flutter::EncodableMap& encodableMap) + { + useShouldOverrideUrlLoading = get_fl_map_value(encodableMap, "useShouldOverrideUrlLoading", useShouldOverrideUrlLoading); + useOnLoadResource = get_fl_map_value(encodableMap, "useOnLoadResource", useOnLoadResource); + useOnDownloadStart = get_fl_map_value(encodableMap, "useOnDownloadStart", useOnDownloadStart); + userAgent = get_fl_map_value(encodableMap, "userAgent", userAgent); + javaScriptEnabled = get_fl_map_value(encodableMap, "javaScriptEnabled", javaScriptEnabled); + resourceCustomSchemes = get_fl_map_value(encodableMap, "resourceCustomSchemes", resourceCustomSchemes); + transparentBackground = get_fl_map_value(encodableMap, "transparentBackground", transparentBackground); + supportZoom = get_fl_map_value(encodableMap, "supportZoom", supportZoom); + } + + InAppWebViewSettings::~InAppWebViewSettings() + { + debugLog("dealloc InAppWebViewSettings"); + } +} diff --git a/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.h b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.h new file mode 100644 index 00000000..ad74f450 --- /dev/null +++ b/flutter_inappwebview_windows/windows/in_app_webview/in_app_webview_settings.h @@ -0,0 +1,26 @@ +#ifndef FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_WEBVIEW_SETTINGS_H_ +#define FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_WEBVIEW_SETTINGS_H_ + +#include +#include + +namespace flutter_inappwebview_plugin +{ + class InAppWebViewSettings + { + public: + bool useShouldOverrideUrlLoading = false; + bool useOnLoadResource = false; + bool useOnDownloadStart = false; + std::string userAgent; + bool javaScriptEnabled = true; + std::vector resourceCustomSchemes; + bool transparentBackground = false; + bool supportZoom = true; + + InAppWebViewSettings(); + InAppWebViewSettings(const flutter::EncodableMap& encodableMap); + ~InAppWebViewSettings(); + }; +} +#endif //FLUTTER_INAPPWEBVIEW_PLUGIN_IN_APP_WEBVIEW_SETTINGS_H_ \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/in_app_webview/webview_channel_delegate.cpp b/flutter_inappwebview_windows/windows/in_app_webview/webview_channel_delegate.cpp index cb6d4f24..c6da13e6 100644 --- a/flutter_inappwebview_windows/windows/in_app_webview/webview_channel_delegate.cpp +++ b/flutter_inappwebview_windows/windows/in_app_webview/webview_channel_delegate.cpp @@ -55,7 +55,7 @@ namespace flutter_inappwebview_plugin } auto arguments = std::make_unique(flutter::EncodableMap{ - {flutter::EncodableValue("url"), make_fl_value(url)}, + {"url", make_fl_value(url)}, }); channel->InvokeMethod("onLoadStart", std::move(arguments)); } @@ -67,7 +67,7 @@ namespace flutter_inappwebview_plugin } auto arguments = std::make_unique(flutter::EncodableMap{ - {flutter::EncodableValue("url"), make_fl_value(url)}, + {"url", make_fl_value(url)}, }); channel->InvokeMethod("onLoadStop", std::move(arguments)); } @@ -89,8 +89,8 @@ namespace flutter_inappwebview_plugin } auto arguments = std::make_unique(flutter::EncodableMap{ - {flutter::EncodableValue("request"), request->toEncodableMap()}, - {flutter::EncodableValue("error"), error->toEncodableMap()}, + {"request", request->toEncodableMap()}, + {"error", error->toEncodableMap()}, }); channel->InvokeMethod("onReceivedError", std::move(arguments)); } @@ -102,8 +102,8 @@ namespace flutter_inappwebview_plugin } auto arguments = std::make_unique(flutter::EncodableMap{ - {flutter::EncodableValue("request"), request->toEncodableMap()}, - {flutter::EncodableValue("errorResponse"), errorResponse->toEncodableMap()}, + {"request", request->toEncodableMap()}, + {"errorResponse", errorResponse->toEncodableMap()}, }); channel->InvokeMethod("onReceivedHttpError", std::move(arguments)); } diff --git a/flutter_inappwebview_windows/windows/types/navigation_action.cpp b/flutter_inappwebview_windows/windows/types/navigation_action.cpp index 680d0483..4e499490 100644 --- a/flutter_inappwebview_windows/windows/types/navigation_action.cpp +++ b/flutter_inappwebview_windows/windows/types/navigation_action.cpp @@ -10,8 +10,8 @@ namespace flutter_inappwebview_plugin flutter::EncodableMap NavigationAction::toEncodableMap() const { return flutter::EncodableMap{ - {make_fl_value("request"), request->toEncodableMap()}, - {make_fl_value("isForMainFrame"), make_fl_value(isForMainFrame)} + {"request", request->toEncodableMap()}, + {"isForMainFrame", make_fl_value(isForMainFrame)} }; } } \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/types/url_request.cpp b/flutter_inappwebview_windows/windows/types/url_request.cpp index ec997b24..42340dd0 100644 --- a/flutter_inappwebview_windows/windows/types/url_request.cpp +++ b/flutter_inappwebview_windows/windows/types/url_request.cpp @@ -11,17 +11,17 @@ namespace flutter_inappwebview_plugin URLRequest::URLRequest(const flutter::EncodableMap& map) : url(get_optional_fl_map_value(map, "url")), method(get_optional_fl_map_value(map, "method")), - headers(get_optional_fl_map_value(map, "headers")), + headers(get_optional_fl_map_value>(map, "headers")), body(get_optional_fl_map_value>(map, "body")) {} flutter::EncodableMap URLRequest::toEncodableMap() const { return flutter::EncodableMap{ - {make_fl_value("url"), make_fl_value(url)}, - {make_fl_value("method"), make_fl_value(method)}, - {make_fl_value("headers"), make_fl_value(headers)}, - {make_fl_value("body"), make_fl_value(body)} + {"url", make_fl_value(url)}, + {"method", make_fl_value(method)}, + {"headers", make_fl_value(headers)}, + {"body", make_fl_value(body)} }; } } \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/types/web_resource_error.cpp b/flutter_inappwebview_windows/windows/types/web_resource_error.cpp index 107fd682..f3fb3a36 100644 --- a/flutter_inappwebview_windows/windows/types/web_resource_error.cpp +++ b/flutter_inappwebview_windows/windows/types/web_resource_error.cpp @@ -15,8 +15,8 @@ namespace flutter_inappwebview_plugin flutter::EncodableMap WebResourceError::toEncodableMap() const { return flutter::EncodableMap{ - {make_fl_value("description"), make_fl_value(description)}, - {make_fl_value("type"), make_fl_value(type)} + {"description", make_fl_value(description)}, + {"type", make_fl_value(type)} }; } } \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/types/web_resource_request.cpp b/flutter_inappwebview_windows/windows/types/web_resource_request.cpp index 3ab71d93..b7074207 100644 --- a/flutter_inappwebview_windows/windows/types/web_resource_request.cpp +++ b/flutter_inappwebview_windows/windows/types/web_resource_request.cpp @@ -11,17 +11,17 @@ namespace flutter_inappwebview_plugin WebResourceRequest::WebResourceRequest(const flutter::EncodableMap& map) : url(get_optional_fl_map_value(map, "url")), method(get_optional_fl_map_value(map, "method")), - headers(get_optional_fl_map_value(map, "headers")), + headers(get_optional_fl_map_value>(map, "headers")), isForMainFrame(get_optional_fl_map_value(map, "isForMainFrame")) {} flutter::EncodableMap WebResourceRequest::toEncodableMap() const { return flutter::EncodableMap{ - {make_fl_value("url"), make_fl_value(url)}, - {make_fl_value("method"), make_fl_value(method)}, - {make_fl_value("headers"), make_fl_value(headers)}, - {make_fl_value("isForMainFrame"), make_fl_value(isForMainFrame)} + {"url", make_fl_value(url)}, + {"method", make_fl_value(method)}, + {"headers", make_fl_value(headers)}, + {"isForMainFrame", make_fl_value(isForMainFrame)} }; } } \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/types/web_resource_response.cpp b/flutter_inappwebview_windows/windows/types/web_resource_response.cpp index 934b94fb..5f76dc9c 100644 --- a/flutter_inappwebview_windows/windows/types/web_resource_response.cpp +++ b/flutter_inappwebview_windows/windows/types/web_resource_response.cpp @@ -14,7 +14,7 @@ namespace flutter_inappwebview_plugin flutter::EncodableMap WebResourceResponse::toEncodableMap() const { return flutter::EncodableMap{ - {make_fl_value("statusCode"), make_fl_value(statusCode)} + {"statusCode", make_fl_value(statusCode)} }; } } \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/utils/flutter.h b/flutter_inappwebview_windows/windows/utils/flutter.h index d539333d..517a0075 100644 --- a/flutter_inappwebview_windows/windows/utils/flutter.h +++ b/flutter_inappwebview_windows/windows/utils/flutter.h @@ -2,8 +2,6 @@ #define FLUTTER_INAPPWEBVIEW_PLUGIN_FLUTTER_UTIL_H_ #include -#include -#include #include "util.h" @@ -86,25 +84,66 @@ namespace flutter_inappwebview_plugin return std::get(map.at(make_fl_value(string))); } - template + template::value && !is_vector::value) || + std::is_same::value || std::is_same::value), int>::type* = nullptr> static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* string) { return make_pointer_optional(std::get_if(&map.at(make_fl_value(string)))); } - template - static inline std::optional> get_optional_fl_map_value(const flutter::EncodableMap& map, const char* string) + template + static inline T get_fl_map_value(const flutter::EncodableMap& map, const char* string, const T& defaultValue) { + auto optional = get_optional_fl_map_value(map, string); + return !optional.has_value() ? defaultValue : optional.value(); + } + + template::value && !std::is_same::value)>::type* = nullptr> + static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* string) + { + using K = typename T::key_type; + using V = typename T::mapped_type; + auto flMap = std::get_if(&map.at(make_fl_value(string))); if (flMap) { - auto mapValue = std::map{}; + T mapValue = {}; for (auto itr = flMap->begin(); itr != flMap->end(); itr++) { - mapValue.insert({ std::get(itr->first), std::get(itr->second) }); + mapValue.insert({ std::get(itr->first), std::get(itr->second) }); } - return make_pointer_optional>(&mapValue); + return make_pointer_optional(&mapValue); } return std::nullopt; } + + template + static inline std::map get_fl_map_value(const flutter::EncodableMap& map, const char* string, const std::map& defaultValue) + { + auto optional = get_optional_fl_map_value>(map, string); + return !optional.has_value() ? defaultValue : optional.value(); + } + + template::value && !std::is_same::value), bool>::type* = nullptr> + static inline std::optional get_optional_fl_map_value(const flutter::EncodableMap& map, const char* string) + { + using V = typename T::value_type; + + auto flList = std::get_if(&map.at(make_fl_value(string))); + if (flList) { + T vecValue(flList->size()); + for (auto itr = flList->begin(); itr != flList->end(); itr++) { + vecValue.push_back(std::get(*itr)); + } + return make_pointer_optional(&vecValue); + } + return std::nullopt; + } + + template + static inline std::vector get_fl_map_value(const flutter::EncodableMap& map, const char* string, const std::vector& defaultValue) + { + auto optional = get_optional_fl_map_value>(map, string); + return !optional.has_value() ? defaultValue : optional.value(); + } } #endif //FLUTTER_INAPPWEBVIEW_PLUGIN_FLUTTER_UTIL_H_ \ No newline at end of file diff --git a/flutter_inappwebview_windows/windows/utils/util.h b/flutter_inappwebview_windows/windows/utils/util.h index e619a246..2513ddd2 100644 --- a/flutter_inappwebview_windows/windows/utils/util.h +++ b/flutter_inappwebview_windows/windows/utils/util.h @@ -12,6 +12,34 @@ namespace flutter_inappwebview_plugin { + template + struct is_vector_impl : std::false_type { }; + + template + struct is_mappish_impl : std::false_type { }; + + template + struct is_vector_impl>::value> + > : std::true_type { }; + + template + struct is_vector_impl::value_type>::iterator>::value> + > : std::true_type { }; + + template + struct is_mappish_impl()[std::declval()])>> + : std::true_type { }; + + template + struct is_mappish : is_mappish_impl::type { }; + + template + struct is_vector : is_vector_impl::type { }; + static inline void debugLog(const std::string& msg) { #ifndef NDEBUG