rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [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 | |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 11 | #include "base/base_export.h" |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 12 | #include "build/build_config.h" |
| 13 | |
| 14 | #if defined(OS_WIN) |
| 15 | #include <windows.h> |
| 16 | #elif defined(OS_MACOSX) |
thomasvl@chromium.org | 1b721c8 | 2010-11-16 06:22:55 +0900 | [diff] [blame] | 17 | #import <CoreFoundation/CoreFoundation.h> |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 18 | #endif // OS_* |
| 19 | |
avi@chromium.org | 67d593d | 2013-06-11 04:06:57 +0900 | [diff] [blame^] | 20 | #include "base/strings/string16.h" |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 21 | |
evan@chromium.org | 9adf119 | 2010-11-13 06:53:16 +0900 | [diff] [blame] | 22 | // Macro useful for writing cross-platform function pointers. |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 23 | #if defined(OS_WIN) && !defined(CDECL) |
| 24 | #define CDECL __cdecl |
| 25 | #else |
| 26 | #define CDECL |
| 27 | #endif |
| 28 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 29 | namespace base { |
| 30 | |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 31 | class FilePath; |
| 32 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 33 | #if defined(OS_WIN) |
| 34 | typedef HMODULE NativeLibrary; |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 35 | #elif defined(OS_MACOSX) |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 36 | enum NativeLibraryType { |
| 37 | BUNDLE, |
| 38 | DYNAMIC_LIB |
| 39 | }; |
rsesek@chromium.org | 9ef941f | 2013-03-15 21:10:58 +0900 | [diff] [blame] | 40 | enum NativeLibraryObjCStatus { |
| 41 | OBJC_UNKNOWN, |
| 42 | OBJC_PRESENT, |
| 43 | OBJC_NOT_PRESENT, |
| 44 | }; |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 45 | struct NativeLibraryStruct { |
| 46 | NativeLibraryType type; |
stuartmorgan@chromium.org | c7f74a2 | 2010-01-29 01:08:09 +0900 | [diff] [blame] | 47 | CFBundleRefNum bundle_resource_ref; |
rsesek@chromium.org | 9ef941f | 2013-03-15 21:10:58 +0900 | [diff] [blame] | 48 | NativeLibraryObjCStatus objc_status; |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 49 | union { |
| 50 | CFBundleRef bundle; |
| 51 | void* dylib; |
| 52 | }; |
| 53 | }; |
| 54 | typedef NativeLibraryStruct* NativeLibrary; |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 55 | #elif defined(OS_POSIX) |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 56 | typedef void* NativeLibrary; |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 57 | #endif // OS_* |
| 58 | |
| 59 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
evan@chromium.org | 629d58e | 2011-04-19 07:06:22 +0900 | [diff] [blame] | 60 | // you're done. Returns NULL on failure. |
| 61 | // If |err| is not NULL, it may be filled in with an error message on |
| 62 | // error. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 63 | BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
| 64 | std::string* error); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 65 | |
ananta@chromium.org | 4de229a | 2010-11-20 08:33:13 +0900 | [diff] [blame] | 66 | #if defined(OS_WIN) |
| 67 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 68 | // you're done. |
| 69 | // This function retrieves the LoadLibrary function exported from kernel32.dll |
| 70 | // and calls it instead of directly calling the LoadLibrary function via the |
| 71 | // import table. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 72 | BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically( |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame] | 73 | const FilePath& library_path); |
ananta@chromium.org | 4de229a | 2010-11-20 08:33:13 +0900 | [diff] [blame] | 74 | #endif // OS_WIN |
| 75 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 76 | // Unloads a native library. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 77 | BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 78 | |
| 79 | // Gets a function pointer from a native library. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 80 | BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 81 | const char* name); |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 82 | |
| 83 | // Returns the full platform specific name for a native library. |
| 84 | // For example: |
| 85 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, |
| 86 | // "mylib.dylib" on Mac. |
darin@chromium.org | e585bed | 2011-08-06 00:34:00 +0900 | [diff] [blame] | 87 | BASE_EXPORT string16 GetNativeLibraryName(const string16& name); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 88 | |
| 89 | } // namespace base |
| 90 | |
| 91 | #endif // BASE_NATIVE_LIBRARY_H_ |