windows: added WebViewEnvironment support for headlessinappwebview and inappbrowser

This commit is contained in:
unknown 2024-01-27 21:36:37 +01:00
parent c452082219
commit db9ff8c4e6
19 changed files with 83 additions and 65 deletions

View File

@ -24,6 +24,7 @@ class _HeadlessInAppWebViewExampleScreenState
: WebUri("http://localhost:${Uri.base.port}/page.html");
headlessWebView = HeadlessInAppWebView(
webViewEnvironment: webViewEnvironment,
initialUrlRequest: URLRequest(url: url),
initialSettings: InAppWebViewSettings(
isInspectable: kDebugMode,

View File

@ -17,7 +17,8 @@ class MyInAppBrowser extends InAppBrowser {
: super(
windowId: windowId,
initialUserScripts: initialUserScripts,
pullToRefreshController: pullToRefreshController);
pullToRefreshController: pullToRefreshController,
webViewEnvironment: webViewEnvironment,);
@override
Future onBrowserCreated() async {

View File

@ -5,10 +5,12 @@
import FlutterMacOS
import Foundation
import flutter_inappwebview_macos
import path_provider_foundation
import url_launcher_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
InAppWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "InAppWebViewFlutterPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}

View File

@ -35,14 +35,14 @@ dependencies:
dependency_overrides:
flutter_inappwebview_platform_interface:
path: ../../flutter_inappwebview_platform_interface
#flutter_inappwebview_android:
# path: ../../flutter_inappwebview_android
#flutter_inappwebview_ios:
# path: ../../flutter_inappwebview_ios
#flutter_inappwebview_macos:
# path: ../../flutter_inappwebview_macos
#flutter_inappwebview_web:
# path: ../../flutter_inappwebview_web
flutter_inappwebview_android:
path: ../../flutter_inappwebview_android
flutter_inappwebview_ios:
path: ../../flutter_inappwebview_ios
flutter_inappwebview_macos:
path: ../../flutter_inappwebview_macos
flutter_inappwebview_web:
path: ../../flutter_inappwebview_web
flutter_inappwebview_windows:
path: ../../flutter_inappwebview_windows

View File

@ -11,6 +11,7 @@ import '../find_interaction/find_interaction_controller.dart';
import '../pull_to_refresh/main.dart';
import '../in_app_webview/in_app_webview_controller.dart';
import '../webview_environment/webview_environment.dart';
///{@macro flutter_inappwebview_platform_interface.PlatformInAppBrowser}
class InAppBrowser implements PlatformInAppBrowserEvents {
@ -22,14 +23,17 @@ class InAppBrowser implements PlatformInAppBrowserEvents {
PullToRefreshController? pullToRefreshController,
FindInteractionController? findInteractionController,
UnmodifiableListView<UserScript>? initialUserScripts,
int? windowId})
int? windowId,
WebViewEnvironment? webViewEnvironment,
})
: this.fromPlatformCreationParams(
PlatformInAppBrowserCreationParams(
contextMenu: contextMenu,
pullToRefreshController: pullToRefreshController?.platform,
findInteractionController: findInteractionController?.platform,
initialUserScripts: initialUserScripts,
windowId: windowId),
windowId: windowId,
webViewEnvironment: webViewEnvironment?.platform,),
);
/// Constructs a [InAppBrowser] from creation params for a specific

View File

