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