[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 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 68 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 69 | // you're done. Returns NULL on failure. |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 70 | // If |error| is not NULL, it may be filled in on load error. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 71 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 72 | NativeLibraryLoadError* error); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 73 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 74 | #if defined(OS_WIN) |
75 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
76 | // you're done. | ||||
77 | // This function retrieves the LoadLibrary function exported from kernel32.dll | ||||
78 | // and calls it instead of directly calling the LoadLibrary function via the | ||||
79 | // import table. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 80 | BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically( |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 81 | const FilePath& library_path); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 82 | #endif // OS_WIN |
83 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 84 | // Unloads a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 85 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 86 | |
87 | // Gets a function pointer from a native library. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 88 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 89 | StringPiece name); |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 90 | |
91 | // Returns the full platform specific name for a native library. | ||||
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame^] | 92 | // |name| must be ASCII. |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 93 | // For example: |
94 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, | ||||
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame^] | 95 | // "libmylib.dylib" on Mac. |
96 | BASE_EXPORT std::string GetNativeLibraryName(StringPiece name); | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 97 | |
98 | } // namespace base | ||||
99 | |||||
100 | #endif // BASE_NATIVE_LIBRARY_H_ |