[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_ | ||||
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 8 | |
9 | // This file defines a cross-platform "NativeLibrary" type which represents | ||||
10 | // a loadable module. | ||||
11 | |||||
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 12 | #include "base/base_api.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 13 | #include "build/build_config.h" |
14 | |||||
15 | #if defined(OS_WIN) | ||||
16 | #include <windows.h> | ||||
17 | #elif defined(OS_MACOSX) | ||||
[email protected] | 6770f2a | 2010-11-15 21:22:55 | [diff] [blame] | 18 | #import <CoreFoundation/CoreFoundation.h> |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 19 | #endif // OS_* |
20 | |||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 21 | #include "base/string16.h" |
22 | |||||
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 23 | // Macro useful for writing cross-platform function pointers. |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 24 | #if defined(OS_WIN) && !defined(CDECL) |
25 | #define CDECL __cdecl | ||||
26 | #else | ||||
27 | #define CDECL | ||||
28 | #endif | ||||
29 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 30 | class FilePath; |
31 | |||||
32 | namespace base { | ||||
33 | |||||
34 | #if defined(OS_WIN) | ||||
35 | typedef HMODULE NativeLibrary; | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 36 | #elif defined(OS_MACOSX) |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 37 | enum NativeLibraryType { |
38 | BUNDLE, | ||||
39 | DYNAMIC_LIB | ||||
40 | }; | ||||
41 | struct NativeLibraryStruct { | ||||
42 | NativeLibraryType type; | ||||
[email protected] | d8921c43 | 2010-01-28 16:08:09 | [diff] [blame] | 43 | CFBundleRefNum bundle_resource_ref; |
[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 | |||||
54 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
55 | // you're done. | ||||
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 56 | BASE_API NativeLibrary LoadNativeLibrary(const FilePath& library_path); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 57 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 58 | #if defined(OS_WIN) |
59 | // Loads a native library from disk. Release it with UnloadNativeLibrary when | ||||
60 | // you're done. | ||||
61 | // This function retrieves the LoadLibrary function exported from kernel32.dll | ||||
62 | // and calls it instead of directly calling the LoadLibrary function via the | ||||
63 | // import table. | ||||
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 64 | BASE_API NativeLibrary LoadNativeLibraryDynamically( |
65 | const FilePath& library_path); | ||||
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 66 | #endif // OS_WIN |
67 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 68 | // Unloads a native library. |
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 69 | BASE_API void UnloadNativeLibrary(NativeLibrary library); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 70 | |
71 | // Gets a function pointer from a native library. | ||||
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 72 | BASE_API void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
73 | const char* name); | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 74 | |
75 | // Returns the full platform specific name for a native library. | ||||
76 | // For example: | ||||
77 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, | ||||
78 | // "mylib.dylib" on Mac. | ||||
[email protected] | 26fbf80 | 2011-03-25 18:48:03 | [diff] [blame^] | 79 | BASE_API string16 GetNativeLibraryName(const string16& name); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 80 | |
81 | } // namespace base | ||||
82 | |||||
83 | #endif // BASE_NATIVE_LIBRARY_H_ |