[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 2 | // 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 | |||||
7 | #include <dlfcn.h> | ||||
8 | |||||
9 | #include "base/file_path.h" | ||||
10 | #include "base/logging.h" | ||||
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame^] | 11 | #include "base/threading/thread_restrictions.h" |
[email protected] | f1d8192 | 2010-07-31 17:47:09 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 13 | |
14 | namespace base { | ||||
15 | |||||
16 | // static | ||||
17 | NativeLibrary LoadNativeLibrary(const FilePath& library_path) { | ||||
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 18 | // dlopen() opens the file off disk. |
19 | base::ThreadRestrictions::AssertIOAllowed(); | ||||
20 | |||||
[email protected] | 0ba5b7b | 2010-04-22 18:07:50 | [diff] [blame] | 21 | // We deliberately do not use RTLD_DEEPBIND. For the history why, please |
22 | // refer to the bug tracker. Some useful bug reports to read include: | ||||
23 | // http://crbug.com/17943, http://crbug.com/17557, http://crbug.com/36892, | ||||
24 | // and http://crbug.com/40794. | ||||
[email protected] | de8dbce | 2009-07-29 00:06:21 | [diff] [blame] | 25 | void* dl = dlopen(library_path.value().c_str(), RTLD_LAZY); |
[email protected] | 6dd6435 | 2009-06-29 23:36:22 | [diff] [blame] | 26 | if (!dl) { |
[email protected] | 0922baea | 2009-08-27 23:06:34 | [diff] [blame] | 27 | std::string error_message = dlerror(); |
28 | // Some obsolete plugins depend on libxul or libxpcom. | ||||
29 | // Ignore the error messages when failing to load these. | ||||
30 | if (error_message.find("libxul.so") == std::string::npos && | ||||
31 | error_message.find("libxpcom.so") == std::string::npos) { | ||||
32 | LOG(ERROR) << "dlopen failed when trying to open " << library_path.value() | ||||
33 | << ": " << error_message; | ||||
34 | } | ||||
[email protected] | 6dd6435 | 2009-06-29 23:36:22 | [diff] [blame] | 35 | } |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 36 | |
37 | return dl; | ||||
38 | } | ||||
39 | |||||
40 | // static | ||||
41 | void UnloadNativeLibrary(NativeLibrary library) { | ||||
42 | int ret = dlclose(library); | ||||
[email protected] | 803ef4ef | 2009-07-29 00:15:24 | [diff] [blame] | 43 | if (ret < 0) { |
44 | LOG(ERROR) << "dlclose failed: " << dlerror(); | ||||
45 | NOTREACHED(); | ||||
46 | } | ||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 47 | } |
48 | |||||
49 | // static | ||||
50 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, | ||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 51 | const char* name) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 52 | return dlsym(library, name); |
53 | } | ||||
54 | |||||
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 55 | // static |
56 | string16 GetNativeLibraryName(const string16& name) { | ||||
57 | return ASCIIToUTF16("lib") + name + ASCIIToUTF16(".so"); | ||||
58 | } | ||||
59 | |||||
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 60 | } // namespace base |