native_library.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2011 The Chromium 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. #ifndef BASE_NATIVE_LIBRARY_H_
  5. #define BASE_NATIVE_LIBRARY_H_
  6. // This file defines a cross-platform "NativeLibrary" type which represents
  7. // a loadable module.
  8. #include <string>
  9. #include "base/base_export.h"
  10. #include "base/files/file_path.h"
  11. #include "base/strings/string_piece.h"
  12. #include "build/build_config.h"
  13. #if BUILDFLAG(IS_WIN)
  14. #include <windows.h>
  15. #elif BUILDFLAG(IS_APPLE)
  16. #import <CoreFoundation/CoreFoundation.h>
  17. #endif // OS_*
  18. namespace base {
  19. #if BUILDFLAG(IS_WIN)
  20. using NativeLibrary = HMODULE;
  21. #elif BUILDFLAG(IS_APPLE)
  22. enum NativeLibraryType {
  23. BUNDLE,
  24. DYNAMIC_LIB
  25. };
  26. enum NativeLibraryObjCStatus {
  27. OBJC_UNKNOWN,
  28. OBJC_PRESENT,
  29. OBJC_NOT_PRESENT,
  30. };
  31. struct NativeLibraryStruct {
  32. NativeLibraryType type;
  33. CFBundleRefNum bundle_resource_ref;
  34. NativeLibraryObjCStatus objc_status;
  35. union {
  36. CFBundleRef bundle;
  37. void* dylib;
  38. };
  39. };
  40. using NativeLibrary = NativeLibraryStruct*;
  41. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  42. using NativeLibrary = void*;
  43. #endif // OS_*
  44. struct BASE_EXPORT NativeLibraryLoadError {
  45. #if BUILDFLAG(IS_WIN)
  46. NativeLibraryLoadError() : code(0) {}
  47. #endif // BUILDFLAG(IS_WIN)
  48. // Returns a string representation of the load error.
  49. std::string ToString() const;
  50. #if BUILDFLAG(IS_WIN)
  51. DWORD code;
  52. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  53. std::string message;
  54. #endif // BUILDFLAG(IS_WIN)
  55. };
  56. struct BASE_EXPORT NativeLibraryOptions {
  57. NativeLibraryOptions() = default;
  58. NativeLibraryOptions(const NativeLibraryOptions& options) = default;
  59. // If |true|, a loaded library is required to prefer local symbol resolution
  60. // before considering global symbols. Note that this is already the default
  61. // behavior on most systems. Setting this to |false| does not guarantee the
  62. // inverse, i.e., it does not force a preference for global symbols over local
  63. // ones.
  64. bool prefer_own_symbols = false;
  65. };
  66. // Loads a native library from disk. Release it with UnloadNativeLibrary when
  67. // you're done. Returns NULL on failure.
  68. // If |error| is not NULL, it may be filled in on load error.
  69. BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
  70. NativeLibraryLoadError* error);
  71. #if BUILDFLAG(IS_WIN)
  72. // Loads a native library from the system directory using the appropriate flags.
  73. // The function first checks to see if the library is already loaded and will
  74. // get a handle if so. This method results in a lock that may block the calling
  75. // thread.
  76. BASE_EXPORT NativeLibrary
  77. LoadSystemLibrary(FilePath::StringPieceType name,
  78. NativeLibraryLoadError* error = nullptr);
  79. // Gets the module handle for the specified system library and pins it to
  80. // ensure it never gets unloaded. If the module is not loaded, it will first
  81. // call LoadSystemLibrary to load it. If the module cannot be pinned, this
  82. // method returns null and includes the error. This method results in a lock
  83. // that may block the calling thread.
  84. BASE_EXPORT NativeLibrary
  85. PinSystemLibrary(FilePath::StringPieceType name,
  86. NativeLibraryLoadError* error = nullptr);
  87. #endif
  88. // Loads a native library from disk. Release it with UnloadNativeLibrary when
  89. // you're done. Returns NULL on failure.
  90. // If |error| is not NULL, it may be filled in on load error.
  91. BASE_EXPORT NativeLibrary LoadNativeLibraryWithOptions(
  92. const FilePath& library_path,
  93. const NativeLibraryOptions& options,
  94. NativeLibraryLoadError* error);
  95. // Unloads a native library.
  96. BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
  97. // Gets a function pointer from a native library.
  98. BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
  99. StringPiece name);
  100. // Returns the full platform-specific name for a native library. |name| must be
  101. // ASCII. This is also the default name for the output of a gn |shared_library|
  102. // target. See tools/gn/docs/reference.md#shared_library.
  103. // For example for "mylib", it returns:
  104. // - "mylib.dll" on Windows
  105. // - "libmylib.so" on Linux
  106. // - "libmylib.dylib" on Mac
  107. BASE_EXPORT std::string GetNativeLibraryName(StringPiece name);
  108. // Returns the full platform-specific name for a gn |loadable_module| target.
  109. // See tools/gn/docs/reference.md#loadable_module
  110. // The returned name is the same as GetNativeLibraryName() on all platforms
  111. // except for Mac where for "mylib" it returns "mylib.so".
  112. BASE_EXPORT std::string GetLoadableModuleName(StringPiece name);
  113. } // namespace base
  114. #endif // BASE_NATIVE_LIBRARY_H_