Home | History | Annotate | Download | only in base
      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
      3 // found in the LICENSE file.
      4 
      5 #include "base/native_library.h"
      6 
      7 #include <dlfcn.h>
      8 
      9 #include "base/file_path.h"
     10 #include "base/file_util.h"
     11 #include "base/mac/scoped_cftyperef.h"
     12 #include "base/string_util.h"
     13 #include "base/threading/thread_restrictions.h"
     14 #include "base/utf_string_conversions.h"
     15 
     16 namespace base {
     17 
     18 // static
     19 NativeLibrary LoadNativeLibrary(const FilePath& library_path,
     20                                 std::string* error) {
     21   // dlopen() etc. open the file off disk.
     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   }
     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));
     38   if (!url)
     39     return NULL;
     40   CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
     41   if (!bundle)
     42     return NULL;
     43 
     44   NativeLibrary native_lib = new NativeLibraryStruct();
     45   native_lib->type = BUNDLE;
     46   native_lib->bundle = bundle;
     47   native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
     48   return native_lib;
     49 }
     50 
     51 // static
     52 void UnloadNativeLibrary(NativeLibrary library) {
     53   if (library->type == BUNDLE) {
     54     CFBundleCloseBundleResourceMap(library->bundle,
     55                                    library->bundle_resource_ref);
     56     CFRelease(library->bundle);
     57   } else {
     58     dlclose(library->dylib);
     59   }
     60   delete library;
     61 }
     62 
     63 // static
     64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
     65                                           const char* name) {
     66   if (library->type == BUNDLE) {
     67     base::mac::ScopedCFTypeRef<CFStringRef> symbol_name(
     68         CFStringCreateWithCString(kCFAllocatorDefault, name,
     69                                   kCFStringEncodingUTF8));
     70     return CFBundleGetFunctionPointerForName(library->bundle, symbol_name);
     71   }
     72   return dlsym(library->dylib, name);
     73 }
     74 
     75 // static
     76 string16 GetNativeLibraryName(const string16& name) {
     77   return name + ASCIIToUTF16(".dylib");
     78 }
     79 
     80 }  // namespace base
     81