Revert of Remove OneClickSigninHelper since it is no longer used. (patchset #5 id...
[chromium-blink-merge.git] / chrome / browser / password_manager / native_backend_gnome_x.h
blob438917a5e009e69a0d92fc5802e092d0db41d404
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/memory/scoped_vector.h"
24 #include "base/time/time.h"
25 #include "chrome/browser/password_manager/password_store_factory.h"
26 #include "chrome/browser/password_manager/password_store_x.h"
27 #include "chrome/browser/profiles/profile.h"
29 namespace autofill {
30 struct PasswordForm;
33 // Many of the gnome_keyring_* functions use variable arguments, which makes
34 // them difficult if not impossible to truly wrap in C. Therefore, we use
35 // appropriately-typed function pointers and scoping to make the fact that we
36 // might be dynamically loading the library almost invisible. As a bonus, we
37 // also get a simple way to mock the library for testing. Classes that inherit
38 // from GnomeKeyringLoader will use its versions of the gnome_keyring_*
39 // functions. Note that it has only static fields.
40 class GnomeKeyringLoader {
41 protected:
42 static bool LoadGnomeKeyring();
44 // Call a given parameter with the name of each function we use from GNOME
45 // Keyring. Make sure to adjust the unit test if you change these.
46 // The list of functions is divided into those we plan to mock in the unittest,
47 // and those which we use without mocking in the test.
48 #define GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F) \
49 F(is_available) \
50 F(store_password) \
51 F(delete_password) \
52 F(find_items) \
53 F(result_to_message)
54 #define GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
55 F(attribute_list_free) \
56 F(attribute_list_new) \
57 F(attribute_list_append_string) \
58 F(attribute_list_append_uint32)
59 #define GNOME_KEYRING_FOR_EACH_FUNC(F) \
60 GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
61 GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F)
63 // Declare the actual function pointers that we'll use in client code.
64 #define GNOME_KEYRING_DECLARE_POINTER(name) \
65 static decltype(&::gnome_keyring_##name) gnome_keyring_##name;
66 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DECLARE_POINTER)
67 #undef GNOME_KEYRING_DECLARE_POINTER
69 // Set to true if LoadGnomeKeyring() has already succeeded.
70 static bool keyring_loaded;
72 private:
73 #if defined(DLOPEN_GNOME_KEYRING)
74 struct FunctionInfo {
75 const char* name;
76 void** pointer;
79 // Make it easy to initialize the function pointers in LoadGnomeKeyring().
80 static const FunctionInfo functions[];
81 #endif // defined(DLOPEN_GNOME_KEYRING)
84 // NativeBackend implementation using GNOME Keyring.
85 class NativeBackendGnome : public PasswordStoreX::NativeBackend,
86 public GnomeKeyringLoader {
87 public:
88 explicit NativeBackendGnome(LocalProfileId id);
90 ~NativeBackendGnome() override;
92 bool Init() override;
94 // Implements NativeBackend interface.
95 password_manager::PasswordStoreChangeList AddLogin(
96 const autofill::PasswordForm& form) override;
97 bool UpdateLogin(const autofill::PasswordForm& form,
98 password_manager::PasswordStoreChangeList* changes) override;
99 bool RemoveLogin(const autofill::PasswordForm& form) override;
100 bool RemoveLoginsCreatedBetween(
101 base::Time delete_begin,
102 base::Time delete_end,
103 password_manager::PasswordStoreChangeList* changes) override;
104 bool RemoveLoginsSyncedBetween(
105 base::Time delete_begin,
106 base::Time delete_end,
107 password_manager::PasswordStoreChangeList* changes) override;
108 bool GetLogins(const autofill::PasswordForm& form,
109 ScopedVector<autofill::PasswordForm>* forms) override;
110 bool GetAutofillableLogins(
111 ScopedVector<autofill::PasswordForm>* forms) override;
112 bool GetBlacklistLogins(ScopedVector<autofill::PasswordForm>* forms) override;
114 private:
115 enum TimestampToCompare {
116 CREATION_TIMESTAMP,
117 SYNC_TIMESTAMP,
120 // Adds a login form without checking for one to replace first.
121 bool RawAddLogin(const autofill::PasswordForm& form);
123 // Reads PasswordForms from the keyring with the given autofillability state.
124 bool GetLoginsList(bool autofillable,
125 ScopedVector<autofill::PasswordForm>* forms);
127 // Helper for GetLoginsCreatedBetween().
128 bool GetAllLogins(ScopedVector<autofill::PasswordForm>* forms);
130 // Retrieves password created/synced in the time interval. Returns |true| if
131 // the operation succeeded.
132 bool GetLoginsBetween(base::Time get_begin,
133 base::Time get_end,
134 TimestampToCompare date_to_compare,
135 ScopedVector<autofill::PasswordForm>* forms);
137 // Removes password created/synced in the time interval. Returns |true| if the
138 // operation succeeded. |changes| will contain the changes applied.
139 bool RemoveLoginsBetween(base::Time get_begin,
140 base::Time get_end,
141 TimestampToCompare date_to_compare,
142 password_manager::PasswordStoreChangeList* changes);
144 // Generates a profile-specific app string based on profile_id_.
145 std::string GetProfileSpecificAppString() const;
147 // The local profile id, used to generate the app string.
148 const LocalProfileId profile_id_;
150 // The app string, possibly based on the local profile id.
151 std::string app_string_;
153 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome);
156 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_