evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 8 | |
| 9 | // This file defines a cross-platform "NativeLibrary" type which represents |
| 10 | // a loadable module. |
| 11 | |
| 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 | |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 20 | #include "base/string16.h" |
| 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 | class FilePath; |
| 30 | |
| 31 | namespace base { |
| 32 | |
| 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 | }; |
| 40 | struct NativeLibraryStruct { |
| 41 | NativeLibraryType type; |
stuartmorgan@chromium.org | c7f74a2 | 2010-01-29 01:08:09 +0900 | [diff] [blame] | 42 | CFBundleRefNum bundle_resource_ref; |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 43 | union { |
| 44 | CFBundleRef bundle; |
| 45 | void* dylib; |
| 46 | }; |
| 47 | }; |
| 48 | typedef NativeLibraryStruct* NativeLibrary; |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 49 | #elif defined(OS_POSIX) |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 50 | typedef void* NativeLibrary; |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 51 | #endif // OS_* |
| 52 | |
| 53 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 54 | // you're done. |
| 55 | NativeLibrary LoadNativeLibrary(const FilePath& library_path); |
| 56 | |
ananta@chromium.org | 4de229a | 2010-11-20 08:33:13 +0900 | [diff] [blame^] | 57 | #if defined(OS_WIN) |
| 58 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 59 | // you're done. |
| 60 | // This function retrieves the LoadLibrary function exported from kernel32.dll |
| 61 | // and calls it instead of directly calling the LoadLibrary function via the |
| 62 | // import table. |
| 63 | NativeLibrary LoadNativeLibraryDynamically(const FilePath& library_path); |
| 64 | #endif // OS_WIN |
| 65 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 66 | // Unloads a native library. |
| 67 | void UnloadNativeLibrary(NativeLibrary library); |
| 68 | |
| 69 | // Gets a function pointer from a native library. |
| 70 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 71 | const char* name); |
| 72 | |
| 73 | // Returns the full platform specific name for a native library. |
| 74 | // For example: |
| 75 | // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, |
| 76 | // "mylib.dylib" on Mac. |
| 77 | string16 GetNativeLibraryName(const string16& name); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 78 | |
| 79 | } // namespace base |
| 80 | |
| 81 | #endif // BASE_NATIVE_LIBRARY_H_ |