This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter_wkwebview] Prevent leaking when a callback method references an object that references itself #6056
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
63478a1
prevent leaks
bparrishMines 1c8bd6c
change doc
bparrishMines a41be67
some experiments
bparrishMines 308dee8
use weak reference callback
bparrishMines 6731367
create a weakref helper method
bparrishMines 96077cb
tests running but failing
bparrishMines f468f2c
some test progress
bparrishMines 6596bd7
use weak reference helper
bparrishMines 2af6812
docs and remove runGarbageCollection
bparrishMines 1114c07
unused imports
bparrishMines c66e8ec
license
bparrishMines 75cab29
remove async from test
bparrishMines 2f9e913
add a test
bparrishMines 7b9261f
include weakReferenceTo in doc
bparrishMines 1610344
move test to integration test
bparrishMines 5f51fbd
dont print
bparrishMines bba8625
organize imports
bparrishMines File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/weak_reference_utils.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// Helper method for creating callbacks methods with a weak reference. | ||
/// | ||
/// Example: | ||
/// ``` | ||
/// final JavascriptChannelRegistry javascriptChannelRegistry = ... | ||
/// | ||
/// final WKScriptMessageHandler handler = WKScriptMessageHandler( | ||
/// didReceiveScriptMessage: withWeakRefenceTo( | ||
/// javascriptChannelRegistry, | ||
/// (WeakReference<JavascriptChannelRegistry> weakReference) { | ||
/// return ( | ||
/// WKUserContentController userContentController, | ||
/// WKScriptMessage message, | ||
/// ) { | ||
/// weakReference.target?.onJavascriptChannelMessage( | ||
/// message.name, | ||
/// message.body!.toString(), | ||
/// ); | ||
/// }; | ||
/// }, | ||
/// ), | ||
/// ); | ||
/// ``` | ||
S withWeakRefenceTo<T extends Object, S extends Object>( | ||
T reference, | ||
S Function(WeakReference<T> weakReference) onCreate, | ||
) { | ||
final WeakReference<T> weakReference = WeakReference<T>(reference); | ||
return onCreate(weakReference); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how the second recommendation makes sense in this case; KVO isn't a one-time deterministic callback, so disposing would sometimes never happen, and sometimes happen multiple times. In this context, only a weak reference seems correct. It seems like that would be true of many of the cases this template is being applied to.
Am I misunderstanding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This explanation was for callbacks in general and not just KVO. I could write a separate one for KVO?
The use of
NSObject.dispose
was for the case that someone would want to guarantee a reference to an object until a specific callback was made. For example,I think this could still apply to KVO. I agree that this use case would probably be rare though.