[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 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 BASE_NATIVE_LIBRARY_H_ |
| 6 | #define BASE_NATIVE_LIBRARY_H_ |
| 7 | |
| 8 | // This file defines a cross-platform "NativeLibrary" type which represents |
| 9 | // a loadable module. |
| 10 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 13 | #include "base/base_export.h" |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 15 | #include "build/build_config.h" |
| 16 | |
| 17 | #if defined(OS_WIN) |
| 18 | #include <windows.h> |
| 19 | #elif defined(OS_MACOSX) |
[email protected] | 6770f2a | 2010-11-15 21:22:55 | [diff] [blame] | 20 | #import <CoreFoundation/CoreFoundation.h> |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 21 | #endif // OS_* |
| 22 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 23 | namespace base { |
| 24 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 25 | class FilePath; |
| 26 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 27 | #if defined(OS_WIN) |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 28 | using NativeLibrary = HMODULE; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 29 | #elif defined(OS_MACOSX) |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 30 | enum NativeLibraryType { |
| 31 | BUNDLE, |
| 32 | DYNAMIC_LIB |
| 33 | }; |
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 34 | enum NativeLibraryObjCStatus { |
| 35 | OBJC_UNKNOWN, |
| 36 | OBJC_PRESENT, |
| 37 | OBJC_NOT_PRESENT, |
| 38 | }; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 39 | struct NativeLibraryStruct { |
| 40 | NativeLibraryType type; |
[email protected] | d8921c43 | 2010-01-28 16:08:09 | [diff] [blame] | 41 | CFBundleRefNum bundle_resource_ref; |
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 42 | NativeLibraryObjCStatus objc_status; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 43 | union { |
| 44 | CFBundleRef bundle; |
| 45 | void* dylib; |
| 46 | }; |
| 47 | }; |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 48 | using NativeLibrary = NativeLibraryStruct*; |
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 49 | #elif defined(OS_POSIX) |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 50 | using NativeLibrary = void*; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 51 | #endif // OS_* |
| 52 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 53 | struct BASE_EXPORT NativeLibraryLoadError { |
| 54 | #if defined(OS_WIN) |
| 55 | NativeLibraryLoadError() : code(0) {} |
| 56 | #endif // OS_WIN |
| 57 | |
| 58 | // Returns a string representation of the load error. |
| 59 | std::string ToString() const; |
| 60 | |
| 61 | #if defined(OS_WIN) |
| 62 | DWORD code; |
| 63 | #else |
| 64 | std::string message; |
| 65 | #endif // OS_WIN |
| 66 | }; |
| 67 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 68 | struct BASE_EXPORT NativeLibraryOptions { |
| 69 | NativeLibraryOptions() = default; |
| 70 | NativeLibraryOptions(const NativeLibraryOptions& options) = default; |
| 71 | |
| 72 | // If |true|, a loaded library is required to prefer local symbol resolution |
| 73 | // before considering global symbols. Note that this is already the default |
| 74 | // behavior on most systems. Setting this to |false| does not guarantee the |
| 75 | // inverse, i.e., it does not force a preference for global symbols over local |
| 76 | // ones. |
| 77 | bool prefer_own_symbols = false; |
| 78 | }; |
| 79 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 80 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 81 | // you're done. Returns NULL on failure. |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 82 | // If |error| is not NULL, it may be filled in on load error. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 83 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 84 | NativeLibraryLoadError* error); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 85 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 86 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 87 | // you're done. Returns NULL on failure. |
| 88 | // If |error| is not NULL, it may be filled in on load error. |
| 89 | BASE_EXPORT NativeLibrary LoadNativeLibraryWithOptions( |
| 90 | const FilePath& library_path, |
| 91 | const NativeLibraryOptions& options, |
| 92 | NativeLibraryLoadError* error); |
| 93 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 94 | // Unloads a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 95 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 96 | |
| 97 | // Gets a function pointer from a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 98 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 99 | StringPiece name); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 100 | |
| 101 | // Returns the full platform specific name for a native library. |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 102 | // |name| must be ASCII. |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 103 | // For example: |
| 104 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 105 | // "libmylib.dylib" on Mac. |
| 106 | BASE_EXPORT std::string GetNativeLibraryName(StringPiece name); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 107 | |
| 108 | } // namespace base |
| 109 | |
| 110 | #endif // BASE_NATIVE_LIBRARY_H_ |