OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/password_manager/proxy/keyring_loader.h" |
| 6 |
| 7 #include <dlfcn.h> |
| 8 #include <stdio.h> |
| 9 |
| 10 #define GNOME_KEYRING_DEFINE_POINTER(name) \ |
| 11 typeof(&::gnome_keyring_##name) GnomeKeyringLoader::gnome_keyring_##name; |
| 12 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DEFINE_POINTER) |
| 13 #undef GNOME_KEYRING_DEFINE_POINTER |
| 14 |
| 15 bool GnomeKeyringLoader::keyring_loaded = false; |
| 16 |
| 17 #if defined(DLOPEN_GNOME_KEYRING) |
| 18 |
| 19 #define GNOME_KEYRING_FUNCTION_INFO(name) \ |
| 20 {"gnome_keyring_"#name, reinterpret_cast<void**>(&gnome_keyring_##name)}, |
| 21 const GnomeKeyringLoader::FunctionInfo GnomeKeyringLoader::functions[] = { |
| 22 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_FUNCTION_INFO) |
| 23 {NULL, NULL} |
| 24 }; |
| 25 #undef GNOME_KEYRING_FUNCTION_INFO |
| 26 |
| 27 // Load the library and initialize the function pointers. |
| 28 bool GnomeKeyringLoader::LoadGnomeKeyring() { |
| 29 if (keyring_loaded) |
| 30 return true; |
| 31 |
| 32 void* handle = dlopen("libgnome-keyring.so.0", RTLD_NOW | RTLD_GLOBAL); |
| 33 if (!handle) { |
| 34 // We wanted to use GNOME Keyring, but we couldn't load it. Warn, because |
| 35 // either the user asked for this, or we autodetected it incorrectly. (Or |
| 36 // the system has broken libraries, which is also good to warn about.) |
| 37 fprintf(stderr, "Could not load libgnome-keyring.so.0: %s\n", dlerror()); |
| 38 return false; |
| 39 } |
| 40 |
| 41 for (size_t i = 0; functions[i].name; ++i) { |
| 42 dlerror(); |
| 43 *functions[i].pointer = dlsym(handle, functions[i].name); |
| 44 const char* error = dlerror(); |
| 45 if (error) { |
| 46 fprintf(stderr, "Unable to load symbol %s: %s\n", |
| 47 functions[i].name, error); |
| 48 dlclose(handle); |
| 49 return false; |
| 50 } |
| 51 } |
| 52 |
| 53 keyring_loaded = true; |
| 54 // We leak the library handle. That's OK: this function is called only once. |
| 55 return true; |
| 56 } |
| 57 |
| 58 #else // defined(DLOPEN_GNOME_KEYRING) |
| 59 |
| 60 bool GnomeKeyringLoader::LoadGnomeKeyring() { |
| 61 if (keyring_loaded) |
| 62 return true; |
| 63 #define GNOME_KEYRING_ASSIGN_POINTER(name) \ |
| 64 gnome_keyring_##name = &::gnome_keyring_##name; |
| 65 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_ASSIGN_POINTER) |
| 66 #undef GNOME_KEYRING_ASSIGN_POINTER |
| 67 keyring_loaded = true; |
| 68 return true; |
| 69 } |
| 70 |
| 71 #endif // defined(DLOPEN_GNOME_KEYRING) |
OLD | NEW |