1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
---|---|
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #include <string> |
6 | |
7 | #include "flutter/lib/ui/isolate_name_server/isolate_name_server.h" |
8 | #include "flutter/lib/ui/isolate_name_server/isolate_name_server_natives.h" |
9 | #include "flutter/lib/ui/ui_dart_state.h" |
10 | #include "third_party/tonic/dart_binding_macros.h" |
11 | #include "third_party/tonic/dart_library_natives.h" |
12 | |
13 | namespace flutter { |
14 | |
15 | Dart_Handle IsolateNameServerNatives::LookupPortByName( |
16 | const std::string& name) { |
17 | auto name_server = UIDartState::Current()->GetIsolateNameServer(); |
18 | if (!name_server) { |
19 | return Dart_Null(); |
20 | } |
21 | Dart_Port port = name_server->LookupIsolatePortByName(name); |
22 | if (port == ILLEGAL_PORT) { |
23 | return Dart_Null(); |
24 | } |
25 | return Dart_NewSendPort(port); |
26 | } |
27 | |
28 | Dart_Handle IsolateNameServerNatives::RegisterPortWithName( |
29 | Dart_Handle port_handle, |
30 | const std::string& name) { |
31 | auto name_server = UIDartState::Current()->GetIsolateNameServer(); |
32 | if (!name_server) { |
33 | return Dart_False(); |
34 | } |
35 | Dart_Port port = ILLEGAL_PORT; |
36 | Dart_SendPortGetId(port_handle, &port); |
37 | if (!name_server->RegisterIsolatePortWithName(port, name)) { |
38 | return Dart_False(); |
39 | } |
40 | return Dart_True(); |
41 | } |
42 | |
43 | Dart_Handle IsolateNameServerNatives::RemovePortNameMapping( |
44 | const std::string& name) { |
45 | auto name_server = UIDartState::Current()->GetIsolateNameServer(); |
46 | if (!name_server) { |
47 | return Dart_False(); |
48 | } |
49 | if (!name_server->RemoveIsolateNameMapping(name)) { |
50 | return Dart_False(); |
51 | } |
52 | return Dart_True(); |
53 | } |
54 | |
55 | #define FOR_EACH_BINDING(V) \ |
56 | V(IsolateNameServerNatives, LookupPortByName) \ |
57 | V(IsolateNameServerNatives, RegisterPortWithName) \ |
58 | V(IsolateNameServerNatives, RemovePortNameMapping) |
59 | |
60 | FOR_EACH_BINDING(DART_NATIVE_CALLBACK_STATIC) |
61 | |
62 | #define DART_REGISTER_NATIVE_STATIC_(CLASS, METHOD) \ |
63 | DART_REGISTER_NATIVE_STATIC(CLASS, METHOD), |
64 | |
65 | void IsolateNameServerNatives::RegisterNatives( |
66 | tonic::DartLibraryNatives* natives) { |
67 | natives->Register({FOR_EACH_BINDING(DART_REGISTER_NATIVE_STATIC_)}); |
68 | } |
69 | |
70 | } // namespace flutter |
71 |