OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/native_library.h" | 5 #include "base/native_library.h" |
6 | 6 |
| 7 #include <dlfcn.h> |
7 #import <Carbon/Carbon.h> | 8 #import <Carbon/Carbon.h> |
8 | 9 |
9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 11 #include "base/file_util.h" |
10 #include "base/scoped_cftyperef.h" | 12 #include "base/scoped_cftyperef.h" |
| 13 #include "base/string_util.h" |
11 | 14 |
12 namespace base { | 15 namespace base { |
13 | 16 |
14 // static | 17 // static |
15 NativeLibrary LoadNativeLibrary(const FilePath& library_path) { | 18 NativeLibrary LoadNativeLibrary(const FilePath& library_path) { |
| 19 if (library_path.Extension() == "dylib" || |
| 20 !file_util::DirectoryExists(library_path)) { |
| 21 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
| 22 if (!dylib) |
| 23 return NULL; |
| 24 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 25 native_lib->type = DYNAMIC_LIB; |
| 26 native_lib->dylib = dylib; |
| 27 return native_lib; |
| 28 } |
16 scoped_cftyperef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( | 29 scoped_cftyperef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( |
17 kCFAllocatorDefault, | 30 kCFAllocatorDefault, |
18 (const UInt8*)library_path.value().c_str(), | 31 (const UInt8*)library_path.value().c_str(), |
19 library_path.value().length(), | 32 library_path.value().length(), |
20 true)); | 33 true)); |
21 if (!url) | 34 if (!url) |
22 return NULL; | 35 return NULL; |
| 36 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
| 37 if (!bundle) |
| 38 return NULL; |
23 | 39 |
24 return CFBundleCreate(kCFAllocatorDefault, url.get()); | 40 NativeLibrary native_lib = new NativeLibraryStruct(); |
| 41 native_lib->type = BUNDLE; |
| 42 native_lib->bundle = bundle; |
| 43 return native_lib; |
25 } | 44 } |
26 | 45 |
27 // static | 46 // static |
28 void UnloadNativeLibrary(NativeLibrary library) { | 47 void UnloadNativeLibrary(NativeLibrary library) { |
29 CFRelease(library); | 48 if (library->type == BUNDLE) |
| 49 CFRelease(library->bundle); |
| 50 else |
| 51 dlclose(library->dylib); |
| 52 delete library; |
30 } | 53 } |
31 | 54 |
32 // static | 55 // static |
33 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 56 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
34 NativeLibraryFunctionNameType name) { | 57 const char* name) { |
35 return CFBundleGetFunctionPointerForName(library, name); | 58 if (library->type == BUNDLE) |
| 59 return CFBundleGetFunctionPointerForName(library->bundle, |
| 60 CFStringCreateWithCString(kCFAllocatorDefault, name, |
| 61 kCFStringEncodingUTF8)); |
| 62 |
| 63 return dlsym(library->dylib, name); |
| 64 } |
| 65 |
| 66 // static |
| 67 string16 GetNativeLibraryName(const string16& name) { |
| 68 return name + ASCIIToUTF16(".dylib"); |
36 } | 69 } |
37 | 70 |
38 } // namespace base | 71 } // namespace base |
OLD | NEW |