Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / password_manager / native_backend_gnome_x.h
blobd7ae092d39f13e3991ab05f4c844f4ba2203aab6
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>
20 #include <string>
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"
30 namespace autofill {
31 struct PasswordForm;
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 {
42 protected:
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) \
50 F(is_available) \
51 F(store_password) \
52 F(delete_password) \
53 F(find_items) \
54 F(result_to_message)
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;
73 private:
74 #if defined(DLOPEN_GNOME_KEYRING)
75 struct FunctionInfo {
76 const char* name;
77 void** pointer;
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 {
88 public:
89 explicit NativeBackendGnome(LocalProfileId id);
91 ~NativeBackendGnome() override;
93 bool Init() 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,
101 password_manager::PasswordStoreChangeList* changes) override;
102 bool RemoveLoginsCreatedBetween(
103 base::Time delete_begin,
104 base::Time delete_end,
105 password_manager::PasswordStoreChangeList* changes) override;
106 bool RemoveLoginsSyncedBetween(
107 base::Time delete_begin,
108 base::Time delete_end,
109 password_manager::PasswordStoreChangeList* changes) override;
110 bool GetLogins(const autofill::PasswordForm& form,
111 ScopedVector<autofill::PasswordForm>* forms) override;
112 bool GetAutofillableLogins(
113 ScopedVector<autofill::PasswordForm>* forms) override;
114 bool GetBlacklistLogins(ScopedVector<autofill::PasswordForm>* forms) override;
116 private:
117 enum TimestampToCompare {
118 CREATION_TIMESTAMP,
119 SYNC_TIMESTAMP,
122 // Adds a login form without checking for one to replace first.
123 bool RawAddLogin(const autofill::PasswordForm& form);
125 // Retrieves all autofillable or all blacklisted credentials (depending on
126 // |autofillable|) from the keyring into |forms|, overwriting the original
127 // contents of |forms|. Returns true on success.
128 bool GetLoginsList(bool autofillable,
129 ScopedVector<autofill::PasswordForm>* forms)
130 WARN_UNUSED_RESULT;
132 // Helper for GetLoginsCreatedBetween().
133 bool GetAllLogins(ScopedVector<autofill::PasswordForm>* forms)
134 WARN_UNUSED_RESULT;
136 // Retrieves password created/synced in the time interval. Returns |true| if
137 // the operation succeeded.
138 bool GetLoginsBetween(base::Time get_begin,
139 base::Time get_end,
140 TimestampToCompare date_to_compare,
141 ScopedVector<autofill::PasswordForm>* forms)
142 WARN_UNUSED_RESULT;
144 // Removes password created/synced in the time interval. Returns |true| if the
145 // operation succeeded. |changes| will contain the changes applied.
146 bool RemoveLoginsBetween(base::Time get_begin,
147 base::Time get_end,
148 TimestampToCompare date_to_compare,
149 password_manager::PasswordStoreChangeList* changes);
151 // The local profile id, used to generate the app string.
152 const LocalProfileId profile_id_;
154 // The app string, possibly based on the local profile id.
155 std::string app_string_;
157 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome);
160 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_