forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added IsolateNameServer functionality (flutter#5410)
* Added IsolateNameServer functionality, which allows for the association of string names with isolate SendPort ids that can be used to establish inter-isolate communications.
- Loading branch information
Showing
25 changed files
with
426 additions
and
22 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2018 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
part of dart.ui; | ||
|
||
abstract class IsolateNameServer { | ||
// Looks up the [SendPort] associated with a given name. Returns null | ||
// if the name does not exist. | ||
static SendPort lookupPortByName(String name) { | ||
if (name == null) { | ||
throw new ArgumentError("'name' cannot be null."); | ||
} | ||
return _lookupPortByName(name); | ||
} | ||
|
||
// Registers a SendPort with a given name. Returns true if registration is | ||
// successful, false if the name entry already exists. | ||
static bool registerPortWithName(SendPort port, String name) { | ||
if (name == null) { | ||
throw new ArgumentError("'name' cannot be null."); | ||
} | ||
if (port == null) { | ||
throw new ArgumentError("'port' cannot be null."); | ||
} | ||
return _registerPortWithName(port, name); | ||
} | ||
|
||
// Removes a name to SendPort mapping given a name. Returns true if the | ||
// mapping was successfully removed, false if the mapping does not exist. | ||
static bool removePortNameMapping(String name) { | ||
if (name == null) { | ||
throw new ArgumentError("'name' cannot be null."); | ||
} | ||
return _removePortNameMapping(name); | ||
} | ||
|
||
static SendPort _lookupPortByName(String name) | ||
native 'IsolateNameServerNatives_LookupPortByName'; | ||
static bool _registerPortWithName(SendPort port, String name) | ||
native 'IsolateNameServerNatives_RegisterPortWithName'; | ||
static bool _removePortNameMapping(String name) | ||
native 'IsolateNameServerNatives_RemovePortNameMapping'; | ||
} |
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,44 @@ | ||
// Copyright 2018 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. | ||
|
||
#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" | ||
|
||
namespace blink { | ||
|
||
Dart_Port IsolateNameServer::LookupIsolatePortByName(const std::string& name) { | ||
std::unique_lock<std::mutex> lock(mutex_); | ||
return LookupIsolatePortByNameUnprotected(name); | ||
} | ||
|
||
Dart_Port 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; | ||
} | ||
|
||
bool IsolateNameServer::RegisterIsolatePortWithName(Dart_Port port, | ||
const std::string& name) { | ||
std::unique_lock<std::mutex> lock(mutex_); | ||
if (LookupIsolatePortByNameUnprotected(name) != ILLEGAL_PORT) { | ||
// Name is already registered. | ||
return false; | ||
} | ||
port_mapping_[name] = port; | ||
return true; | ||
} | ||
|
||
bool IsolateNameServer::RemoveIsolateNameMapping(const std::string& name) { | ||
std::unique_lock<std::mutex> lock(mutex_); | ||
auto port_iterator = port_mapping_.find(name); | ||
if (port_iterator == port_mapping_.end()) { | ||
return false; | ||
} | ||
port_mapping_.erase(port_iterator); | ||
return true; | ||
} | ||
|
||
} // namespace blink |
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,50 @@ | ||
// Copyright 2018 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. | ||
|
||
#ifndef FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_H_ | ||
#define FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_H_ | ||
|
||
#include <map> | ||
#include <mutex> | ||
#include <string> | ||
|
||
#include "flutter/fml/synchronization/thread_annotations.h" | ||
#include "lib/fxl/macros.h" | ||
#include "third_party/dart/runtime/include/dart_api.h" | ||
|
||
#define LOCK_UNLOCK(m) FML_ACQUIRE(m) FML_RELEASE(m) | ||
|
||
namespace blink { | ||
|
||
class IsolateNameServer { | ||
public: | ||
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) | ||
LOCK_UNLOCK(mutex_); | ||
|
||
// 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) | ||
LOCK_UNLOCK(mutex_); | ||
|
||
// 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) LOCK_UNLOCK(mutex_); | ||
|
||
private: | ||
Dart_Port LookupIsolatePortByNameUnprotected(const std::string& name) | ||
FML_EXCLUSIVE_LOCKS_REQUIRED(mutex_); | ||
|
||
mutable std::mutex mutex_; | ||
std::map<std::string, Dart_Port> port_mapping_ FML_GUARDED_BY(mutex_); | ||
|
||
FXL_DISALLOW_COPY_AND_ASSIGN(IsolateNameServer); | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_H_ |
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,64 @@ | ||
// Copyright 2018 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. | ||
|
||
#include <string> | ||
|
||
#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" | ||
#include "flutter/lib/ui/isolate_name_server/isolate_name_server_natives.h" | ||
#include "flutter/lib/ui/ui_dart_state.h" | ||
#include "lib/tonic/dart_binding_macros.h" | ||
#include "lib/tonic/dart_library_natives.h" | ||
|
||
namespace blink { | ||
|
||
Dart_Handle IsolateNameServerNatives::LookupPortByName( | ||
const std::string& name) { | ||
IsolateNameServer* name_server = | ||
UIDartState::Current()->GetIsolateNameServer(); | ||
Dart_Port port = name_server->LookupIsolatePortByName(name); | ||
if (port == ILLEGAL_PORT) { | ||
return Dart_Null(); | ||
} | ||
return Dart_NewSendPort(port); | ||
} | ||
|
||
Dart_Handle IsolateNameServerNatives::RegisterPortWithName( | ||
Dart_Handle port_handle, | ||
const std::string& name) { | ||
IsolateNameServer* name_server = | ||
UIDartState::Current()->GetIsolateNameServer(); | ||
Dart_Port port = ILLEGAL_PORT; | ||
Dart_SendPortGetId(port_handle, &port); | ||
if (!name_server->RegisterIsolatePortWithName(port, name)) { | ||
return Dart_False(); | ||
} | ||
return Dart_True(); | ||
} | ||
|
||
Dart_Handle IsolateNameServerNatives::RemovePortNameMapping( | ||
const std::string& name) { | ||
IsolateNameServer* name_server = | ||
UIDartState::Current()->GetIsolateNameServer(); | ||
if (!name_server->RemoveIsolateNameMapping(name)) { | ||
return Dart_False(); | ||
} | ||
return Dart_True(); | ||
} | ||
|
||
#define FOR_EACH_BINDING(V) \ | ||
V(IsolateNameServerNatives, LookupPortByName) \ | ||
V(IsolateNameServerNatives, RegisterPortWithName) \ | ||
V(IsolateNameServerNatives, RemovePortNameMapping) | ||
|
||
FOR_EACH_BINDING(DART_NATIVE_CALLBACK_STATIC) | ||
|
||
#define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD) \ | ||
DART_REGISTER_NATIVE_STATIC(CLASS, METHOD), | ||
|
||
void IsolateNameServerNatives::RegisterNatives( | ||
tonic::DartLibraryNatives* natives) { | ||
natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE_STATIC_)}); | ||
} | ||
|
||
} // namespace blink |
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,27 @@ | ||
// Copyright 2018 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. | ||
|
||
#ifndef FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_NATIVES_H_ | ||
#define FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_NATIVES_H_ | ||
|
||
#include "third_party/dart/runtime/include/dart_api.h" | ||
|
||
namespace tonic { | ||
class DartLibraryNatives; | ||
} // namespace tonic | ||
|
||
namespace blink { | ||
|
||
class IsolateNameServerNatives { | ||
public: | ||
static Dart_Handle LookupPortByName(const std::string& name); | ||
static Dart_Handle RegisterPortWithName(Dart_Handle port_handle, | ||
const std::string& name); | ||
static Dart_Handle RemovePortNameMapping(const std::string& name); | ||
static void RegisterNatives(tonic::DartLibraryNatives* natives); | ||
}; | ||
|
||
} // namespace blink | ||
|
||
#endif // FLUTTER_LIB_UI_ISOLATE_NAME_SERVER_NATIVES_H_ |
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
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
Oops, something went wrong.