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.
4
5#ifndef CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_
6#define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/time/time.h"
13#include "chrome/browser/password_manager/password_store_factory.h"
14#include "chrome/browser/password_manager/password_store_x.h"
15#include "chrome/browser/profiles/profile.h"
16
17class Pickle;
18class PickleIterator;
19
20namespace autofill {
21struct PasswordForm;
22}
23
24namespace base {
25class WaitableEvent;
26}
27
28namespace dbus {
29class Bus;
30class ObjectProxy;
31}
32
33// NativeBackend implementation using KWallet.
34class NativeBackendKWallet : public PasswordStoreX::NativeBackend {
35 public:
36  explicit NativeBackendKWallet(LocalProfileId id);
37
38  virtual ~NativeBackendKWallet();
39
40  virtual bool Init() OVERRIDE;
41
42  // Implements NativeBackend interface.
43  virtual password_manager::PasswordStoreChangeList AddLogin(
44      const autofill::PasswordForm& form) OVERRIDE;
45  virtual bool UpdateLogin(
46      const autofill::PasswordForm& form,
47      password_manager::PasswordStoreChangeList* changes) OVERRIDE;
48  virtual bool RemoveLogin(const autofill::PasswordForm& form) OVERRIDE;
49  virtual bool RemoveLoginsCreatedBetween(
50      base::Time delete_begin,
51      base::Time delete_end,
52      password_manager::PasswordStoreChangeList* changes) OVERRIDE;
53  virtual bool RemoveLoginsSyncedBetween(
54      base::Time delete_begin,
55      base::Time delete_end,
56      password_manager::PasswordStoreChangeList* changes) OVERRIDE;
57  virtual bool GetLogins(const autofill::PasswordForm& form,
58                         PasswordFormList* forms) OVERRIDE;
59  virtual bool GetAutofillableLogins(PasswordFormList* forms) OVERRIDE;
60  virtual bool GetBlacklistLogins(PasswordFormList* forms) OVERRIDE;
61
62 protected:
63  // Invalid handle returned by WalletHandle().
64  static const int kInvalidKWalletHandle = -1;
65
66  // Internally used by Init(), but also for testing to provide a mock bus.
67  bool InitWithBus(scoped_refptr<dbus::Bus> optional_bus);
68
69  // Deserializes a list of PasswordForms from the wallet.
70  static void DeserializeValue(const std::string& signon_realm,
71                               const Pickle& pickle,
72                               PasswordFormList* forms);
73
74 private:
75  enum InitResult {
76    INIT_SUCCESS,    // Init succeeded.
77    TEMPORARY_FAIL,  // Init failed, but might succeed after StartKWalletd().
78    PERMANENT_FAIL   // Init failed, and is not likely to work later either.
79  };
80
81  enum TimestampToCompare {
82    CREATION_TIMESTAMP,
83    SYNC_TIMESTAMP,
84  };
85
86  // Initialization.
87  bool StartKWalletd();
88  InitResult InitWallet();
89  void InitOnDBThread(scoped_refptr<dbus::Bus> optional_bus,
90                      base::WaitableEvent* event,
91                      bool* success);
92
93  // Reads PasswordForms from the wallet that match the given signon_realm.
94  bool GetLoginsList(PasswordFormList* forms,
95                     const std::string& signon_realm,
96                     int wallet_handle);
97
98  // Reads PasswordForms from the wallet with the given autofillability state.
99  bool GetLoginsList(PasswordFormList* forms,
100                     bool autofillable,
101                     int wallet_handle);
102
103  // Helper for some of the above GetLoginsList() methods.
104  bool GetAllLogins(PasswordFormList* forms, int wallet_handle);
105
106  // Writes a list of PasswordForms to the wallet with the given signon_realm.
107  // Overwrites any existing list for this signon_realm. Removes the entry if
108  // |forms| is empty. Returns true on success.
109  bool SetLoginsList(const PasswordFormList& forms,
110                     const std::string& signon_realm,
111                     int wallet_handle);
112
113  // Removes password created/synced in the time interval. Returns |true| if the
114  // operation succeeded. |changes| will contain the changes applied.
115  bool RemoveLoginsBetween(base::Time delete_begin,
116                           base::Time delete_end,
117                           TimestampToCompare date_to_compare,
118                           password_manager::PasswordStoreChangeList* changes);
119
120  // Opens the wallet and ensures that the "Chrome Form Data" folder exists.
121  // Returns kInvalidWalletHandle on error.
122  int WalletHandle();
123
124  // Serializes a list of PasswordForms to be stored in the wallet.
125  static void SerializeValue(const PasswordFormList& forms, Pickle* pickle);
126
127  // Deserializes a list of PasswordForms from the wallet.
128  // |size_32| controls reading the size field within the pickle as 32 bits.
129  // We used to use Pickle::WriteSize() to write the number of password forms,
130  // but that has a different size on 32- and 64-bit systems. So, now we always
131  // write a 64-bit quantity, but we support trying to read it as either size
132  // when reading old pickles that fail to deserialize using the native size.
133  static bool DeserializeValueSize(const std::string& signon_realm,
134                                   const PickleIterator& iter,
135                                   int version, bool size_32, bool warn_only,
136                                   PasswordFormList* forms);
137
138  // In case the fields in the pickle ever change, version them so we can try to
139  // read old pickles. (Note: do not eat old pickles past the expiration date.)
140  static const int kPickleVersion = 4;
141
142  // Generates a profile-specific folder name based on profile_id_.
143  std::string GetProfileSpecificFolderName() const;
144
145  // The local profile id, used to generate the folder name.
146  const LocalProfileId profile_id_;
147
148  // The KWallet folder name, possibly based on the local profile id.
149  std::string folder_name_;
150
151  // DBus handle for communication with klauncher and kwalletd.
152  scoped_refptr<dbus::Bus> session_bus_;
153  // Object proxy for kwalletd. We do not own this.
154  dbus::ObjectProxy* kwallet_proxy_;
155
156  // The name of the wallet we've opened. Set during Init().
157  std::string wallet_name_;
158  // The application name (e.g. "Chromium"), shown in KWallet auth dialogs.
159  const std::string app_name_;
160
161  DISALLOW_COPY_AND_ASSIGN(NativeBackendKWallet);
162};
163
164#endif  // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_
165