Protect WebURLLoaderImpl::Context while receiving responses.
[chromium-blink-merge.git] / base / native_library_mac.mm
blob4a0947561c892953dc54bc0b2cc3df73ca19acb2
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>
8 #include <mach-o/getsect.h>
10 #include "base/file_util.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/mac/scoped_cftyperef.h"
14 #include "base/string_util.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/utf_string_conversions.h"
18 namespace base {
20 static NativeLibraryObjCStatus GetObjCStatusForImage(
21     const void* function_pointer) {
22   Dl_info info;
23   if (!dladdr(function_pointer, &info))
24     return OBJC_UNKNOWN;
26   // See if the the image contains an "ObjC image info" segment. This method
27   // of testing is used in _CFBundleGrokObjcImageInfoFromFile in
28   // CF-744/CFBundle.c, around lines 2447-2474.
29   //
30   // In 32-bit images, ObjC can be recognized in __OBJC,__image_info, whereas
31   // in 64-bit, the data is in __DATA,__objc_imageinfo.
32 #if __LP64__
33   const section_64* section = getsectbynamefromheader_64(
34       reinterpret_cast<const struct mach_header_64*>(info.dli_fbase),
35       SEG_DATA, "__objc_imageinfo");
36 #else
37   const section* section = getsectbynamefromheader(
38       reinterpret_cast<const struct mach_header*>(info.dli_fbase),
39       SEG_OBJC, "__image_info");
40 #endif
41   return section == NULL ? OBJC_NOT_PRESENT : OBJC_PRESENT;
44 // static
45 NativeLibrary LoadNativeLibrary(const base::FilePath& library_path,
46                                 std::string* error) {
47   // dlopen() etc. open the file off disk.
48   if (library_path.Extension() == "dylib" ||
49       !file_util::DirectoryExists(library_path)) {
50     void* dylib = dlopen(library_path.value().c_str(), RTLD_LAZY);
51     if (!dylib)
52       return NULL;
53     NativeLibrary native_lib = new NativeLibraryStruct();
54     native_lib->type = DYNAMIC_LIB;
55     native_lib->dylib = dylib;
56     native_lib->objc_status = OBJC_UNKNOWN;
57     return native_lib;
58   }
59   base::mac::ScopedCFTypeRef<CFURLRef> url(
60       CFURLCreateFromFileSystemRepresentation(
61           kCFAllocatorDefault,
62           (const UInt8*)library_path.value().c_str(),
63           library_path.value().length(),
64           true));
65   if (!url)
66     return NULL;
67   CFBundleRef bundle = CFBundleCreate(kCFAllocatorDefault, url.get());
68   if (!bundle)
69     return NULL;
71   NativeLibrary native_lib = new NativeLibraryStruct();
72   native_lib->type = BUNDLE;
73   native_lib->bundle = bundle;
74   native_lib->bundle_resource_ref = CFBundleOpenBundleResourceMap(bundle);
75   native_lib->objc_status = OBJC_UNKNOWN;
76   return native_lib;
79 // static
80 void UnloadNativeLibrary(NativeLibrary library) {
81   if (library->objc_status == OBJC_NOT_PRESENT) {
82     if (library->type == BUNDLE) {
83       CFBundleCloseBundleResourceMap(library->bundle,
84                                      library->bundle_resource_ref);
85       CFRelease(library->bundle);
86     } else {
87       dlclose(library->dylib);
88     }
89   } else {
90     VLOG(2) << "Not unloading NativeLibrary because it may contain an ObjC "
91                "segment. library->objc_status = " << library->objc_status;
92     // Deliberately do not CFRelease the bundle or dlclose the dylib because
93     // doing so can corrupt the ObjC runtime method caches. See
94     // http://crbug.com/172319 for details.
95   }
96   delete library;
99 // static
100 void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
101                                           const char* name) {
102   void* function_pointer = NULL;
104   // Get the function pointer using the right API for the type.
105   if (library->type == BUNDLE) {
106     base::mac::ScopedCFTypeRef<CFStringRef> symbol_name(
107         CFStringCreateWithCString(kCFAllocatorDefault, name,
108                                   kCFStringEncodingUTF8));
109     function_pointer = CFBundleGetFunctionPointerForName(library->bundle,
110                                                          symbol_name);
111   } else {
112     function_pointer = dlsym(library->dylib, name);
113   }
115   // If this library hasn't been tested for having ObjC, use the function
116   // pointer to look up the section information for the library.
117   if (function_pointer && library->objc_status == OBJC_UNKNOWN)
118     library->objc_status = GetObjCStatusForImage(function_pointer);
120   return function_pointer;
123 // static
124 string16 GetNativeLibraryName(const string16& name) {
125   return name + ASCIIToUTF16(".dylib");
128 }  // namespace base