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
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#pragma once
8
9#include <dbus/dbus-glib.h>
10#include <glib.h>
11
12#include <string>
13
14#include "base/basictypes.h"
15#include "base/time.h"
16#include "chrome/browser/password_manager/password_store_x.h"
17#include "webkit/glue/password_form.h"
18
19class Pickle;
20
21// NativeBackend implementation using KWallet.
22class NativeBackendKWallet : public PasswordStoreX::NativeBackend {
23 public:
24  NativeBackendKWallet();
25
26  virtual ~NativeBackendKWallet();
27
28  virtual bool Init();
29
30  // Implements NativeBackend interface.
31  virtual bool AddLogin(const webkit_glue::PasswordForm& form);
32  virtual bool UpdateLogin(const webkit_glue::PasswordForm& form);
33  virtual bool RemoveLogin(const webkit_glue::PasswordForm& form);
34  virtual bool RemoveLoginsCreatedBetween(const base::Time& delete_begin,
35                                          const base::Time& delete_end);
36  virtual bool GetLogins(const webkit_glue::PasswordForm& form,
37                         PasswordFormList* forms);
38  virtual bool GetLoginsCreatedBetween(const base::Time& delete_begin,
39                                       const base::Time& delete_end,
40                                       PasswordFormList* forms);
41  virtual bool GetAutofillableLogins(PasswordFormList* forms);
42  virtual bool GetBlacklistLogins(PasswordFormList* forms);
43
44 private:
45  // Initialization.
46  bool StartKWalletd();
47  bool InitWallet();
48
49  // Reads PasswordForms from the wallet that match the given signon_realm.
50  bool GetLoginsList(PasswordFormList* forms,
51                     const std::string& signon_realm,
52                     int wallet_handle);
53
54  // Reads PasswordForms from the wallet with the given autofillability state.
55  bool GetLoginsList(PasswordFormList* forms,
56                     bool autofillable,
57                     int wallet_handle);
58
59  // Reads PasswordForms from the wallet created in the given time range.
60  bool GetLoginsList(PasswordFormList* forms,
61                     const base::Time& begin,
62                     const base::Time& end,
63                     int wallet_handle);
64
65  // Helper for some of the above GetLoginsList() methods.
66  bool GetAllLogins(PasswordFormList* forms, int wallet_handle);
67
68  // Writes a list of PasswordForms to the wallet with the given signon_realm.
69  // Overwrites any existing list for this signon_realm. Removes the entry if
70  // |forms| is empty. Returns true on success.
71  bool SetLoginsList(const PasswordFormList& forms,
72                     const std::string& signon_realm,
73                     int wallet_handle);
74
75  // Checks if the last DBus call returned an error. If it did, logs the error
76  // message, frees it and returns true.
77  // This must be called after every DBus call.
78  bool CheckError();
79
80  // Opens the wallet and ensures that the "Chrome Form Data" folder exists.
81  // Returns kInvalidWalletHandle on error.
82  int WalletHandle();
83
84  // Compares two PasswordForms and returns true if they are the same.
85  // If |update_check| is false, we only check the fields that are checked by
86  // LoginDatabase::UpdateLogin() when updating logins; otherwise, we check the
87  // fields that are checked by LoginDatabase::RemoveLogin() for removing them.
88  static bool CompareForms(const webkit_glue::PasswordForm& a,
89                           const webkit_glue::PasswordForm& b,
90                           bool update_check);
91
92  // Serializes a list of PasswordForms to be stored in the wallet.
93  static void SerializeValue(const PasswordFormList& forms, Pickle* pickle);
94
95  // Checks a serialized list of PasswordForms for sanity. Returns true if OK.
96  // Note that |realm| is only used for generating a useful warning message.
97  static bool CheckSerializedValue(const GArray* byte_array, const char* realm);
98
99  // Deserializes a list of PasswordForms from the wallet.
100  static void DeserializeValue(const std::string& signon_realm,
101                               const Pickle& pickle,
102                               PasswordFormList* forms);
103
104  // Convenience function to read a GURL from a Pickle. Assumes the URL has
105  // been written as a std::string. Returns true on success.
106  static bool ReadGURL(const Pickle& pickle, void** iter, GURL* url);
107
108  // In case the fields in the pickle ever change, version them so we can try to
109  // read old pickles. (Note: do not eat old pickles past the expiration date.)
110  static const int kPickleVersion = 0;
111
112  // Name of the application - will appear in kwallet's dialogs.
113  static const char* kAppId;
114  // Name of the folder to store passwords in.
115  static const char* kKWalletFolder;
116
117  // DBus stuff.
118  static const char* kKWalletServiceName;
119  static const char* kKWalletPath;
120  static const char* kKWalletInterface;
121  static const char* kKLauncherServiceName;
122  static const char* kKLauncherPath;
123  static const char* kKLauncherInterface;
124
125  // Invalid handle returned by WalletHandle().
126  static const int kInvalidKWalletHandle = -1;
127
128  // Error from the last DBus call. NULL when there's no error. Freed and
129  // cleared by CheckError().
130  GError* error_;
131  // Connection to the DBus session bus.
132  DBusGConnection* connection_;
133  // Proxy to the kwallet DBus service.
134  DBusGProxy* proxy_;
135
136  // The name of the wallet we've opened. Set during Init().
137  std::string wallet_name_;
138
139  DISALLOW_COPY_AND_ASSIGN(NativeBackendKWallet);
140};
141
142#endif  // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_KWALLET_X_H_
143