blob: 891f35bd540a07c5049417ba37e4e2e20ded9df7 [file] [log] [blame]
rvargas@google.come106ef62011-03-26 03:48:03 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
evan@chromium.orgda642ae2009-04-21 09:56:07 +09002// 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
darin@chromium.orge585bed2011-08-06 00:34:00 +090011#include "base/base_export.h"
evan@chromium.orgda642ae2009-04-21 09:56:07 +090012#include "build/build_config.h"
13
14#if defined(OS_WIN)
15#include <windows.h>
16#elif defined(OS_MACOSX)
thomasvl@chromium.org1b721c82010-11-16 06:22:55 +090017#import <CoreFoundation/CoreFoundation.h>
evan@chromium.orgda642ae2009-04-21 09:56:07 +090018#endif // OS_*
19
avi@chromium.org67d593d2013-06-11 04:06:57 +090020#include "base/strings/string16.h"
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090021
evan@chromium.org9adf1192010-11-13 06:53:16 +090022// Macro useful for writing cross-platform function pointers.
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090023#if defined(OS_WIN) && !defined(CDECL)
24#define CDECL __cdecl
25#else
26#define CDECL
27#endif
28
evan@chromium.orgda642ae2009-04-21 09:56:07 +090029namespace base {
30
brettw@chromium.orga7086942013-02-02 14:12:33 +090031class FilePath;
32
evan@chromium.orgda642ae2009-04-21 09:56:07 +090033#if defined(OS_WIN)
34typedef HMODULE NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090035#elif defined(OS_MACOSX)
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090036enum NativeLibraryType {
37 BUNDLE,
38 DYNAMIC_LIB
39};
rsesek@chromium.org9ef941f2013-03-15 21:10:58 +090040enum NativeLibraryObjCStatus {
41 OBJC_UNKNOWN,
42 OBJC_PRESENT,
43 OBJC_NOT_PRESENT,
44};
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090045struct NativeLibraryStruct {
46 NativeLibraryType type;
stuartmorgan@chromium.orgc7f74a22010-01-29 01:08:09 +090047 CFBundleRefNum bundle_resource_ref;
rsesek@chromium.org9ef941f2013-03-15 21:10:58 +090048 NativeLibraryObjCStatus objc_status;
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090049 union {
50 CFBundleRef bundle;
51 void* dylib;
52 };
53};
54typedef NativeLibraryStruct* NativeLibrary;
evan@chromium.org875bb6e2009-12-29 09:32:52 +090055#elif defined(OS_POSIX)
evan@chromium.orgda642ae2009-04-21 09:56:07 +090056typedef void* NativeLibrary;
evan@chromium.orgda642ae2009-04-21 09:56:07 +090057#endif // OS_*
58
59// Loads a native library from disk. Release it with UnloadNativeLibrary when
evan@chromium.org629d58e2011-04-19 07:06:22 +090060// you're done. Returns NULL on failure.
61// If |err| is not NULL, it may be filled in with an error message on
62// error.
darin@chromium.orge585bed2011-08-06 00:34:00 +090063BASE_EXPORT NativeLibrary LoadNativeLibrary(const FilePath& library_path,
64 std::string* error);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090065
ananta@chromium.org4de229a2010-11-20 08:33:13 +090066#if defined(OS_WIN)
67// Loads a native library from disk. Release it with UnloadNativeLibrary when
68// you're done.
69// This function retrieves the LoadLibrary function exported from kernel32.dll
70// and calls it instead of directly calling the LoadLibrary function via the
71// import table.
darin@chromium.orge585bed2011-08-06 00:34:00 +090072BASE_EXPORT NativeLibrary LoadNativeLibraryDynamically(
rvargas@google.come106ef62011-03-26 03:48:03 +090073 const FilePath& library_path);
ananta@chromium.org4de229a2010-11-20 08:33:13 +090074#endif // OS_WIN
75
evan@chromium.orgda642ae2009-04-21 09:56:07 +090076// Unloads a native library.
darin@chromium.orge585bed2011-08-06 00:34:00 +090077BASE_EXPORT void UnloadNativeLibrary(NativeLibrary library);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090078
79// Gets a function pointer from a native library.
darin@chromium.orge585bed2011-08-06 00:34:00 +090080BASE_EXPORT void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
81 const char* name);
jcampan@chromium.orgd9e2be82009-06-06 07:18:09 +090082
83// Returns the full platform specific name for a native library.
84// For example:
85// "mylib" returns "mylib.dll" on Windows, "libmylib.so" on Linux,
86// "mylib.dylib" on Mac.
darin@chromium.orge585bed2011-08-06 00:34:00 +090087BASE_EXPORT string16 GetNativeLibraryName(const string16& name);
evan@chromium.orgda642ae2009-04-21 09:56:07 +090088
89} // namespace base
90
91#endif // BASE_NATIVE_LIBRARY_H_