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
|
// Copyright 2024 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_COMMERCE_CORE_COMMERCE_INFO_CACHE_H_
#define COMPONENTS_COMMERCE_CORE_COMMERCE_INFO_CACHE_H_
#include <memory>
#include <string>
#include <unordered_map>
#include "base/cancelable_callback.h"
#include "base/functional/callback.h"
#include "base/time/time.h"
class GURL;
namespace base {
class Time;
}
namespace commerce {
struct PriceInsightsInfo;
struct ProductInfo;
class CommerceInfoCache {
public:
struct CacheEntry {
public:
CacheEntry();
CacheEntry(const CacheEntry&) = delete;
CacheEntry& operator=(const CacheEntry&) = delete;
~CacheEntry();
// Whether the fallback local extraction needs to run for page.
bool needs_local_extraction_run{false};
// The time that the local extraction execution started. This is primarily
// used for metrics.
base::Time local_extraction_execution_start_time;
std::unique_ptr<base::CancelableOnceClosure> run_local_extraction_task;
// The product info associated with the URL or nullptr if not available.
std::unique_ptr<ProductInfo> product_info;
// A flag indicating whether we should check for product info on-demand.
// This will be used to prevent repeated attempts.
bool run_product_info_on_demand{true};
// The price insights info associated with the URL or nullptr if not
// available.
std::unique_ptr<PriceInsightsInfo> price_insights_info;
};
CommerceInfoCache();
CommerceInfoCache(const CommerceInfoCache&);
CommerceInfoCache& operator=(const CommerceInfoCache&);
~CommerceInfoCache();
// Notify the cache that a URL is being referenced by some system we care
// about. This should be called once per instance. For example, if two tabs
// have the same URL open, this method should be called once for each.
void AddRef(const GURL& url);
// Notify the cache that a URL is no longer being used by a particular source.
// This method should be called 1:1 with RemoveRef. The cache will not remove
// an entry until all instances of a URL are cleared.
void RemoveRef(const GURL& url);
// Returns whether the cache is maintaining a URL. Returning |true| here does
// not necessarily mean there is information cached, only that it can be.
bool IsUrlReferenced(const GURL& url);
// Gets a pointer to the cache entry or |nullptr| if it doesn't exist. The
// pointer returned here should not be held by the client.
CommerceInfoCache::CacheEntry* GetEntryForUrl(const GURL& url);
// Gets the number of times a URL is being referenced in the cache.
size_t GetUrlRefCount(const GURL& url);
private:
// A map of URLs to the number of times that URL is being used or is open.
std::unordered_map<std::string, size_t> referenced_urls_;
std::unordered_map<std::string,
std::unique_ptr<CommerceInfoCache::CacheEntry>>
cache_;
};
} // namespace commerce
#endif // COMPONENTS_COMMERCE_CORE_COMMERCE_INFO_CACHE_H_
|