This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Started handling messages from background isolates for iOS #35174
Merged
auto-submit
merged 19 commits into
flutter:main
from
gaaclarke:isolate-platform-channels
Sep 8, 2022
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8dac13c
Started handling messages from background isolates.
gaaclarke 359010b
- switched to `get rootIsolateId`
gaaclarke 41b710b
switched to storing a platform message handler on the isolate group data
gaaclarke 9bc7750
cleanup
gaaclarke e14b1d1
fixed build files
gaaclarke a4c9807
jasons feedback on communicating the platform message handler
gaaclarke a064bf2
added unit test for platform message response dart port
gaaclarke d156d0e
fixed analyzer error
gaaclarke a458d8d
added a root isolate token test
gaaclarke d2a4429
added todo
gaaclarke cf86d36
turned everything off for other platforms
gaaclarke c888035
linter
gaaclarke fa2be9d
jason feedback
gaaclarke ea07367
jason feedback (error send without registering)
gaaclarke 042114b
fixed new imports
gaaclarke 046fb08
dan feedback
gaaclarke 8b3d890
dan feedback - renamed GetRootIsolateId
gaaclarke 9cc8567
dan feedback
gaaclarke 9acbe3b
removed `isolate_id` from variable names
gaaclarke 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -73,6 +73,24 @@ const String _kFlutterKeyDataChannel = 'flutter/keydata'; | |
ByteData? _wrapUnmodifiableByteData(ByteData? byteData) => | ||
byteData == null ? null : UnmodifiableByteDataView(byteData); | ||
|
||
/// A token that represents a root isolate. | ||
class RootIsolateToken { | ||
RootIsolateToken._(this._token); | ||
|
||
/// An enumeration representing the root isolate (0 if not a root isolate). | ||
final int _token; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a nit, consider adding a doc comment here explaining what this is, even though it's not public. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
/// The token for the root isolate that is executing this Dart code. If this | ||
/// Dart code is not executing on a root isolate [instance] will be null. | ||
static final RootIsolateToken? instance = () { | ||
final int token = __getRootIsolateToken(); | ||
return token == 0 ? null : RootIsolateToken._(token); | ||
}(); | ||
|
||
@FfiNative<Int64 Function()>('PlatformConfigurationNativeApi::GetRootIsolateToken') | ||
external static int __getRootIsolateToken(); | ||
} | ||
|
||
/// Platform event dispatcher singleton. | ||
/// | ||
/// The most basic interface to the host operating system's interface. | ||
|
@@ -532,12 +550,51 @@ class PlatformDispatcher { | |
} | ||
} | ||
|
||
String? _sendPlatformMessage(String name,PlatformMessageResponseCallback? callback, ByteData? data) => | ||
String? _sendPlatformMessage(String name, PlatformMessageResponseCallback? callback, ByteData? data) => | ||
__sendPlatformMessage(name, callback, data); | ||
|
||
@FfiNative<Handle Function(Handle, Handle, Handle)>('PlatformConfigurationNativeApi::SendPlatformMessage') | ||
external static String? __sendPlatformMessage(String name, PlatformMessageResponseCallback? callback, ByteData? data); | ||
|
||
/// Sends a message to a platform-specific plugin via a [SendPort]. | ||
/// | ||
/// This operates similarly to [sendPlatformMessage] but is used when sending | ||
/// messages from background isolates. The [port] parameter allows Flutter to | ||
/// know which isolate to send the result to. The [name] parameter is the name | ||
/// of the channel communication will happen on. The [data] parameter is the | ||
/// payload of the message. The [identifier] parameter is a unique integer | ||
/// assigned to the message. | ||
void sendPortPlatformMessage( | ||
String name, | ||
ByteData? data, | ||
int identifier, | ||
SendPort port) { | ||
final String? error = | ||
_sendPortPlatformMessage(name, identifier, port.nativePort, data); | ||
gaaclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (error != null) { | ||
throw Exception(error); | ||
} | ||
} | ||
|
||
String? _sendPortPlatformMessage(String name, int identifier, int port, ByteData? data) => | ||
__sendPortPlatformMessage(name, identifier, port, data); | ||
|
||
@FfiNative<Handle Function(Handle, Handle, Handle, Handle)>('PlatformConfigurationNativeApi::SendPortPlatformMessage') | ||
external static String? __sendPortPlatformMessage(String name, int identifier, int port, ByteData? data); | ||
|
||
/// Registers the current isolate with the isolate identified with by the | ||
/// [token]. This is required if platform channels are to be used on a | ||
/// background isolate. | ||
void registerBackgroundIsolate(RootIsolateToken token) { | ||
if (!Platform.isIOS) { | ||
// Issue: https://github.com/flutter/flutter/issues/13937 | ||
throw UnimplementedError("Platform doesn't yet support platform channels on background isolates."); | ||
} | ||
__registerBackgroundIsolate(token._token); | ||
} | ||
@FfiNative<Void Function(Int64)>('PlatformConfigurationNativeApi::RegisterBackgroundIsolate') | ||
external static void __registerBackgroundIsolate(int rootIsolateId); | ||
|
||
/// Called whenever this platform dispatcher receives a message from a | ||
/// platform-specific plugin. | ||
/// | ||
|
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.