Commit e2bb2d69 authored by Marc Treib's avatar Marc Treib Committed by Commit Bot

Delete unused methods of DomDistillerService and DomDistillerStore

Bug: 1007942
Change-Id: Ibf1dd753fbd6a8922b0c909a8657728eab6af2c1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1831901Reviewed-by: default avatarTommy Nyquist <nyquist@chromium.org>
Commit-Queue: Marc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701469}
parent 8217eeb3
......@@ -50,22 +50,6 @@ std::string LazyDomDistillerService::GetUrlForEntry(
return instance()->GetUrlForEntry(entry_id);
}
const std::string LazyDomDistillerService::AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) {
return instance()->AddToList(url, std::move(distiller_page), article_cb);
}
std::vector<ArticleEntry> LazyDomDistillerService::GetEntries() const {
return instance()->GetEntries();
}
std::unique_ptr<ArticleEntry> LazyDomDistillerService::RemoveEntry(
const std::string& entry_id) {
return instance()->RemoveEntry(entry_id);
}
std::unique_ptr<ViewerHandle> LazyDomDistillerService::ViewEntry(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
......
......@@ -36,15 +36,8 @@ class LazyDomDistillerService : public DomDistillerServiceInterface,
public:
// DomDistillerServiceInterface implementation:
const std::string AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) override;
bool HasEntry(const std::string& entry_id) override;
std::string GetUrlForEntry(const std::string& entry_id) override;
std::vector<ArticleEntry> GetEntries() const override;
std::unique_ptr<ArticleEntry> RemoveEntry(
const std::string& entry_id) override;
std::unique_ptr<ViewerHandle> ViewEntry(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
......
......@@ -32,14 +32,6 @@ ArticleEntry CreateSkeletonEntryForUrl(const GURL& url) {
return skeleton;
}
void RunArticleAvailableCallback(
const DomDistillerService::ArticleAvailableCallback& article_cb,
const ArticleEntry& entry,
const DistilledArticleProto* article_proto,
bool distillation_succeeded) {
article_cb.Run(distillation_succeeded);
}
} // namespace
DomDistillerService::DomDistillerService(
......@@ -67,47 +59,6 @@ DomDistillerService::CreateDefaultDistillerPageWithHandle(
std::move(handle));
}
const std::string DomDistillerService::AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) {
ArticleEntry entry;
const bool is_already_added = store_ && store_->GetEntryByUrl(url, &entry);
TaskTracker* task_tracker = nullptr;
if (is_already_added) {
task_tracker = GetTaskTrackerForEntry(entry);
if (task_tracker == nullptr) {
// Entry is in the store but there is no task tracker. This could
// happen when distillation has already completed. For now just return
// true.
// TODO(shashishekhar): Change this to check if article is available,
// An article may not be available for a variety of reasons, e.g.
// distillation failure or blobs not available locally.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(article_cb, true));
return entry.entry_id();
}
} else {
GetOrCreateTaskTrackerForUrl(url, &task_tracker);
}
if (!article_cb.is_null()) {
task_tracker->AddSaveCallback(
base::Bind(&RunArticleAvailableCallback, article_cb));
}
if (!is_already_added) {
task_tracker->AddSaveCallback(base::Bind(
&DomDistillerService::AddDistilledPageToList, base::Unretained(this)));
task_tracker->StartDistiller(distiller_factory_.get(),
std::move(distiller_page));
task_tracker->StartBlobFetcher();
}
return task_tracker->GetEntryId();
}
bool DomDistillerService::HasEntry(const std::string& entry_id) {
return store_ && store_->GetEntryById(entry_id, nullptr);
}
......@@ -120,32 +71,6 @@ std::string DomDistillerService::GetUrlForEntry(const std::string& entry_id) {
return "";
}
std::vector<ArticleEntry> DomDistillerService::GetEntries() const {
if (!store_) {
return std::vector<ArticleEntry>();
}
return store_->GetEntries();
}
std::unique_ptr<ArticleEntry> DomDistillerService::RemoveEntry(
const std::string& entry_id) {
std::unique_ptr<ArticleEntry> entry(new ArticleEntry);
entry->set_entry_id(entry_id);
TaskTracker* task_tracker = GetTaskTrackerForEntry(*entry);
if (task_tracker != nullptr) {
task_tracker->CancelSaveCallbacks();
}
if (!store_ || !store_->GetEntryById(entry_id, entry.get())) {
return std::unique_ptr<ArticleEntry>();
}
if (store_->RemoveEntry(*entry)) {
return entry;
}
return std::unique_ptr<ArticleEntry>();
}
std::unique_ptr<ViewerHandle> DomDistillerService::ViewEntry(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
......@@ -259,19 +184,6 @@ void DomDistillerService::CancelTask(TaskTracker* task) {
}
}
void DomDistillerService::AddDistilledPageToList(
const ArticleEntry& entry,
const DistilledArticleProto* article_proto,
bool distillation_succeeded) {
DCHECK(IsEntryValid(entry));
if (store_ && distillation_succeeded) {
DCHECK(article_proto);
DCHECK_GT(article_proto->pages_size(), 0);
store_->AddEntry(entry);
DCHECK_EQ(article_proto->pages_size(), entry.pages_size());
}
}
DistilledPagePrefs* DomDistillerService::GetDistilledPagePrefs() {
return distilled_page_prefs_.get();
}
......
......@@ -11,7 +11,6 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/dom_distiller/core/article_entry.h"
#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/dom_distiller/core/distiller_page.h"
......@@ -20,7 +19,6 @@ class GURL;
namespace dom_distiller {
class DistilledArticleProto;
class DistilledContentStore;
class DistillerFactory;
class DistillerPageFactory;
......@@ -37,17 +35,6 @@ class DomDistillerServiceInterface {
typedef base::Callback<void(bool)> ArticleAvailableCallback;
virtual ~DomDistillerServiceInterface() {}
// Distill the article at |url| and add the resulting entry to the DOM
// distiller list. |article_cb| is always invoked, and the bool argument to it
// represents whether the article is available offline.
// Use CreateDefaultDistillerPage() to create a default |distiller_page|.
// The provided |distiller_page| is only used if there is not already a
// distillation task in progress for the given |url|.
virtual const std::string AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) = 0;
// Returns whether an article stored has the given entry id.
virtual bool HasEntry(const std::string& entry_id) = 0;
......@@ -56,13 +43,6 @@ class DomDistillerServiceInterface {
// empty string if there is no entry associated with the given entry ID.
virtual std::string GetUrlForEntry(const std::string& entry_id) = 0;
// Gets the full list of entries.
virtual std::vector<ArticleEntry> GetEntries() const = 0;
// Removes the specified entry from the dom distiller store.
virtual std::unique_ptr<ArticleEntry> RemoveEntry(
const std::string& entry_id) = 0;
// Request to view an article by entry id. Returns a null pointer if no entry
// with |entry_id| exists. The ViewerHandle should be destroyed before the
// ViewRequestDelegate. The request will be cancelled when the handle is
......@@ -113,15 +93,8 @@ class DomDistillerService : public DomDistillerServiceInterface {
~DomDistillerService() override;
// DomDistillerServiceInterface implementation.
const std::string AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) override;
bool HasEntry(const std::string& entry_id) override;
std::string GetUrlForEntry(const std::string& entry_id) override;
std::vector<ArticleEntry> GetEntries() const override;
std::unique_ptr<ArticleEntry> RemoveEntry(
const std::string& entry_id) override;
std::unique_ptr<ViewerHandle> ViewEntry(
ViewRequestDelegate* delegate,
std::unique_ptr<DistillerPage> distiller_page,
......@@ -138,9 +111,6 @@ class DomDistillerService : public DomDistillerServiceInterface {
private:
void CancelTask(TaskTracker* task);
void AddDistilledPageToList(const ArticleEntry& entry,
const DistilledArticleProto* article_proto,
bool distillation_succeeded);
TaskTracker* CreateTaskTracker(const ArticleEntry& entry);
......
......@@ -33,6 +33,8 @@ namespace test {
namespace {
const char kTestEntryId[] = "id0";
class FakeViewRequestDelegate : public ViewRequestDelegate {
public:
~FakeViewRequestDelegate() override {}
......@@ -46,12 +48,6 @@ class MockArticleAvailableCallback {
MOCK_METHOD1(DistillationCompleted, void(bool));
};
DomDistillerService::ArticleAvailableCallback ArticleCallback(
MockArticleAvailableCallback* callback) {
return base::Bind(&MockArticleAvailableCallback::DistillationCompleted,
base::Unretained(callback));
}
void RunDistillerCallback(FakeDistiller* distiller,
std::unique_ptr<DistilledArticleProto> proto) {
distiller->RunDistillerCallback(std::move(proto));
......@@ -75,9 +71,13 @@ std::unique_ptr<DistilledArticleProto> CreateDefaultArticle() {
class DomDistillerServiceTest : public testing::Test {
public:
void SetUp() override {
// Create a test entry in the DB.
ArticleEntry entry;
entry.set_entry_id(kTestEntryId);
entry.add_pages()->set_url("http://www.example.com/p1");
db_model_[kTestEntryId] = entry;
FakeDB<ArticleEntry>* fake_db = new FakeDB<ArticleEntry>(&db_model_);
FakeDB<ArticleEntry>::EntryMap store_model;
store_ = test::util::CreateStoreWithFakeDB(fake_db, store_model);
store_ = test::util::CreateStoreWithFakeDB(fake_db, db_model_);
distiller_factory_ = new MockDistillerFactory();
distiller_page_factory_ = new MockDistillerPageFactory();
service_.reset(new DomDistillerService(
......@@ -111,18 +111,10 @@ TEST_F(DomDistillerServiceTest, TestViewEntry) {
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
GURL url("http://www.example.com/p1");
std::string entry_id("id0");
ArticleEntry entry;
entry.set_entry_id(entry_id);
entry.add_pages()->set_url(url.spec());
store_->AddEntry(entry);
FakeViewRequestDelegate viewer_delegate;
std::unique_ptr<ViewerHandle> handle = service_->ViewEntry(
&viewer_delegate, service_->CreateDefaultDistillerPage(gfx::Size()),
entry_id);
kTestEntryId);
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
......@@ -213,304 +205,5 @@ TEST_F(DomDistillerServiceTest, TestViewUrlCancelled) {
EXPECT_TRUE(distiller_destroyed);
}
TEST_F(DomDistillerServiceTest, TestViewUrlDoesNotAddEntry) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
FakeViewRequestDelegate viewer_delegate;
GURL url("http://www.example.com/p1");
std::unique_ptr<ViewerHandle> handle = service_->ViewUrl(
&viewer_delegate, service_->CreateDefaultDistillerPage(gfx::Size()), url);
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(url.spec());
EXPECT_CALL(viewer_delegate, OnArticleReady(proto.get()));
RunDistillerCallback(distiller, std::move(proto));
base::RunLoop().RunUntilIdle();
// The entry should not be added to the store.
EXPECT_EQ(0u, store_->GetEntries().size());
}
TEST_F(DomDistillerServiceTest, TestAddAndRemoveEntry) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
GURL url("http://www.example.com/p1");
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(true));
std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
EXPECT_EQ(url, distiller->GetUrl());
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(url.spec());
RunDistillerCallback(distiller, std::move(proto));
ArticleEntry entry;
EXPECT_TRUE(store_->GetEntryByUrl(url, &entry));
EXPECT_EQ(entry.entry_id(), entry_id);
EXPECT_EQ(1u, store_->GetEntries().size());
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, store_->GetEntries().size());
}
TEST_F(DomDistillerServiceTest, TestCancellation) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(false));
GURL url("http://www.example.com/p1");
std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
// Remove entry will cause the |article_cb| to be called with false value.
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
}
TEST_F(DomDistillerServiceTest, TestMultipleCallbacks) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
const int kClientsCount = 5;
MockArticleAvailableCallback article_cb[kClientsCount];
// Adding a URL and then distilling calls all clients.
GURL url("http://www.example.com/p1");
const std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb[0]));
EXPECT_CALL(article_cb[0], DistillationCompleted(true));
for (int i = 1; i < kClientsCount; ++i) {
EXPECT_EQ(entry_id,
service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb[i])));
EXPECT_CALL(article_cb[i], DistillationCompleted(true));
}
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(url.spec());
RunDistillerCallback(distiller, std::move(proto));
// Add the same url again, all callbacks should be called with true.
for (int i = 0; i < kClientsCount; ++i) {
EXPECT_CALL(article_cb[i], DistillationCompleted(true));
EXPECT_EQ(entry_id,
service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb[i])));
}
base::RunLoop().RunUntilIdle();
}
TEST_F(DomDistillerServiceTest, TestMultipleCallbacksOnRemove) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
const int kClientsCount = 5;
MockArticleAvailableCallback article_cb[kClientsCount];
// Adding a URL and remove the entry before distillation. Callback should be
// called with false.
GURL url("http://www.example.com/p1");
const std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb[0]));
EXPECT_CALL(article_cb[0], DistillationCompleted(false));
for (int i = 1; i < kClientsCount; ++i) {
EXPECT_EQ(entry_id,
service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb[i])));
EXPECT_CALL(article_cb[i], DistillationCompleted(false));
}
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
}
TEST_F(DomDistillerServiceTest, TestMultiplePageArticle) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
const int kPageCount = 8;
std::string base_url("http://www.example.com/p");
GURL pages_url[kPageCount];
for (int page_num = 0; page_num < kPageCount; ++page_num) {
pages_url[page_num] = GURL(base_url + base::NumberToString(page_num));
}
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(true));
std::string entry_id = service_->AddToList(
pages_url[0], service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
ArticleEntry entry;
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
EXPECT_EQ(pages_url[0], distiller->GetUrl());
// Create the article with pages to pass to the distiller.
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(pages_url[0].spec());
for (int page_num = 1; page_num < kPageCount; ++page_num) {
DistilledPageProto* distilled_page = proto->add_pages();
distilled_page->set_url(pages_url[page_num].spec());
}
RunDistillerCallback(distiller, std::move(proto));
EXPECT_TRUE(store_->GetEntryByUrl(pages_url[0], &entry));
EXPECT_EQ(kPageCount, entry.pages_size());
// An article should have just one entry.
EXPECT_EQ(1u, store_->GetEntries().size());
// All pages should have correct urls.
for (int page_num = 0; page_num < kPageCount; ++page_num) {
EXPECT_EQ(pages_url[page_num].spec(), entry.pages(page_num).url());
}
// Should be able to query article using any of the pages url.
for (int page_num = 0; page_num < kPageCount; ++page_num) {
EXPECT_TRUE(store_->GetEntryByUrl(pages_url[page_num], &entry));
}
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, store_->GetEntries().size());
}
TEST_F(DomDistillerServiceTest, TestHasEntry) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
GURL url("http://www.example.com/p1");
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(true));
std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
EXPECT_EQ(url, distiller->GetUrl());
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(url.spec());
RunDistillerCallback(distiller, std::move(proto));
// Check that HasEntry returns true for the article just added.
EXPECT_TRUE(service_->HasEntry(entry_id));
// Remove article and check that there is no longer an entry for the given
// entry id.
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, store_->GetEntries().size());
EXPECT_FALSE(service_->HasEntry(entry_id));
}
TEST_F(DomDistillerServiceTest, TestGetUrlForOnePageEntry) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
GURL url("http://www.example.com/p1");
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(true));
std::string entry_id = service_->AddToList(
url, service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
EXPECT_EQ(url, distiller->GetUrl());
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(url.spec());
RunDistillerCallback(distiller, std::move(proto));
// Check if retrieved URL is same as given URL.
GURL retrieved_url(service_->GetUrlForEntry(entry_id));
EXPECT_EQ(url, retrieved_url);
// Remove article and check that there is no longer an entry for the given
// entry id.
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, store_->GetEntries().size());
EXPECT_EQ("", service_->GetUrlForEntry(entry_id));
}
TEST_F(DomDistillerServiceTest, TestGetUrlForMultiPageEntry) {
FakeDistiller* distiller = new FakeDistiller(false);
EXPECT_CALL(*distiller_factory_, CreateDistillerImpl())
.WillOnce(Return(distiller));
const int kPageCount = 8;
std::string base_url("http://www.example.com/p");
GURL pages_url[kPageCount];
for (int page_num = 0; page_num < kPageCount; ++page_num) {
pages_url[page_num] = GURL(base_url + base::NumberToString(page_num));
}
MockArticleAvailableCallback article_cb;
EXPECT_CALL(article_cb, DistillationCompleted(true));
std::string entry_id = service_->AddToList(
pages_url[0], service_->CreateDefaultDistillerPage(gfx::Size()),
ArticleCallback(&article_cb));
ArticleEntry entry;
ASSERT_FALSE(distiller->GetArticleCallback().is_null());
EXPECT_EQ(pages_url[0], distiller->GetUrl());
// Create the article with pages to pass to the distiller.
std::unique_ptr<DistilledArticleProto> proto =
CreateArticleWithURL(pages_url[0].spec());
for (int page_num = 1; page_num < kPageCount; ++page_num) {
DistilledPageProto* distilled_page = proto->add_pages();
distilled_page->set_url(pages_url[page_num].spec());
}
RunDistillerCallback(distiller, std::move(proto));
EXPECT_TRUE(store_->GetEntryByUrl(pages_url[0], &entry));
// Check if retrieved URL is same as given URL for the first page.
GURL retrieved_url(service_->GetUrlForEntry(entry_id));
EXPECT_EQ(pages_url[0], retrieved_url);
// Remove the article and check that no URL can be retrieved for the entry.
service_->RemoveEntry(entry_id);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(0u, store_->GetEntries().size());
EXPECT_EQ("", service_->GetUrlForEntry(entry_id));
}
} // namespace test
} // namespace dom_distiller
......@@ -49,67 +49,10 @@ bool DomDistillerStore::GetEntryByUrl(const GURL& url, ArticleEntry* entry) {
return model_.GetEntryByUrl(url, entry);
}
bool DomDistillerStore::AddEntry(const ArticleEntry& entry) {
return ChangeEntry(entry, SyncChange::ACTION_ADD);
}
bool DomDistillerStore::UpdateEntry(const ArticleEntry& entry) {
return ChangeEntry(entry, SyncChange::ACTION_UPDATE);
}
bool DomDistillerStore::RemoveEntry(const ArticleEntry& entry) {
return ChangeEntry(entry, SyncChange::ACTION_DELETE);
}
bool DomDistillerStore::ChangeEntry(const ArticleEntry& entry,
SyncChange::SyncChangeType changeType) {
if (!database_loaded_) {
return false;
}
bool hasEntry = model_.GetEntryById(entry.entry_id(), nullptr);
if (hasEntry) {
if (changeType == SyncChange::ACTION_ADD) {
DVLOG(1) << "Already have entry with id " << entry.entry_id() << ".";
return false;
}
} else if (changeType != SyncChange::ACTION_ADD) {
DVLOG(1) << "No entry with id " << entry.entry_id() << " found.";
return false;
}
SyncChangeList changes_to_apply;
changes_to_apply.push_back(
SyncChange(FROM_HERE, changeType, CreateLocalData(entry)));
SyncChangeList changes_applied;
SyncChangeList changes_missing;
ApplyChangesToModel(changes_to_apply, &changes_applied, &changes_missing);
if (changeType == SyncChange::ACTION_UPDATE && changes_applied.size() != 1) {
DVLOG(1) << "Failed to update entry with id " << entry.entry_id() << ".";
return false;
}
DCHECK_EQ(size_t(0), changes_missing.size());
DCHECK_EQ(size_t(1), changes_applied.size());
ApplyChangesToDatabase(changes_applied);
return true;
}
std::vector<ArticleEntry> DomDistillerStore::GetEntries() const {
return model_.GetEntries();
}
void DomDistillerStore::ApplyChangesToModel(const SyncChangeList& changes,
SyncChangeList* changes_applied,
SyncChangeList* changes_missing) {
model_.ApplyChangesToModel(changes, changes_applied, changes_missing);
}
void DomDistillerStore::OnDatabaseInit(
leveldb_proto::Enums::InitStatus status) {
if (status != leveldb_proto::Enums::InitStatus::kOK) {
......@@ -188,7 +131,8 @@ void DomDistillerStore::MergeDataWithModel(const SyncDataList& data,
SyncChangeList changes_to_apply;
model_.CalculateChangesForMerge(data, &changes_to_apply, changes_missing);
ApplyChangesToModel(changes_to_apply, changes_applied, changes_missing);
model_.ApplyChangesToModel(changes_to_apply, changes_applied,
changes_missing);
}
} // namespace dom_distiller
......@@ -24,11 +24,6 @@ class DomDistillerStoreInterface {
public:
virtual ~DomDistillerStoreInterface() {}
virtual bool AddEntry(const ArticleEntry& entry) = 0;
// Returns false if |entry| is not present or |entry| was not updated.
virtual bool UpdateEntry(const ArticleEntry& entry) = 0;
virtual bool RemoveEntry(const ArticleEntry& entry) = 0;
// Lookup an ArticleEntry by ID or URL. Returns whether a corresponding entry
// was found. On success, if |entry| is not null, it will contain the entry.
virtual bool GetEntryById(const std::string& entry_id,
......@@ -70,10 +65,6 @@ class DomDistillerStore : public DomDistillerStoreInterface {
~DomDistillerStore() override;
bool AddEntry(const ArticleEntry& entry) override;
bool UpdateEntry(const ArticleEntry& entry) override;
bool RemoveEntry(const ArticleEntry& entry) override;
bool GetEntryById(const std::string& entry_id, ArticleEntry* entry) override;
bool GetEntryByUrl(const GURL& url, ArticleEntry* entry) override;
std::vector<ArticleEntry> GetEntries() const override;
......@@ -83,22 +74,12 @@ class DomDistillerStore : public DomDistillerStoreInterface {
void OnDatabaseLoad(bool success, std::unique_ptr<EntryVector> entries);
void OnDatabaseSave(bool success);
// Returns true if the change is successfully applied.
bool ChangeEntry(const ArticleEntry& entry,
syncer::SyncChange::SyncChangeType changeType);
void MergeDataWithModel(const syncer::SyncDataList& data,
syncer::SyncChangeList* changes_applied,
syncer::SyncChangeList* changes_missing);
bool ApplyChangesToDatabase(const syncer::SyncChangeList& change_list);
// Applies the changes to |model_|. If the model returns an error, disables
// syncing and database changes and returns false.
void ApplyChangesToModel(const syncer::SyncChangeList& change_list,
syncer::SyncChangeList* changes_applied,
syncer::SyncChangeList* changes_missing);
std::unique_ptr<leveldb_proto::ProtoDatabase<ArticleEntry>> database_;
bool database_loaded_;
......
......@@ -173,59 +173,4 @@ TEST_F(DomDistillerStoreTest, TestDatabaseLoadMerge) {
EXPECT_TRUE(AreEntryMapsEqual(db_model_, expected_model));
}
TEST_F(DomDistillerStoreTest, TestAddAndRemoveEntry) {
CreateStore();
fake_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
fake_db_->LoadCallback(true);
EXPECT_TRUE(store_->GetEntries().empty());
EXPECT_TRUE(db_model_.empty());
store_->AddEntry(GetSampleEntry(0));
EntryMap expected_model;
AddEntry(GetSampleEntry(0), &expected_model);
EXPECT_TRUE(AreEntriesEqual(store_->GetEntries(), expected_model));
EXPECT_TRUE(AreEntryMapsEqual(db_model_, expected_model));
store_->RemoveEntry(GetSampleEntry(0));
expected_model.clear();
EXPECT_TRUE(AreEntriesEqual(store_->GetEntries(), expected_model));
EXPECT_TRUE(AreEntryMapsEqual(db_model_, expected_model));
}
TEST_F(DomDistillerStoreTest, TestAddAndUpdateEntry) {
CreateStore();
fake_db_->InitStatusCallback(leveldb_proto::Enums::InitStatus::kOK);
fake_db_->LoadCallback(true);
EXPECT_TRUE(store_->GetEntries().empty());
EXPECT_TRUE(db_model_.empty());
store_->AddEntry(GetSampleEntry(0));
EntryMap expected_model;
AddEntry(GetSampleEntry(0), &expected_model);
EXPECT_TRUE(AreEntriesEqual(store_->GetEntries(), expected_model));
EXPECT_TRUE(AreEntryMapsEqual(db_model_, expected_model));
EXPECT_FALSE(store_->UpdateEntry(GetSampleEntry(0)));
ArticleEntry updated_entry(GetSampleEntry(0));
updated_entry.set_title("updated title.");
EXPECT_TRUE(store_->UpdateEntry(updated_entry));
expected_model.clear();
AddEntry(updated_entry, &expected_model);
EXPECT_TRUE(AreEntriesEqual(store_->GetEntries(), expected_model));
EXPECT_TRUE(AreEntryMapsEqual(db_model_, expected_model));
store_->RemoveEntry(updated_entry);
EXPECT_FALSE(store_->UpdateEntry(updated_entry));
EXPECT_FALSE(store_->UpdateEntry(GetSampleEntry(0)));
}
} // namespace dom_distiller
......@@ -41,19 +41,8 @@ class TestDomDistillerService : public DomDistillerServiceInterface {
TestDomDistillerService() {}
~TestDomDistillerService() override {}
MOCK_METHOD3(AddToList,
const std::string(const GURL&,
DistillerPage*,
const ArticleAvailableCallback&));
const std::string AddToList(
const GURL& url,
std::unique_ptr<DistillerPage> distiller_page,
const ArticleAvailableCallback& article_cb) override {
return AddToList(url, distiller_page.get(), article_cb);
}
MOCK_METHOD1(HasEntry, bool(const std::string&));
MOCK_METHOD1(GetUrlForEntry, std::string(const std::string&));
MOCK_CONST_METHOD0(GetEntries, std::vector<ArticleEntry>());
MOCK_METHOD0(ViewUrlImpl, ViewerHandle*());
std::unique_ptr<ViewerHandle> ViewUrl(
ViewRequestDelegate*,
......@@ -68,10 +57,6 @@ class TestDomDistillerService : public DomDistillerServiceInterface {
const std::string&) override {
return std::unique_ptr<ViewerHandle>(ViewEntryImpl());
}
MOCK_METHOD0(RemoveEntryImpl, ArticleEntry*());
std::unique_ptr<ArticleEntry> RemoveEntry(const std::string&) override {
return std::unique_ptr<ArticleEntry>(RemoveEntryImpl());
}
std::unique_ptr<DistillerPage> CreateDefaultDistillerPage(
const gfx::Size& render_view_size) override {
return std::unique_ptr<DistillerPage>();
......
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