@ -5,6 +5,7 @@ import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import '../find_interaction/find_interaction_controller.dart';
import '../webview_environment/webview_environment.dart';
import 'in_app_webview_controller.dart';
import '../pull_to_refresh/pull_to_refresh_controller.dart';
@ -42,6 +43,7 @@ class HeadlessInAppWebView {
HeadlessInAppWebView? headlessWebView,
InAppWebViewKeepAlive? keepAlive,
bool? preventGestureDelay,
WebViewEnvironment? webViewEnvironment,
@Deprecated('Use onGeolocationPermissionsHidePrompt instead')
void Function(InAppWebViewController controller)?
androidOnGeolocationPermissionsHidePrompt,
@ -307,6 +309,7 @@ class HeadlessInAppWebView {
pullToRefreshController: pullToRefreshController?.platform,
findInteractionController: findInteractionController?.platform,
contextMenu: contextMenu,
webViewEnvironment: webViewEnvironment?.platform,
onWebViewCreated: onWebViewCreated != null
? (controller) => onWebViewCreated.call(controller)
: null,

View File

@ -18,12 +18,11 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_inappwebview_platform_interface: #^1.0.10
path: ../flutter_inappwebview_platform_interface
#flutter_inappwebview_android: ^1.0.12
#flutter_inappwebview_ios: ^1.0.13
#flutter_inappwebview_macos: ^1.0.11
#flutter_inappwebview_web: ^1.0.8
flutter_inappwebview_platform_interface: ^1.0.10
flutter_inappwebview_android: ^1.0.12
flutter_inappwebview_ios: ^1.0.13
flutter_inappwebview_macos: ^1.0.11
flutter_inappwebview_web: ^1.0.8
dev_dependencies:
flutter_test:

View File

@ -19,6 +19,7 @@ import '../in_app_webview/in_app_webview_settings.dart';
import '../print_job/main.dart';
import '../web_uri.dart';
import '../webview_environment/platform_webview_environment.dart';
import 'in_app_browser_menu_item.dart';
import 'in_app_browser_settings.dart';
import '../debug_logging_settings.dart';
@ -36,7 +37,9 @@ class PlatformInAppBrowserCreationParams {
this.pullToRefreshController,
this.findInteractionController,
this.initialUserScripts,
this.windowId});
this.windowId,
this.webViewEnvironment,
});
///{@macro flutter_inappwebview_platform_interface.PlatformInAppBrowser.contextMenu}
final ContextMenu? contextMenu;
@ -52,6 +55,12 @@ class PlatformInAppBrowserCreationParams {
///{@macro flutter_inappwebview_platform_interface.PlatformInAppBrowser.windowId}
final int? windowId;
///Used create the [PlatformInAppBrowser] using the specified environment.
///
///**Officially Supported Platforms/Implementations**:
///- Windows
final PlatformWebViewEnvironment? webViewEnvironment;
}
///{@template flutter_inappwebview_platform_interface.PlatformInAppBrowser}

View File

@ -5,6 +5,7 @@ import 'package:flutter/services.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import '../inappwebview_platform.dart';
import '../types/disposable.dart';
import '../webview_environment/platform_webview_environment.dart';
import 'platform_inappwebview_controller.dart';
import 'platform_webview.dart';
@ -17,6 +18,7 @@ class PlatformHeadlessInAppWebViewCreationParams
/// Used by the platform implementation to create a new [PlatformHeadlessInAppWebView].
const PlatformHeadlessInAppWebViewCreationParams(
{this.initialSize = const Size(-1, -1),
this.webViewEnvironment,
super.controllerFromPlatform,
super.windowId,
super.onWebViewCreated,
@ -141,6 +143,12 @@ class PlatformHeadlessInAppWebViewCreationParams
///- Web
///- MacOS
final Size initialSize;
///Used create the [PlatformHeadlessInAppWebView] using the specified environment.
///
///**Officially Supported Platforms/Implementations**:
///- Windows
final PlatformWebViewEnvironment? webViewEnvironment;
}
///{@template flutter_inappwebview_platform_interface.PlatformHeadlessInAppWebView}

View File

@ -7,6 +7,7 @@ import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_pla
import '../find_interaction/find_interaction_controller.dart';
import '../in_app_webview/in_app_webview_controller.dart';
import '../webview_environment/webview_environment.dart';
/// Object specifying creation parameters for creating a [WindowsInAppBrowser].
///
@ -21,7 +22,8 @@ class WindowsInAppBrowserCreationParams
super.pullToRefreshController,
this.findInteractionController,
super.initialUserScripts,
super.windowId});
super.windowId,
this.webViewEnvironment});
/// Creates a [WindowsInAppBrowserCreationParams] instance based on [PlatformInAppBrowserCreationParams].
factory WindowsInAppBrowserCreationParams.fromPlatformInAppBrowserCreationParams(
@ -34,11 +36,15 @@ class WindowsInAppBrowserCreationParams
findInteractionController:
params.findInteractionController as WindowsFindInteractionController?,
initialUserScripts: params.initialUserScripts,
windowId: params.windowId);
windowId: params.windowId,
webViewEnvironment: params.webViewEnvironment as WindowsWebViewEnvironment?);
}
@override
final WindowsFindInteractionController? findInteractionController;
@override
final WindowsWebViewEnvironment? webViewEnvironment;
}
///{@macro flutter_inappwebview_platform_interface.PlatformInAppBrowser}
@ -168,6 +174,7 @@ class WindowsInAppBrowser extends PlatformInAppBrowser with ChannelController {
() => initialUserScripts?.map((e) => e.toMap()).toList() ?? []);
args.putIfAbsent('pullToRefreshSettings', () => pullToRefreshSettings);
args.putIfAbsent('menuItems', () => menuItemList);
args.putIfAbsent('webViewEnvironmentId', () => _windowsParams.webViewEnvironment?.id);
return args;
}

