password_store_mac.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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_MAC_H_
6#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
7
8#include <vector>
9
10#include "base/callback_forward.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/threading/thread.h"
13#include "components/password_manager/core/browser/login_database.h"
14#include "components/password_manager/core/browser/password_store.h"
15
16namespace crypto {
17class AppleKeychain;
18}
19
20// Implements PasswordStore on top of the OS X Keychain, with an internal
21// database for extra metadata. For an overview of the interactions with the
22// Keychain, as well as the rationale for some of the behaviors, see the
23// Keychain integration design doc:
24// http://dev.chromium.org/developers/design-documents/os-x-password-manager-keychain-integration
25class PasswordStoreMac : public PasswordStore {
26 public:
27  // Takes ownership of |keychain| and |login_db|, both of which must be
28  // non-NULL.
29  PasswordStoreMac(
30      scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
31      scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
32      crypto::AppleKeychain* keychain,
33      LoginDatabase* login_db);
34
35  // Initializes |thread_|.
36  virtual bool Init(
37      const syncer::SyncableService::StartSyncFlare& flare) OVERRIDE;
38
39 protected:
40  virtual ~PasswordStoreMac();
41
42  virtual scoped_refptr<base::SingleThreadTaskRunner>
43      GetBackgroundTaskRunner() OVERRIDE;
44
45 private:
46  virtual void ReportMetricsImpl() OVERRIDE;
47  virtual PasswordStoreChangeList AddLoginImpl(
48      const autofill::PasswordForm& form) OVERRIDE;
49  virtual PasswordStoreChangeList UpdateLoginImpl(
50      const autofill::PasswordForm& form) OVERRIDE;
51  virtual PasswordStoreChangeList RemoveLoginImpl(
52      const autofill::PasswordForm& form) OVERRIDE;
53  virtual PasswordStoreChangeList RemoveLoginsCreatedBetweenImpl(
54      const base::Time& delete_begin, const base::Time& delete_end) OVERRIDE;
55  virtual void GetLoginsImpl(
56      const autofill::PasswordForm& form,
57      AuthorizationPromptPolicy prompt_policy,
58      const ConsumerCallbackRunner& callback_runner) OVERRIDE;
59  virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) OVERRIDE;
60  virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) OVERRIDE;
61  virtual bool FillAutofillableLogins(
62      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
63  virtual bool FillBlacklistLogins(
64      std::vector<autofill::PasswordForm*>* forms) OVERRIDE;
65
66  // Adds the given form to the Keychain if it's something we want to store
67  // there (i.e., not a blacklist entry). Returns true if the operation
68  // succeeded (either we added successfully, or we didn't need to).
69  bool AddToKeychainIfNecessary(const autofill::PasswordForm& form);
70
71  // Returns true if our database contains a form that exactly matches the given
72  // keychain form.
73  bool DatabaseHasFormMatchingKeychainForm(
74      const autofill::PasswordForm& form);
75
76  // Returns all the Keychain entries that we own but no longer have
77  // corresponding metadata for in our database.
78  // Caller is responsible for deleting the forms.
79  std::vector<autofill::PasswordForm*> GetUnusedKeychainForms();
80
81  // Removes the given forms from the database.
82  void RemoveDatabaseForms(
83      const std::vector<autofill::PasswordForm*>& forms);
84
85  // Removes the given forms from the Keychain.
86  void RemoveKeychainForms(
87      const std::vector<autofill::PasswordForm*>& forms);
88
89  scoped_ptr<crypto::AppleKeychain> keychain_;
90  scoped_ptr<LoginDatabase> login_metadata_db_;
91
92  // Thread that the synchronous methods are run on.
93  scoped_ptr<base::Thread> thread_;
94
95  DISALLOW_COPY_AND_ASSIGN(PasswordStoreMac);
96};
97
98#endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_MAC_H_
99