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 #include <gnome-keyring.h>
12 #include "base/basictypes.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/password_manager/password_store_factory.h"
15 #include "chrome/browser/password_manager/password_store_x.h"
16 #include "chrome/browser/profiles/profile.h"
22 // Many of the gnome_keyring_* functions use variable arguments, which makes
23 // them difficult if not impossible to truly wrap in C. Therefore, we use
24 // appropriately-typed function pointers and scoping to make the fact that we
25 // might be dynamically loading the library almost invisible. As a bonus, we
26 // also get a simple way to mock the library for testing. Classes that inherit
27 // from GnomeKeyringLoader will use its versions of the gnome_keyring_*
28 // functions. Note that it has only static fields.
29 class GnomeKeyringLoader
{
31 static bool LoadGnomeKeyring();
33 // Call a given parameter with the name of each function we use from GNOME
34 // Keyring. Make sure to adjust the unit test if you change these.
35 // The list of functions is divided into those we plan to mock in the unittest,
36 // and those which we use without mocking in the test.
37 #define GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F) \
43 #define GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
44 F(attribute_list_free) \
45 F(attribute_list_new) \
46 F(attribute_list_append_string) \
47 F(attribute_list_append_uint32)
48 #define GNOME_KEYRING_FOR_EACH_FUNC(F) \
49 GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
50 GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F)
52 // Declare the actual function pointers that we'll use in client code.
53 #define GNOME_KEYRING_DECLARE_POINTER(name) \
54 static typeof(&::gnome_keyring_##name) gnome_keyring_##name;
55 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DECLARE_POINTER
)
56 #undef GNOME_KEYRING_DECLARE_POINTER
58 // Set to true if LoadGnomeKeyring() has already succeeded.
59 static bool keyring_loaded
;
62 #if defined(DLOPEN_GNOME_KEYRING)
68 // Make it easy to initialize the function pointers in LoadGnomeKeyring().
69 static const FunctionInfo functions
[];
70 #endif // defined(DLOPEN_GNOME_KEYRING)
73 // NativeBackend implementation using GNOME Keyring.
74 class NativeBackendGnome
: public PasswordStoreX::NativeBackend
,
75 public GnomeKeyringLoader
{
77 explicit NativeBackendGnome(LocalProfileId id
);
79 virtual ~NativeBackendGnome();
81 virtual bool Init() OVERRIDE
;
83 // Implements NativeBackend interface.
84 virtual bool AddLogin(const autofill::PasswordForm
& form
) OVERRIDE
;
85 virtual bool UpdateLogin(const autofill::PasswordForm
& form
) OVERRIDE
;
86 virtual bool RemoveLogin(const autofill::PasswordForm
& form
) OVERRIDE
;
87 virtual bool RemoveLoginsCreatedBetween(
88 const base::Time
& delete_begin
, const base::Time
& delete_end
) OVERRIDE
;
89 virtual bool GetLogins(const autofill::PasswordForm
& form
,
90 PasswordFormList
* forms
) OVERRIDE
;
91 virtual bool GetLoginsCreatedBetween(const base::Time
& get_begin
,
92 const base::Time
& get_end
,
93 PasswordFormList
* forms
) OVERRIDE
;
94 virtual bool GetAutofillableLogins(PasswordFormList
* forms
) OVERRIDE
;
95 virtual bool GetBlacklistLogins(PasswordFormList
* forms
) OVERRIDE
;
98 // Adds a login form without checking for one to replace first.
99 bool RawAddLogin(const autofill::PasswordForm
& form
);
101 // Reads PasswordForms from the keyring with the given autofillability state.
102 bool GetLoginsList(PasswordFormList
* forms
, bool autofillable
);
104 // Helper for GetLoginsCreatedBetween().
105 bool GetAllLogins(PasswordFormList
* forms
);
107 // Generates a profile-specific app string based on profile_id_.
108 std::string
GetProfileSpecificAppString() const;
110 // The local profile id, used to generate the app string.
111 const LocalProfileId profile_id_
;
113 // The app string, possibly based on the local profile id.
114 std::string app_string_
;
116 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome
);
119 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_