Home | History | Annotate | Download | only in base
      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
      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/logging.h"
     11 #include "base/string_util.h"
     12 
     13 namespace base {
     14 
     15 // static
     16 NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
     17   void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY);
     18   if (!dl) {
     19     std::string error_message = dlerror();
     20     // Some obsolete plugins depend on libxul or libxpcom.
     21     // Ignore the error messages when failing to load these.
     22     if (error_message.find("libxul.so") == std::string::npos &&
     23         error_message.find("libxpcom.so") == std::string::npos) {
     24       LOG(ERROR) << "dlopen failed when trying to open " << library_path.value()
     25                  << ": " << error_message;
     26     }
     27   }
     28 
     29   return dl;
     30 }
     31 
     32 // static
     33 void UnloadNativeLibrary(NativeLibrary library) {
     34   int ret = dlclose(library);
     35   if (ret < 0) {
     36     LOG(ERROR) << "dlclose failed: " << dlerror();
     37     NOTREACHED();
     38   }
     39 }
     40 
     41 // static
     42 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
     43                                           const char* name) {
     44   return dlsym(library, name);
     45 }
     46 
     47 // static
     48 string16 GetNativeLibraryName(const string16& name) {
     49   return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so");
     50 }
     51 
     52 }  // namespace base
     53