password_store_mac.h revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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(
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const syncer::SyncableService::StartSyncFlare& flare) OVERRIDE;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Stops |thread_|.
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual void Shutdown() OVERRIDE;
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PasswordStoreMac();
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual scoped_refptr<base::SingleThreadTaskRunner>
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      GetBackgroundTaskRunner() OVERRIDE;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ReportMetricsImpl() OVERRIDE;
54c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList AddLoginImpl(
5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
56c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList UpdateLoginImpl(
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
58c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList RemoveLoginImpl(
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const autofill::PasswordForm& form) OVERRIDE;
60c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  virtual password_manager::PasswordStoreChangeList
616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      RemoveLoginsCreatedBetweenImpl(base::Time delete_begin,
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                                     base::Time delete_end) OVERRIDE;
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual password_manager::PasswordStoreChangeList
64f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      RemoveLoginsSyncedBetweenImpl(base::Time delete_begin,
65f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                                    base::Time delete_end) OVERRIDE;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void GetLoginsImpl(
6758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      AuthorizationPromptPolicy prompt_policy,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const ConsumerCallbackRunner& callback_runner) OVERRIDE;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool FillAutofillableLogins(
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool FillBlacklistLogins(
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the given form to the Keychain if it's something we want to store
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // there (i.e., not a blacklist entry). Returns true if the operation
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeeded (either we added successfully, or we didn't need to).
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool AddToKeychainIfNecessary(const autofill::PasswordForm& form);
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if our database contains a form that exactly matches the given
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keychain form.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DatabaseHasFormMatchingKeychainForm(
8558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const autofill::PasswordForm& form);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns all the Keychain entries that we own but no longer have
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // corresponding metadata for in our database.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Caller is responsible for deleting the forms.
9058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  std::vector<autofill::PasswordForm*> GetUnusedKeychainForms();
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the given forms from the database.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveDatabaseForms(
9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<autofill::PasswordForm*>& forms);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the given forms from the Keychain.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RemoveKeychainForms(
9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<autofill::PasswordForm*>& forms);
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<crypto::AppleKeychain> keychain_;
101c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  scoped_ptr<password_manager::LoginDatabase> login_metadata_db_;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Thread that the synchronous methods are run on.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::Thread> thread_;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PasswordStoreMac);
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
110