View File

@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import '../find_interaction/find_interaction_controller.dart';
import '../webview_environment/webview_environment.dart';
import 'in_app_webview_controller.dart';
/// Object specifying creation parameters for creating a [WindowsHeadlessInAppWebView].
@ -18,6 +19,7 @@ class WindowsHeadlessInAppWebViewCreationParams
WindowsHeadlessInAppWebViewCreationParams(
{super.controllerFromPlatform,
super.initialSize,
this.webViewEnvironment,
super.windowId,
super.onWebViewCreated,
super.onLoadStart,
@ -133,6 +135,7 @@ class WindowsHeadlessInAppWebViewCreationParams
PlatformHeadlessInAppWebViewCreationParams params)
: this(
controllerFromPlatform: params.controllerFromPlatform,
webViewEnvironment: params.webViewEnvironment as WindowsWebViewEnvironment?,
initialSize: params.initialSize,
windowId: params.windowId,
onWebViewCreated: params.onWebViewCreated,
@ -242,6 +245,9 @@ class WindowsHeadlessInAppWebViewCreationParams
@override
final WindowsFindInteractionController? findInteractionController;
@override
final WindowsWebViewEnvironment? webViewEnvironment;
}
///{@macro flutter_inappwebview_platform_interface.PlatformHeadlessInAppWebView}
@ -339,7 +345,8 @@ class WindowsHeadlessInAppWebView extends PlatformHeadlessInAppWebView
params.initialUserScripts?.map((e) => e.toMap()).toList() ??
[],
'pullToRefreshSettings': pullToRefreshSettings,
'initialSize': params.initialSize.toMap()
'initialSize': params.initialSize.toMap(),
'webViewEnvironmentId': params.webViewEnvironment?.id,
});
try {
await _sharedChannel.invokeMethod('run', args);

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
import '../webview_environment/webview_environment.dart';
import 'headless_in_app_webview.dart';
import '../find_interaction/find_interaction_controller.dart';
@ -23,7 +24,7 @@ class WindowsInAppWebViewWidgetCreationParams
super.keepAlive,
super.preventGestureDelay,
super.windowId,
super.webViewEnvironment,
this.webViewEnvironment,
super.onWebViewCreated,
super.onLoadStart,
super.onLoadStop,
@ -146,7 +147,7 @@ class WindowsInAppWebViewWidgetCreationParams
keepAlive: params.keepAlive,
preventGestureDelay: params.preventGestureDelay,
windowId: params.windowId,
webViewEnvironment: params.webViewEnvironment,
webViewEnvironment: params.webViewEnvironment as WindowsWebViewEnvironment?,
onWebViewCreated: params.onWebViewCreated,
onLoadStart: params.onLoadStart,
onLoadStop: params.onLoadStop,
@ -254,6 +255,9 @@ class WindowsInAppWebViewWidgetCreationParams
@override
final WindowsFindInteractionController? findInteractionController;
@override
final WindowsWebViewEnvironment? webViewEnvironment;
}
///{@macro flutter_inappwebview_platform_interface.PlatformInAppWebViewWidget}

View File

