7e5fadb0877994e6772dde666c8e2580edb65c18
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_store_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_PASSWORD_STORE_X_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_
7
8 #include <vector>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "components/password_manager/core/browser/password_store_default.h"
13
14 class LoginDatabase;
15 class PrefService;
16
17 namespace user_prefs {
18 class PrefRegistrySyncable;
19 }
20
21 // PasswordStoreX is used on Linux and other non-Windows, non-Mac OS X
22 // operating systems. It uses a "native backend" to actually store the password
23 // data when such a backend is available, and otherwise falls back to using the
24 // login database like PasswordStoreDefault. It also handles automatically
25 // migrating password data to a native backend from the login database.
26 //
27 // There are currently native backends for GNOME Keyring and KWallet.
28 class PasswordStoreX : public PasswordStoreDefault {
29  public:
30   // NativeBackends more or less implement the PaswordStore interface, but
31   // with return values rather than implicit consumer notification.
32   class NativeBackend {
33    public:
34     typedef std::vector<autofill::PasswordForm*> PasswordFormList;
35
36     virtual ~NativeBackend() {}
37
38     virtual bool Init() = 0;
39
40     virtual bool AddLogin(const autofill::PasswordForm& form) = 0;
41     virtual bool UpdateLogin(const autofill::PasswordForm& form) = 0;
42     virtual bool RemoveLogin(const autofill::PasswordForm& form) = 0;
43     virtual bool RemoveLoginsCreatedBetween(const base::Time& delete_begin,
44                                             const base::Time& delete_end) = 0;
45     virtual bool GetLogins(const autofill::PasswordForm& form,
46                            PasswordFormList* forms) = 0;
47     virtual bool GetLoginsCreatedBetween(const base::Time& get_begin,
48                                          const base::Time& get_end,
49                                          PasswordFormList* forms) = 0;
50     virtual bool GetAutofillableLogins(PasswordFormList* forms) = 0;
51     virtual bool GetBlacklistLogins(PasswordFormList* forms) = 0;
52   };
53
54   // Takes ownership of |login_db| and |backend|. |backend| may be NULL in which
55   // case this PasswordStoreX will act the same as PasswordStoreDefault.
56   PasswordStoreX(scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
57                  scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
58                  LoginDatabase* login_db,
59                  NativeBackend* backend);
60
61 #if !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX)
62   // Registers the pref setting used for the methods below.
63   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
64
65   // Returns true if passwords have been tagged with the local profile id.
66   static bool PasswordsUseLocalProfileId(PrefService* prefs);
67
68   // Sets the persistent bit indicating that passwords have been tagged with the
69   // local profile id. This cannot be unset; passwords get migrated only once.
70   // The caller promises that |prefs| will not be deleted any time soon.
71   static void SetPasswordsUseLocalProfileId(PrefService* prefs);
72 #endif  // !defined(OS_MACOSX) && !defined(OS_CHROMEOS) && defined(OS_POSIX)
73
74  private:
75   friend class PasswordStoreXTest;
76
77   virtual ~PasswordStoreX();
78
79   // Implements PasswordStore interface.
80   virtual PasswordStoreChangeList AddLoginImpl(
81       const autofill::PasswordForm& form) OVERRIDE;
82   virtual PasswordStoreChangeList UpdateLoginImpl(
83       const autofill::PasswordForm& form) OVERRIDE;
84   virtual PasswordStoreChangeList RemoveLoginImpl(
85       const autofill::PasswordForm& form) OVERRIDE;
86   virtual PasswordStoreChangeList RemoveLoginsCreatedBetweenImpl(
87       const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
88   virtual void GetLoginsImpl(
89       const autofill::PasswordForm& form,
90       AuthorizationPromptPolicy prompt_policy,
91       const ConsumerCallbackRunner& callback_runner) OVERRIDE;
92   virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
93   virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
94   virtual bool FillAutofillableLogins(
95       std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
96   virtual bool FillBlacklistLogins(
97       std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
98
99   // Sort logins by origin, like the ORDER BY clause in login_database.cc.
100   void SortLoginsByOrigin(NativeBackend::PasswordFormList* list);
101
102   // Check to see whether migration is necessary, and perform it if so.
103   void CheckMigration();
104
105   // Return true if we should try using the native backend.
106   bool use_native_backend() { return !!backend_.get(); }
107
108   // Return true if we can fall back on the default store, warning the first
109   // time we call it when falling back is necessary. See |allow_fallback_|.
110   bool allow_default_store();
111
112   // Synchronously migrates all the passwords stored in the login database to
113   // the native backend. If successful, the login database will be left with no
114   // stored passwords, and the number of passwords migrated will be returned.
115   // (This might be 0 if migration was not necessary.) Returns < 0 on failure.
116   ssize_t MigrateLogins();
117
118   // The native backend in use, or NULL if none.
119   scoped_ptr<NativeBackend> backend_;
120   // Whether we have already attempted migration to the native store.
121   bool migration_checked_;
122   // Whether we should allow falling back to the default store. If there is
123   // nothing to migrate, then the first attempt to use the native store will
124   // be the first time we try to use it and we should allow falling back. If
125   // we have migrated successfully, then we do not allow falling back.
126   bool allow_fallback_;
127
128   DISALLOW_COPY_AND_ASSIGN(PasswordStoreX);
129 };
130
131 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_X_H_