1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_
8 // libgnome-keyring has been deprecated in favor of libsecret.
9 // See: https://mail.gnome.org/archives/commits-list/2013-October/msg08876.html
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.
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>
22 #include "base/basictypes.h"
23 #include "base/compiler_specific.h"
24 #include "base/memory/scoped_vector.h"
25 #include "base/time/time.h"
26 #include "chrome/browser/password_manager/password_store_factory.h"
27 #include "chrome/browser/password_manager/password_store_x.h"
28 #include "chrome/browser/profiles/profile.h"
34 // Many of the gnome_keyring_* functions use variable arguments, which makes
35 // them difficult if not impossible to truly wrap in C. Therefore, we use
36 // appropriately-typed function pointers and scoping to make the fact that we
37 // might be dynamically loading the library almost invisible. As a bonus, we
38 // also get a simple way to mock the library for testing. Classes that inherit
39 // from GnomeKeyringLoader will use its versions of the gnome_keyring_*
40 // functions. Note that it has only static fields.
41 class GnomeKeyringLoader
{
43 static bool LoadGnomeKeyring();
45 // Call a given parameter with the name of each function we use from GNOME
46 // Keyring. Make sure to adjust the unit test if you change these.
47 // The list of functions is divided into those we plan to mock in the unittest,
48 // and those which we use without mocking in the test.
49 #define GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F) \
55 #define GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
56 F(attribute_list_free) \
57 F(attribute_list_new) \
58 F(attribute_list_append_string) \
59 F(attribute_list_append_uint32)
60 #define GNOME_KEYRING_FOR_EACH_FUNC(F) \
61 GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
62 GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F)
64 // Declare the actual function pointers that we'll use in client code.
65 #define GNOME_KEYRING_DECLARE_POINTER(name) \
66 static decltype(&::gnome_keyring_##name) gnome_keyring_##name;
67 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DECLARE_POINTER
)
68 #undef GNOME_KEYRING_DECLARE_POINTER
70 // Set to true if LoadGnomeKeyring() has already succeeded.
71 static bool keyring_loaded
;
74 #if defined(DLOPEN_GNOME_KEYRING)
80 // Make it easy to initialize the function pointers in LoadGnomeKeyring().
81 static const FunctionInfo functions
[];
82 #endif // defined(DLOPEN_GNOME_KEYRING)
85 // NativeBackend implementation using GNOME Keyring.
86 class NativeBackendGnome
: public PasswordStoreX::NativeBackend
,
87 public GnomeKeyringLoader
{
89 explicit NativeBackendGnome(LocalProfileId id
);
91 ~NativeBackendGnome() override
;
95 // Implements NativeBackend interface.
96 password_manager::PasswordStoreChangeList
AddLogin(
97 const autofill::PasswordForm
& form
) override
;
98 bool UpdateLogin(const autofill::PasswordForm
& form
,
99 password_manager::PasswordStoreChangeList
* changes
) override
;
100 bool RemoveLogin(const autofill::PasswordForm
& form
) override
;
101 bool RemoveLoginsCreatedBetween(
102 base::Time delete_begin
,
103 base::Time delete_end
,
104 password_manager::PasswordStoreChangeList
* changes
) override
;
105 bool RemoveLoginsSyncedBetween(
106 base::Time delete_begin
,
107 base::Time delete_end
,
108 password_manager::PasswordStoreChangeList
* changes
) override
;
109 bool GetLogins(const autofill::PasswordForm
& form
,
110 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
111 bool GetAutofillableLogins(
112 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
113 bool GetBlacklistLogins(ScopedVector
<autofill::PasswordForm
>* forms
) override
;
116 enum TimestampToCompare
{
121 // Adds a login form without checking for one to replace first.
122 bool RawAddLogin(const autofill::PasswordForm
& form
);
124 // Retrieves all autofillable or all blacklisted credentials (depending on
125 // |autofillable|) from the keyring into |forms|, overwriting the original
126 // contents of |forms|. Returns true on success.
127 bool GetLoginsList(bool autofillable
,
128 ScopedVector
<autofill::PasswordForm
>* forms
)
131 // Helper for GetLoginsCreatedBetween().
132 bool GetAllLogins(ScopedVector
<autofill::PasswordForm
>* forms
)
135 // Retrieves password created/synced in the time interval. Returns |true| if
136 // the operation succeeded.
137 bool GetLoginsBetween(base::Time get_begin
,
139 TimestampToCompare date_to_compare
,
140 ScopedVector
<autofill::PasswordForm
>* forms
)
143 // Removes password created/synced in the time interval. Returns |true| if the
144 // operation succeeded. |changes| will contain the changes applied.
145 bool RemoveLoginsBetween(base::Time get_begin
,
147 TimestampToCompare date_to_compare
,
148 password_manager::PasswordStoreChangeList
* changes
);
150 // The local profile id, used to generate the app string.
151 const LocalProfileId profile_id_
;
153 // The app string, possibly based on the local profile id.
154 std::string app_string_
;
156 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome
);
159 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_