1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_STRIKE_DATABASES_STRIKE_DATABASE_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_STRIKE_DATABASES_STRIKE_DATABASE_H_
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "components/autofill/core/browser/strike_databases/strike_database_base.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
namespace autofill {
extern const base::FilePath::StringViewType kStrikeDatabaseFileName;
// Manages data on whether different Autofill opportunities should be offered to
// the user. Projects can earn strikes in a number of ways; for instance, if a
// user ignores or declines a prompt, or if a user accepts a prompt but the task
// fails.
// This class is a Singleton which contains StrikeData information for all
// projects. It should not be used directly, but rather by implementing the
// StrikeDatabaseIntegratorBase (which contains a pointer to StrikeDatabase)
// for specific projects.
class StrikeDatabase : public StrikeDatabaseBase {
public:
using ClearStrikesCallback = base::RepeatingCallback<void(bool success)>;
using GetValueCallback =
base::RepeatingCallback<void(bool success,
std::unique_ptr<StrikeData> data)>;
using LoadKeysCallback =
base::RepeatingCallback<void(bool success,
std::unique_ptr<std::vector<std::string>>)>;
using SetValueCallback = base::RepeatingCallback<void(bool success)>;
using StrikesCallback = base::RepeatingCallback<void(int num_strikes)>;
using StrikeDataProto = leveldb_proto::ProtoDatabase<StrikeData>;
StrikeDatabase(leveldb_proto::ProtoDatabaseProvider* db_provider,
base::FilePath profile_path);
~StrikeDatabase() override;
// StrikeDatabaseBase:
int AddStrikes(int strikes_increase, const std::string& key) override;
int RemoveStrikes(int strikes_decrease, const std::string& key) override;
int GetStrikes(const std::string& key) override;
void ClearStrikes(const std::string& key) override;
std::vector<std::string> GetAllStrikeKeysForProject(
const std::string& project_prefix) override;
void ClearStrikesForKeys(
const std::vector<std::string>& keys_to_remove) override;
void ClearAllStrikesForProject(const std::string& project_prefix) override;
void ClearAllStrikes() override;
std::string GetPrefixFromKey(const std::string& key) const override;
void SetStrikeData(const std::string& key, int num_strikes) override;
int64_t GetLastUpdatedTimestamp(const std::string& key) override;
protected:
friend class StrikeDatabaseIntegratorBase;
// Constructor for testing that does not initialize a ProtoDatabase.
StrikeDatabase();
// The persistent ProtoDatabase for storing strike information.
std::unique_ptr<leveldb_proto::ProtoDatabase<StrikeData>> db_;
// Cached StrikeDatabase entries.
std::map<std::string, StrikeData> strike_map_cache_;
// Whether or not the ProtoDatabase database has been initialized and entries
// have been loaded.
bool database_initialized_ = false;
// Number of attempts at initializing the ProtoDatabase.
int num_init_attempts_ = 0;
// StrikeDatabaseBase:
std::map<std::string, StrikeData>& GetStrikeCache() override;
private:
FRIEND_TEST_ALL_PREFIXES(ChromeBrowsingDataRemoverDelegateTest,
StrikeDatabaseEmptyOnAutofillRemoveEverything);
FRIEND_TEST_ALL_PREFIXES(CreditCardSaveStrikeDatabaseTest,
GetKeyForCreditCardSaveTest);
FRIEND_TEST_ALL_PREFIXES(CreditCardSaveStrikeDatabaseTest,
GetIdForCreditCardSaveTest);
FRIEND_TEST_ALL_PREFIXES(CreditCardSaveStrikeDatabaseTest,
RemoveExpiredStrikesOnLoadTest);
friend class FakeCreditCardServer;
friend class StrikeDatabaseTest;
friend class StrikeDatabaseTester;
void OnDatabaseInit(leveldb_proto::Enums::InitStatus status);
void OnDatabaseLoadKeysAndEntries(
bool success,
std::unique_ptr<std::map<std::string, StrikeData>> entries);
// Passes the number of strikes for |key| to |outer_callback|. In the case
// that the database fails to retrieve the strike update or if no entry is
// found for |key|, 0 is passed.
virtual void GetProtoStrikes(const std::string& key,
const StrikesCallback& outer_callback);
// Removes all database entries, which ensures there will be no saved strikes
// the next time the cache is recreated from the underlying ProtoDatabase.
virtual void ClearAllProtoStrikes(const ClearStrikesCallback& outer_callback);
// Removes database entry for |key|, which ensures there will be no saved
// strikes the next time the cache is recreated from the underlying
// ProtoDatabase.
virtual void ClearAllProtoStrikesForKey(
const std::string& key,
const ClearStrikesCallback& outer_callback);
// Same as |ClearAllProtoStrikesForKey()| but for a vector of |keys|.
virtual void ClearAllProtoStrikesForKeys(
const std::vector<std::string>& keys,
const ClearStrikesCallback& outer_callback);
// Passes success status and StrikeData entry for |key| to |inner_callback|.
void GetProtoStrikeData(const std::string& key,
const GetValueCallback& inner_callback);
// Sets the entry for |key| to |strike_data|. Success status is passed to the
// callback.
void SetProtoStrikeData(const std::string& key,
const StrikeData& strike_data,
const SetValueCallback& inner_callback);
// Passes number of strikes to |outer_callback|.
static void OnGetProtoStrikes(StrikesCallback outer_callback,
bool success,
std::unique_ptr<StrikeData> strike_data);
// Exposed for testing purposes.
void LoadKeys(const LoadKeysCallback& callback);
// Sets the entry for |key| in |strike_map_cache_| to |data|.
void UpdateCache(const std::string& key, const StrikeData& data);
base::WeakPtrFactory<StrikeDatabase> weak_ptr_factory_{this};
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_STRIKE_DATABASES_STRIKE_DATABASE_H_
|