| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_NATIVE_LIBRARY_H_ | 5 #ifndef BASE_NATIVE_LIBRARY_H_ |
| 6 #define BASE_NATIVE_LIBRARY_H_ | 6 #define BASE_NATIVE_LIBRARY_H_ |
| 7 | 7 |
| 8 // This file defines a cross-platform "NativeLibrary" type which represents | 8 // This file defines a cross-platform "NativeLibrary" type which represents |
| 9 // a loadable module. | 9 // a loadable module. |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/base_export.h" | 13 #include "base/base_export.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 | 17 |
| 18 #if defined(OS_WIN) | 18 #if defined(OS_MACOSX) |
| 19 #include <windows.h> | |
| 20 #elif defined(OS_MACOSX) | |
| 21 #import <CoreFoundation/CoreFoundation.h> | 19 #import <CoreFoundation/CoreFoundation.h> |
| 22 #endif // OS_* | 20 #endif // OS_* |
| 23 | 21 |
| 24 namespace base { | 22 namespace base { |
| 25 | 23 |
| 26 class FilePath; | 24 class FilePath; |
| 27 | 25 |
| 28 #if defined(OS_WIN) | 26 #if defined(OS_MACOSX) |
| 29 typedef HMODULE NativeLibrary; | |
| 30 #elif defined(OS_MACOSX) | |
| 31 enum NativeLibraryType { | 27 enum NativeLibraryType { |
| 32 BUNDLE, | 28 BUNDLE, |
| 33 DYNAMIC_LIB | 29 DYNAMIC_LIB |
| 34 }; | 30 }; |
| 35 enum NativeLibraryObjCStatus { | 31 enum NativeLibraryObjCStatus { |
| 36 OBJC_UNKNOWN, | 32 OBJC_UNKNOWN, |
| 37 OBJC_PRESENT, | 33 OBJC_PRESENT, |
| 38 OBJC_NOT_PRESENT, | 34 OBJC_NOT_PRESENT, |
| 39 }; | 35 }; |
| 40 struct NativeLibraryStruct { | 36 struct NativeLibraryStruct { |
| 41 NativeLibraryType type; | 37 NativeLibraryType type; |
| 42 CFBundleRefNum bundle_resource_ref; | 38 CFBundleRefNum bundle_resource_ref; |
| 43 NativeLibraryObjCStatus objc_status; | 39 NativeLibraryObjCStatus objc_status; |
| 44 union { | 40 union { |
| 45 CFBundleRef bundle; | 41 CFBundleRef bundle; |
| 46 void* dylib; | 42 void* dylib; |
| 47 }; | 43 }; |
| 48 }; | 44 }; |
| 49 typedef NativeLibraryStruct* NativeLibrary; | 45 typedef NativeLibraryStruct* NativeLibrary; |
| 50 #elif defined(OS_POSIX) | 46 #elif defined(OS_POSIX) |
| 51 typedef void* NativeLibrary; | 47 typedef void* NativeLibrary; |
| 52 #endif // OS_* | 48 #endif // OS_* |
| 53 | 49 |
| 54 struct BASE_EXPORT NativeLibraryLoadError { | 50 struct BASE_EXPORT NativeLibraryLoadError { |
| 55 #if defined(OS_WIN) | |
| 56 NativeLibraryLoadError() : code(0) {} | |
| 57 #endif // OS_WIN | |
| 58 | 51 |
| 59 // Returns a string representation of the load error. | 52 // Returns a string representation of the load error. |
| 60 std::string ToString() const; | 53 std::string ToString() const; |
| 61 | 54 |
| 62 #if defined(OS_WIN) | |
| 63 DWORD code; | |
| 64 #else | |
| 65 std::string message; | 55 std::string message; |
| 66 #endif // OS_WIN | |
| 67 }; | 56 }; |
| 68 | 57 |
| 69 // Loads a native library from disk. Release it with UnloadNativeLibrary when | 58 // Loads a native library from disk. Release it with UnloadNativeLibrary when |
| 70 // you're done. Returns NULL on failure. | 59 // you're done. Returns NULL on failure. |
| 71 // If |error| is not NULL, it may be filled in on load error. | 60 // If |error| is not NULL, it may be filled in on load error. |
| 72 BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, | 61 BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path, |
| 73 NativeLibraryLoadError* error); | 62 NativeLibraryLoadError* error); |
| 74 | 63 |
| 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. | |
| 81 BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically( | |
| 82 const FilePath& library_path); | |
| 83 #endif // OS_WIN | |
| 84 | |
| 85 // Unloads a native library. | 64 // Unloads a native library. |
| 86 BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); | 65 BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library); |
| 87 | 66 |
| 88 // Gets a function pointer from a native library. | 67 // Gets a function pointer from a native library. |
| 89 BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 68 BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
| 90 const char* name); | 69 const char* name); |
| 91 | 70 |
| 92 // Returns the full platform specific name for a native library. | 71 // Returns the full platform specific name for a native library. |
| 93 // For example: | 72 // For example: |
| 94 // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, | 73 // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux, |
| 95 // "mylib.dylib" on Mac. | 74 // "mylib.dylib" on Mac. |
| 96 BASE_EXPORT string16 GetNativeLibraryName(const string16& name); | 75 BASE_EXPORT string16 GetNativeLibraryName(const string16& name); |
| 97 | 76 |
| 98 } // namespace base | 77 } // namespace base |
| 99 | 78 |
| 100 #endif // BASE_NATIVE_LIBRARY_H_ | 79 #endif // BASE_NATIVE_LIBRARY_H_ |
| OLD | NEW |