OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ | 5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ |
6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ | 6 #define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <dbus/dbus-glib.h> | |
10 #include <glib.h> | |
11 | |
12 #include <string> | 9 #include <string> |
13 | 10 |
14 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/memory/ref_counted.h" |
15 #include "base/time.h" | 13 #include "base/time.h" |
16 #include "chrome/browser/password_manager/password_store_x.h" | 14 #include "chrome/browser/password_manager/password_store_x.h" |
17 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
18 | 16 |
19 class Pickle; | 17 class Pickle; |
20 class PrefService; | 18 class PrefService; |
21 | 19 |
22 namespace webkit_glue { | 20 namespace webkit_glue { |
23 struct PasswordForm; | 21 struct PasswordForm; |
24 } | 22 } |
25 | 23 |
| 24 namespace base { |
| 25 class WaitableEvent; |
| 26 } |
| 27 |
| 28 namespace dbus { |
| 29 class Bus; |
| 30 class ObjectProxy; |
| 31 } |
| 32 |
26 // NativeBackend implementation using KWallet. | 33 // NativeBackend implementation using KWallet. |
27 class NativeBackendKWallet : public PasswordStoreX::NativeBackend { | 34 class NativeBackendKWallet : public PasswordStoreX::NativeBackend { |
28 public: | 35 public: |
29 NativeBackendKWallet(LocalProfileId id, PrefService* prefs); | 36 NativeBackendKWallet(LocalProfileId id, PrefService* prefs); |
30 | 37 |
31 virtual ~NativeBackendKWallet(); | 38 virtual ~NativeBackendKWallet(); |
32 | 39 |
33 virtual bool Init() OVERRIDE; | 40 virtual bool Init() OVERRIDE; |
34 | 41 |
35 // Implements NativeBackend interface. | 42 // Implements NativeBackend interface. |
36 virtual bool AddLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 43 virtual bool AddLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
37 virtual bool UpdateLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 44 virtual bool UpdateLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
38 virtual bool RemoveLogin(const webkit_glue::PasswordForm& form) OVERRIDE; | 45 virtual bool RemoveLogin(const webkit_glue::PasswordForm& form) OVERRIDE; |
39 virtual bool RemoveLoginsCreatedBetween( | 46 virtual bool RemoveLoginsCreatedBetween( |
40 const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE; | 47 const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE; |
41 virtual bool GetLogins(const webkit_glue::PasswordForm& form, | 48 virtual bool GetLogins(const webkit_glue::PasswordForm& form, |
42 PasswordFormList* forms) OVERRIDE; | 49 PasswordFormList* forms) OVERRIDE; |
43 virtual bool GetLoginsCreatedBetween(const base::Time& delete_begin, | 50 virtual bool GetLoginsCreatedBetween(const base::Time& delete_begin, |
44 const base::Time& delete_end, | 51 const base::Time& delete_end, |
45 PasswordFormList* forms) OVERRIDE; | 52 PasswordFormList* forms) OVERRIDE; |
46 virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE; | 53 virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE; |
47 virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE; | 54 virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE; |
48 | 55 |
| 56 protected: |
| 57 // Internally used by Init(), but also for testing to provide a mock bus. |
| 58 bool InitWithBus(dbus::Bus* bus); |
| 59 |
49 private: | 60 private: |
50 // Initialization. | 61 // Initialization. |
| 62 void InitOnDBThread(base::WaitableEvent* event, bool* success); |
51 bool StartKWalletd(); | 63 bool StartKWalletd(); |
52 bool InitWallet(); | 64 bool InitWallet(); |
53 | 65 |
54 // Reads PasswordForms from the wallet that match the given signon_realm. | 66 // Reads PasswordForms from the wallet that match the given signon_realm. |
55 bool GetLoginsList(PasswordFormList* forms, | 67 bool GetLoginsList(PasswordFormList* forms, |
56 const std::string& signon_realm, | 68 const std::string& signon_realm, |
57 int wallet_handle); | 69 int wallet_handle); |
58 | 70 |
59 // Reads PasswordForms from the wallet with the given autofillability state. | 71 // Reads PasswordForms from the wallet with the given autofillability state. |
60 bool GetLoginsList(PasswordFormList* forms, | 72 bool GetLoginsList(PasswordFormList* forms, |
61 bool autofillable, | 73 bool autofillable, |
62 int wallet_handle); | 74 int wallet_handle); |
63 | 75 |
64 // Reads PasswordForms from the wallet created in the given time range. | 76 // Reads PasswordForms from the wallet created in the given time range. |
65 bool GetLoginsList(PasswordFormList* forms, | 77 bool GetLoginsList(PasswordFormList* forms, |
66 const base::Time& begin, | 78 const base::Time& begin, |
67 const base::Time& end, | 79 const base::Time& end, |
68 int wallet_handle); | 80 int wallet_handle); |
69 | 81 |
70 // Helper for some of the above GetLoginsList() methods. | 82 // Helper for some of the above GetLoginsList() methods. |
71 bool GetAllLogins(PasswordFormList* forms, int wallet_handle); | 83 bool GetAllLogins(PasswordFormList* forms, int wallet_handle); |
72 | 84 |
73 // Writes a list of PasswordForms to the wallet with the given signon_realm. | 85 // Writes a list of PasswordForms to the wallet with the given signon_realm. |
74 // Overwrites any existing list for this signon_realm. Removes the entry if | 86 // Overwrites any existing list for this signon_realm. Removes the entry if |
75 // |forms| is empty. Returns true on success. | 87 // |forms| is empty. Returns true on success. |
76 bool SetLoginsList(const PasswordFormList& forms, | 88 bool SetLoginsList(const PasswordFormList& forms, |
77 const std::string& signon_realm, | 89 const std::string& signon_realm, |
78 int wallet_handle); | 90 int wallet_handle); |
79 | 91 |
80 // Checks if the last DBus call returned an error. If it did, logs the error | |
81 // message, frees it and returns true. | |
82 // This must be called after every DBus call. | |
83 bool CheckError(); | |
84 | |
85 // Opens the wallet and ensures that the "Chrome Form Data" folder exists. | 92 // Opens the wallet and ensures that the "Chrome Form Data" folder exists. |
86 // Returns kInvalidWalletHandle on error. | 93 // Returns kInvalidWalletHandle on error. |
87 int WalletHandle(); | 94 int WalletHandle(); |
88 | 95 |
89 // Compares two PasswordForms and returns true if they are the same. | 96 // Compares two PasswordForms and returns true if they are the same. |
90 // If |update_check| is false, we only check the fields that are checked by | 97 // If |update_check| is false, we only check the fields that are checked by |
91 // LoginDatabase::UpdateLogin() when updating logins; otherwise, we check the | 98 // LoginDatabase::UpdateLogin() when updating logins; otherwise, we check the |
92 // fields that are checked by LoginDatabase::RemoveLogin() for removing them. | 99 // fields that are checked by LoginDatabase::RemoveLogin() for removing them. |
93 static bool CompareForms(const webkit_glue::PasswordForm& a, | 100 static bool CompareForms(const webkit_glue::PasswordForm& a, |
94 const webkit_glue::PasswordForm& b, | 101 const webkit_glue::PasswordForm& b, |
95 bool update_check); | 102 bool update_check); |
96 | 103 |
97 // Serializes a list of PasswordForms to be stored in the wallet. | 104 // Serializes a list of PasswordForms to be stored in the wallet. |
98 static void SerializeValue(const PasswordFormList& forms, Pickle* pickle); | 105 static void SerializeValue(const PasswordFormList& forms, Pickle* pickle); |
99 | 106 |
100 // Checks a serialized list of PasswordForms for sanity. Returns true if OK. | 107 // Checks a serialized list of PasswordForms for sanity. Returns true if OK. |
101 // Note that |realm| is only used for generating a useful warning message. | 108 // Note that |realm| is only used for generating a useful warning message. |
102 static bool CheckSerializedValue(const GArray* byte_array, const char* realm); | 109 static bool CheckSerializedValue(const uint8_t* byte_array, size_t length, |
| 110 const std::string& realm); |
103 | 111 |
104 // Deserializes a list of PasswordForms from the wallet. | 112 // Deserializes a list of PasswordForms from the wallet. |
105 static void DeserializeValue(const std::string& signon_realm, | 113 static void DeserializeValue(const std::string& signon_realm, |
106 const Pickle& pickle, | 114 const Pickle& pickle, |
107 PasswordFormList* forms); | 115 PasswordFormList* forms); |
108 | 116 |
109 // Convenience function to read a GURL from a Pickle. Assumes the URL has | 117 // Convenience function to read a GURL from a Pickle. Assumes the URL has |
110 // been written as a std::string. Returns true on success. | 118 // been written as a std::string. Returns true on success. |
111 static bool ReadGURL(const Pickle& pickle, void** iter, GURL* url); | 119 static bool ReadGURL(const Pickle& pickle, void** iter, GURL* url); |
112 | 120 |
113 // In case the fields in the pickle ever change, version them so we can try to | 121 // In case the fields in the pickle ever change, version them so we can try to |
114 // read old pickles. (Note: do not eat old pickles past the expiration date.) | 122 // read old pickles. (Note: do not eat old pickles past the expiration date.) |
115 static const int kPickleVersion = 0; | 123 static const int kPickleVersion = 0; |
116 | 124 |
117 // Name of the folder to store passwords in. | 125 // Name of the folder to store passwords in. |
118 static const char kKWalletFolder[]; | 126 static const char kKWalletFolder[]; |
119 | 127 |
120 // DBus stuff. | 128 // DBus service, path, and interface names for klauncher and kwalletd. |
121 static const char kKWalletServiceName[]; | 129 static const char kKWalletServiceName[]; |
122 static const char kKWalletPath[]; | 130 static const char kKWalletPath[]; |
123 static const char kKWalletInterface[]; | 131 static const char kKWalletInterface[]; |
124 static const char kKLauncherServiceName[]; | 132 static const char kKLauncherServiceName[]; |
125 static const char kKLauncherPath[]; | 133 static const char kKLauncherPath[]; |
126 static const char kKLauncherInterface[]; | 134 static const char kKLauncherInterface[]; |
127 | 135 |
128 // Invalid handle returned by WalletHandle(). | 136 // Invalid handle returned by WalletHandle(). |
129 static const int kInvalidKWalletHandle = -1; | 137 static const int kInvalidKWalletHandle = -1; |
130 | 138 |
131 // Generates a profile-specific folder name based on profile_id_. | 139 // Generates a profile-specific folder name based on profile_id_. |
132 std::string GetProfileSpecificFolderName() const; | 140 std::string GetProfileSpecificFolderName() const; |
133 | 141 |
134 // Migrates non-profile-specific logins to be profile-specific. | 142 // Migrates non-profile-specific logins to be profile-specific. |
135 void MigrateToProfileSpecificLogins(); | 143 void MigrateToProfileSpecificLogins(); |
136 | 144 |
137 // The local profile id, used to generate the folder name. | 145 // The local profile id, used to generate the folder name. |
138 const LocalProfileId profile_id_; | 146 const LocalProfileId profile_id_; |
139 | 147 |
140 // The pref service to use for persistent migration settings. | 148 // The pref service to use for persistent migration settings. |
141 PrefService* prefs_; | 149 PrefService* prefs_; |
142 | 150 |
143 // The KWallet folder name, possibly based on the local profile id. | 151 // The KWallet folder name, possibly based on the local profile id. |
144 std::string folder_name_; | 152 std::string folder_name_; |
145 | 153 |
146 // True once MigrateToProfileSpecificLogins() has been attempted. | 154 // True once MigrateToProfileSpecificLogins() has been attempted. |
147 bool migrate_tried_; | 155 bool migrate_tried_; |
148 | 156 |
149 // Error from the last DBus call. NULL when there's no error. Freed and | 157 // DBus handle for communication with klauncher and kwalletd. |
150 // cleared by CheckError(). | 158 scoped_refptr<dbus::Bus> session_bus_; |
151 GError* error_; | 159 // Object proxy for kwalletd. We do not own this. |
152 // Connection to the DBus session bus. | 160 dbus::ObjectProxy* kwallet_proxy_; |
153 DBusGConnection* connection_; | |
154 // Proxy to the kwallet DBus service. | |
155 DBusGProxy* proxy_; | |
156 | 161 |
157 // The name of the wallet we've opened. Set during Init(). | 162 // The name of the wallet we've opened. Set during Init(). |
158 std::string wallet_name_; | 163 std::string wallet_name_; |
159 // The application name (e.g. "Chromium"), shown in KWallet auth dialogs. | 164 // The application name (e.g. "Chromium"), shown in KWallet auth dialogs. |
160 const std::string app_name_; | 165 const std::string app_name_; |
161 | 166 |
162 DISALLOW_COPY_AND_ASSIGN(NativeBackendKWallet); | 167 DISALLOW_COPY_AND_ASSIGN(NativeBackendKWallet); |
163 }; | 168 }; |
164 | 169 |
165 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ | 170 #endif // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_ |
OLD | NEW |