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