Flutter Engine
 
Loading...
Searching...
No Matches
test_dart_native_resolver.cc
Go to the documentation of this file.
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
6
7#include <mutex>
8#include <vector>
9
10#include "flutter/fml/logging.h"
13
14namespace flutter::testing {
15
17
19
21 Dart_NativeFunction callback) {
22 native_callbacks_[name] = callback;
23}
25 void* callback_ptr) {
26 ffi_native_callbacks_[name] = callback_ptr;
27}
28
29Dart_NativeFunction TestDartNativeResolver::ResolveCallback(
30 const std::string& name) const {
31 auto found = native_callbacks_.find(name);
32 if (found == native_callbacks_.end()) {
33 return nullptr;
34 }
35
36 return found->second;
37}
38
39void* TestDartNativeResolver::ResolveFfiCallback(
40 const std::string& name) const {
41 auto found = ffi_native_callbacks_.find(name);
42 if (found == ffi_native_callbacks_.end()) {
43 return nullptr;
44 }
45 return found->second;
46}
47
48static std::mutex gIsolateResolversMutex;
49static std::map<Dart_IsolateGroup, std::weak_ptr<TestDartNativeResolver>>
51
52Dart_NativeFunction TestDartNativeResolver::DartNativeEntryResolverCallback(
53 Dart_Handle dart_name,
54 int num_of_arguments,
55 bool* auto_setup_scope) {
56 auto name = tonic::StdStringFromDart(dart_name);
57
58 std::scoped_lock lock(gIsolateResolversMutex);
59 auto found = gIsolateResolvers.find(Dart_CurrentIsolateGroup());
60 if (found == gIsolateResolvers.end()) {
61 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
62 return nullptr;
63 }
64
65 if (auto resolver = found->second.lock()) {
66 return resolver->ResolveCallback(name);
67 } else {
68 gIsolateResolvers.erase(found);
69 }
70
71 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
72 return nullptr;
73}
74
75static const uint8_t* DartNativeEntrySymbolCallback(
76 Dart_NativeFunction function) {
77 return reinterpret_cast<const uint8_t*>("¯\\_(ツ)_/¯");
78}
79
80void* TestDartNativeResolver::FfiNativeResolver(const char* name,
81 uintptr_t args_n) {
82 std::scoped_lock lock(gIsolateResolversMutex);
83 auto found = gIsolateResolvers.find(Dart_CurrentIsolateGroup());
84 if (found == gIsolateResolvers.end()) {
85 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
86 return nullptr;
87 }
88
89 if (auto resolver = found->second.lock()) {
90 return resolver->ResolveFfiCallback(name);
91 } else {
92 gIsolateResolvers.erase(found);
93 }
94
95 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
96 return nullptr;
97}
98
100 FML_CHECK(!Dart_IsError(Dart_RootLibrary()));
101 auto result = Dart_SetNativeResolver(Dart_RootLibrary(),
102 DartNativeEntryResolverCallback,
105 << "Could not set native resolver in test.";
106
107 result = Dart_SetFfiNativeResolver(Dart_RootLibrary(), &FfiNativeResolver);
109 << "Could not set FFI native resolver in test.";
110
111 std::scoped_lock lock(gIsolateResolversMutex);
112 gIsolateResolvers[Dart_CurrentIsolateGroup()] = shared_from_this();
113
114 std::vector<Dart_IsolateGroup> isolates_with_dead_resolvers;
115 for (const auto& entry : gIsolateResolvers) {
116 if (!entry.second.lock()) {
117 isolates_with_dead_resolvers.push_back(entry.first);
118 }
119 }
120
121 for (const auto& dead_isolate : isolates_with_dead_resolvers) {
122 gIsolateResolvers.erase(dead_isolate);
123 }
124}
125
126} // namespace flutter::testing
void AddFfiNativeCallback(const std::string &name, void *callback_ptr)
void AddNativeCallback(const std::string &name, Dart_NativeFunction callback)
FlutterDesktopBinaryReply callback
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
static std::mutex gIsolateResolversMutex
static std::map< Dart_IsolateGroup, std::weak_ptr< TestDartNativeResolver > > gIsolateResolvers
static const uint8_t * DartNativeEntrySymbolCallback(Dart_NativeFunction function)
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
bool CheckAndHandleError(Dart_Handle handle)
Definition dart_error.cc:33
std::string StdStringFromDart(Dart_Handle handle)