Flutter Engine
native_library_posix.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/fml/native_library.h"
6 
7 #include <dlfcn.h>
8 #include <fcntl.h>
9 
10 namespace fml {
11 
12 NativeLibrary::NativeLibrary(const char* path) {
13  ::dlerror();
14  handle_ = ::dlopen(path, RTLD_NOW);
15  if (handle_ == nullptr) {
16  FML_DLOG(ERROR) << "Could not open library '" << path << "' due to error '"
17  << ::dlerror() << "'.";
18  }
19 }
20 
21 NativeLibrary::NativeLibrary(Handle handle, bool close_handle)
22  : handle_(handle), close_handle_(close_handle) {}
23 
24 NativeLibrary::~NativeLibrary() {
25  if (handle_ == nullptr) {
26  return;
27  }
28 
29  if (close_handle_) {
30  ::dlerror();
31  if (::dlclose(handle_) != 0) {
32  handle_ = nullptr;
33  FML_LOG(ERROR) << "Could not close library due to error '" << ::dlerror()
34  << "'.";
35  }
36  }
37 }
38 
39 NativeLibrary::Handle NativeLibrary::GetHandle() const {
40  return handle_;
41 }
42 
44  auto library = fml::AdoptRef(new NativeLibrary(path));
45  return library->GetHandle() != nullptr ? library : nullptr;
46 }
47 
49  Handle handle,
50  bool close_handle_when_done) {
51  auto library =
52  fml::AdoptRef(new NativeLibrary(handle, close_handle_when_done));
53  return library->GetHandle() != nullptr ? library : nullptr;
54 }
55 
57  return fml::AdoptRef(new NativeLibrary(RTLD_DEFAULT, false));
58 }
59 
60 const uint8_t* NativeLibrary::ResolveSymbol(const char* symbol) {
61  auto* resolved_symbol = static_cast<const uint8_t*>(::dlsym(handle_, symbol));
62  if (resolved_symbol == nullptr) {
63  FML_DLOG(INFO) << "Could not resolve symbol in library: " << symbol;
64  }
65  return resolved_symbol;
66 }
67 
68 } // namespace fml
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
static fml::RefPtr< NativeLibrary > CreateForCurrentProcess()
static fml::RefPtr< NativeLibrary > CreateWithHandle(Handle handle, bool close_handle_when_done)
#define FML_LOG(severity)
Definition: logging.h:65
Definition: ascii_trie.cc:9
static fml::RefPtr< NativeLibrary > Create(const char *path)
RefPtr< T > AdoptRef(T *ptr)
Definition: ref_ptr.h:218
#define FML_DLOG(severity)
Definition: logging.h:85
const uint8_t * ResolveSymbol(const char *symbol)