Skip to content

Commit

Permalink
Fixed User Script remove methods, Fixed missing break statement on An…
Browse files Browse the repository at this point in the history
…droid when parsing ChromeCustomTabsOptions.displayMode in Java code
  • Loading branch information
pichillilorenzo committed Nov 16, 2022
1 parent 33e42bb commit 7539d26
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 5.7.1+1

- Fixed User Script remove methods
- Fixed missing `break` statement on Android when parsing `ChromeCustomTabsOptions.displayMode` in Java code

## 5.7.1

- Exposed "shared" property of HttpServer bind method to support more use-cases. (thanks to [LugonjaAleksandar](https://github.com/LugonjaAleksandar))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ public ChromeCustomTabsOptions parse(Map<String, Object> options) {
boolean isSticky = (boolean) displayModeMap.get("isSticky");
int layoutInDisplayCutoutMode = (int) displayModeMap.get("layoutInDisplayCutoutMode");
displayMode = new TrustedWebActivityDisplayMode.ImmersiveMode(isSticky, layoutInDisplayCutoutMode);
break;
case "DEFAULT_MODE":
displayMode = new TrustedWebActivityDisplayMode.DefaultMode();
break;
}
}
break;
Expand Down
18 changes: 18 additions & 0 deletions example/ios/Flutter/Flutter.podspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# NOTE: This podspec is NOT to be published. It is only used as a local source!
# This is a generated file; do not edit or check into version control.
#

