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