blob: b17aaa66be8d40152dcd81b34011e07a589aedcf [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]df0ca6c82010-10-17 04:09:0612#include "base/mac/scoped_cftyperef.h"
[email protected]108c2a12009-06-05 22:18:0913#include "base/string_util.h"
[email protected]be130682010-11-12 21:53:1614#include "base/thread_restrictions.h"
[email protected]81e34d82010-08-19 18:36:2515#include "base/utf_string_conversions.h"
[email protected]f38e25f2009-04-21 00:56:0716
17namespace base {
18
19// static
20NativeLibrary LoadNativeLibrary(const FilePath& library_path) {
[email protected]be130682010-11-12 21:53:1621 // dlopen() etc. open the file off disk.
[email protected]108c2a12009-06-05 22:18:0922 if (library_path.Extension() == "dylib" ||
23 !file_util::DirectoryExists(library_path)) {
24 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
25 if (!dylib)
26 return NULL;
27 NativeLibrary native_lib = new NativeLibraryStruct();
28 native_lib->type = DYNAMIC_LIB;
29 native_lib->dylib = dylib;
30 return native_lib;
31 }
[email protected]df0ca6c82010-10-17 04:09:0632 base::mac::ScopedCFTypeRef<CFURLRef> url(
33 CFURLCreateFromFileSystemRepresentation(
34 kCFAllocatorDefault,
35 (const UInt8*)library_path.value().c_str(),
36 library_path.value().length(),
37 true));
[email protected]f38e25f2009-04-21 00:56:0738 if (!url)
39 return NULL;
[email protected]108c2a12009-06-05 22:18:0940 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
41 if (!bundle)
42 return NULL;
[email protected]f38e25f2009-04-21 00:56:0743
[email protected]108c2a12009-06-05 22:18:0944 NativeLibrary native_lib = new NativeLibraryStruct();
45 native_lib->type = BUNDLE;
46 native_lib->bundle = bundle;
[email protected]d8921c432010-01-28 16:08:0947 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
[email protected]108c2a12009-06-05 22:18:0948 return native_lib;
[email protected]f38e25f2009-04-21 00:56:0749}
50
51// static
52void UnloadNativeLibrary(NativeLibrary library) {
[email protected]d8921c432010-01-28 16:08:0953 if (library->type == BUNDLE) {
54 CFBundleCloseBundleResourceMap(library->bundle,
55 library->bundle_resource_ref);
[email protected]108c2a12009-06-05 22:18:0956 CFRelease(library->bundle);
[email protected]d8921c432010-01-28 16:08:0957 } else {
[email protected]108c2a12009-06-05 22:18:0958 dlclose(library->dylib);
[email protected]d8921c432010-01-28 16:08:0959 }
[email protected]108c2a12009-06-05 22:18:0960 delete library;
[email protected]f38e25f2009-04-21 00:56:0761}
62
63// static
64void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
[email protected]108c2a12009-06-05 22:18:0965 const char* name) {
[email protected]b7efd7692009-12-15 22:34:0966 if (library->type == BUNDLE) {
[email protected]df0ca6c82010-10-17 04:09:0667 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name(
[email protected]108c2a12009-06-05 22:18:0968 CFStringCreateWithCString(kCFAllocatorDefault, name,
69 kCFStringEncodingUTF8));
[email protected]b7efd7692009-12-15 22:34:0970 return CFBundleGetFunctionPointerForName(library->bundle, symbol_name);
71 }
[email protected]108c2a12009-06-05 22:18:0972 return dlsym(library->dylib, name);
73}
74
75// static
76string16 GetNativeLibraryName(const string16& name) {
77 return name + ASCIIToUTF16(".dylib");
[email protected]f38e25f2009-04-21 00:56:0778}
79
80} // namespace base