Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shell/platform/darwin/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ source_set("flutter_channels") {
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/lib/ui:ui",
"//flutter/runtime",
"//flutter/shell/common",
"//third_party/skia",
Expand Down
1 change: 1 addition & 0 deletions shell/platform/darwin/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source_set("common") {
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/lib/ui:ui",
"//flutter/runtime",
"//flutter/shell/common",
"//third_party/dart/runtime:dart_api",
Expand Down
9 changes: 5 additions & 4 deletions shell/platform/darwin/common/buffer_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
#include <vector>

#include "flutter/fml/mapping.h"
#import "flutter/lib/ui/window/platform_message.h"
Copy link
Member Author

@gaaclarke gaaclarke May 4, 2021

Choose a reason for hiding this comment

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

This could be removed if my other PR lands where I migrate PlatformMessage to std::unique_ptr, then I could steal its std::vector instead of wrapping the PlatformMessage.


namespace flutter {

std::vector<uint8_t> GetVectorFromNSData(NSData* data);
std::vector<uint8_t> CopyNSDataToVector(NSData* data);

NSData* GetNSDataFromVector(const std::vector<uint8_t>& buffer);
std::unique_ptr<fml::Mapping> CovertNSDataToMapping(NSData* data);

std::unique_ptr<fml::Mapping> GetMappingFromNSData(NSData* data);
NSData* ConvertMessageToNSData(fml::RefPtr<PlatformMessage> message);

NSData* GetNSDataFromMapping(std::unique_ptr<fml::Mapping> mapping);
NSData* ConvertMappingToNSData(std::unique_ptr<fml::Mapping> mapping);

} // namespace flutter

Expand Down
120 changes: 113 additions & 7 deletions shell/platform/darwin/common/buffer_conversions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,130 @@
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/common/buffer_conversions.h"
#import "flutter/fml/platform/darwin/scoped_nsobject.h"

namespace {
class NSDataMapping : public fml::Mapping {
public:
NSDataMapping(NSData* data) : data_([data retain]) {}

size_t GetSize() const override { return [data_.get() length]; }

const uint8_t* GetMapping() const override {
return static_cast<const uint8_t*>([data_.get() bytes]);
}

private:
fml::scoped_nsobject<NSData> data_;
};
}

/// A proxy object that behaves like NSData represented in a Mapping.
/// This isn't a subclass of NSData because NSData is in a class cluster.
@interface FlutterMappingData : NSObject
@end

@implementation FlutterMappingData {
NSData* _data;
std::unique_ptr<fml::Mapping> _mapping;
}

+ (NSData*)dataWithMapping:(std::unique_ptr<fml::Mapping>)mapping {
return (NSData*)[[[FlutterMappingData alloc] initWithMapping:std::move(mapping)] autorelease];
}

- (instancetype)initWithMapping:(std::unique_ptr<fml::Mapping>)mapping {
self = [super init];
if (self) {
const void* rawData = mapping->GetMapping();

// Const cast is required because the NSData API requires it despite
// guarentees that the buffer won't be deleted or modified.
_data = [[NSData alloc] initWithBytesNoCopy:const_cast<void*>(rawData)
length:mapping->GetSize()
freeWhenDone:NO];

_mapping = std::move(mapping);
}
return self;
}

- (void)dealloc {
[_data release];
[super dealloc];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
return _data;
}

- (BOOL)respondsToSelector:(SEL)aSelector {
return [NSData instancesRespondToSelector:aSelector];
}
@end

/// A proxy object that behaves like NSData represented in a PlatformMessage.
/// This isn't a subclass of NSData because NSData is in a class cluster.
@interface FlutterMessageData : NSObject
@end

@implementation FlutterMessageData {
NSData* _data;
fml::RefPtr<flutter::PlatformMessage> _platformMessage;
}

+ (NSData*)dataWithMessage:(fml::RefPtr<flutter::PlatformMessage>)platformMessage {
return (NSData*)[[[FlutterMessageData alloc] initWithMessage:std::move(platformMessage)]
autorelease];
}

- (instancetype)initWithMessage:(fml::RefPtr<flutter::PlatformMessage>)platformMessage {
self = [super init];
if (self) {
const void* rawData = platformMessage->data().data();

// Const cast is required because the NSData API requires it despite
// guarentees that the buffer won't be deleted or modified.
_data = [[NSData alloc] initWithBytesNoCopy:const_cast<void*>(rawData)
length:platformMessage->data().size()
freeWhenDone:NO];

_platformMessage = std::move(platformMessage);
}
return self;
}

- (void)dealloc {
[_data release];
[super dealloc];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
return _data;
}

- (BOOL)respondsToSelector:(SEL)aSelector {
return [NSData instancesRespondToSelector:aSelector];
}
@end

namespace flutter {

std::vector<uint8_t> GetVectorFromNSData(NSData* data) {
std::vector<uint8_t> CopyNSDataToVector(NSData* data) {
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(data.bytes);
return std::vector<uint8_t>(bytes, bytes + data.length);
}

NSData* GetNSDataFromVector(const std::vector<uint8_t>& buffer) {
return [NSData dataWithBytes:buffer.data() length:buffer.size()];
std::unique_ptr<fml::Mapping> CovertNSDataToMapping(NSData* data) {
return std::make_unique<NSDataMapping>(data);
}

std::unique_ptr<fml::Mapping> GetMappingFromNSData(NSData* data) {
return std::make_unique<fml::DataMapping>(GetVectorFromNSData(data));
NSData* ConvertMessageToNSData(fml::RefPtr<PlatformMessage> message) {
return [FlutterMessageData dataWithMessage:std::move(message)];
}

NSData* GetNSDataFromMapping(std::unique_ptr<fml::Mapping> mapping) {
return [NSData dataWithBytes:mapping->GetMapping() length:mapping->GetSize()];
NSData* ConvertMappingToNSData(std::unique_ptr<fml::Mapping> mapping) {
return [FlutterMappingData dataWithMapping:std::move(mapping)];
}

} // namespace flutter
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ + (instancetype)sharedInstance {
}

- (NSData*)encode:(id)message {
NSAssert(!message || [message isKindOfClass:[NSData class]], @"");
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,7 +807,7 @@ - (void)sendOnChannel:(NSString*)channel
fml::RefPtr<flutter::PlatformMessage> platformMessage =
(message == nil) ? fml::MakeRefCounted<flutter::PlatformMessage>(channel.UTF8String, response)
: fml::MakeRefCounted<flutter::PlatformMessage>(
channel.UTF8String, flutter::GetVectorFromNSData(message), response);
channel.UTF8String, flutter::CopyNSDataToVector(message), response);

_shell->GetPlatformView()->DispatchPlatformMessage(platformMessage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
void PlatformMessageResponseDarwin::Complete(std::unique_ptr<fml::Mapping> data) {
fml::RefPtr<PlatformMessageResponseDarwin> self(this);
platform_task_runner_->PostTask(fml::MakeCopyable([self, data = std::move(data)]() mutable {
self->callback_.get()(GetNSDataFromMapping(std::move(data)));
NSData* callbackData = ConvertMappingToNSData(std::move(data));
self->callback_.get()(callbackData);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
FlutterBinaryMessageHandler handler = it->second;
NSData* data = nil;
if (message->hasData()) {
data = GetNSDataFromVector(message->data());
data = ConvertMessageToNSData(std::move(message));
}
handler(data, ^(NSData* reply) {
if (completer) {
if (reply) {
completer->Complete(GetMappingFromNSData(reply));
completer->Complete(CovertNSDataToMapping(reply));
} else {
completer->CompleteEmpty();
}
Expand Down