password_store_mac.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback_forward.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/thread.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/login_database.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace crypto {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AppleKeychain;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
20c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochnamespace password_manager {
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass LoginDatabase;
22c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
23c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Implements PasswordStore on top of the OS X Keychain, with an internal
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// database for extra metadata. For an overview of the interactions with the
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Keychain, as well as the rationale for some of the behaviors, see the
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Keychain integration design doc:
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// http://dev.chromium.org/developers/design-documents/os-x-password-manager-keychain-integration
29c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass PasswordStoreMac : public password_manager::PasswordStore {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Takes ownership of |keychain| and |login_db|, both of which must be
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // non-NULL.
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreMac(
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      crypto::AppleKeychain* keychain,
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      password_manager::LoginDatabase* login_db);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Initializes |thread_|.
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool Init(
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const syncer::SyncableService::StartSyncFlare& flare,
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& sync_username) OVERRIDE;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Stops |thread_|.
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual void Shutdown() OVERRIDE;
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PasswordStoreMac();
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual scoped_refptr<base::SingleThreadTaskRunner>
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GetBackgroundTaskRunner() OVERRIDE;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void ReportMetricsImpl(const std::string& sync_username) OVERRIDE;
55c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList AddLoginImpl(
5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
57c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList UpdateLoginImpl(
5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
59c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList RemoveLoginImpl(
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
61c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      RemoveLoginsCreatedBetweenImpl(base::Time delete_begin,
636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                                     base::Time delete_end) OVERRIDE;
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual password_manager::PasswordStoreChangeList
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RemoveLoginsSyncedBetweenImpl(base::Time delete_begin,
66f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                    base::Time delete_end) OVERRIDE;
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void GetLoginsImpl(
6858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form,
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AuthorizationPromptPolicy prompt_policy,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const ConsumerCallbackRunner& callback_runner) OVERRIDE;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool FillAutofillableLogins(
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool FillBlacklistLogins(
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the given form to the Keychain if it's something we want to store
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // there (i.e., not a blacklist entry). Returns true if the operation
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeeded (either we added successfully, or we didn't need to).
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool AddToKeychainIfNecessary(const autofill::PasswordForm& form);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if our database contains a form that exactly matches the given
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keychain form.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DatabaseHasFormMatchingKeychainForm(
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the given forms from the database.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveDatabaseForms(
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<autofill::PasswordForm*>& forms);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the given forms from the Keychain.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveKeychainForms(
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<autofill::PasswordForm*>& forms);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Searches the database for forms without a corresponding entry in the
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // keychain. Removes those forms from the database, and returns them in
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // |forms|. Ownership of |forms| is passed to the caller.
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  void CleanOrphanedForms(std::vector<autofill::PasswordForm*>* forms);
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<crypto::AppleKeychain> keychain_;
102c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  scoped_ptr<password_manager::LoginDatabase> login_metadata_db_;
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Thread that the synchronous methods are run on.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Thread> thread_;
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PasswordStoreMac);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
111