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_FML_NATIVE_LIBRARY_H_
6#define FLUTTER_FML_NATIVE_LIBRARY_H_
7
8#include <optional>
9
10#include "flutter/fml/build_config.h"
11#include "flutter/fml/macros.h"
12#include "flutter/fml/memory/ref_counted.h"
13#include "flutter/fml/memory/ref_ptr.h"
14
15#if defined(FML_OS_WIN)
16#include <windows.h>
17#endif // defined(FML_OS_WIN)
18
19namespace fml {
20class NativeLibrary : public fml::RefCountedThreadSafe<NativeLibrary> {
21 public:
22#if FML_OS_WIN
23 using Handle = HMODULE;
24 using SymbolHandle = FARPROC;
25#else // FML_OS_WIN
26 using Handle = void*;
27 using SymbolHandle = void*;
28#endif // FML_OS_WIN
29
30 static fml::RefPtr<NativeLibrary> Create(const char* path);
31
32 static fml::RefPtr<NativeLibrary> CreateWithHandle(
33 Handle handle,
34 bool close_handle_when_done);
35
36 static fml::RefPtr<NativeLibrary> CreateForCurrentProcess();
37
38 template <typename T>
39 const std::optional<T> ResolveFunction(const char* symbol) {
40 auto* resolved_symbol = Resolve(symbol);
41 if (!resolved_symbol) {
42 return std::nullopt;
43 }
44 return std::optional<T>(reinterpret_cast<T>(resolved_symbol));
45 }
46
47 const uint8_t* ResolveSymbol(const char* symbol) {
48 auto* resolved_symbol = reinterpret_cast<const uint8_t*>(Resolve(symbol));
49 if (resolved_symbol == nullptr) {
50 FML_DLOG(INFO) << "Could not resolve symbol in library: " << symbol;
51 }
52 return resolved_symbol;
53 }
54
55 private:
56 Handle handle_ = nullptr;
57 bool close_handle_ = true;
58
59 explicit NativeLibrary(const char* path);
60
61 NativeLibrary(Handle handle, bool close_handle);
62
63 ~NativeLibrary();
64
65 Handle GetHandle() const;
66 SymbolHandle Resolve(const char* symbol) const;
67
68 FML_DISALLOW_COPY_AND_ASSIGN(NativeLibrary);
69 FML_FRIEND_REF_COUNTED_THREAD_SAFE(NativeLibrary);
70 FML_FRIEND_MAKE_REF_COUNTED(NativeLibrary);
71};
72
73} // namespace fml
74
75#endif // FLUTTER_FML_NATIVE_LIBRARY_H_
76

source code of flutter_engine/flutter/fml/native_library.h