blob: d13a4b619c3f9e7208fc4aa7dea94a855e1019bc [file] [log] [blame]
[email protected]81e34d82010-08-19 18:36:251// Copyright (c) 2010 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#include "base/native_library.h"
6
[email protected]108c2a12009-06-05 22:18:097#include <dlfcn.h>
[email protected]f38e25f2009-04-21 00:56:078#import <Carbon/Carbon.h>
9
10#include "base/file_path.h"
[email protected]108c2a12009-06-05 22:18:0911#include "base/file_util.h"
[email protected]f38e25f2009-04-21 00:56:0712#include "base/scoped_cftyperef.h"
[email protected]108c2a12009-06-05 22:18:0913#include "base/string_util.h"
[email protected]81e34d82010-08-19 18:36:2514#include "base/utf_string_conversions.h"
[email protected]f38e25f2009-04-21 00:56:0715
16namespace base {
17
18// static
19NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
[email protected]108c2a12009-06-05 22:18:0920 if (library_path.Extension() == "dylib" ||
21 !file_util::DirectoryExists(library_path)) {
22 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
23 if (!dylib)
24 return NULL;
25 NativeLibrary native_lib = new NativeLibraryStruct();
26 native_lib->type = DYNAMIC_LIB;
27 native_lib->dylib = dylib;
28 return native_lib;
29 }
[email protected]f38e25f2009-04-21 00:56:0730 scoped_cftyperef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(
31 kCFAllocatorDefault,
32 (const UInt8*)library_path.value().c_str(),
33 library_path.value().length(),
34 true));
35 if (!url)
36 return NULL;
[email protected]108c2a12009-06-05 22:18:0937 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
38 if (!bundle)
39 return NULL;
[email protected]f38e25f2009-04-21 00:56:0740
[email protected]108c2a12009-06-05 22:18:0941 NativeLibrary native_lib = new NativeLibraryStruct();
42 native_lib->type = BUNDLE;
43 native_lib->bundle = bundle;
[email protected]d8921c432010-01-28 16:08:0944 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
[email protected]108c2a12009-06-05 22:18:0945 return native_lib;
[email protected]f38e25f2009-04-21 00:56:0746}
47
48// static
49void UnloadNativeLibrary(NativeLibrary library) {
[email protected]d8921c432010-01-28 16:08:0950 if (library->type == BUNDLE) {
51 CFBundleCloseBundleResourceMap(library->bundle,
52 library->bundle_resource_ref);
[email protected]108c2a12009-06-05 22:18:0953 CFRelease(library->bundle);
[email protected]d8921c432010-01-28 16:08:0954 } else {
[email protected]108c2a12009-06-05 22:18:0955 dlclose(library->dylib);
[email protected]d8921c432010-01-28 16:08:0956 }
[email protected]108c2a12009-06-05 22:18:0957 delete library;
[email protected]f38e25f2009-04-21 00:56:0758}
59
60// static
61void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0962 const char* name) {
[email protected]b7efd7692009-12-15 22:34:0963 if (library->type == BUNDLE) {
64 scoped_cftyperef<CFStringRef> symbol_name(
[email protected]108c2a12009-06-05 22:18:0965 CFStringCreateWithCString(kCFAllocatorDefault, name,
66 kCFStringEncodingUTF8));
[email protected]b7efd7692009-12-15 22:34:0967 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name);
68 }
[email protected]108c2a12009-06-05 22:18:0969 return dlsym(library->dylib, name);
70}
71
72// static
73string16 GetNativeLibraryName(const string16& name) {
74 return name + ASCIIToUTF16(".dylib");
[email protected]f38e25f2009-04-21 00:56:0775}
76
77} // namespace base