OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/native_library.h" | 5 #include "base/native_library.h" |
6 | 6 |
7 #include <dlfcn.h> | 7 #include <dlfcn.h> |
8 #include <mach-o/getsect.h> | 8 #include <mach-o/getsect.h> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 !file_util::DirectoryExists(library_path)) { | 49 !file_util::DirectoryExists(library_path)) { |
50 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); | 50 void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY); |
51 if (!dylib) | 51 if (!dylib) |
52 return NULL; | 52 return NULL; |
53 NativeLibrary native_lib = new NativeLibraryStruct(); | 53 NativeLibrary native_lib = new NativeLibraryStruct(); |
54 native_lib->type = DYNAMIC_LIB; | 54 native_lib->type = DYNAMIC_LIB; |
55 native_lib->dylib = dylib; | 55 native_lib->dylib = dylib; |
56 native_lib->objc_status = OBJC_UNKNOWN; | 56 native_lib->objc_status = OBJC_UNKNOWN; |
57 return native_lib; | 57 return native_lib; |
58 } | 58 } |
59 base::mac::ScopedCFTypeRef<CFURLRef> url( | 59 base::ScopedCFTypeRef<CFURLRef> url(CFURLCreateFromFileSystemRepresentation( |
60 CFURLCreateFromFileSystemRepresentation( | 60 kCFAllocatorDefault, |
61 kCFAllocatorDefault, | 61 (const UInt8*)library_path.value().c_str(), |
62 (const UInt8*)library_path.value().c_str(), | 62 library_path.value().length(), |
63 library_path.value().length(), | 63 true)); |
64 true)); | |
65 if (!url) | 64 if (!url) |
66 return NULL; | 65 return NULL; |
67 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); | 66 CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get()); |
68 if (!bundle) | 67 if (!bundle) |
69 return NULL; | 68 return NULL; |
70 | 69 |
71 NativeLibrary native_lib = new NativeLibraryStruct(); | 70 NativeLibrary native_lib = new NativeLibraryStruct(); |
72 native_lib->type = BUNDLE; | 71 native_lib->type = BUNDLE; |
73 native_lib->bundle = bundle; | 72 native_lib->bundle = bundle; |
74 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); | 73 native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle); |
(...skipping 21 matching lines...) Expand all Loading... |
96 delete library; | 95 delete library; |
97 } | 96 } |
98 | 97 |
99 // static | 98 // static |
100 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | 99 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
101 const char* name) { | 100 const char* name) { |
102 void* function_pointer = NULL; | 101 void* function_pointer = NULL; |
103 | 102 |
104 // Get the function pointer using the right API for the type. | 103 // Get the function pointer using the right API for the type. |
105 if (library->type == BUNDLE) { | 104 if (library->type == BUNDLE) { |
106 base::mac::ScopedCFTypeRef<CFStringRef> symbol_name( | 105 base::ScopedCFTypeRef<CFStringRef> symbol_name(CFStringCreateWithCString( |
107 CFStringCreateWithCString(kCFAllocatorDefault, name, | 106 kCFAllocatorDefault, name, kCFStringEncodingUTF8)); |
108 kCFStringEncodingUTF8)); | |
109 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, | 107 function_pointer = CFBundleGetFunctionPointerForName(library->bundle, |
110 symbol_name); | 108 symbol_name); |
111 } else { | 109 } else { |
112 function_pointer = dlsym(library->dylib, name); | 110 function_pointer = dlsym(library->dylib, name); |
113 } | 111 } |
114 | 112 |
115 // If this library hasn't been tested for having ObjC, use the function | 113 // If this library hasn't been tested for having ObjC, use the function |
116 // pointer to look up the section information for the library. | 114 // pointer to look up the section information for the library. |
117 if (function_pointer && library->objc_status == OBJC_UNKNOWN) | 115 if (function_pointer && library->objc_status == OBJC_UNKNOWN) |
118 library->objc_status = GetObjCStatusForImage(function_pointer); | 116 library->objc_status = GetObjCStatusForImage(function_pointer); |
119 | 117 |
120 return function_pointer; | 118 return function_pointer; |
121 } | 119 } |
122 | 120 |
123 // static | 121 // static |
124 string16 GetNativeLibraryName(const string16& name) { | 122 string16 GetNativeLibraryName(const string16& name) { |
125 return name + ASCIIToUTF16(".dylib"); | 123 return name + ASCIIToUTF16(".dylib"); |
126 } | 124 } |
127 | 125 |
128 } // namespace base | 126 } // namespace base |
OLD | NEW |