@ -26,12 +26,7 @@ namespace flutter_inappwebview_plugin
nullptr,
windowClass_.hInstance, nullptr);
CreateInAppWebViewEnvParams webViewEnvParams = {
hwnd,
false
};
InAppWebView::createInAppWebViewEnv(webViewEnvParams, nullptr,
InAppWebView::createInAppWebViewEnv(hwnd, false, nullptr,
[=](wil::com_ptr<ICoreWebView2Environment> webViewEnv,
wil::com_ptr<ICoreWebView2Controller> webViewController,
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)
@ -80,7 +75,6 @@ namespace flutter_inappwebview_plugin
if (expiresDate.has_value()) {
parameters["expires"] = expiresDate.value() / 1000;
}
debugLog(maxAge.value());
if (maxAge.has_value()) {
// time(NULL) represents the current unix timestamp in seconds
parameters["expires"] = time(NULL) + maxAge.value();

View File

@ -56,7 +56,7 @@ namespace flutter_inappwebview_plugin
auto initialFile = get_optional_fl_map_value<std::string>(params, "initialFile");
auto initialDataMap = get_optional_fl_map_value<flutter::EncodableMap>(params, "initialData");
auto initialUserScriptList = get_optional_fl_map_value<flutter::EncodableList>(params, "initialUserScripts");
auto webViewEnvironmentId = get_optional_fl_map_value<std::string>(*arguments, "webViewEnvironmentId");
auto webViewEnvironmentId = get_optional_fl_map_value<std::string>(params, "webViewEnvironmentId");
RECT bounds;
GetClientRect(plugin->registrar->GetView()->GetNativeWindow(), &bounds);
@ -70,15 +70,10 @@ namespace flutter_inappwebview_plugin
nullptr,
windowClass_.hInstance, nullptr);
CreateInAppWebViewEnvParams webViewEnvParams = {
hwnd,
false
};
auto webViewEnvironment = webViewEnvironmentId.has_value() && map_contains(plugin->webViewEnvironmentManager->webViewEnvironments, webViewEnvironmentId.value())
? plugin->webViewEnvironmentManager->webViewEnvironments.at(webViewEnvironmentId.value()).get() : nullptr;
InAppWebView::createInAppWebViewEnv(webViewEnvParams, webViewEnvironment,
InAppWebView::createInAppWebViewEnv(hwnd, false, webViewEnvironment,
[=](wil::com_ptr<ICoreWebView2Environment> webViewEnv,
wil::com_ptr<ICoreWebView2Controller> webViewController,
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)

View File

@ -42,12 +42,6 @@ namespace flutter_inappwebview_plugin
ShowWindow(m_hWnd, settings->hidden ? SW_HIDE : SW_SHOW);
CreateInAppWebViewEnvParams webViewEnvParams = {
m_hWnd,
false
};
InAppWebViewCreationParams webViewParams = {
id,
params.initialWebViewSettings,
@ -57,7 +51,7 @@ namespace flutter_inappwebview_plugin
auto webViewEnvironment = params.webViewEnvironmentId.has_value() && map_contains(plugin->webViewEnvironmentManager->webViewEnvironments, params.webViewEnvironmentId.value())
? plugin->webViewEnvironmentManager->webViewEnvironments.at(params.webViewEnvironmentId.value()).get() : nullptr;
InAppWebView::createInAppWebViewEnv(webViewEnvParams, webViewEnvironment,
InAppWebView::createInAppWebViewEnv(m_hWnd, false, webViewEnvironment,
[this, params, webViewParams](wil::com_ptr<ICoreWebView2Environment> webViewEnv, wil::com_ptr<ICoreWebView2Controller> webViewController, wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController) -> void
{
if (webViewEnv && webViewController) {

View File

@ -26,7 +26,7 @@ namespace flutter_inappwebview_plugin
if (string_equals(methodName, "open")) {
createInAppBrowser(arguments);
result->Success(flutter::EncodableValue(true));
result->Success(true);
}
else if (string_equals(methodName, "openWithSystemBrowser")) {
auto url = get_fl_map_value<std::string>(*arguments, "url");

View File

@ -57,11 +57,11 @@ namespace flutter_inappwebview_plugin
this->inAppBrowser = inAppBrowser;
}
void InAppWebView::createInAppWebViewEnv(const CreateInAppWebViewEnvParams& params, const WebViewEnvironment* webViewEnvironment, std::function<void(wil::com_ptr<ICoreWebView2Environment> webViewEnv,
void InAppWebView::createInAppWebViewEnv(const HWND parentWindow, const bool& willBeSurface, const WebViewEnvironment* webViewEnvironment, std::function<void(wil::com_ptr<ICoreWebView2Environment> webViewEnv,
wil::com_ptr<ICoreWebView2Controller> webViewController,
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)> completionHandler)
{
auto callback = [params, completionHandler](HRESULT result, wil::com_ptr<ICoreWebView2Environment> env) -> HRESULT
auto callback = [parentWindow, willBeSurface, completionHandler](HRESULT result, wil::com_ptr<ICoreWebView2Environment> env) -> HRESULT
{
if (failedAndLog(result) || !env) {
completionHandler(nullptr, nullptr, nullptr);
@ -69,8 +69,8 @@ namespace flutter_inappwebview_plugin
}
wil::com_ptr<ICoreWebView2Environment3> webViewEnv3;
if (params.willBeSurface && succeededOrLog(env->QueryInterface(IID_PPV_ARGS(&webViewEnv3)))) {
failedLog(webViewEnv3->CreateCoreWebView2CompositionController(params.parentWindow, Callback<ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler>(
if (willBeSurface && succeededOrLog(env->QueryInterface(IID_PPV_ARGS(&webViewEnv3)))) {
failedLog(webViewEnv3->CreateCoreWebView2CompositionController(parentWindow, Callback<ICoreWebView2CreateCoreWebView2CompositionControllerCompletedHandler>(
[completionHandler, env](HRESULT result, wil::com_ptr<ICoreWebView2CompositionController> compositionController) -> HRESULT
{
wil::com_ptr<ICoreWebView2Controller3> webViewController = compositionController.try_query<ICoreWebView2Controller3>();
@ -93,7 +93,7 @@ namespace flutter_inappwebview_plugin
).Get()));
}
else {
failedLog(env->CreateCoreWebView2Controller(params.parentWindow, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
failedLog(env->CreateCoreWebView2Controller(parentWindow, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>(
[completionHandler, env](HRESULT result, wil::com_ptr<ICoreWebView2Controller> controller) -> HRESULT
{
if (failedAndLog(result) || !controller) {

View File

@ -12,10 +12,10 @@
#include "../plugin_scripts_js/plugin_scripts_util.h"
#include "../types/content_world.h"
#include "../types/navigation_action.h"
#include "../types/screenshot_configuration.h"
#include "../types/url_request.h"
#include "../types/web_history.h"
#include "../webview_environment/webview_environment.h"
#include "../types/screenshot_configuration.h"
#include "in_app_webview_settings.h"
#include "user_content_controller.h"
#include "webview_channel_delegate.h"
@ -83,11 +83,6 @@ namespace flutter_inappwebview_plugin
const std::optional<std::vector<std::shared_ptr<UserScript>>> initialUserScripts;
};
struct CreateInAppWebViewEnvParams {
const HWND parentWindow;
const bool willBeSurface;
};
class InAppWebView
{
public:
@ -114,7 +109,7 @@ namespace flutter_inappwebview_plugin
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController);
~InAppWebView();
static void createInAppWebViewEnv(const CreateInAppWebViewEnvParams& params, const WebViewEnvironment* webViewEnvironment, std::function<void(wil::com_ptr<ICoreWebView2Environment> webViewEnv,
static void createInAppWebViewEnv(const HWND parentWindow, const bool& willBeSurface, const WebViewEnvironment* webViewEnvironment, std::function<void(wil::com_ptr<ICoreWebView2Environment> webViewEnv,
wil::com_ptr<ICoreWebView2Controller> webViewController,
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)> completionHandler);

View File

@ -11,8 +11,8 @@
#include "../utils/log.h"
#include "../utils/string.h"
#include "../utils/vector.h"
#include "in_app_webview_manager.h"
#include "../webview_environment/webview_environment_manager.h"
#include "in_app_webview_manager.h"
namespace flutter_inappwebview_plugin
{
@ -95,15 +95,10 @@ namespace flutter_inappwebview_plugin
nullptr,
windowClass_.hInstance, nullptr);
CreateInAppWebViewEnvParams webViewEnvParams = {
hwnd,
true
};
auto webViewEnvironment = webViewEnvironmentId.has_value() && map_contains(plugin->webViewEnvironmentManager->webViewEnvironments, webViewEnvironmentId.value())
? plugin->webViewEnvironmentManager->webViewEnvironments.at(webViewEnvironmentId.value()).get() : nullptr;
InAppWebView::createInAppWebViewEnv(webViewEnvParams, webViewEnvironment,
InAppWebView::createInAppWebViewEnv(hwnd, true, webViewEnvironment,
[=](wil::com_ptr<ICoreWebView2Environment> webViewEnv,
wil::com_ptr<ICoreWebView2Controller> webViewController,
wil::com_ptr<ICoreWebView2CompositionController> webViewCompositionController)
@ -147,7 +142,7 @@ namespace flutter_inappwebview_plugin
webViews.insert({ textureId, std::move(customPlatformView) });
result_->Success(flutter::EncodableValue(textureId));
result_->Success(textureId);
}
else {
result_->Error("0", "Cannot create the InAppWebView instance!");