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 #ifndef BASE_NATIVE_LIBRARY_H_
      6 #define BASE_NATIVE_LIBRARY_H_
      7 
      8 // This file defines a cross-platform "NativeLibrary" type which represents
      9 // a loadable module.
     10 
     11 #include <string>
     12 
     13 #include "base/base_export.h"
     14 #include "base/compiler_specific.h"
     15 #include "base/strings/string16.h"
     16 #include "build/build_config.h"
     17 
     18 #if defined(OS_WIN)
     19 #include <windows.h>
     20 #elif defined(OS_MACOSX)
     21 #import <CoreFoundation/CoreFoundation.h>
     22 #endif  // OS_*
     23 
     24 namespace base {
     25 
     26 class FilePath;
     27 
     28 #if defined(OS_WIN)
     29 typedef HMODULE NativeLibrary;
     30 #elif defined(OS_MACOSX)
     31 enum NativeLibraryType {
     32   BUNDLE,
     33   DYNAMIC_LIB
     34 };
     35 enum NativeLibraryObjCStatus {
     36   OBJC_UNKNOWN,
     37   OBJC_PRESENT,
     38   OBJC_NOT_PRESENT,
     39 };
     40 struct NativeLibraryStruct {
     41   NativeLibraryType type;
     42   CFBundleRefNum bundle_resource_ref;
     43   NativeLibraryObjCStatus objc_status;
     44   union {
     45     CFBundleRef bundle;
     46     void* dylib;
     47   };
     48 };
     49 typedef NativeLibraryStruct* NativeLibrary;
     50 #elif defined(OS_POSIX)
     51 typedef void* NativeLibrary;
     52 #endif  // OS_*
     53 
     54 struct BASE_EXPORT NativeLibraryLoadError {
     55 #if defined(OS_WIN)
     56   NativeLibraryLoadError() : code(0) {}
     57 #endif  // OS_WIN
     58 
     59   // Returns a string representation of the load error.
     60   std::string ToString() const;
     61 
     62 #if defined(OS_WIN)
     63   DWORD code;
     64 #else
     65   std::string message;
     66 #endif  // OS_WIN
     67 };
     68 
     69 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
     70 // you're done.  Returns NULL on failure.
     71 // If |error| is not NULL, it may be filled in on load error.
     72 BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
     73                                             NativeLibraryLoadError* error);
     74 
     75 #if defined(OS_WIN)
     76 // Loads a native library from disk.  Release it with UnloadNativeLibrary when
     77 // you're done.
     78 // This function retrieves the LoadLibrary function exported from kernel32.dll
     79 // and calls it instead of directly calling the LoadLibrary function via the
     80 // import table.
     81 BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically(
     82     const FilePath& library_path);
     83 #endif  // OS_WIN
     84 
     85 // Unloads a native library.
     86 BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
     87 
     88 // Gets a function pointer from a native library.
     89 BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
     90                                                       const char* name);
     91 
     92 // Returns the full platform specific name for a native library.
     93 // For example:
     94 // "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
     95 // "mylib.dylib" on Mac.
     96 BASE_EXPORT string16 GetNativeLibraryName(const string16& name);
     97 
     98 }  // namespace base
     99 
    100 #endif  // BASE_NATIVE_LIBRARY_H_
    101