1 // Copyright 2016 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 #ifndef COMPONENTS_OS_CRYPT_KEYRING_UTIL_LINUX_H_
6 #define COMPONENTS_OS_CRYPT_KEYRING_UTIL_LINUX_H_
7 
8 // libgnome-keyring has been deprecated in favor of libsecret.
9 // See: https://mail.gnome.org/archives/commits-list/2013-October/msg08876.html
10 //
11 // The define below turns off the deprecations, in order to avoid build
12 // failures with Gnome 3.12. When we move to libsecret, the define can be
13 // removed, together with the include below it.
14 //
15 // The porting is tracked in http://crbug.com/355223
16 #define GNOME_KEYRING_DEPRECATED
17 #define GNOME_KEYRING_DEPRECATED_FOR(x)
18 #include <gnome-keyring.h>
19 
20 #include "base/component_export.h"
21 #include "base/macros.h"
22 
23 // Many of the gnome_keyring_* functions use variable arguments, which makes
24 // them difficult if not impossible to truly wrap in C. Therefore, we use
25 // appropriately-typed function pointers and scoping to make the fact that we
26 // might be dynamically loading the library almost invisible. As a bonus, we
27 // also get a simple way to mock the library for testing. Classes that inherit
28 // from GnomeKeyringLoader will use its versions of the gnome_keyring_*
29 // functions. Note that it has only static fields.
30 class GnomeKeyringLoader {
31  public:
32   static COMPONENT_EXPORT(OS_CRYPT) bool LoadGnomeKeyring();
33 
34   // Declare the actual function pointers that we'll use in client code.
35   // These functions will contact the service.
36   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_is_available)
37       gnome_keyring_is_available_ptr;
38   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_store_password)
39       gnome_keyring_store_password_ptr;
40   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_delete_password)
41       gnome_keyring_delete_password_ptr;
42   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_find_items)
43       gnome_keyring_find_items_ptr;
44   static COMPONENT_EXPORT(OS_CRYPT) decltype(
45       &::gnome_keyring_find_password_sync) gnome_keyring_find_password_sync_ptr;
46   static COMPONENT_EXPORT(OS_CRYPT) decltype(
47       &::gnome_keyring_store_password_sync)
48       gnome_keyring_store_password_sync_ptr;
49 
50   // These functions do not contact the service.
51   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_result_to_message)
52       gnome_keyring_result_to_message_ptr;
53   static COMPONENT_EXPORT(OS_CRYPT) decltype(
54       &::gnome_keyring_attribute_list_free)
55       gnome_keyring_attribute_list_free_ptr;
56   static COMPONENT_EXPORT(OS_CRYPT) decltype(
57       &::gnome_keyring_attribute_list_new) gnome_keyring_attribute_list_new_ptr;
58   static COMPONENT_EXPORT(OS_CRYPT) decltype(
59       &::gnome_keyring_attribute_list_append_string)
60       gnome_keyring_attribute_list_append_string_ptr;
61   static COMPONENT_EXPORT(OS_CRYPT) decltype(
62       &::gnome_keyring_attribute_list_append_uint32)
63       gnome_keyring_attribute_list_append_uint32_ptr;
64   static COMPONENT_EXPORT(OS_CRYPT) decltype(&::gnome_keyring_free_password)
65       gnome_keyring_free_password_ptr;
66   // We also use gnome_keyring_attribute_list_index(), which is a macro and
67   // can't be referenced.
68 
69  protected:
70   // Set to true if LoadGnomeKeyring() has already succeeded.
COMPONENT_EXPORT(OS_CRYPT)71   static COMPONENT_EXPORT(OS_CRYPT) bool keyring_loaded;
72 
73  private:
74   struct FunctionInfo {
75     const char* name;
76     void** pointer;
77   };
78 
79   // Make it easy to initialize the function pointers in LoadGnomeKeyring().
80   static const FunctionInfo functions[];
81 };
82 
83 #endif  // COMPONENTS_OS_CRYPT_KEYRING_UTIL_LINUX_H_
84