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