Pod::Spec.new do |s|
s.name = 'Flutter'
s.version = '1.0.0'
s.summary = 'A UI toolkit for beautiful and fast apps.'
s.homepage = 'https://flutter.dev'
s.license = { :type => 'BSD' }
s.author = { 'Flutter Dev Team' => '[email protected]' }
s.source = { :git => 'https://github.com/flutter/engine', :tag => s.version.to_s }
s.ios.deployment_target = '11.0'
# Framework linking is handled by Flutter tooling, not CocoaPods.
# Add a placeholder to satisfy `s.dependency 'Flutter'` plugin podspecs.
s.vendored_frameworks = 'path/to/nothing'
end
7 changes: 4 additions & 3 deletions example/ios/Flutter/flutter_export_environment.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#!/bin/sh
# This is a generated file; do not edit or check into version control.
export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/2.10.4"
export "FLUTTER_ROOT=/Users/lorenzopichilli/fvm/versions/3.3.5"
export "FLUTTER_APPLICATION_PATH=/Users/lorenzopichilli/flutter_inappwebview_v5/example"
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_TARGET=/Users/lorenzopichilli/flutter_inappwebview_v5/example/lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=1.0.0"
export "FLUTTER_BUILD_NUMBER=1"
export "DART_DEFINES=Zmx1dHRlci5pbnNwZWN0b3Iuc3RydWN0dXJlZEVycm9ycz10cnVl,RkxVVFRFUl9XRUJfQVVUT19ERVRFQ1Q9dHJ1ZQ=="
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
export "PACKAGE_CONFIG=.dart_tool/package_config.json"
export "PACKAGE_CONFIG=/Users/lorenzopichilli/flutter_inappwebview_v5/example/.dart_tool/package_config.json"
4 changes: 3 additions & 1 deletion ios/Classes/Types/WKUserContentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ extension WKUserContentController {

var userScriptsUpdated: [WKUserScript] = []
for script in userScripts {
if !userScripts.contains(script) {
if !scriptsToRemove.contains(script) {
userScriptsUpdated.append(script)
}
}
Expand All @@ -296,6 +296,7 @@ extension WKUserContentController {
for script in allUserOnlyScripts {
if let scriptName = script.groupName, scriptName == groupName {
scriptsToRemove.append(script)
userOnlyScripts[script.injectionTime]!.remove(script)
}
}
removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts)
Expand All @@ -307,6 +308,7 @@ extension WKUserContentController {
for script in allPluginScripts {
if let scriptName = script.groupName, scriptName == groupName {
scriptsToRemove.append(script)
pluginScripts[script.injectionTime]!.remove(script)
}
}
removeUserScripts(scriptsToRemove: scriptsToRemove, shouldAddPreviousScripts: shouldAddPreviousScripts)
Expand Down
76 changes: 63 additions & 13 deletions lib/src/in_app_webview/in_app_webview_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class InAppWebViewController {
static MethodChannel _staticChannel = IN_APP_WEBVIEW_STATIC_CHANNEL;
Map<String, JavaScriptHandlerCallback> javaScriptHandlersMap =
HashMap<String, JavaScriptHandlerCallback>();
List<UserScript> _userScripts = [];
final Map<UserScriptInjectionTime, List<UserScript>> _userScripts = {
UserScriptInjectionTime.AT_DOCUMENT_START: <UserScript>[],
UserScriptInjectionTime.AT_DOCUMENT_END: <UserScript>[]
};
Set<String> _webMessageListenerObjNames = Set();
Map<String, ScriptHtmlTagAttributes> _injectedScriptsFromURL = {};

Expand All @@ -75,8 +78,22 @@ class InAppWebViewController {
MethodChannel('com.pichillilorenzo/flutter_inappwebview_$id');
this._channel.setMethodCallHandler(handleMethod);
this._webview = webview;
this._userScripts =
List<UserScript>.from(webview.initialUserScripts ?? <UserScript>[]);

final initialUserScripts = webview.initialUserScripts;
if (initialUserScripts != null) {
for (final userScript in initialUserScripts) {
if (userScript.injectionTime ==
UserScriptInjectionTime.AT_DOCUMENT_START) {
this
._userScripts[UserScriptInjectionTime.AT_DOCUMENT_START]
?.add(userScript);
} else {
this
._userScripts[UserScriptInjectionTime.AT_DOCUMENT_END]
?.add(userScript);
}
}
}
this._init();
}

Expand All @@ -86,8 +103,22 @@ class InAppWebViewController {
UnmodifiableListView<UserScript>? initialUserScripts) {
this._channel = channel;
this._inAppBrowser = inAppBrowser;
this._userScripts =
List<UserScript>.from(initialUserScripts ?? <UserScript>[]);

if (initialUserScripts != null) {
for (final userScript in initialUserScripts) {
if (userScript.injectionTime ==
UserScriptInjectionTime.AT_DOCUMENT_START) {
this
._userScripts[UserScriptInjectionTime.AT_DOCUMENT_START]
?.add(userScript);
} else {
this
._userScripts[UserScriptInjectionTime.AT_DOCUMENT_END]
?.add(userScript);
}
}
}

this._init();
}

Expand Down Expand Up @@ -2117,8 +2148,9 @@ class InAppWebViewController {

Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('userScript', () => userScript.toMap());
if (!_userScripts.contains(userScript)) {
_userScripts.add(userScript);
if (!(_userScripts[userScript.injectionTime]?.contains(userScript) ??
false)) {
_userScripts[userScript.injectionTime]?.add(userScript);
await _channel.invokeMethod('addUserScript', args);
}
}
Expand Down Expand Up @@ -2148,12 +2180,12 @@ class InAppWebViewController {
assert(_webview?.windowId == null ||
defaultTargetPlatform != TargetPlatform.iOS);

var index = _userScripts.indexOf(userScript);
if (index == -1) {
var index = _userScripts[userScript.injectionTime]?.indexOf(userScript);
if (index == null || index == -1) {
return false;
}

_userScripts.remove(userScript);
_userScripts[userScript.injectionTime]?.remove(userScript);
Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('userScript', () => userScript.toMap());
args.putIfAbsent('index', () => index);
Expand All @@ -2172,6 +2204,22 @@ class InAppWebViewController {
assert(_webview?.windowId == null ||
defaultTargetPlatform != TargetPlatform.iOS);

final List<UserScript> userScriptsAtDocumentStart = List.from(
_userScripts[UserScriptInjectionTime.AT_DOCUMENT_START] ?? []);
for (final userScript in userScriptsAtDocumentStart) {
if (userScript.groupName == groupName) {
_userScripts[userScript.injectionTime]?.remove(userScript);
}
}

final List<UserScript> userScriptsAtDocumentEnd =
List.from(_userScripts[UserScriptInjectionTime.AT_DOCUMENT_END] ?? []);
for (final userScript in userScriptsAtDocumentEnd) {
if (userScript.groupName == groupName) {
_userScripts[userScript.injectionTime]?.remove(userScript);
}
}

Map<String, dynamic> args = <String, dynamic>{};
args.putIfAbsent('groupName', () => groupName);
await _channel.invokeMethod('removeUserScriptsByGroupName', args);
Expand All @@ -2188,8 +2236,8 @@ class InAppWebViewController {
assert(_webview?.windowId == null ||
defaultTargetPlatform != TargetPlatform.iOS);

for (var i = 0; i < userScripts.length; i++) {
await removeUserScript(userScript: userScripts[i]);
for (final userScript in userScripts) {
await removeUserScript(userScript: userScript);
}
}

Expand All @@ -2204,7 +2252,9 @@ class InAppWebViewController {
assert(_webview?.windowId == null ||
defaultTargetPlatform != TargetPlatform.iOS);

_userScripts.clear();
_userScripts[UserScriptInjectionTime.AT_DOCUMENT_START]?.clear();
_userScripts[UserScriptInjectionTime.AT_DOCUMENT_END]?.clear();

Map<String, dynamic> args = <String, dynamic>{};
await _channel.invokeMethod('removeAllUserScripts', args);
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_inappwebview
description: A Flutter plugin that allows you to add an inline webview, to use an headless webview, and to open an in-app browser window.
version: 5.7.1
version: 5.7.1+1
homepage: https://inappwebview.dev/
repository: https://github.com/pichillilorenzo/flutter_inappwebview
issue_tracker: https://github.com/pichillilorenzo/flutter_inappwebview/issues
Expand Down

0 comments on commit 7539d26

Please sign in to comment.