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