blob: b4f3a3cd1ba267000848b09c0ee9d8af7983387f [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
[email protected]f38e25f2009-04-21 00:56:0768// Loads a native library from disk. Release it with UnloadNativeLibrary when
[email protected]84479322011-04-18 22:06:2269// you're done. Returns NULL on failure.
[email protected]0f998442014-03-25 01:59:0970// If |error| is not NULL, it may be filled in on load error.
[email protected]0bea7252011-08-05 15:34:0071BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0972 NativeLibraryLoadError* error);
[email protected]f38e25f2009-04-21 00:56:0773
[email protected]3e246222010-11-19 23:33:1374#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.
[email protected]0bea7252011-08-05 15:34:0080BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically(
[email protected]26fbf802011-03-25 18:48:0381 const FilePath& library_path);
[email protected]3e246222010-11-19 23:33:1382#endif // OS_WIN
83
[email protected]f38e25f2009-04-21 00:56:0784// Unloads a native library.
[email protected]0bea7252011-08-05 15:34:0085BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
[email protected]f38e25f2009-04-21 00:56:0786
87// Gets a function pointer from a native library.
[email protected]0bea7252011-08-05 15:34:0088BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:4089 StringPiece name);
[email protected]108c2a12009-06-05 22:18:0990
91// Returns the full platform specific name for a native library.
thestig02c965b2016-06-14 18:52:2392// |name| must be ASCII.
[email protected]108c2a12009-06-05 22:18:0993// For example:
94// "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
thestig02c965b2016-06-14 18:52:2395// "libmylib.dylib" on Mac.
96BASE_EXPORT std::string GetNativeLibraryName(StringPiece name);
[email protected]f38e25f2009-04-21 00:56:0797
98} // namespace base
99
100#endif // BASE_NATIVE_LIBRARY_H_