Commit 86a07e4d authored by Yue Zhang's avatar Yue Zhang Committed by Chromium LUCI CQ

[ChromeCart] Add more functions to chrome cart database

TBR: yusufo@chromium.org
Bug: 1157892
Change-Id: I6199dacb72f3e9e9133d28115a7a621a8c0297fb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2613954Reviewed-by: default avatarYue Zhang <yuezhanggg@chromium.org>
Reviewed-by: default avatarWei-Yin Chen (陳威尹) <wychen@chromium.org>
Commit-Queue: Yue Zhang <yuezhanggg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844185}
parent 42877545
...@@ -12,3 +12,25 @@ CartDB::CartDB(content::BrowserContext* browser_context) ...@@ -12,3 +12,25 @@ CartDB::CartDB(content::BrowserContext* browser_context)
ProfileProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance() ProfileProtoDBFactory<cart_db::ChromeCartContentProto>::GetInstance()
->GetForProfile(browser_context)) {} ->GetForProfile(browser_context)) {}
CartDB::~CartDB() = default; CartDB::~CartDB() = default;
void CartDB::LoadCart(const std::string& domain, LoadCallback callback) {
proto_db_->LoadOneEntry(domain, std::move(callback));
}
void CartDB::LoadAllCarts(LoadCallback callback) {
proto_db_->LoadAllEntries(std::move(callback));
}
void CartDB::AddCart(const std::string& domain,
const cart_db::ChromeCartContentProto& proto,
OperationCallback callback) {
proto_db_->InsertContent(domain, proto, std::move(callback));
}
void CartDB::DeleteCart(const std::string& domain, OperationCallback callback) {
proto_db_->DeleteOneEntry(domain, std::move(callback));
}
void CartDB::DeleteAllCarts(OperationCallback callback) {
proto_db_->DeleteAllContent(std::move(callback));
}
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_CART_CART_DB_H_ #ifndef CHROME_BROWSER_CART_CART_DB_H_
#define CHROME_BROWSER_CART_CART_DB_H_ #define CHROME_BROWSER_CART_CART_DB_H_
#include "base/callback_helpers.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
namespace content { namespace content {
...@@ -20,11 +21,37 @@ class ProfileProtoDB; ...@@ -20,11 +21,37 @@ class ProfileProtoDB;
class CartDB { class CartDB {
public: public:
using KeyAndValue = std::pair<std::string, cart_db::ChromeCartContentProto>;
// Callback which is used when cart content is acquired.
using LoadCallback = base::OnceCallback<void(bool, std::vector<KeyAndValue>)>;
// Used for confirming an operation was completed successfully (e.g.
// insert, delete).
using OperationCallback = base::OnceCallback<void(bool)>;
explicit CartDB(content::BrowserContext* browser_context); explicit CartDB(content::BrowserContext* browser_context);
CartDB(const CartDB&) = delete; CartDB(const CartDB&) = delete;
CartDB& operator=(const CartDB&) = delete; CartDB& operator=(const CartDB&) = delete;
~CartDB(); ~CartDB();
// Load the cart for a domain.
void LoadCart(const std::string& domain, LoadCallback callback);
// Load all carts in the database.
void LoadAllCarts(LoadCallback callback);
// Add a cart to the database.
void AddCart(const std::string& domain,
const cart_db::ChromeCartContentProto& proto,
OperationCallback callback);
// Delete the cart from certain domain in the database.
void DeleteCart(const std::string& domain, OperationCallback callback);
// Delete all carts in the database.
void DeleteAllCarts(OperationCallback callback);
private: private:
ProfileProtoDB<cart_db::ChromeCartContentProto>* proto_db_; ProfileProtoDB<cart_db::ChromeCartContentProto>* proto_db_;
base::WeakPtrFactory<CartDB> weak_ptr_factory_{this}; base::WeakPtrFactory<CartDB> weak_ptr_factory_{this};
......
...@@ -373,7 +373,6 @@ void ProfileProtoDB<T>::OnLoadContent(LoadCallback callback, ...@@ -373,7 +373,6 @@ void ProfileProtoDB<T>::OnLoadContent(LoadCallback callback,
for (const auto& proto : *content) { for (const auto& proto : *content) {
// TODO(crbug.com/1157881) relax requirement for proto to have a key field // TODO(crbug.com/1157881) relax requirement for proto to have a key field
// and return key value pairs OnLoadContent. // and return key value pairs OnLoadContent.
DCHECK(proto.has_key());
results.emplace_back(proto.key(), proto); results.emplace_back(proto.key(), proto);
} }
} }
......
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