blob: 1e764da89aa6846c4d5b646a301d0d28ad9de799 [file] [log] [blame]
[email protected]26fbf802011-03-25 18:48:031// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f38e25f2009-04-21 00:56:072// 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
[email protected]0f998442014-03-25 01:59:0911#include <string>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
[email protected]5a8d4ce2013-12-18 17:42:2714#include "base/compiler_specific.h"
[email protected]0f998442014-03-25 01:59:0915#include "base/strings/string16.h"
[email protected]f38e25f2009-04-21 00:56:0716#include "build/build_config.h"
17
18#if defined(OS_WIN)
19#include <windows.h>
20#elif defined(OS_MACOSX)
[email protected]6770f2a2010-11-15 21:22:5521#import <CoreFoundation/CoreFoundation.h>
[email protected]f38e25f2009-04-21 00:56:0722#endif // OS_*
23
[email protected]f38e25f2009-04-21 00:56:0724namespace base {
25
[email protected]a3ef4832013-02-02 05:12:3326class FilePath;
27
[email protected]f38e25f2009-04-21 00:56:0728#if defined(OS_WIN)
29typedef HMODULE NativeLibrary;
[email protected]f38e25f2009-04-21 00:56:0730#elif defined(OS_MACOSX)
[email protected]108c2a12009-06-05 22:18:0931enum NativeLibraryType {
32 BUNDLE,
33 DYNAMIC_LIB
34};
[email protected]5625f3cf2013-03-15 12:10:5835enum NativeLibraryObjCStatus {
36 OBJC_UNKNOWN,
37 OBJC_PRESENT,
38 OBJC_NOT_PRESENT,
39};
[email protected]108c2a12009-06-05 22:18:0940struct NativeLibraryStruct {
41 NativeLibraryType type;
[email protected]d8921c432010-01-28 16:08:0942 CFBundleRefNum bundle_resource_ref;
[email protected]5625f3cf2013-03-15 12:10:5843 NativeLibraryObjCStatus objc_status;
[email protected]108c2a12009-06-05 22:18:0944 union {
45 CFBundleRef bundle;
46 void* dylib;
47 };
48};
49typedef NativeLibraryStruct* NativeLibrary;
[email protected]e43eddf12009-12-29 00:32:5250#elif defined(OS_POSIX)
[email protected]f38e25f2009-04-21 00:56:0751typedef void* NativeLibrary;
[email protected]f38e25f2009-04-21 00:56:0752#endif // OS_*
53
[email protected]0f998442014-03-25 01:59:0954struct 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
[email protected]f38e25f2009-04-21 00:56:0769// Loads a native library from disk. Release it with UnloadNativeLibrary when
[email protected]84479322011-04-18 22:06:2270// you're done. Returns NULL on failure.
[email protected]0f998442014-03-25 01:59:0971// If |error| is not NULL, it may be filled in on load error.
[email protected]0bea7252011-08-05 15:34:0072BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0973 NativeLibraryLoadError* error);
[email protected]f38e25f2009-04-21 00:56:0774
[email protected]3e246222010-11-19 23:33:1375#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.
[email protected]0bea7252011-08-05 15:34:0081BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically(
[email protected]26fbf802011-03-25 18:48:0382 const FilePath& library_path);
[email protected]3e246222010-11-19 23:33:1383#endif // OS_WIN
84
[email protected]f38e25f2009-04-21 00:56:0785// Unloads a native library.
[email protected]0bea7252011-08-05 15:34:0086BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
[email protected]f38e25f2009-04-21 00:56:0787
88// Gets a function pointer from a native library.
[email protected]0bea7252011-08-05 15:34:0089BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
90 const char* name);
[email protected]108c2a12009-06-05 22:18:0991
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.
[email protected]0bea7252011-08-05 15:34:0096BASE_EXPORT string16 GetNativeLibraryName(const string16& name);
[email protected]f38e25f2009-04-21 00:56:0797
98} // namespace base
99
100#endif // BASE_NATIVE_LIBRARY_H_