[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" |
[email protected] | 5a8d4ce | 2013-12-18 17:42:27 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame^] | 15 | #include "base/strings/string16.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 16 | #include "build/build_config.h" |
17 | |||||
18 | #if defined(OS_WIN) | ||||
19 | #include <windows.h> | ||||
20 | #elif defined(OS_MACOSX) | ||||
[email protected] | 6770f2a | 2010-11-15 21:22:55 | [diff] [blame] | 21 | #import <CoreFoundation/CoreFoundation.h> |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 22 | #endif // OS_* |
23 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 24 | namespace base { |
25 | |||||
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 26 | class FilePath; |
27 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 28 | #if defined(OS_WIN) |
29 | typedef HMODULE NativeLibrary; | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 30 | #elif defined(OS_MACOSX) |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 31 | enum NativeLibraryType { |
32 | BUNDLE, | ||||
33 | DYNAMIC_LIB | ||||
34 | }; | ||||
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 35 | enum NativeLibraryObjCStatus { |
36 | OBJC_UNKNOWN, | ||||
37 | OBJC_PRESENT, | ||||
38 | OBJC_NOT_PRESENT, | ||||
39 | }; | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 40 | struct NativeLibraryStruct { |
41 | NativeLibraryType type; | ||||
[email protected] | d8921c43 | 2010-01-28 16:08:09 | [diff] [blame] | 42 | CFBundleRefNum bundle_resource_ref; |
[email protected] | 5625f3cf | 2013-03-15 12:10:58 | [diff] [blame] | 43 | NativeLibraryObjCStatus objc_status; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 44 | union { |
45 | CFBundleRef bundle; | ||||
46 | void* dylib; | ||||
47 | }; | ||||
48 | }; | ||||
49 | typedef NativeLibraryStruct* NativeLibrary; | ||||
[email protected] | e43eddf1 | 2009-12-29 00:32:52 | [diff] [blame] | 50 | #elif defined(OS_POSIX) |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 51 | typedef void* NativeLibrary; |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 52 | #endif // OS_* |
53 | |||||
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame^] | 54 | struct BASE_EXPORT NativeLibraryLoadError { |
55 | #if defined(OS_WIN) | ||||
56 | NativeLibraryLoadError() : code(0) {} | ||||
57 | #endif // OS_WIN | ||||
58 | |||||
59 | // Returns a string representation of the load error. | ||||
60 | std::string ToString() const; | ||||
61 | |||||
62 | #if defined(OS_WIN) | ||||
63 | DWORD code; | ||||
64 | #else | ||||
65 | std::string message; | ||||
66 | #endif // OS_WIN | ||||
67 | }; | ||||
68 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 69 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 70 | // you're done. Returns NULL on failure. |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame^] | 71 | // If |error| is not NULL, it may be filled in on load error. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 72 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame^] | 73 | NativeLibraryLoadError* error); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 74 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 75 | #if defined(OS_WIN) |
76 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
77 | // you're done. | ||||
78 | // This function retrieves the LoadLibrary function exported from kernel32.dll | ||||
79 | // and calls it instead of directly calling the LoadLibrary function via the | ||||
80 | // import table. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 81 | BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically( |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame] | 82 | const FilePath& library_path); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 83 | #endif // OS_WIN |
84 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 85 | // Unloads a native library. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 86 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 87 | |
88 | // Gets a function pointer from a native library. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 89 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
90 | const char* name); | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 91 | |
92 | // Returns the full platform specific name for a native library. | ||||
93 | // For example: | ||||
94 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, | ||||
95 | // "mylib.dylib" on Mac. | ||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 96 | BASE_EXPORT string16 GetNativeLibraryName(const string16& name); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 97 | |
98 | } // namespace base | ||||
99 | |||||
100 | #endif // BASE_NATIVE_LIBRARY_H_ |