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_ |
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 | |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 12 | #include "base/base_api.h" |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 13 | #include "build/build_config.h" |
| 14 | |
| 15 | #if defined(OS_WIN) |
| 16 | #include <windows.h> |
| 17 | #elif defined(OS_MACOSX) |
thomasvl@chromium.org | 1b721c8 | 2010-11-16 06:22:55 +0900 | [diff] [blame] | 18 | #import <CoreFoundation/CoreFoundation.h> |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 19 | #endif // OS_* |
| 20 | |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 21 | #include "base/string16.h" |
| 22 | |
evan@chromium.org | 9adf119 | 2010-11-13 06:53:16 +0900 | [diff] [blame] | 23 | // Macro useful for writing cross-platform function pointers. |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 24 | #if defined(OS_WIN) && !defined(CDECL) |
| 25 | #define CDECL __cdecl |
| 26 | #else |
| 27 | #define CDECL |
| 28 | #endif |
| 29 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 30 | class FilePath; |
| 31 | |
| 32 | namespace base { |
| 33 | |
| 34 | #if defined(OS_WIN) |
| 35 | typedef HMODULE NativeLibrary; |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 36 | #elif defined(OS_MACOSX) |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 37 | enum NativeLibraryType { |
| 38 | BUNDLE, |
| 39 | DYNAMIC_LIB |
| 40 | }; |
| 41 | struct NativeLibraryStruct { |
| 42 | NativeLibraryType type; |
stuartmorgan@chromium.org | c7f74a2 | 2010-01-29 01:08:09 +0900 | [diff] [blame] | 43 | CFBundleRefNum bundle_resource_ref; |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [diff] [blame] | 44 | union { |
| 45 | CFBundleRef bundle; |
| 46 | void* dylib; |
| 47 | }; |
| 48 | }; |
| 49 | typedef NativeLibraryStruct* NativeLibrary; |
evan@chromium.org | 875bb6e | 2009-12-29 09:32:52 +0900 | [diff] [blame] | 50 | #elif defined(OS_POSIX) |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 51 | typedef void* NativeLibrary; |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 52 | #endif // OS_* |
| 53 | |
| 54 | // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 55 | // you're done. |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 56 | BASE_API NativeLibrary LoadNativeLibrary(const FilePath& library_path); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 57 | |
ananta@chromium.org | 4de229a | 2010-11-20 08:33:13 +0900 | [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. |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 64 | BASE_API NativeLibrary LoadNativeLibraryDynamically( |
| 65 | const FilePath& library_path); |
ananta@chromium.org | 4de229a | 2010-11-20 08:33:13 +0900 | [diff] [blame] | 66 | #endif // OS_WIN |
| 67 | |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 68 | // Unloads a native library. |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 69 | BASE_API void UnloadNativeLibrary(NativeLibrary library); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 70 | |
| 71 | // Gets a function pointer from a native library. |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 72 | BASE_API void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 73 | const char* name); |
jcampan@chromium.org | d9e2be8 | 2009-06-06 07:18:09 +0900 | [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. |
rvargas@google.com | e106ef6 | 2011-03-26 03:48:03 +0900 | [diff] [blame^] | 79 | BASE_API string16 GetNativeLibraryName(const string16& name); |
evan@chromium.org | da642ae | 2009-04-21 09:56:07 +0900 | [diff] [blame] | 80 | |
| 81 | } // namespace base |
| 82 | |
| 83 | #endif // BASE_NATIVE_LIBRARY_H_ |