#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_
#include <vector>
#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list_threadsafe.h"
#include "base/threading/thread.h"
#include "base/threading/thread_checker.h"
#include "base/time/time.h"
#include "components/password_manager/core/browser/password_store_change.h"
#include "sync/api/syncable_service.h"
class PasswordStore;
class PasswordStoreConsumer;
class PasswordSyncableService;
class Task;
namespace autofill {
struct PasswordForm;
}
namespace browser_sync {
class PasswordChangeProcessor;
class PasswordDataTypeController;
class PasswordModelAssociator;
class PasswordModelWorker;
}
namespace passwords_helper {
void AddLogin(PasswordStore* store, const autofill::PasswordForm& form);
void RemoveLogin(PasswordStore* store, const autofill::PasswordForm& form);
void UpdateLogin(PasswordStore* store, const autofill::PasswordForm& form);
}
namespace syncer {
class SyncableService;
}
class PasswordStore : public base::RefCountedThreadSafe<PasswordStore> {
public:
enum AuthorizationPromptPolicy {
ALLOW_PROMPT,
DISALLOW_PROMPT
};
class GetLoginsRequest {
public:
explicit GetLoginsRequest(PasswordStoreConsumer* consumer);
virtual ~GetLoginsRequest();
void set_ignore_logins_cutoff(const base::Time& cutoff) {
ignore_logins_cutoff_ = cutoff;
}
void ApplyIgnoreLoginsCutoff();
void ForwardResult();
std::vector<autofill::PasswordForm*>* result() const {
return result_.get();
}
private:
base::Time ignore_logins_cutoff_;
base::WeakPtr<PasswordStoreConsumer> consumer_weak_;
scoped_ptr< std::vector<autofill::PasswordForm*> > result_;
base::ThreadChecker thread_checker_;
scoped_refptr<base::MessageLoopProxy> origin_loop_;
DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
};
class Observer {
public:
virtual void OnLoginsChanged(const PasswordStoreChangeList& changes) = 0;
protected:
virtual ~Observer() {}
};
PasswordStore(
scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner);
virtual bool Init(const syncer::SyncableService::StartSyncFlare& flare);
virtual void AddLogin(const autofill::PasswordForm& form);
virtual void UpdateLogin(const autofill::PasswordForm& form);
virtual void RemoveLogin(const autofill::PasswordForm& form);
virtual void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
const base::Time& delete_end);
virtual void GetLogins(
const autofill::PasswordForm& form,
AuthorizationPromptPolicy prompt_policy,
PasswordStoreConsumer* consumer);
virtual void GetAutofillableLogins(PasswordStoreConsumer* consumer);
virtual void GetBlacklistLogins(PasswordStoreConsumer* consumer);
virtual void ReportMetrics();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
virtual void Shutdown();
#if defined(PASSWORD_MANAGER_ENABLE_SYNC)
base::WeakPtr<syncer::SyncableService> GetPasswordSyncableService();
#endif
protected:
friend class base::RefCountedThreadSafe<PasswordStore>;
friend class browser_sync::PasswordChangeProcessor;
friend class browser_sync::PasswordDataTypeController;
friend class browser_sync::PasswordModelAssociator;
friend class browser_sync::PasswordModelWorker;
friend class PasswordSyncableService;
friend void passwords_helper::AddLogin(PasswordStore*,
const autofill::PasswordForm&);
friend void passwords_helper::RemoveLogin(PasswordStore*,
const autofill::PasswordForm&);
friend void passwords_helper::UpdateLogin(PasswordStore*,
const autofill::PasswordForm&);
FRIEND_TEST_ALL_PREFIXES(PasswordStoreTest, IgnoreOldWwwGoogleLogins);
typedef base::Callback<PasswordStoreChangeList(void)> ModificationTask;
virtual ~PasswordStore();
bool ScheduleTask(const base::Closure& task);
virtual scoped_refptr<base::SingleThreadTaskRunner> GetBackgroundTaskRunner();
virtual void ReportMetricsImpl() = 0;
virtual PasswordStoreChangeList AddLoginImpl(
const autofill::PasswordForm& form) = 0;
virtual PasswordStoreChangeList UpdateLoginImpl(
const autofill::PasswordForm& form) = 0;
virtual PasswordStoreChangeList RemoveLoginImpl(
const autofill::PasswordForm& form) = 0;
virtual PasswordStoreChangeList RemoveLoginsCreatedBetweenImpl(
const base::Time& delete_begin, const base::Time& delete_end) = 0;
typedef base::Callback<void(const std::vector<autofill::PasswordForm*>&)>
ConsumerCallbackRunner;
virtual void GetLoginsImpl(
const autofill::PasswordForm& form,
AuthorizationPromptPolicy prompt_policy,
const ConsumerCallbackRunner& callback_runner) = 0;
virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) = 0;
virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
virtual bool FillAutofillableLogins(
std::vector<autofill::PasswordForm*>* forms) = 0;
virtual bool FillBlacklistLogins(
std::vector<autofill::PasswordForm*>* forms) = 0;
virtual void ForwardLoginsResult(GetLoginsRequest* request);
void LogStatsForBulkDeletion(int num_deletions);
scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_;
scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner_;
scoped_ptr<PasswordSyncableService> syncable_service_;
private:
template<typename BackendFunc>
void Schedule(BackendFunc func, PasswordStoreConsumer* consumer);
virtual void WrapModificationTask(ModificationTask task);
void NotifyLoginsChanged(const PasswordStoreChangeList& changes);
void CopyAndForwardLoginsResult(
PasswordStore::GetLoginsRequest* request,
const std::vector<autofill::PasswordForm*>& matched_forms);
#if defined(PASSWORD_MANAGER_ENABLE_SYNC)
void InitSyncableService(
const syncer::SyncableService::StartSyncFlare& flare);
void DestroySyncableService();
#endif
scoped_refptr<ObserverListThreadSafe<Observer> > observers_;
bool shutdown_called_;
DISALLOW_COPY_AND_ASSIGN(PasswordStore);
};
#endif