Flutter Engine
The Flutter Engine
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
5#include "flutter/testing/test_dart_native_resolver.h"
6
7#include <mutex>
8#include <vector>
9
10#include "flutter/fml/logging.h"
13
14namespace flutter {
15namespace testing {
16
18
20
23 native_callbacks_[name] = callback;
24}
26 void* callback_ptr) {
27 ffi_native_callbacks_[name] = callback_ptr;
28}
29
30Dart_NativeFunction TestDartNativeResolver::ResolveCallback(
31 const std::string& name) const {
32 auto found = native_callbacks_.find(name);
33 if (found == native_callbacks_.end()) {
34 return nullptr;
35 }
36
37 return found->second;
38}
39
40void* TestDartNativeResolver::ResolveFfiCallback(
41 const std::string& name) const {
42 auto found = ffi_native_callbacks_.find(name);
43 if (found == ffi_native_callbacks_.end()) {
44 return nullptr;
45 }
46 return found->second;
47}
48
49static std::mutex gIsolateResolversMutex;
50static std::map<Dart_Isolate, std::weak_ptr<TestDartNativeResolver>>
52
53Dart_NativeFunction TestDartNativeResolver::DartNativeEntryResolverCallback(
54 Dart_Handle dart_name,
55 int num_of_arguments,
56 bool* auto_setup_scope) {
57 auto name = tonic::StdStringFromDart(dart_name);
58
59 std::scoped_lock lock(gIsolateResolversMutex);
60 auto found = gIsolateResolvers.find(Dart_CurrentIsolate());
61 if (found == gIsolateResolvers.end()) {
62 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
63 return nullptr;
64 }
65
66 if (auto resolver = found->second.lock()) {
67 return resolver->ResolveCallback(name);
68 } else {
69 gIsolateResolvers.erase(found);
70 }
71
72 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
73 return nullptr;
74}
75
76static const uint8_t* DartNativeEntrySymbolCallback(
78 return reinterpret_cast<const uint8_t*>("¯\\_(ツ)_/¯");
79}
80
81void* TestDartNativeResolver::FfiNativeResolver(const char* name,
82 uintptr_t args_n) {
83 std::scoped_lock lock(gIsolateResolversMutex);
84 auto found = gIsolateResolvers.find(Dart_CurrentIsolate());
85 if (found == gIsolateResolvers.end()) {
86 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
87 return nullptr;
88 }
89
90 if (auto resolver = found->second.lock()) {
91 return resolver->ResolveFfiCallback(name);
92 } else {
93 gIsolateResolvers.erase(found);
94 }
95
96 FML_LOG(ERROR) << "Could not resolve native method for :" << name;
97 return nullptr;
98}
99
103 DartNativeEntryResolverCallback,
106 << "Could not set native resolver in test.";
107
108 result = Dart_SetFfiNativeResolver(Dart_RootLibrary(), &FfiNativeResolver);
110 << "Could not set FFI native resolver in test.";
111
112 std::scoped_lock lock(gIsolateResolversMutex);
113 gIsolateResolvers[Dart_CurrentIsolate()] = shared_from_this();
114
115 std::vector<Dart_Isolate> isolates_with_dead_resolvers;
116 for (const auto& entry : gIsolateResolvers) {
117 if (!entry.second.lock()) {
118 isolates_with_dead_resolvers.push_back(entry.first);
119 }
120 }
121
122 for (const auto& dead_isolate : isolates_with_dead_resolvers) {
123 gIsolateResolvers.erase(dead_isolate);
124 }
125}
126
127} // namespace testing
128} // namespace flutter
void AddFfiNativeCallback(const std::string &name, void *callback_ptr)
void AddNativeCallback(const std::string &name, Dart_NativeFunction callback)
DART_EXPORT Dart_Handle Dart_SetFfiNativeResolver(Dart_Handle library, Dart_FfiNativeResolver resolver)
DART_EXPORT Dart_Handle Dart_SetNativeResolver(Dart_Handle library, Dart_NativeEntryResolver resolver, Dart_NativeEntrySymbol symbol)
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
DART_EXPORT Dart_Isolate Dart_CurrentIsolate(void)
void(* Dart_NativeFunction)(Dart_NativeArguments arguments)
Definition: dart_api.h:3207
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_RootLibrary(void)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
GAsyncResult * result
#define FML_LOG(severity)
Definition: logging.h:82
#define FML_CHECK(condition)
Definition: logging.h:85
Dart_NativeFunction function
Definition: fuchsia.cc:51
static std::mutex gIsolateResolversMutex
static std::map< Dart_Isolate, std::weak_ptr< TestDartNativeResolver > > gIsolateResolvers
static const uint8_t * DartNativeEntrySymbolCallback(Dart_NativeFunction function)
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
bool CheckAndHandleError(Dart_Handle handle)
Definition: dart_error.cc:33
std::string StdStringFromDart(Dart_Handle handle)
#define ERROR(message)
Definition: elf_loader.cc:260