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.
5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
10 #include "base/callback_forward.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/threading/thread.h"
14 #include "components/password_manager/core/browser/login_database.h"
15 #include "components/password_manager/core/browser/password_store.h"
21 namespace password_manager
{
25 // Implements PasswordStore on top of the OS X Keychain, with an internal
26 // database for extra metadata. For an overview of the interactions with the
27 // Keychain, as well as the rationale for some of the behaviors, see the
28 // Keychain integration design doc:
29 // http://dev.chromium.org/developers/design-documents/os-x-password-manager-keychain-integration
30 class PasswordStoreMac
: public password_manager::PasswordStore
{
32 // The |login_db| must not have been Init()-ed yet. It will be initialized in
33 // a deferred manner on the background thread.
35 scoped_refptr
<base::SingleThreadTaskRunner
> main_thread_runner
,
36 scoped_refptr
<base::SingleThreadTaskRunner
> db_thread_runner
,
37 scoped_ptr
<crypto::AppleKeychain
> keychain
,
38 scoped_ptr
<password_manager::LoginDatabase
> login_db
);
40 // Initializes |thread_|.
41 bool Init(const syncer::SyncableService::StartSyncFlare
& flare
) override
;
44 void Shutdown() override
;
46 // To be used for testing.
47 password_manager::LoginDatabase
* login_metadata_db() const {
48 return login_metadata_db_
.get();
51 // To be used for testing.
52 crypto::AppleKeychain
* keychain() const { return keychain_
.get(); }
55 ~PasswordStoreMac() override
;
57 // Opens |login_metadata_db_| on the background |thread_|.
58 void InitOnBackgroundThread();
60 scoped_refptr
<base::SingleThreadTaskRunner
> GetBackgroundTaskRunner()
64 void ReportMetricsImpl(const std::string
& sync_username
,
65 bool custom_passphrase_sync_enabled
) override
;
66 password_manager::PasswordStoreChangeList
AddLoginImpl(
67 const autofill::PasswordForm
& form
) override
;
68 password_manager::PasswordStoreChangeList
UpdateLoginImpl(
69 const autofill::PasswordForm
& form
) override
;
70 password_manager::PasswordStoreChangeList
RemoveLoginImpl(
71 const autofill::PasswordForm
& form
) override
;
72 password_manager::PasswordStoreChangeList
RemoveLoginsCreatedBetweenImpl(
73 base::Time delete_begin
,
74 base::Time delete_end
) override
;
75 password_manager::PasswordStoreChangeList
RemoveLoginsSyncedBetweenImpl(
76 base::Time delete_begin
,
77 base::Time delete_end
) override
;
78 ScopedVector
<autofill::PasswordForm
> FillMatchingLogins(
79 const autofill::PasswordForm
& form
,
80 AuthorizationPromptPolicy prompt_policy
) override
;
81 void GetAutofillableLoginsImpl(
82 scoped_ptr
<PasswordStore::GetLoginsRequest
> request
) override
;
83 void GetBlacklistLoginsImpl(
84 scoped_ptr
<PasswordStore::GetLoginsRequest
> request
) override
;
85 bool FillAutofillableLogins(
86 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
87 bool FillBlacklistLogins(
88 ScopedVector
<autofill::PasswordForm
>* forms
) override
;
89 void AddSiteStatsImpl(
90 const password_manager::InteractionsStats
& stats
) override
;
91 void RemoveSiteStatsImpl(const GURL
& origin_domain
) override
;
92 scoped_ptr
<password_manager::InteractionsStats
> GetSiteStatsImpl(
93 const GURL
& origin_domain
) override
;
95 // Adds the given form to the Keychain if it's something we want to store
96 // there (i.e., not a blacklist entry or a federated login). Returns true if
97 // the operation succeeded (either we added successfully, or we didn't need
99 bool AddToKeychainIfNecessary(const autofill::PasswordForm
& form
);
101 // Returns true if our database contains a form that exactly matches the given
103 bool DatabaseHasFormMatchingKeychainForm(
104 const autofill::PasswordForm
& form
);
106 // Removes the given forms from the database. After the call |forms| contains
107 // only those forms which were successfully removed.
108 void RemoveDatabaseForms(ScopedVector
<autofill::PasswordForm
>* forms
);
110 // Removes the given forms from the Keychain.
111 void RemoveKeychainForms(
112 const std::vector
<autofill::PasswordForm
*>& forms
);
114 // Searches the database for forms without a corresponding entry in the
115 // keychain. Removes those forms from the database, and adds them to
117 void CleanOrphanedForms(ScopedVector
<autofill::PasswordForm
>* orphaned_forms
);
119 scoped_ptr
<crypto::AppleKeychain
> keychain_
;
121 // The login metadata SQL database. The LoginDatabase instance is received via
122 // the in an uninitialized state, so as to allow injecting mocks, then Init()
123 // is called on the DB thread in a deferred manner. If opening the DB fails,
124 // |login_metadata_db_| will be reset to NULL for the lifetime of |this|.
125 scoped_ptr
<password_manager::LoginDatabase
> login_metadata_db_
;
127 // Thread that the synchronous methods are run on.
128 scoped_ptr
<base::Thread
> thread_
;
130 DISALLOW_COPY_AND_ASSIGN(PasswordStoreMac
);
133 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_