blob: 8d85f21eb60ac9e0497c41322fb971a4a8db5d09 [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"
Cliff Smolinskyf395bef2019-04-12 23:45:4414#include "base/files/file_path.h"
thestige38fbd62016-06-10 21:54:4015#include "base/strings/string_piece.h"
[email protected]f38e25f2009-04-21 00:56:0716#include "build/build_config.h"
17
18#if defined(OS_WIN)
19#include <windows.h>
Avi Drissman5b286372020-07-28 21:59:3820#elif defined(OS_APPLE)
[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
26#if defined(OS_WIN)
thestige38fbd62016-06-10 21:54:4027using NativeLibrary = HMODULE;
Avi Drissman5b286372020-07-28 21:59:3828#elif defined(OS_APPLE)
[email protected]108c2a12009-06-05 22:18:0929enum NativeLibraryType {
30 BUNDLE,
31 DYNAMIC_LIB
32};
[email protected]5625f3cf2013-03-15 12:10:5833enum NativeLibraryObjCStatus {
34 OBJC_UNKNOWN,
35 OBJC_PRESENT,
36 OBJC_NOT_PRESENT,
37};
[email protected]108c2a12009-06-05 22:18:0938struct NativeLibraryStruct {
39 NativeLibraryType type;
[email protected]d8921c432010-01-28 16:08:0940 CFBundleRefNum bundle_resource_ref;
[email protected]5625f3cf2013-03-15 12:10:5841 NativeLibraryObjCStatus objc_status;
[email protected]108c2a12009-06-05 22:18:0942 union {
43 CFBundleRef bundle;
44 void* dylib;
45 };
46};
thestige38fbd62016-06-10 21:54:4047using NativeLibrary = NativeLibraryStruct*;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3948#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
thestige38fbd62016-06-10 21:54:4049using NativeLibrary = void*;
[email protected]f38e25f2009-04-21 00:56:0750#endif // OS_*
51
[email protected]0f998442014-03-25 01:59:0952struct BASE_EXPORT NativeLibraryLoadError {
53#if defined(OS_WIN)
54 NativeLibraryLoadError() : code(0) {}
55#endif // OS_WIN
56
57 // Returns a string representation of the load error.
58 std::string ToString() const;
59
60#if defined(OS_WIN)
61 DWORD code;
Fabrice de Gans-Riberi306871de2018-05-16 19:38:3962#elif defined(OS_POSIX) || defined(OS_FUCHSIA)
[email protected]0f998442014-03-25 01:59:0963 std::string message;
64#endif // OS_WIN
65};
66
rockot596a0dd2016-08-26 00:57:5167struct BASE_EXPORT NativeLibraryOptions {
68 NativeLibraryOptions() = default;
69 NativeLibraryOptions(const NativeLibraryOptions& options) = default;
70
71 // If |true|, a loaded library is required to prefer local symbol resolution
72 // before considering global symbols. Note that this is already the default
73 // behavior on most systems. Setting this to |false| does not guarantee the
74 // inverse, i.e., it does not force a preference for global symbols over local
75 // ones.
76 bool prefer_own_symbols = false;
77};
78
[email protected]f38e25f2009-04-21 00:56:0779// Loads a native library from disk. Release it with UnloadNativeLibrary when
[email protected]84479322011-04-18 22:06:2280// you're done. Returns NULL on failure.
[email protected]0f998442014-03-25 01:59:0981// If |error| is not NULL, it may be filled in on load error.
[email protected]0bea7252011-08-05 15:34:0082BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0983 NativeLibraryLoadError* error);
[email protected]f38e25f2009-04-21 00:56:0784
Cliff Smolinskyf395bef2019-04-12 23:45:4485#if defined(OS_WIN)
86// Loads a native library from the system directory using the appropriate flags.
87// The function first checks to see if the library is already loaded and will
Cliff Smolinskyc5c52102019-05-03 20:51:5488// get a handle if so. This method results in a lock that may block the calling
89// thread.
90BASE_EXPORT NativeLibrary
91LoadSystemLibrary(FilePath::StringPieceType name,
92 NativeLibraryLoadError* error = nullptr);
93
94// Gets the module handle for the specified system library and pins it to
95// ensure it never gets unloaded. If the module is not loaded, it will first
96// call LoadSystemLibrary to load it. If the module cannot be pinned, this
97// method returns null and includes the error. This method results in a lock
98// that may block the calling thread.
99BASE_EXPORT NativeLibrary
100PinSystemLibrary(FilePath::StringPieceType name,
101 NativeLibraryLoadError* error = nullptr);
Cliff Smolinskyf395bef2019-04-12 23:45:44102#endif
103
rockot596a0dd2016-08-26 00:57:51104// Loads a native library from disk. Release it with UnloadNativeLibrary when
105// you're done. Returns NULL on failure.
106// If |error| is not NULL, it may be filled in on load error.
107BASE_EXPORT NativeLibrary LoadNativeLibraryWithOptions(
108 const FilePath& library_path,
109 const NativeLibraryOptions& options,
110 NativeLibraryLoadError* error);
111
[email protected]f38e25f2009-04-21 00:56:07112// Unloads a native library.
[email protected]0bea7252011-08-05 15:34:00113BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
[email protected]f38e25f2009-04-21 00:56:07114
115// Gets a function pointer from a native library.
[email protected]0bea7252011-08-05 15:34:00116BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:40117 StringPiece name);
[email protected]108c2a12009-06-05 22:18:09118
Xiaohan Wangd807ec32018-04-03 01:31:44119// Returns the full platform-specific name for a native library. |name| must be
120// ASCII. This is also the default name for the output of a gn |shared_library|
121// target. See tools/gn/docs/reference.md#shared_library.
122// For example for "mylib", it returns:
123// - "mylib.dll" on Windows
124// - "libmylib.so" on Linux
125// - "libmylib.dylib" on Mac
thestig02c965b2016-06-14 18:52:23126BASE_EXPORT std::string GetNativeLibraryName(StringPiece name);
[email protected]f38e25f2009-04-21 00:56:07127
Xiaohan Wangd807ec32018-04-03 01:31:44128// Returns the full platform-specific name for a gn |loadable_module| target.
129// See tools/gn/docs/reference.md#loadable_module
130// The returned name is the same as GetNativeLibraryName() on all platforms
131// except for Mac where for "mylib" it returns "mylib.so".
132BASE_EXPORT std::string GetLoadableModuleName(StringPiece name);
133
[email protected]f38e25f2009-04-21 00:56:07134} // namespace base
135
136#endif // BASE_NATIVE_LIBRARY_H_