Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / password_manager / native_backend_gnome_x.h
blobe115462adec0ef22871895d33842847ef68ee0b1
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>
10 #include <string>
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"
18 class PrefService;
20 namespace autofill {
21 struct PasswordForm;
24 // Many of the gnome_keyring_* functions use variable arguments, which makes
25 // them difficult if not impossible to truly wrap in C. Therefore, we use
26 // appropriately-typed function pointers and scoping to make the fact that we
27 // might be dynamically loading the library almost invisible. As a bonus, we
28 // also get a simple way to mock the library for testing. Classes that inherit
29 // from GnomeKeyringLoader will use its versions of the gnome_keyring_*
30 // functions. Note that it has only static fields.
31 class GnomeKeyringLoader {
32 protected:
33 static bool LoadGnomeKeyring();
35 // Call a given parameter with the name of each function we use from GNOME
36 // Keyring. Make sure to adjust the unit test if you change these.
37 // The list of functions is divided into those we plan to mock in the unittest,
38 // and those which we use without mocking in the test.
39 #define GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F) \
40 F(is_available) \
41 F(store_password) \
42 F(delete_password) \
43 F(find_items) \
44 F(result_to_message)
45 #define GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
46 F(attribute_list_free) \
47 F(attribute_list_new) \
48 F(attribute_list_append_string) \
49 F(attribute_list_append_uint32)
50 #define GNOME_KEYRING_FOR_EACH_FUNC(F) \
51 GNOME_KEYRING_FOR_EACH_NON_MOCKED_FUNC(F) \
52 GNOME_KEYRING_FOR_EACH_MOCKED_FUNC(F)
54 // Declare the actual function pointers that we'll use in client code.
55 #define GNOME_KEYRING_DECLARE_POINTER(name) \
56 static typeof(&::gnome_keyring_##name) gnome_keyring_##name;
57 GNOME_KEYRING_FOR_EACH_FUNC(GNOME_KEYRING_DECLARE_POINTER)
58 #undef GNOME_KEYRING_DECLARE_POINTER
60 // Set to true if LoadGnomeKeyring() has already succeeded.
61 static bool keyring_loaded;
63 private:
64 #if defined(DLOPEN_GNOME_KEYRING)
65 struct FunctionInfo {
66 const char* name;
67 void** pointer;
70 // Make it easy to initialize the function pointers in LoadGnomeKeyring().
71 static const FunctionInfo functions[];
72 #endif // defined(DLOPEN_GNOME_KEYRING)
75 // NativeBackend implementation using GNOME Keyring.
76 class NativeBackendGnome : public PasswordStoreX::NativeBackend,
77 public GnomeKeyringLoader {
78 public:
79 NativeBackendGnome(LocalProfileId id, PrefService* prefs);
81 virtual ~NativeBackendGnome();
83 virtual bool Init() OVERRIDE;
85 // Implements NativeBackend interface.
86 virtual bool AddLogin(const autofill::PasswordForm& form) OVERRIDE;
87 virtual bool UpdateLogin(const autofill::PasswordForm& form) OVERRIDE;
88 virtual bool RemoveLogin(const autofill::PasswordForm& form) OVERRIDE;
89 virtual bool RemoveLoginsCreatedBetween(
90 const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
91 virtual bool GetLogins(const autofill::PasswordForm& form,
92 PasswordFormList* forms) OVERRIDE;
93 virtual bool GetLoginsCreatedBetween(const base::Time& get_begin,
94 const base::Time& get_end,
95 PasswordFormList* forms) OVERRIDE;
96 virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE;
97 virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE;
99 private:
100 // Adds a login form without checking for one to replace first.
101 bool RawAddLogin(const autofill::PasswordForm& form);
103 // Reads PasswordForms from the keyring with the given autofillability state.
104 bool GetLoginsList(PasswordFormList* forms, bool autofillable);
106 // Helper for GetLoginsCreatedBetween().
107 bool GetAllLogins(PasswordFormList* forms);
109 // Generates a profile-specific app string based on profile_id_.
110 std::string GetProfileSpecificAppString() const;
112 // Migrates non-profile-specific logins to be profile-specific.
113 void MigrateToProfileSpecificLogins();
115 // The local profile id, used to generate the app string.
116 const LocalProfileId profile_id_;
118 // The pref service to use for persistent migration settings.
119 PrefService* prefs_;
121 // The app string, possibly based on the local profile id.
122 std::string app_string_;
124 // True once MigrateToProfileSpecificLogins() has been attempted.
125 bool migrate_tried_;
127 DISALLOW_COPY_AND_ASSIGN(NativeBackendGnome);
130 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_GNOME_X_H_