Commit 261ab98f authored by Doug Arnett's avatar Doug Arnett Committed by Commit Bot

[Previews] Removes HintCacheStore interface

Step 1 of dropping HintCacheStore interface - it uses a using
definition to avoid more changes for this CL and keep diffs meaningful.
Next step will be to rename HintCacheLevelDBStore to HintCacheStore.

Change-Id: I94997cd0506ee834958f6096179128041d66557b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1539006Reviewed-by: default avatarTarun Bansal <tbansal@chromium.org>
Commit-Queue: Doug Arnett <dougarnett@chromium.org>
Cr-Commit-Position: refs/heads/master@{#644906}
parent c31623e9
...@@ -8,7 +8,6 @@ static_library("content") { ...@@ -8,7 +8,6 @@ static_library("content") {
"hint_cache.h", "hint_cache.h",
"hint_cache_leveldb_store.cc", "hint_cache_leveldb_store.cc",
"hint_cache_leveldb_store.h", "hint_cache_leveldb_store.h",
"hint_cache_store.h",
"hints_fetcher.cc", "hints_fetcher.cc",
"hints_fetcher.h", "hints_fetcher.h",
"previews_decider_impl.cc", "previews_decider_impl.cc",
......
...@@ -13,10 +13,12 @@ ...@@ -13,10 +13,12 @@
#include "base/optional.h" #include "base/optional.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "components/optimization_guide/proto/hints.pb.h" #include "components/optimization_guide/proto/hints.pb.h"
#include "components/previews/content/hint_cache_store.h" #include "components/previews/content/hint_cache_leveldb_store.h"
namespace previews { namespace previews {
using HintCacheStore = HintCacheLevelDBStore;
using HintLoadedCallback = using HintLoadedCallback =
base::OnceCallback<void(const optimization_guide::proto::Hint*)>; base::OnceCallback<void(const optimization_guide::proto::Hint*)>;
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "components/leveldb_proto/public/proto_database_provider.h" #include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/previews/content/proto/hint_cache.pb.h"
namespace previews { namespace previews {
...@@ -118,7 +119,7 @@ void HintCacheLevelDBStore::Initialize(bool purge_existing_data, ...@@ -118,7 +119,7 @@ void HintCacheLevelDBStore::Initialize(bool purge_existing_data,
purge_existing_data, std::move(callback))); purge_existing_data, std::move(callback)));
} }
std::unique_ptr<HintCacheStore::ComponentUpdateData> std::unique_ptr<HintCacheLevelDBStore::ComponentUpdateData>
HintCacheLevelDBStore::MaybeCreateComponentUpdateData( HintCacheLevelDBStore::MaybeCreateComponentUpdateData(
const base::Version& version) const { const base::Version& version) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
...@@ -141,7 +142,7 @@ HintCacheLevelDBStore::MaybeCreateComponentUpdateData( ...@@ -141,7 +142,7 @@ HintCacheLevelDBStore::MaybeCreateComponentUpdateData(
return std::make_unique<LevelDBComponentUpdateData>(version); return std::make_unique<LevelDBComponentUpdateData>(version);
} }
std::unique_ptr<HintCacheStore::ComponentUpdateData> std::unique_ptr<HintCacheLevelDBStore::ComponentUpdateData>
HintCacheLevelDBStore::CreateUpdateDataForFetchedHints() const { HintCacheLevelDBStore::CreateUpdateDataForFetchedHints() const {
// TODO(mcrouse): Currently returns a LevelDBComponentUpdateData, future // TODO(mcrouse): Currently returns a LevelDBComponentUpdateData, future
// refactor will create a LevelDBFetchedHintsData that will take a cache // refactor will create a LevelDBFetchedHintsData that will take a cache
...@@ -150,7 +151,7 @@ HintCacheLevelDBStore::CreateUpdateDataForFetchedHints() const { ...@@ -150,7 +151,7 @@ HintCacheLevelDBStore::CreateUpdateDataForFetchedHints() const {
} }
void HintCacheLevelDBStore::UpdateComponentData( void HintCacheLevelDBStore::UpdateComponentData(
std::unique_ptr<ComponentUpdateData> component_data, std::unique_ptr<HintCacheLevelDBStore::ComponentUpdateData> component_data,
base::OnceClosure callback) { base::OnceClosure callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(component_data); DCHECK(component_data);
...@@ -281,7 +282,7 @@ HintCacheLevelDBStore::GetMetadataEntryKeyPrefix() { ...@@ -281,7 +282,7 @@ HintCacheLevelDBStore::GetMetadataEntryKeyPrefix() {
} }
// static // static
HintCacheStore::EntryKey HintCacheLevelDBStore::GetMetadataTypeEntryKey( HintCacheLevelDBStore::EntryKey HintCacheLevelDBStore::GetMetadataTypeEntryKey(
MetadataType metadata_type) { MetadataType metadata_type) {
return GetMetadataEntryKeyPrefix() + return GetMetadataEntryKeyPrefix() +
std::to_string(static_cast<int>(metadata_type)); std::to_string(static_cast<int>(metadata_type));
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
#ifndef COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_LEVELDB_STORE_H_ #ifndef COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_LEVELDB_STORE_H_
#define COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_LEVELDB_STORE_H_ #define COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_LEVELDB_STORE_H_
#include "components/previews/content/hint_cache_store.h"
#include <map> #include <map>
#include <string> #include <string>
#include <unordered_set> #include <unordered_set>
...@@ -17,19 +15,34 @@ ...@@ -17,19 +15,34 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "base/version.h"
#include "components/leveldb_proto/public/proto_database.h" #include "components/leveldb_proto/public/proto_database.h"
#include "components/previews/content/proto/hint_cache.pb.h"
namespace base { namespace base {
class SequencedTaskRunner; class SequencedTaskRunner;
} // namespace base } // namespace base
namespace previews { namespace optimization_guide {
namespace proto {
class Hint;
} // namespace proto
} // namespace optimization_guide
// Concrete implementation of HintCacheStore using LevelDB. All public calls in namespace previews {
// the store must be made on the same thread. namespace proto {
class HintCacheLevelDBStore : public HintCacheStore { class StoreEntry;
} // namespace proto
// The HintCache backing store, which is responsible for storing all hints that
// are locally available. While the HintCache itself may retain some hints in a
// memory cache, all of its hints are initially loaded asynchronously by the
// store. All calls to this store must be made from the same thread.
class HintCacheLevelDBStore {
public: public:
using HintLoadedCallback = base::OnceCallback<void(
const std::string&,
std::unique_ptr<optimization_guide::proto::Hint>)>;
using EntryKey = std::string;
using StoreEntryProtoDatabase = using StoreEntryProtoDatabase =
leveldb_proto::ProtoDatabase<previews::proto::StoreEntry>; leveldb_proto::ProtoDatabase<previews::proto::StoreEntry>;
...@@ -49,31 +62,81 @@ class HintCacheLevelDBStore : public HintCacheStore { ...@@ -49,31 +62,81 @@ class HintCacheLevelDBStore : public HintCacheStore {
kMaxValue = kFailed, kMaxValue = kFailed,
}; };
// Abstract base class for storing hint component update data. The data itself
// is populated by moving component hints into it on a background thread; it
// is then used to update the store's component data on the UI thread.
class ComponentUpdateData {
public:
explicit ComponentUpdateData(const base::Version& version)
: version_(version) {
DCHECK(version_.IsValid());
}
virtual ~ComponentUpdateData() = default;
const base::Version& version() const { return version_; }
// Pure virtual function for moving a hint into ComponentUpdateData. After
// MoveHintIntoUpdateData() is called, |hint| is no longer valid.
virtual void MoveHintIntoUpdateData(
optimization_guide::proto::Hint&& hint) = 0;
private:
// The component version of the update data.
base::Version version_;
};
HintCacheLevelDBStore( HintCacheLevelDBStore(
const base::FilePath& database_dir, const base::FilePath& database_dir,
scoped_refptr<base::SequencedTaskRunner> store_task_runner); scoped_refptr<base::SequencedTaskRunner> store_task_runner);
HintCacheLevelDBStore(const base::FilePath& database_dir, HintCacheLevelDBStore(const base::FilePath& database_dir,
std::unique_ptr<StoreEntryProtoDatabase> database); std::unique_ptr<StoreEntryProtoDatabase> database);
~HintCacheLevelDBStore() override; ~HintCacheLevelDBStore();
// HintCacheStore overrides: // Initializes the hint cache store. If |purge_existing_data| is set to true,
void Initialize(bool purge_existing_data, // then the cache is purged during initialization and starts in a fresh state.
base::OnceClosure callback) override; // When initialization completes, the provided callback is run asynchronously.
void Initialize(bool purge_existing_data, base::OnceClosure callback);
// Creates and returns a ComponentUpdateData object. This object is used to
// collect hints within a component in a format usable on a background
// thread and is later returned to the store in UpdateComponentData(). The
// ComponentUpdateData object is only created when the provided component
// version is newer than the store's version, indicating fresh hints. If the
// component's version is not newer than the store's version, then no
// ComponentUpdateData is created and nullptr is returned. This prevents
// unnecessary processing of the component's hints by the caller.
std::unique_ptr<ComponentUpdateData> MaybeCreateComponentUpdateData( std::unique_ptr<ComponentUpdateData> MaybeCreateComponentUpdateData(
const base::Version& version) const override; const base::Version& version) const;
std::unique_ptr<HintCacheStore::ComponentUpdateData> // Creates and returns an UpdateData object for Fetched Hints.
CreateUpdateDataForFetchedHints() const override; // This object is used to collect a batch of hints in a format that is usable
// to update the store on a background thread. This is always created when
// hints have been successfully fetched from the remote Optimization Guide
// Service so the store can expire old hints, remove hints specified by the
// server, and store the fresh hints.
std::unique_ptr<HintCacheLevelDBStore::ComponentUpdateData>
CreateUpdateDataForFetchedHints() const;
// Updates the component data (both version and hints) contained within the
// store. When this is called, all pre-existing component data within the
// store is purged and only the new data is retained. After the store is
// fully updated with the new component data, the callback is run
// asynchronously.
void UpdateComponentData(std::unique_ptr<ComponentUpdateData> component_data, void UpdateComponentData(std::unique_ptr<ComponentUpdateData> component_data,
base::OnceClosure callback) override; base::OnceClosure callback);
// Finds a hint entry key associated with the specified host suffix. Returns
// true if a hint entry key is found, in which case |out_hint_entry_key| is
// populated with the key.
bool FindHintEntryKey(const std::string& host_suffix, bool FindHintEntryKey(const std::string& host_suffix,
EntryKey* out_hint_entry_key) const override; EntryKey* out_hint_entry_key) const;
void LoadHint(const EntryKey& hint_entry_key, // Loads the hint specified by |hint_entry_key|.
HintLoadedCallback callback) override; // After the load finishes, the hint data is passed to |callback|. In the case
// where the hint cannot be loaded, the callback is run with a nullptr.
// Depending on the load result, the callback may be synchronous or
// asynchronous.
void LoadHint(const EntryKey& hint_entry_key, HintLoadedCallback callback);
private: private:
friend class HintCacheLevelDBStoreTest; friend class HintCacheLevelDBStoreTest;
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_STORE_H_
#define COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_STORE_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/version.h"
namespace optimization_guide {
namespace proto {
class Hint;
} // namespace proto
} // namespace optimization_guide
namespace previews {
// Abstract base class for the HintCache backing store, which is responsible for
// storing all hints that are locally available. While the HintCache itself may
// retain some hints in a memory cache, all of its hints are initially loaded
// asynchronously by the store. All calls to the HintCacheStore must be made
// from the same thread.
class HintCacheStore {
public:
using EntryKey = std::string;
using HintLoadedCallback = base::OnceCallback<void(
const std::string&,
std::unique_ptr<optimization_guide::proto::Hint>)>;
// Abstract base class for storing hint component update data. Concrete
// derived classes store the data in a form usable by their creating concrete
// HintCacheStore. The data itself is populated by moving component hints into
// it on a background thread; it is then used to update the store's component
// data on the UI thread. Each concrete HintCacheStore class must implement
// its own concrete ComponentUpdateData, which provides a store-specific
// implementation of MoveHintIntoUpdateData().
class ComponentUpdateData {
public:
explicit ComponentUpdateData(const base::Version& version)
: version_(version) {
DCHECK(version_.IsValid());
}
virtual ~ComponentUpdateData() = default;
const base::Version& version() const { return version_; }
// Pure virtual function for moving a hint into ComponentUpdateData. After
// MoveHintIntoUpdateData() is called, |hint| is no longer valid.
virtual void MoveHintIntoUpdateData(
optimization_guide::proto::Hint&& hint) = 0;
private:
// The component version of the update data.
base::Version version_;
};
HintCacheStore() = default;
virtual ~HintCacheStore() = default;
// Pure virtual function for initializing the store. What initialization
// entails is dependent upon the concrete store. If |purge_existing_data| is
// set to true, then the cache is purged during initialization and starts in
// a fresh state. When initialization completes, the provided callback is run
// asynchronously.
virtual void Initialize(bool purge_existing_data,
base::OnceClosure callback) = 0;
// Pure virtual function for creating and returning a concrete
// ComponentUpdateData object. This object is used to collect hints within a
// component in a format usable by the concrete HintCacheStore on a background
// thread and is later returned to the store in UpdateComponentData(). The
// ComponentUpdateData object is only created when the provided component
// version is newer than the store's version, indicating fresh hints. If the
// component's version is not newer than the store's version, then no
// ComponentUpdateData is created and nullptr is returned. This prevents
// unnecessary processing of the component's hints by the caller.
std::unique_ptr<ComponentUpdateData> virtual MaybeCreateComponentUpdateData(
const base::Version& version) const = 0;
// Pure virtual function to create an UpdateData object for Fetched Hints.
// This object is used to collect a batch of hints in a format that is usable
// by the concrete HintCacheStore. This is always created when hints have been
// successfully fetched from the remote Optimization Guide Service so the
// store can expire old hints, remove hints specified by the server, and
// store the fresh hints.
virtual std::unique_ptr<ComponentUpdateData> CreateUpdateDataForFetchedHints()
const = 0;
// Pure virtual function for updating the component data (both version and
// hints) contained within the store. When this is called, all pre-existing
// component data within the store is purged and only the new data is
// retained. After the store is fully updated with the new component data,
// the callback is run asynchronously
virtual void UpdateComponentData(
std::unique_ptr<ComponentUpdateData> component_data,
base::OnceClosure callback) = 0;
// Pure virtual function for finding a hint entry key associated with the
// specified host suffix. Returns true if a hint entry key is found, in which
// case |out_hint_entry_key| is populated with the key.
virtual bool FindHintEntryKey(const std::string& host_suffix,
EntryKey* out_hint_entry_key) const = 0;
// Pure virtual function for loading the hint specified by |hint_entry_key|.
// After the load finishes, the hint data is passed to |callback|. In the case
// where the hint cannot be loaded, the callback is run with a nullptr.
// Depending on the load result, the callback may be synchronous or
// asynchronous.
virtual void LoadHint(const EntryKey& hint_entry_key,
HintLoadedCallback callback) = 0;
};
} // namespace previews
#endif // COMPONENTS_PREVIEWS_CONTENT_HINT_CACHE_STORE_H_
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
#include "components/blacklist/opt_out_blacklist/opt_out_blacklist_item.h" #include "components/blacklist/opt_out_blacklist/opt_out_blacklist_item.h"
#include "components/blacklist/opt_out_blacklist/opt_out_store.h" #include "components/blacklist/opt_out_blacklist/opt_out_store.h"
#include "components/optimization_guide/optimization_guide_service.h" #include "components/optimization_guide/optimization_guide_service.h"
#include "components/previews/content/hint_cache_store.h" #include "components/previews/content/hint_cache_leveldb_store.h"
#include "components/previews/content/previews_hints.h" #include "components/previews/content/previews_hints.h"
#include "components/previews/content/previews_top_host_provider.h" #include "components/previews/content/previews_top_host_provider.h"
#include "components/previews/content/previews_ui_service.h" #include "components/previews/content/previews_ui_service.h"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment