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#ifndef FLUTTER_TESTING_TEST_DART_NATIVE_RESOLVER_H_
6#define FLUTTER_TESTING_TEST_DART_NATIVE_RESOLVER_H_
7
8#include <functional>
9#include <map>
10#include <memory>
11
12#include "flutter/fml/macros.h"
13#include "third_party/dart/runtime/include/dart_api.h"
14
15#define CREATE_NATIVE_ENTRY(native_entry) \
16 ([&]() { \
17 static ::flutter::testing::NativeEntry closure; \
18 static Dart_NativeFunction entrypoint = [](Dart_NativeArguments args) { \
19 closure(args); \
20 }; \
21 closure = (native_entry); \
22 return entrypoint; \
23 })()
24
25namespace flutter {
26namespace testing {
27
28using NativeEntry = std::function<void(Dart_NativeArguments)>;
29
30class TestDartNativeResolver
31 : public std::enable_shared_from_this<TestDartNativeResolver> {
32 public:
33 TestDartNativeResolver();
34
35 ~TestDartNativeResolver();
36
37 void AddNativeCallback(std::string name, Dart_NativeFunction callback);
38
39 void SetNativeResolverForIsolate();
40
41 private:
42 std::map<std::string, Dart_NativeFunction> native_callbacks_;
43
44 Dart_NativeFunction ResolveCallback(std::string name) const;
45
46 static Dart_NativeFunction DartNativeEntryResolverCallback(
47 Dart_Handle dart_name,
48 int num_of_arguments,
49 bool* auto_setup_scope);
50
51 FML_DISALLOW_COPY_AND_ASSIGN(TestDartNativeResolver);
52};
53
54} // namespace testing
55} // namespace flutter
56
57#endif // FLUTTER_TESTING_TEST_DART_NATIVE_RESOLVER_H_
58