Commit 38b464ff authored by Nikita Podguzov's avatar Nikita Podguzov Committed by Commit Bot

Printing API: Add DeletePrintJobs() method in the PrintJobDatabase

This is required to clean several expired print jobs at once.

Bug: 992889
Change-Id: Iecfedcd1282fe610c6bcc60c7f42746916dc9497
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1804486
Commit-Queue: Nikita Podguzov <nikitapodguzov@google.com>
Reviewed-by: default avatarSean Kau <skau@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697184}
parent 4921cd04
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_CHROMEOS_PRINTING_HISTORY_PRINT_JOB_DATABASE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/callback_forward.h"
......@@ -24,7 +25,7 @@ class PrintJobDatabase {
using SavePrintJobCallback = base::OnceCallback<void(bool success)>;
using DeletePrintJobCallback = base::OnceCallback<void(bool success)>;
using DeletePrintJobsCallback = base::OnceCallback<void(bool success)>;
using GetPrintJobsCallback = base::OnceCallback<void(
bool success,
......@@ -42,9 +43,9 @@ class PrintJobDatabase {
virtual void SavePrintJob(const printing::proto::PrintJobInfo& print_job_info,
SavePrintJobCallback callback) = 0;
// Removes the print job associated with given |id| from the storage.
virtual void DeletePrintJob(const std::string& id,
DeletePrintJobCallback callback) = 0;
// Removes the print jobs associated with given |ids| from the storage.
virtual void DeletePrintJobs(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback) = 0;
// Retrieves all print jobs from the storage.
virtual void GetPrintJobs(GetPrintJobsCallback callback) = 0;
......
......@@ -89,8 +89,8 @@ void PrintJobDatabaseImpl::SavePrintJob(
std::move(callback)));
}
void PrintJobDatabaseImpl::DeletePrintJob(const std::string& id,
DeletePrintJobCallback callback) {
void PrintJobDatabaseImpl::DeletePrintJobs(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback) {
if (init_status_ == InitStatus::FAILED) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), false));
......@@ -100,18 +100,16 @@ void PrintJobDatabaseImpl::DeletePrintJob(const std::string& id,
// Defer execution if database is uninitialized.
if (init_status_ != InitStatus::INITIALIZED) {
deferred_callbacks_.push(base::BindOnce(
&PrintJobDatabaseImpl::DeletePrintJob, weak_ptr_factory_.GetWeakPtr(),
id, std::move(callback)));
&PrintJobDatabaseImpl::DeletePrintJobs, weak_ptr_factory_.GetWeakPtr(),
ids, std::move(callback)));
return;
}
auto keys_to_remove = std::make_unique<std::vector<std::string>>();
keys_to_remove->push_back(id);
database_->UpdateEntries(
/*entries_to_save=*/std::make_unique<EntryVector>(),
/*keys_to_remove=*/std::move(keys_to_remove),
/*keys_to_remove=*/std::make_unique<std::vector<std::string>>(ids),
base::BindOnce(&PrintJobDatabaseImpl::OnPrintJobDeleted,
weak_ptr_factory_.GetWeakPtr(), id, std::move(callback)));
weak_ptr_factory_.GetWeakPtr(), ids, std::move(callback)));
}
void PrintJobDatabaseImpl::GetPrintJobs(GetPrintJobsCallback callback) {
......@@ -197,11 +195,13 @@ void PrintJobDatabaseImpl::OnPrintJobSaved(
FROM_HERE, base::BindOnce(std::move(callback), success));
}
void PrintJobDatabaseImpl::OnPrintJobDeleted(const std::string& id,
DeletePrintJobCallback callback,
bool success) {
void PrintJobDatabaseImpl::OnPrintJobDeleted(
const std::vector<std::string>& ids,
DeletePrintJobsCallback callback,
bool success) {
if (success)
cache_.erase(id);
for (const std::string& id : ids)
cache_.erase(id);
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), success));
}
......
......@@ -34,8 +34,8 @@ class PrintJobDatabaseImpl : public PrintJobDatabase {
bool IsInitialized() override;
void SavePrintJob(const printing::proto::PrintJobInfo& print_job_info,
SavePrintJobCallback callback) override;
void DeletePrintJob(const std::string& id,
DeletePrintJobCallback callback) override;
void DeletePrintJobs(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback) override;
void GetPrintJobs(GetPrintJobsCallback callback) override;
private:
......@@ -58,8 +58,8 @@ class PrintJobDatabaseImpl : public PrintJobDatabase {
SavePrintJobCallback callback,
bool success);
void OnPrintJobDeleted(const std::string& id,
DeletePrintJobCallback callback,
void OnPrintJobDeleted(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback,
bool success);
void GetPrintJobsFromProtoDatabase(GetPrintJobsCallback callback);
......
......@@ -90,16 +90,16 @@ class PrintJobDatabaseImplTest : public ::testing::Test {
run_loop.Run();
}
void DeletePrintJob(const std::string& id) {
void DeletePrintJobs(const std::vector<std::string>& ids) {
base::RunLoop run_loop;
print_job_database_->DeletePrintJob(
id, base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobDeleted,
base::Unretained(this), run_loop.QuitClosure()));
print_job_database_->DeletePrintJobs(
ids, base::BindOnce(&PrintJobDatabaseImplTest::OnPrintJobsDeleted,
base::Unretained(this), run_loop.QuitClosure()));
run_loop.Run();
}
void OnPrintJobDeleted(base::RepeatingClosure run_loop_closure,
bool success) {
void OnPrintJobsDeleted(base::RepeatingClosure run_loop_closure,
bool success) {
EXPECT_TRUE(success);
run_loop_closure.Run();
}
......@@ -152,13 +152,13 @@ TEST_F(PrintJobDatabaseImplTest, SavePrintJob) {
EXPECT_EQ(kTitle1, entries[0].title());
}
TEST_F(PrintJobDatabaseImplTest, DeletePrintJob) {
TEST_F(PrintJobDatabaseImplTest, DeletePrintJobs) {
Initialize();
PrintJobInfo print_job_info1 = ConstructPrintJobInfo(kId1, kTitle1);
SavePrintJob(print_job_info1);
PrintJobInfo print_job_info2 = ConstructPrintJobInfo(kId2, kTitle2);
SavePrintJob(print_job_info2);
DeletePrintJob(kId1);
DeletePrintJobs({kId1});
std::vector<PrintJobInfo> entries = GetPrintJobs();
EXPECT_EQ(1u, entries.size());
EXPECT_EQ(kId2, entries[0].id());
......
......@@ -28,9 +28,10 @@ void TestPrintJobDatabase::SavePrintJob(
std::move(callback).Run(true);
}
void TestPrintJobDatabase::DeletePrintJob(const std::string& id,
DeletePrintJobCallback callback) {
database_.erase(id);
void TestPrintJobDatabase::DeletePrintJobs(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback) {
for (const std::string& id : ids)
database_.erase(id);
std::move(callback).Run(true);
}
......
......@@ -23,8 +23,8 @@ class TestPrintJobDatabase : public PrintJobDatabase {
bool IsInitialized() override;
void SavePrintJob(const printing::proto::PrintJobInfo& print_job_info,
SavePrintJobCallback callback) override;
void DeletePrintJob(const std::string& id,
DeletePrintJobCallback callback) override;
void DeletePrintJobs(const std::vector<std::string>& ids,
DeletePrintJobsCallback callback) override;
void GetPrintJobs(GetPrintJobsCallback callback) override;
private:
......
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