Skip to content

Commit

Permalink
[Subscriptions] Implement storage
Browse files Browse the repository at this point in the history
This CL implements the detailed logics of storage using
SessionProtoStorage. We also add a series of conversion methods between
subscription types and the std::string to enable the parse. We will add
the test in the followup CL.

Bug: 1311754
Change-Id: Ie91ea44f4ec492c4a96a3facd9ea19c87cee445a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3808612
Commit-Queue: Zhiyuan Cai <zhiyuancai@chromium.org>
Reviewed-by: David Maunder <davidjm@chromium.org>
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1035158}
  • Loading branch information
Zhiyuan Cai authored and Chromium LUCI CQ committed Aug 15, 2022
1 parent e781d13 commit d6d3fe1
Show file tree
Hide file tree
Showing 23 changed files with 473 additions and 97 deletions.
1 change: 1 addition & 0 deletions chrome/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2069,6 +2069,7 @@ static_library("browser") {
"//components/client_hints/browser",
"//components/cloud_devices/common",
"//components/commerce/core:commerce_heuristics_data",
"//components/commerce/core:commerce_subscription_db_content_proto",
"//components/commerce/core:feature_list",
"//components/commerce/core:persisted_state_db_content_proto",
"//components/commerce/core:shopping_service",
Expand Down
10 changes: 9 additions & 1 deletion chrome/browser/commerce/shopping_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/persisted_state_db/session_proto_db_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/commerce/core/shopping_service.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/storage_partition.h"
Expand Down Expand Up @@ -42,6 +44,9 @@ ShoppingServiceFactory::ShoppingServiceFactory()
DependsOn(BookmarkModelFactory::GetInstance());
DependsOn(OptimizationGuideKeyedServiceFactory::GetInstance());
DependsOn(IdentityManagerFactory::GetInstance());
DependsOn(SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>::
GetInstance());
}

KeyedService* ShoppingServiceFactory::BuildServiceInstanceFor(
Expand All @@ -52,7 +57,10 @@ KeyedService* ShoppingServiceFactory::BuildServiceInstanceFor(
OptimizationGuideKeyedServiceFactory::GetForProfile(profile),
profile->GetPrefs(), IdentityManagerFactory::GetForProfile(profile),
profile->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess());
->GetURLLoaderFactoryForBrowserProcess(),
SessionProtoDBFactory<commerce_subscription_db::
CommerceSubscriptionContentProto>::GetInstance()
->GetForProfile(context));
}

