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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webview_flutter_wkwebview] Prevent leaking when a callback method re…
…ferences an object that references itself (#6056)
- Loading branch information
1 parent
1be7a6a
commit 5e5dee8
Showing
5 changed files
with
215 additions
and
70 deletions.
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