Commit 6e46908a authored by Yue Zhang's avatar Yue Zhang Committed by Chromium LUCI CQ

[ChromeCart] Add chrome cart proto database

This CL adds a profile proto database for chrome cart. Detailed
functions of the database will be added in a later patch.

Bug: 1157892
Change-Id: I30fb26b11463bf66a075f193bf5131baef69a92e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2612276
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Reviewed-by: default avatarRobert Kaplow <rkaplow@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#843168}
parent 7fa3da5c
......@@ -3477,6 +3477,8 @@ static_library("browser") {
"browsing_data/chrome_browsing_data_lifetime_manager.h",
"browsing_data/chrome_browsing_data_lifetime_manager_factory.cc",
"browsing_data/chrome_browsing_data_lifetime_manager_factory.h",
"cart/cart_db.cc",
"cart/cart_db.h",
"cart/cart_handler.cc",
"cart/cart_handler.h",
"cart/cart_service.cc",
......@@ -4140,6 +4142,7 @@ static_library("browser") {
"webauthn/observable_authenticator_list.h",
]
deps += [
":cart_db_content_proto",
":theme_properties",
"//base/util/memory_pressure",
"//base/util/timer",
......@@ -7003,3 +7006,9 @@ proto_library("status_proto") {
sources = [ "policy/messaging_layer/util/status.proto" ]
generate_python = false
}
if (!is_android) {
proto_library("cart_db_content_proto") {
sources = [ "cart/cart_db_content.proto" ]
}
}
// Copyright 2021 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.
#include "chrome/browser/cart/cart_db.h"
#include "chrome/browser/cart/cart_db_content.pb.h"
#include "chrome/browser/persisted_state_db/profile_proto_db_factory.h"
CartDB::CartDB(content::BrowserContext* browser_context)
: proto_db_(
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance()
->GetForProfile(browser_context)) {}
CartDB::~CartDB() = default;
// Copyright 2021 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 CHROME_BROWSER_CART_CART_DB_H_
#define CHROME_BROWSER_CART_CART_DB_H_
#include "base/memory/weak_ptr.h"
namespace content {
class BrowserContext;
} // namespace content
namespace cart_db {
class ChromeCartContentProto;
} // namespace cart_db
template <typename T>
class ProfileProtoDB;
class CartDB {
public:
explicit CartDB(content::BrowserContext* browser_context);
CartDB(const CartDB&) = delete;
CartDB& operator=(const CartDB&) = delete;
~CartDB();
private:
ProfileProtoDB<cart_db::ChromeCartContentProto>* proto_db_;
base::WeakPtrFactory<CartDB> weak_ptr_factory_{this};
};
#endif // CHROME_BROWSER_CART_CART_DB_H_
// Copyright 2021 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.
syntax = "proto3";
option optimize_for = LITE_RUNTIME;
package cart_db;
// Used for storing ChromeCart Content.
message ChromeCartContentProto {
// Original key for data.
string key = 1;
// Merchant name of the site that the cart belongs to.
string merchant = 2;
// URL that leads to the cart page.
string merchant_cart_url = 3;
// Timestamp that last time user interacts with this cart.
double timestamp = 4;
// Image URLs of products within the cart.
repeated string product_image_urls = 5;
}
......@@ -7,7 +7,8 @@
#include "components/prefs/pref_service.h"
#include "components/search/ntp_features.h"
CartService::CartService(Profile* profile) : profile_(profile) {}
CartService::CartService(Profile* profile)
: profile_(profile), cart_db_(std::make_unique<CartDB>(profile_)) {}
CartService::~CartService() = default;
void CartService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
......
......@@ -5,12 +5,15 @@
#define CHROME_BROWSER_CART_CART_SERVICE_H_
#include "base/memory/weak_ptr.h"
#include "chrome/browser/cart/cart_db.h"
#include "chrome/browser/cart/cart_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_registry_simple.h"
// Service to maintain and read/write data for chrome cart module.
// TODO(crbug.com/1157892) Make this BrowserContext-based and get rid of Profile
// usage so that we can modularize this.
class CartService : public KeyedService {
public:
CartService(const CartService&) = delete;
......@@ -32,6 +35,7 @@ class CartService : public KeyedService {
// service.
explicit CartService(Profile* profile);
Profile* profile_;
std::unique_ptr<CartDB> cart_db_;
base::WeakPtrFactory<CartService> weak_ptr_factory_{this};
};
......
......@@ -32,6 +32,8 @@ source_set("persisted_state_db") {
"//chrome/browser/tab:jni_headers",
"//components/embedder_support/android:browser_context",
]
} else {
deps += [ "//chrome/browser:cart_db_content_proto" ]
}
}
......
......@@ -21,3 +21,19 @@ ProfileProtoDBFactory<
persisted_state_db::PersistedStateContentProto>::GetInstance() {
return GetPersistedStateProfileProtoDBFactory();
}
#if !defined(OS_ANDROID)
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>*
GetChromeCartProfileProtoDBFactory() {
static base::NoDestructor<
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>>
instance;
return instance.get();
}
template <>
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>*
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance() {
return GetChromeCartProfileProtoDBFactory();
}
#endif
......@@ -5,19 +5,30 @@
#ifndef CHROME_BROWSER_PERSISTED_STATE_DB_PROFILE_PROTO_DB_FACTORY_H_
#define CHROME_BROWSER_PERSISTED_STATE_DB_PROFILE_PROTO_DB_FACTORY_H_
#include "build/build_config.h"
#include "chrome/browser/persisted_state_db/profile_proto_db.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#if !defined(OS_ANDROID)
#include "chrome/browser/cart/cart_db_content.pb.h"
#endif
namespace {
const char kPersistedStateDBFolder[] = "persisted_state_db";
const char kChromeCartDBFolder[] = "chrome_cart_db";
} // namespace
ProfileProtoDBFactory<persisted_state_db::PersistedStateContentProto>*
GetPersistedStateProfileProtoDBFactory();
#if !defined(OS_ANDROID)
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>*
GetChromeCartProfileProtoDBFactory();
#endif
// Factory to create a ProtoDB per profile and per proto. Incognito is
// currently not supported and the factory will return nullptr for an incognito
// profile.
......@@ -75,10 +86,6 @@ KeyedService* ProfileProtoDBFactory<T>::BuildServiceInstanceFor(
content::BrowserContext::GetDefaultStoragePartition(context)
->GetProtoDatabaseProvider();
static_assert(
std::is_base_of<persisted_state_db::PersistedStateContentProto, T>::value,
"Provided template is not supported. To support add unique folder in the "
"below proto -> folder name mapping below this assert.");
// The following will become a proto -> dir and proto ->
// leveldb_proto::ProtoDbType mapping as more protos are added.
if (std::is_base_of<persisted_state_db::PersistedStateContentProto,
......@@ -87,10 +94,20 @@ KeyedService* ProfileProtoDBFactory<T>::BuildServiceInstanceFor(
context, proto_database_provider,
context->GetPath().AppendASCII(kPersistedStateDBFolder),
leveldb_proto::ProtoDbType::PERSISTED_STATE_DATABASE);
#if !defined(OS_ANDROID)
} else if (std::is_base_of<cart_db::ChromeCartContentProto, T>::value) {
return new ProfileProtoDB<T>(
context, proto_database_provider,
context->GetPath().AppendASCII(kChromeCartDBFolder),
leveldb_proto::ProtoDbType::CART_DATABASE);
#endif
} else {
// Must add in leveldb_proto::ProtoDbType and database directory folder for
// new protos
DCHECK(false);
// new protos.
DCHECK(false) << "Provided template is not supported. To support add "
"unique folder in the above proto -> folder name mapping. "
"This check could also fail because the template is not "
"supported on current platform.";
}
}
......
......@@ -122,6 +122,7 @@
#else
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/browsing_data/chrome_browsing_data_lifetime_manager_factory.h"
#include "chrome/browser/cart/cart_db_content.pb.h"
#include "chrome/browser/cart/cart_service_factory.h"
#include "chrome/browser/feedback/feedback_uploader_factory_chrome.h"
#include "chrome/browser/media/feeds/media_feeds_service_factory.h"
......@@ -449,6 +450,7 @@ void ChromeBrowserMainExtraPartsProfiles::
#endif
#if !defined(OS_ANDROID)
CartServiceFactory::GetInstance();
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance();
#endif
}
......
......@@ -90,6 +90,8 @@ std::string SharedProtoDatabaseClientList::ProtoDbTypeToString(
return "VideoTutorialsDatabase";
case ProtoDbType::FEED_KEY_VALUE_DATABASE:
return "FeedKeyValueDatabase";
case ProtoDbType::CART_DATABASE:
return "CartDatabase";
case ProtoDbType::LAST:
NOTREACHED();
return std::string();
......
......@@ -55,6 +55,7 @@ enum class ProtoDbType {
NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE = 29,
VIDEO_TUTORIALS_DATABASE = 30,
FEED_KEY_VALUE_DATABASE = 31,
CART_DATABASE = 32,
LAST,
};
......@@ -71,6 +72,7 @@ constexpr ProtoDbType kWhitelistedDbForSharedImpl[]{
ProtoDbType::NEARBY_SHARE_PUBLIC_CERTIFICATE_DATABASE,
ProtoDbType::VIDEO_TUTORIALS_DATABASE,
ProtoDbType::FEED_KEY_VALUE_DATABASE,
ProtoDbType::CART_DATABASE,
ProtoDbType::LAST, // Marks the end of list.
};
......
......@@ -8194,6 +8194,7 @@ reviews. Googlers can read more about this at go/gwsq-gerrit.
label="Database for storing budget information for origins."/>
<suffix name="CachedImageFetcherDatabase"
label="Database for CachedImageFetcher metadata."/>
<suffix name="CartDatabase" label="Database for chrome cart."/>
<suffix name="DomDistillerStore" label="Databases for DomDistillerStore">
<obsolete>
Deprecated since 2019-10.
......
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