bool ShoppingServiceFactory::ServiceIsCreatedWithBrowserContext() const {
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/persisted_state_db/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ source_set("persisted_state_db") {

deps = [
"//base:base",
"//components/commerce/core:commerce_subscription_db_content_proto",
"//components/commerce/core:persisted_state_db_content_proto",
"//components/keyed_service/content",
"//components/leveldb_proto",
Expand All @@ -30,7 +31,6 @@ source_set("persisted_state_db") {
if (is_android) {
deps += [
"//chrome/browser/tab:jni_headers",
"//components/commerce/core:commerce_subscription_db_content_proto",
"//components/commerce/core:merchant_signal_db_proto",
]
} else {
Expand Down
34 changes: 17 additions & 17 deletions chrome/browser/persisted_state_db/session_proto_db_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ SessionProtoDBFactory<
return GetPersistedStateSessionProtoDBFactory();
}

SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
GetCommerceSubscriptionSessionProtoDBFactory() {
static base::NoDestructor<SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>>
instance;
return instance.get();
}

template <>
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>::GetInstance() {
return GetCommerceSubscriptionSessionProtoDBFactory();
}

#if !BUILDFLAG(IS_ANDROID)
SessionProtoDBFactory<cart_db::ChromeCartContentProto>*
GetChromeCartSessionProtoDBFactory() {
Expand Down Expand Up @@ -53,23 +70,6 @@ SessionProtoDBFactory<coupon_db::CouponContentProto>::GetInstance() {
}

#else
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
GetCommerceSubscriptionSessionProtoDBFactory() {
static base::NoDestructor<SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>>
instance;
return instance.get();
}

template <>
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>::GetInstance() {
return GetCommerceSubscriptionSessionProtoDBFactory();
}

SessionProtoDBFactory<merchant_signal_db::MerchantSignalContentProto>*
GetMerchantSignalSessionProtoDBFactory() {
static base::NoDestructor<
Expand Down
23 changes: 12 additions & 11 deletions chrome/browser/persisted_state_db/session_proto_db_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "base/no_destructor.h"
#include "build/build_config.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "components/session_proto_db/session_proto_db.h"
Expand All @@ -17,7 +18,6 @@
#include "components/commerce/core/proto/cart_db_content.pb.h"
#include "components/commerce/core/proto/coupon_db_content.pb.h"
#else
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/commerce/core/proto/merchant_signal_db_content.pb.h"
#endif

Expand All @@ -38,13 +38,14 @@ GetChromeCartSessionProtoDBFactory();
SessionProtoDBFactory<coupon_db::CouponContentProto>*
GetCouponSessionProtoDBFactory();
#else
SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
GetCommerceSubscriptionSessionProtoDBFactory();
SessionProtoDBFactory<merchant_signal_db::MerchantSignalContentProto>*
GetMerchantSignalSessionProtoDBFactory();
#endif

SessionProtoDBFactory<
commerce_subscription_db::CommerceSubscriptionContentProto>*
GetCommerceSubscriptionSessionProtoDBFactory();

// Factory to create a ProtoDB per browsing session (BrowserContext) and per
// proto. Incognito is currently not supported and the factory will return
// nullptr for an incognito profile.
Expand Down Expand Up @@ -109,6 +110,13 @@ KeyedService* SessionProtoDBFactory<T>::BuildServiceInstanceFor(
context, proto_database_provider,
context->GetPath().AppendASCII(kPersistedStateDBFolder),
leveldb_proto::ProtoDbType::PERSISTED_STATE_DATABASE);
} else if (std::is_base_of<
commerce_subscription_db::CommerceSubscriptionContentProto,
T>::value) {
return new SessionProtoDB<T>(
context, proto_database_provider,
context->GetPath().AppendASCII(kCommerceSubscriptionDBFolder),
leveldb_proto::ProtoDbType::COMMERCE_SUBSCRIPTION_DATABASE);
#if !BUILDFLAG(IS_ANDROID)
} else if (std::is_base_of<cart_db::ChromeCartContentProto, T>::value) {
return new SessionProtoDB<T>(
Expand All @@ -121,13 +129,6 @@ KeyedService* SessionProtoDBFactory<T>::BuildServiceInstanceFor(
context->GetPath().AppendASCII(kCouponDBFolder),
leveldb_proto::ProtoDbType::COUPON_DATABASE);
#else
} else if (std::is_base_of<
commerce_subscription_db::CommerceSubscriptionContentProto,
T>::value) {
return new SessionProtoDB<T>(
context, proto_database_provider,
context->GetPath().AppendASCII(kCommerceSubscriptionDBFolder),
leveldb_proto::ProtoDbType::COMMERCE_SUBSCRIPTION_DATABASE);
} else if (std::is_base_of<merchant_signal_db::MerchantSignalContentProto,
T>::value) {
return new SessionProtoDB<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
#include "chrome/common/chrome_features.h"
#include "components/breadcrumbs/core/breadcrumbs_status.h"
#include "components/captive_portal/core/buildflags.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/commerce/core/proto/persisted_state_db_content.pb.h"
#include "components/optimization_guide/core/optimization_guide_switches.h"
#include "components/optimization_guide/machine_learning_tflite_buildflags.h"
Expand All @@ -144,7 +145,6 @@
#include "chrome/browser/commerce/merchant_viewer/merchant_viewer_data_manager_factory.h"
#include "chrome/browser/media/android/cdm/media_drm_origin_id_manager_factory.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/commerce/core/proto/merchant_signal_db_content.pb.h"
#else
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
Expand Down Expand Up @@ -473,10 +473,8 @@ void ChromeBrowserMainExtraPartsProfiles::
SessionProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance();
SessionProtoDBFactory<coupon_db::CouponContentProto>::GetInstance();
#endif
#if BUILDFLAG(IS_ANDROID)
SessionProtoDBFactory<commerce_subscription_db::
CommerceSubscriptionContentProto>::GetInstance();
#endif
#if BUILDFLAG(IS_ANDROID)
SessionProtoDBFactory<
merchant_signal_db::MerchantSignalContentProto>::GetInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ IN_PROC_BROWSER_TEST_F(ProfileKeyedServiceBrowserTest,
"SendTabToSelfSyncService",
"SerialConnectionManager",
"SessionDataService",
"SessionProtoDBFactory",
"SessionSyncService",
"SessionsAPI",
"SettingsOverridesAPI",
Expand Down
13 changes: 8 additions & 5 deletions components/commerce/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ if (is_android) {
proto_in_dir = "//"
sources = [ "proto/merchant_signal_db_content.proto" ]
}

proto_library("commerce_subscription_db_content_proto") {
proto_in_dir = "//"
sources = [ "proto/commerce_subscription_db_content.proto" ]
}
} else {
proto_library("cart_db_content_proto") {
proto_in_dir = "//"
Expand All @@ -101,6 +96,11 @@ proto_library("persisted_state_db_content_proto") {
sources = [ "proto/persisted_state_db_content.proto" ]
}

proto_library("commerce_subscription_db_content_proto") {
proto_in_dir = "//"
sources = [ "proto/commerce_subscription_db_content.proto" ]
}

source_set("commerce_heuristics_data") {
sources = [
"commerce_heuristics_data.cc",
Expand Down Expand Up @@ -159,19 +159,22 @@ static_library("shopping_service") {
]

deps = [
":commerce_subscription_db_content_proto",
":feature_list",
":metrics",
":proto",
"//base",
"//components/bookmarks/browser",
"//components/commerce/core/subscriptions:subscriptions",
"//components/keyed_service/core",
"//components/leveldb_proto",
"//components/optimization_guide/core",
"//components/optimization_guide/proto:optimization_guide_proto",
"//components/power_bookmarks/core",
"//components/power_bookmarks/core:proto",
"//components/prefs:prefs",
"//components/resources:components_resources_grit",
"//components/session_proto_db:core",
"//services/data_decoder/public/cpp",
"//services/network/public/cpp:cpp",
"//ui/base",
Expand Down
1 change: 1 addition & 0 deletions components/commerce/core/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ include_rules = [
"+components/optimization_guide",
"+components/power_bookmarks/core",
"+components/prefs",
"+components/session_proto_db/session_proto_storage.h",
"+third_party/re2",
"+components/endpoint_fetcher/endpoint_fetcher.h",
"+components/search",
Expand Down
7 changes: 6 additions & 1 deletion components/commerce/core/mock_shopping_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
namespace commerce {

MockShoppingService::MockShoppingService()
: commerce::ShoppingService(nullptr, nullptr, nullptr, nullptr, nullptr) {}
: commerce::ShoppingService(nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr) {}

MockShoppingService::~MockShoppingService() = default;

Expand Down
11 changes: 8 additions & 3 deletions components/commerce/core/shopping_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/metrics/metrics_utils.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/commerce/core/proto/merchant_trust.pb.h"
#include "components/commerce/core/proto/price_tracking.pb.h"
#include "components/commerce/core/shopping_bookmark_model_observer.h"
Expand All @@ -28,6 +29,7 @@
#include "components/optimization_guide/core/new_optimization_guide_decider.h"
#include "components/optimization_guide/core/optimization_guide_util.h"
#include "components/optimization_guide/proto/hints.pb.h"
#include "components/session_proto_db/session_proto_storage.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/resource/resource_bundle.h"

Expand Down Expand Up @@ -55,7 +57,10 @@ ShoppingService::ShoppingService(
optimization_guide::NewOptimizationGuideDecider* opt_guide,
PrefService* pref_service,
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
SessionProtoStorage<
commerce_subscription_db::CommerceSubscriptionContentProto>*
subscription_proto_db)
: opt_guide_(opt_guide),
pref_service_(pref_service),
weak_ptr_factory_(this) {
Expand All @@ -77,9 +82,9 @@ ShoppingService::ShoppingService(
opt_guide_->RegisterOptimizationTypes(types);
}

if (identity_manager) {
if (identity_manager && subscription_proto_db) {
subscriptions_manager_ = std::make_unique<SubscriptionsManager>(
identity_manager, std::move(url_loader_factory));
identity_manager, std::move(url_loader_factory), subscription_proto_db);
}

if (bookmark_model) {
Expand Down
9 changes: 8 additions & 1 deletion components/commerce/core/shopping_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/scoped_observation.h"
#include "base/sequence_checker.h"
#include "base/supports_user_data.h"
#include "components/commerce/core/proto/commerce_subscription_db_content.pb.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/optimization_guide/core/optimization_guide_decision.h"
#include "services/data_decoder/public/cpp/data_decoder.h"
Expand All @@ -26,6 +27,9 @@ class PrefService;

class PrefRegistrySimple;

template <typename T>
class SessionProtoStorage;

namespace base {
class Value;
}
Expand Down Expand Up @@ -142,7 +146,10 @@ class ShoppingService : public KeyedService, public base::SupportsUserData {
optimization_guide::NewOptimizationGuideDecider* opt_guide,
PrefService* pref_service,
signin::IdentityManager* identity_manager,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
SessionProtoStorage<
commerce_subscription_db::CommerceSubscriptionContentProto>*
subscription_proto_db);
~ShoppingService() override;

ShoppingService(const ShoppingService&) = delete;
Expand Down
3 changes: 2 additions & 1 deletion components/commerce/core/shopping_service_test_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ ShoppingServiceTestBase::ShoppingServiceTestBase()
bookmark_model_.get(), opt_guide_.get(), pref_service_.get(),
identity_test_env_->identity_manager(),
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
test_url_loader_factory_.get()));
test_url_loader_factory_.get()),
nullptr);
}

ShoppingServiceTestBase::~ShoppingServiceTestBase() = default;
Expand Down
4 changes: 4 additions & 0 deletions components/commerce/core/subscriptions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ source_set("subscriptions") {

deps = [
"//base",
"//components/commerce/core:commerce_subscription_db_content_proto",
"//components/commerce/core:feature_list",
"//components/endpoint_fetcher:endpoint_fetcher",
"//components/leveldb_proto",
"//components/session_proto_db:core",
"//components/signin/public/identity_manager:identity_manager",
"//net/traffic_annotation:traffic_annotation",
"//services/data_decoder/public/cpp",
Expand All @@ -34,6 +37,7 @@ source_set("subscriptions_unit_tests") {
":subscriptions",
"//base/test:test_support",
"//components/commerce/core:feature_list",
"//components/leveldb_proto",
"//components/signin/public/identity_manager:test_support",
"//services/network/public/cpp:cpp",
"//testing/gmock",
Expand Down

0 comments on commit d6d3fe1

Please sign in to comment.