Tab Audio Mirroring: WebContentsAudioInputStream is a new implementation which repres...
[chromium-blink-merge.git] / base / native_library_mac.mm
blob9f7a9679dcae220a544ae485e0572c5c2c98901c
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/native_library.h"
7 #include <dlfcn.h>
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #include "base/string_util.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "base/utf_string_conversions.h"
16 namespace base {
18 // static
19 NativeLibrary LoadNativeLibrary(const FilePath& library_path,
20                                 std::string* error) {
21   // dlopen() etc. open the file off disk.
22   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   }
32   base::mac::ScopedCFTypeRef<CFURLRef> url(
33       CFURLCreateFromFileSystemRepresentation(
34           kCFAllocatorDefault,
35           (const UInt8*)library_path.value().c_str(),
36           library_path.value().length(),
37           true));
38   if (!url)
39     return NULL;
40   CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
41   if (!bundle)
42     return NULL;
44   NativeLibrary native_lib = new NativeLibraryStruct();
45   native_lib->type = BUNDLE;
46   native_lib->bundle = bundle;
47   native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
48   return native_lib;
51 // static
52 void UnloadNativeLibrary(NativeLibrary library) {
53   if (library->type == BUNDLE) {
54     CFBundleCloseBundleResourceMap(library->bundle,
55                                    library->bundle_resource_ref);
56     CFRelease(library->bundle);
57   } else {
58     dlclose(library->dylib);
59   }
60   delete library;
63 // static
64 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
65                                           const char* name) {
66   if (library->type == BUNDLE) {
67     base::mac::ScopedCFTypeRef<CFStringRef> symbol_name(
68         CFStringCreateWithCString(kCFAllocatorDefault, name,
69                                   kCFStringEncodingUTF8));
70     return CFBundleGetFunctionPointerForName(library->bundle, symbol_name);
71   }
72   return dlsym(library->dylib, name);
75 // static
76 string16 GetNativeLibraryName(const string16& name) {
77   return name + ASCIIToUTF16(".dylib");
80 }  // namespace base