blob: e2b9ca7e6d1d5ad37f21f6174325708ab13820c7 [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"
thestige38fbd62016-06-10 21:54:4014#include "base/strings/string_piece.h"
[email protected]f38e25f2009-04-21 00:56:0715#include "build/build_config.h"
16
17#if defined(OS_WIN)
18#include <windows.h>
19#elif defined(OS_MACOSX)
[email protected]6770f2a2010-11-15 21:22:5520#import <CoreFoundation/CoreFoundation.h>
[email protected]f38e25f2009-04-21 00:56:0721#endif // OS_*
22
[email protected]f38e25f2009-04-21 00:56:0723namespace base {
24
[email protected]a3ef4832013-02-02 05:12:3325class FilePath;
26
[email protected]f38e25f2009-04-21 00:56:0727#if defined(OS_WIN)
thestige38fbd62016-06-10 21:54:4028using NativeLibrary = HMODULE;
[email protected]f38e25f2009-04-21 00:56:0729#elif defined(OS_MACOSX)
[email protected]108c2a12009-06-05 22:18:0930enum NativeLibraryType {
31 BUNDLE,
32 DYNAMIC_LIB
33};
[email protected]5625f3cf2013-03-15 12:10:5834enum NativeLibraryObjCStatus {
35 OBJC_UNKNOWN,
36 OBJC_PRESENT,
37 OBJC_NOT_PRESENT,
38};
[email protected]108c2a12009-06-05 22:18:0939struct NativeLibraryStruct {
40 NativeLibraryType type;
[email protected]d8921c432010-01-28 16:08:0941 CFBundleRefNum bundle_resource_ref;
[email protected]5625f3cf2013-03-15 12:10:5842 NativeLibraryObjCStatus objc_status;
[email protected]108c2a12009-06-05 22:18:0943 union {
44 CFBundleRef bundle;
45 void* dylib;
46 };
47};
thestige38fbd62016-06-10 21:54:4048using NativeLibrary = NativeLibraryStruct*;
[email protected]e43eddf12009-12-29 00:32:5249#elif defined(OS_POSIX)
thestige38fbd62016-06-10 21:54:4050using NativeLibrary = void*;
[email protected]f38e25f2009-04-21 00:56:0751#endif // OS_*
52
[email protected]0f998442014-03-25 01:59:0953struct BASE_EXPORT NativeLibraryLoadError {
54#if defined(OS_WIN)
55 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
rockot596a0dd2016-08-26 00:57:5168struct BASE_EXPORT NativeLibraryOptions {
69 NativeLibraryOptions() = default;
70 NativeLibraryOptions(const NativeLibraryOptions& options) = default;
71
72 // If |true|, a loaded library is required to prefer local symbol resolution
73 // before considering global symbols. Note that this is already the default
74 // behavior on most systems. Setting this to |false| does not guarantee the
75 // inverse, i.e., it does not force a preference for global symbols over local
76 // ones.
77 bool prefer_own_symbols = false;
78};
79
[email protected]f38e25f2009-04-21 00:56:0780// Loads a native library from disk. Release it with UnloadNativeLibrary when
[email protected]84479322011-04-18 22:06:2281// you're done. Returns NULL on failure.
[email protected]0f998442014-03-25 01:59:0982// If |error| is not NULL, it may be filled in on load error.
[email protected]0bea7252011-08-05 15:34:0083BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0984 NativeLibraryLoadError* error);
[email protected]f38e25f2009-04-21 00:56:0785
rockot596a0dd2016-08-26 00:57:5186// Loads a native library from disk. Release it with UnloadNativeLibrary when
87// you're done. Returns NULL on failure.
88// If |error| is not NULL, it may be filled in on load error.
89BASE_EXPORT NativeLibrary LoadNativeLibraryWithOptions(
90 const FilePath& library_path,
91 const NativeLibraryOptions& options,
92 NativeLibraryLoadError* error);
93
[email protected]f38e25f2009-04-21 00:56:0794// Unloads a native library.
[email protected]0bea7252011-08-05 15:34:0095BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
[email protected]f38e25f2009-04-21 00:56:0796
97// Gets a function pointer from a native library.
[email protected]0bea7252011-08-05 15:34:0098BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4099 StringPiece name);
[email protected]108c2a12009-06-05 22:18:09100
101// Returns the full platform specific name for a native library.
thestig02c965b2016-06-14 18:52:23102// |name| must be ASCII.
[email protected]108c2a12009-06-05 22:18:09103// For example:
104// "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
thestig02c965b2016-06-14 18:52:23105// "libmylib.dylib" on Mac.
106BASE_EXPORT std::string GetNativeLibraryName(StringPiece name);
[email protected]f38e25f2009-04-21 00:56:07107
108} // namespace base
109
110#endif // BASE_NATIVE_LIBRARY_H_