2022-04-21 21:14:51 +00:00
|
|
|
import 'dart:async';
|
2023-11-27 11:09:17 +00:00
|
|
|
import '../src/inappwebview_platform.dart';
|
2022-04-25 20:36:21 +00:00
|
|
|
import 'headless_inappwebview_manager.dart';
|
2022-04-21 21:14:51 +00:00
|
|
|
import 'web_platform_manager.dart';
|
|
|
|
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
|
|
|
|
|
|
|
|
import 'in_app_web_view_web_element.dart';
|
2022-04-25 20:36:21 +00:00
|
|
|
import 'platform_util.dart';
|
2022-04-22 00:24:50 +00:00
|
|
|
import 'package:js/js.dart';
|
|
|
|
|
2023-12-06 01:19:40 +00:00
|
|
|
import 'shims/platform_view_registry.dart' show platformViewRegistry;
|
|
|
|
|
2022-04-21 21:14:51 +00:00
|
|
|
/// Builds an iframe based WebView.
|
|
|
|
///
|
2023-11-24 16:56:57 +00:00
|
|
|
/// This is used as the default implementation for `WebView` on web.
|
2023-11-27 11:09:17 +00:00
|
|
|
class InAppWebViewFlutterPlugin {
|
|
|
|
/// Constructs a new instance of [InAppWebViewFlutterPlugin].
|
|
|
|
InAppWebViewFlutterPlugin(Registrar registrar) {
|
|
|
|
platformViewRegistry.registerViewFactory(
|
2022-04-25 21:21:26 +00:00
|
|
|
'com.pichillilorenzo/flutter_inappwebview', (int viewId) {
|
|
|
|
var webView =
|
|
|
|
InAppWebViewWebElement(viewId: viewId, messenger: registrar);
|
|
|
|
WebPlatformManager.webViews.putIfAbsent(viewId, () => webView);
|
2022-10-20 14:34:37 +00:00
|
|
|
return webView.iframeContainer;
|
2022-04-25 21:21:26 +00:00
|
|
|
});
|
2022-04-21 21:14:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void registerWith(Registrar registrar) {
|
2023-11-27 11:09:17 +00:00
|
|
|
WebPlatformInAppWebViewPlatform.registerWith();
|
2022-05-13 07:35:50 +00:00
|
|
|
// ignore: unused_local_variable
|
2023-11-27 11:09:17 +00:00
|
|
|
final pluginInstance = InAppWebViewFlutterPlugin(registrar);
|
2022-05-13 07:35:50 +00:00
|
|
|
// ignore: unused_local_variable
|
2022-04-25 20:36:21 +00:00
|
|
|
final platformUtil = PlatformUtil(messenger: registrar);
|
2022-05-13 07:35:50 +00:00
|
|
|
// ignore: unused_local_variable
|
2022-04-25 20:36:21 +00:00
|
|
|
final headlessManager = HeadlessInAppWebViewManager(messenger: registrar);
|
2022-04-22 00:24:50 +00:00
|
|
|
_nativeCommunication = allowInterop(_dartNativeCommunication);
|
2022-04-21 21:14:51 +00:00
|
|
|
}
|
2022-04-22 00:24:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows assigning a function to be callable from `window.flutter_inappwebview.nativeCommunication()`
|
|
|
|
@JS('flutter_inappwebview.nativeCommunication')
|
2022-04-25 21:21:26 +00:00
|
|
|
external set _nativeCommunication(
|
|
|
|
Future<dynamic> Function(String method, dynamic viewId, [List? args]) f);
|
2022-04-22 00:24:50 +00:00
|
|
|
|
|
|
|
/// Allows calling the assigned function from Dart as well.
|
|
|
|
@JS()
|
2022-04-25 21:21:26 +00:00
|
|
|
external Future<dynamic> nativeCommunication(String method, dynamic viewId,
|
|
|
|
[List? args]);
|
2022-04-22 00:24:50 +00:00
|
|
|
|
2022-04-25 21:21:26 +00:00
|
|
|
Future<dynamic> _dartNativeCommunication(String method, dynamic viewId,
|
|
|
|
[List? args]) async {
|
2022-04-22 00:24:50 +00:00
|
|
|
if (WebPlatformManager.webViews.containsKey(viewId)) {
|
2022-04-25 21:21:26 +00:00
|
|
|
var webViewHtmlElement =
|
|
|
|
WebPlatformManager.webViews[viewId] as InAppWebViewWebElement;
|
2022-04-22 00:24:50 +00:00
|
|
|
switch (method) {
|
2022-04-22 11:39:21 +00:00
|
|
|
case 'onLoadStart':
|
2022-04-27 14:59:49 +00:00
|
|
|
String url = args![0];
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onLoadStart(url);
|
|
|
|
break;
|
|
|
|
case 'onLoadStop':
|
2022-04-27 14:59:49 +00:00
|
|
|
String url = args![0];
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onLoadStop(url);
|
|
|
|
break;
|
|
|
|
case 'onUpdateVisitedHistory':
|
2022-04-27 14:59:49 +00:00
|
|
|
String url = args![0];
|
2022-04-22 11:39:21 +00:00
|
|
|
webViewHtmlElement.onUpdateVisitedHistory(url);
|
2022-04-22 00:24:50 +00:00
|
|
|
break;
|
2022-04-22 12:41:05 +00:00
|
|
|
case 'onScrollChanged':
|
2022-04-27 14:59:49 +00:00
|
|
|
int x = (args![0] as double).toInt();
|
|
|
|
int y = (args[1] as double).toInt();
|
2022-04-22 12:41:05 +00:00
|
|
|
webViewHtmlElement.onScrollChanged(x, y);
|
|
|
|
break;
|
|
|
|
case 'onConsoleMessage':
|
2022-04-27 14:59:49 +00:00
|
|
|
String type = args![0];
|
|
|
|
String? message = args[1];
|
2022-04-22 12:41:05 +00:00
|
|
|
webViewHtmlElement.onConsoleMessage(type, message);
|
|
|
|
break;
|
2022-04-23 20:10:02 +00:00
|
|
|
case 'onCreateWindow':
|
2022-04-27 14:59:49 +00:00
|
|
|
int windowId = args![0];
|
|
|
|
String url = args[1] ?? 'about:blank';
|
|
|
|
String? target = args[2];
|
|
|
|
String? windowFeatures = args[3];
|
2022-04-25 21:21:26 +00:00
|
|
|
return await webViewHtmlElement.onCreateWindow(
|
|
|
|
windowId, url, target, windowFeatures);
|
2022-04-23 20:10:02 +00:00
|
|
|
case 'onWindowFocus':
|
|
|
|
webViewHtmlElement.onWindowFocus();
|
|
|
|
break;
|
|
|
|
case 'onWindowBlur':
|
|
|
|
webViewHtmlElement.onWindowBlur();
|
|
|
|
break;
|
2022-05-11 22:19:43 +00:00
|
|
|
case 'onPrintRequest':
|
2022-04-27 14:59:49 +00:00
|
|
|
String? url = args![0];
|
2022-05-11 22:19:43 +00:00
|
|
|
webViewHtmlElement.onPrintRequest(url);
|
2022-04-23 20:10:02 +00:00
|
|
|
break;
|
|
|
|
case 'onEnterFullscreen':
|
|
|
|
webViewHtmlElement.onEnterFullscreen();
|
|
|
|
break;
|
|
|
|
case 'onExitFullscreen':
|
|
|
|
webViewHtmlElement.onExitFullscreen();
|
|
|
|
break;
|
|
|
|
case 'onTitleChanged':
|
2022-04-27 14:59:49 +00:00
|
|
|
String? title = args![0];
|
2022-04-23 20:10:02 +00:00
|
|
|
webViewHtmlElement.onTitleChanged(title);
|
|
|
|
break;
|
|
|
|
case 'onZoomScaleChanged':
|
2022-04-27 14:59:49 +00:00
|
|
|
double oldScale = args![0];
|
|
|
|
double newScale = args[1];
|
2022-04-23 20:10:02 +00:00
|
|
|
webViewHtmlElement.onZoomScaleChanged(oldScale, newScale);
|
|
|
|
break;
|
2022-04-27 14:59:49 +00:00
|
|
|
case 'onInjectedScriptLoaded':
|
|
|
|
String id = args![0];
|
|
|
|
webViewHtmlElement.onInjectedScriptLoaded(id);
|
|
|
|
break;
|
|
|
|
case 'onInjectedScriptError':
|
|
|
|
String id = args![0];
|
|
|
|
webViewHtmlElement.onInjectedScriptError(id);
|
|
|
|
break;
|
2022-04-22 00:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-25 21:21:26 +00:00
|
|
|
}
|