Use new Dart_PortEx api to ensure SendPort can be rebuild properly. (flutter/engine#52498)
Currently used Dart API can not be reliably used to rebuild dart SendPort object, [new API was introduced in dart sdk ](https://dart.googlesource.com/sdk/+/de4029ee1ba3bc89d839ae7d2fd0706d29abae58) and this PR moves flutter engine to use that new API. BUG: https://github.com/flutter/flutter/issues/147469
This commit is contained in:
committed by
GitHub
parent
730fc72a32
commit
8bd9e60267
@@ -10,24 +10,25 @@ IsolateNameServer::IsolateNameServer() {}
|
||||
|
||||
IsolateNameServer::~IsolateNameServer() = default;
|
||||
|
||||
Dart_Port IsolateNameServer::LookupIsolatePortByName(const std::string& name) {
|
||||
Dart_PortEx IsolateNameServer::LookupIsolatePortByName(
|
||||
const std::string& name) {
|
||||
std::scoped_lock lock(mutex_);
|
||||
return LookupIsolatePortByNameUnprotected(name);
|
||||
}
|
||||
|
||||
Dart_Port IsolateNameServer::LookupIsolatePortByNameUnprotected(
|
||||
Dart_PortEx IsolateNameServer::LookupIsolatePortByNameUnprotected(
|
||||
const std::string& name) {
|
||||
auto port_iterator = port_mapping_.find(name);
|
||||
if (port_iterator != port_mapping_.end()) {
|
||||
return port_iterator->second;
|
||||
}
|
||||
return ILLEGAL_PORT;
|
||||
return {ILLEGAL_PORT, ILLEGAL_PORT};
|
||||
}
|
||||
|
||||
bool IsolateNameServer::RegisterIsolatePortWithName(Dart_Port port,
|
||||
bool IsolateNameServer::RegisterIsolatePortWithName(Dart_PortEx port,
|
||||
const std::string& name) {
|
||||
std::scoped_lock lock(mutex_);
|
||||
if (LookupIsolatePortByNameUnprotected(name) != ILLEGAL_PORT) {
|
||||
if (LookupIsolatePortByNameUnprotected(name).port_id != ILLEGAL_PORT) {
|
||||
// Name is already registered.
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -22,21 +22,21 @@ class IsolateNameServer {
|
||||
|
||||
// Looks up the Dart_Port associated with a given name. Returns ILLEGAL_PORT
|
||||
// if the name does not exist.
|
||||
Dart_Port LookupIsolatePortByName(const std::string& name);
|
||||
Dart_PortEx LookupIsolatePortByName(const std::string& name);
|
||||
|
||||
// Registers a Dart_Port with a given name. Returns true if registration is
|
||||
// successful, false if the name entry already exists.
|
||||
bool RegisterIsolatePortWithName(Dart_Port port, const std::string& name);
|
||||
bool RegisterIsolatePortWithName(Dart_PortEx port, const std::string& name);
|
||||
|
||||
// Removes a name to Dart_Port mapping given a name. Returns true if the
|
||||
// mapping was successfully removed, false if the mapping does not exist.
|
||||
bool RemoveIsolateNameMapping(const std::string& name);
|
||||
|
||||
private:
|
||||
Dart_Port LookupIsolatePortByNameUnprotected(const std::string& name);
|
||||
Dart_PortEx LookupIsolatePortByNameUnprotected(const std::string& name);
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
std::map<std::string, Dart_Port> port_mapping_;
|
||||
std::map<std::string, Dart_PortEx> port_mapping_;
|
||||
|
||||
FML_DISALLOW_COPY_AND_ASSIGN(IsolateNameServer);
|
||||
};
|
||||
|
||||
@@ -19,11 +19,11 @@ Dart_Handle IsolateNameServerNatives::LookupPortByName(
|
||||
if (!name_server) {
|
||||
return Dart_Null();
|
||||
}
|
||||
Dart_Port port = name_server->LookupIsolatePortByName(name);
|
||||
if (port == ILLEGAL_PORT) {
|
||||
Dart_PortEx port = name_server->LookupIsolatePortByName(name);
|
||||
if (port.port_id == ILLEGAL_PORT) {
|
||||
return Dart_Null();
|
||||
}
|
||||
return Dart_NewSendPort(port);
|
||||
return Dart_NewSendPortEx(port);
|
||||
}
|
||||
|
||||
bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle,
|
||||
@@ -32,8 +32,8 @@ bool IsolateNameServerNatives::RegisterPortWithName(Dart_Handle port_handle,
|
||||
if (!name_server) {
|
||||
return false;
|
||||
}
|
||||
Dart_Port port = ILLEGAL_PORT;
|
||||
Dart_SendPortGetId(port_handle, &port);
|
||||
Dart_PortEx port;
|
||||
Dart_SendPortGetIdEx(port_handle, &port);
|
||||
if (!name_server->RegisterIsolatePortWithName(port, name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -25,11 +25,14 @@ TEST_F(DartVMTest, SimpleIsolateNameServer) {
|
||||
ASSERT_TRUE(vm);
|
||||
ASSERT_TRUE(vm.GetVMData());
|
||||
auto ns = vm->GetIsolateNameServer();
|
||||
ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), ILLEGAL_PORT);
|
||||
ASSERT_EQ(ns->LookupIsolatePortByName("foobar").port_id, ILLEGAL_PORT);
|
||||
ASSERT_FALSE(ns->RemoveIsolateNameMapping("foobar"));
|
||||
ASSERT_TRUE(ns->RegisterIsolatePortWithName(123, "foobar"));
|
||||
ASSERT_FALSE(ns->RegisterIsolatePortWithName(123, "foobar"));
|
||||
ASSERT_EQ(ns->LookupIsolatePortByName("foobar"), 123);
|
||||
Dart_PortEx correct_portex = {123, 456};
|
||||
ASSERT_TRUE(ns->RegisterIsolatePortWithName(correct_portex, "foobar"));
|
||||
ASSERT_FALSE(ns->RegisterIsolatePortWithName(correct_portex, "foobar"));
|
||||
Dart_PortEx response = ns->LookupIsolatePortByName("foobar");
|
||||
ASSERT_EQ(response.port_id, correct_portex.port_id);
|
||||
ASSERT_EQ(response.origin_id, correct_portex.origin_id);
|
||||
ASSERT_TRUE(ns->RemoveIsolateNameMapping("foobar"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user