Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macos] Add reply to binary messenger #9953

Merged
merged 4 commits into from
Jul 22, 2019

Conversation

franciscojma86
Copy link
Contributor

No description provided.

captures->reply = callback;
auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytesNoCopy:(void*)data length:data_size freeWhenDone:NO];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to take ownership of the data here. Currently, it is owned by the engine but it may be released after this callout. The caller could then use the data past the current scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to dataWithBytes:length: to copy the data.

captures->reply(reply_data);
delete captures;
};
FlutterPlatformMessageCreateResponseHandle(_engine, message_reply, captures, &response_handle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to check if the engine could create a response handle here. This call will return kSuccess in such cases. In case of failures, you will have to remember to delete the captures yourself because you aren't going to get the callback. I suggest using a unique pointer for the captures and only release() it on kSuccess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

if (result != kSuccess) {
NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel, result);
}
FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe log in case of failure here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -73,7 +73,7 @@ - (NSData*)encode:(id)message {
}

- (id)decode:(NSData*)message {
if (message == nil)
if (message == nil || message.length == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cal get rid of the message==nil check. message.length sends an Objective C message of length to message. Messages to nil objects return zero.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

- (void)sendOnChannel:(NSString*)channel
message:(NSData* _Nullable)message
binaryReply:(FlutterBinaryReply _Nullable)callback {
FlutterPlatformMessageResponseHandle* response_handle = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Please define this nearer to where it is used. Right above the FlutterPlatformMessageCreateResponseHandle works better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

result = FlutterPlatformMessageReleaseResponseHandle(_engine, response_handle);
if (result != kSuccess) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please run clang-format over this. I believe the presubmits will block this patch anyway because of the formatting. It is a good idea to clang-format on save of each engine file. You can setup your editor to do this so you don't forget.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You fixed this right before I sent my review :) Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:)

@franciscojma86 franciscojma86 merged commit 6f3a2eb into flutter:master Jul 22, 2019
@franciscojma86 franciscojma86 deleted the response-macos branch July 22, 2019 18:08
@@ -73,7 +73,7 @@ - (NSData*)encode:(id)message {
}

- (id)decode:(NSData*)message {
if (message == nil)
if (message.length == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change? It's actually changing the behavior of this API; before this change it's an assertion failure to send a non-nil but zero-length message, and now it's not.

(And if this is a change we want to make, then the now-redundant if below it should be removed.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting an assertion failure since the data I was getting from the response was not null, but length 0. I now think that was WAI here, but added a length > 0 check in the channel to avoid hitting this assertion

@@ -345,6 +345,49 @@ - (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)messa
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect sendOnChannel:message: to be implemented by calling sendOnChannel:message:binaryReply: with a nil reply callback, so that we don't have duplicate codepaths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
auto captures = reinterpret_cast<Captures*>(user_data);
NSData* reply_data = [NSData dataWithBytes:(void*)data length:data_size];
captures->reply(reply_data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If |callback| is nil, which the API allows, this will crash. The callback-handling code needs to be wrapped in a nil check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@stuartmorgan
Copy link
Contributor

I didn't notice this was merged while I was writing my review until after submitting. It's up to you whether you want to revert and reland with the comments addressed, or do a follow-up ASAP.

@franciscojma86
Copy link
Contributor Author

Addressed comments in new PR #10009

engine-flutter-autoroll added a commit to flutter/flutter that referenced this pull request Jul 23, 2019
flutter/engine@9e9da56...d6970bf

git log 9e9da56..d6970bf --no-merges --oneline
d6970bf Roll src/third_party/skia ebab03ffbffb..6dc14ded91ea (6 commits) (flutter/engine#10005)
6f3a2eb [macos] Add reply to binary messenger (flutter/engine#9953)
a7ef508 Clean up cirrus.yml file a little (flutter/engine#9958)
68d269e Roll src/third_party/dart a089199b93..fedd74669a (8 commits) (flutter/engine#10001)
a004a6e Roll fuchsia/sdk/core/linux-amd64 from FBDZ1EXEOcpLsJY4a5JRok5wPvXm5SURbJw1V7lpfKUC to tY_fod_tTCLft3FAUUxCP_HdLIahaJJK4WCXOA7nNGQC (flutter/engine#10000)

The AutoRoll server is located here: https://autoroll.skia.org/r/flutter-engine-flutter-autoroll

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+/master/autoroll/README.md

If the roll is causing failures, please contact the current sheriff ([email protected]), and stop
the roller if necessary.
johnsonmh pushed a commit to johnsonmh/flutter that referenced this pull request Jul 30, 2019
flutter/engine@9e9da56...d6970bf

git log 9e9da56..d6970bf --no-merges --oneline
d6970bf Roll src/third_party/skia ebab03ffbffb..6dc14ded91ea (6 commits) (flutter/engine#10005)
6f3a2eb [macos] Add reply to binary messenger (flutter/engine#9953)
a7ef508 Clean up cirrus.yml file a little (flutter/engine#9958)
68d269e Roll src/third_party/dart a089199b93..fedd74669a (8 commits) (flutter/engine#10001)
a004a6e Roll fuchsia/sdk/core/linux-amd64 from FBDZ1EXEOcpLsJY4a5JRok5wPvXm5SURbJw1V7lpfKUC to tY_fod_tTCLft3FAUUxCP_HdLIahaJJK4WCXOA7nNGQC (flutter/engine#10000)

The AutoRoll server is located here: https://autoroll.skia.org/r/flutter-engine-flutter-autoroll

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+/master/autoroll/README.md

If the roll is causing failures, please contact the current sheriff ([email protected]), and stop
the roller if necessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants