• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_PASSWORD_STORE_X_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
7 #pragma once
8 
9 #include <vector>
10 
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time.h"
13 #include "chrome/browser/password_manager/password_store_default.h"
14 
15 class LoginDatabase;
16 class Profile;
17 class WebDataService;
18 
19 // PasswordStoreX is used on Linux and other non-Windows, non-Mac OS X
20 // operating systems. It uses a "native backend" to actually store the password
21 // data when such a backend is available, and otherwise falls back to using the
22 // login database like PasswordStoreDefault. It also handles automatically
23 // migrating password data to a native backend from the login database.
24 //
25 // There are currently native backends for GNOME Keyring and KWallet.
26 class PasswordStoreX : public PasswordStoreDefault {
27  public:
28   // NativeBackends more or less implement the PaswordStore interface, but
29   // with return values rather than implicit consumer notification.
30   class NativeBackend {
31    public:
32     typedef std::vector<webkit_glue::PasswordForm*> PasswordFormList;
33 
~NativeBackend()34     virtual ~NativeBackend() {}
35 
36     virtual bool Init() = 0;
37 
38     virtual bool AddLogin(const webkit_glue::PasswordForm& form) = 0;
39     virtual bool UpdateLogin(const webkit_glue::PasswordForm& form) = 0;
40     virtual bool RemoveLogin(const webkit_glue::PasswordForm& form) = 0;
41     virtual bool RemoveLoginsCreatedBetween(const base::Time& delete_begin,
42                                             const base::Time& delete_end) = 0;
43     virtual bool GetLogins(const webkit_glue::PasswordForm& form,
44                            PasswordFormList* forms) = 0;
45     virtual bool GetLoginsCreatedBetween(const base::Time& get_begin,
46                                          const base::Time& get_end,
47                                          PasswordFormList* forms) = 0;
48     virtual bool GetAutofillableLogins(PasswordFormList* forms) = 0;
49     virtual bool GetBlacklistLogins(PasswordFormList* forms) = 0;
50   };
51 
52   // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
53   // case this PasswordStoreX will act the same as PasswordStoreDefault.
54   PasswordStoreX(LoginDatabase* login_db,
55                    Profile* profile,
56                    WebDataService* web_data_service,
57                    NativeBackend* backend);
58 
59  private:
60   friend class PasswordStoreXTest;
61 
62   virtual ~PasswordStoreX();
63 
64   // Implements PasswordStore interface.
65   virtual void AddLoginImpl(const webkit_glue::PasswordForm& form);
66   virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form);
67   virtual void RemoveLoginImpl(const webkit_glue::PasswordForm& form);
68   virtual void RemoveLoginsCreatedBetweenImpl(const base::Time& delete_begin,
69                                               const base::Time& delete_end);
70   virtual void GetLoginsImpl(GetLoginsRequest* request,
71                              const webkit_glue::PasswordForm& form);
72   virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request);
73   virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request);
74   virtual bool FillAutofillableLogins(
75       std::vector<webkit_glue::PasswordForm*>* forms);
76   virtual bool FillBlacklistLogins(
77       std::vector<webkit_glue::PasswordForm*>* forms);
78 
79   // Check to see whether migration is necessary, and perform it if so.
80   void CheckMigration();
81 
82   // Return true if we should try using the native backend.
use_native_backend()83   bool use_native_backend() { return !!backend_.get(); }
84 
85   // Return true if we can fall back on the default store, warning the first
86   // time we call it when falling back is necessary. See |allow_fallback_|.
87   bool allow_default_store();
88 
89   // Synchronously migrates all the passwords stored in the login database to
90   // the native backend. If successful, the login database will be left with no
91   // stored passwords, and the number of passwords migrated will be returned.
92   // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
93   ssize_t MigrateLogins();
94 
95   // The native backend in use, or NULL if none.
96   scoped_ptr<NativeBackend> backend_;
97   // Whether we have already attempted migration to the native store.
98   bool migration_checked_;
99   // Whether we should allow falling back to the default store. If there is
100   // nothing to migrate, then the first attempt to use the native store will
101   // be the first time we try to use it and we should allow falling back. If
102   // we have migrated successfully, then we do not allow falling back.
103   bool allow_fallback_;
104 
105   DISALLOW_COPY_AND_ASSIGN(PasswordStoreX);
106 };
107 
108 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
109