Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
[Offline pages] Fixing crashes caused by access to offline pages mark…
Browse files Browse the repository at this point in the history
…ed for deletion

This patch fixes the problems caused by:
* HasOfflinePages
* GetPageByBookmarkId
* GetPageByOfflineURL
* GetPageByOnlineURL
All of which allow for access to the offline page that is marked for
deletion, which means there is no valid bookmark corresponding to them.
This leads to crashes in the bookmark UI, as well as exposing
functionality that should not work.

BUG=560424

Review URL: https://codereview.chromium.org/1475883003

Cr-Commit-Position: refs/heads/master@{#361746}
(cherry picked from commit 1a6c2e1)

Review URL: https://codereview.chromium.org/1487913002 .

Cr-Commit-Position: refs/branch-heads/2564@{#166}
Cr-Branched-From: 1283eca-refs/heads/master@{#359700}
  • Loading branch information
fgorski committed Nov 30, 2015
1 parent 8252cb1 commit 130d38b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
18 changes: 14 additions & 4 deletions components/offline_pages/offline_page_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,13 @@ void OfflinePageModel::ClearAll(const base::Closure& callback) {

bool OfflinePageModel::HasOfflinePages() const {
DCHECK(is_loaded_);
return !offline_pages_.empty();
// Check that at least one page is not marked for deletion. Because we have
// pages marked for deletion, we cannot simply invert result of |empty()|.
for (const auto& id_page_pair : offline_pages_) {
if (!id_page_pair.second.IsMarkedForDeletion())
return true;
}
return false;
}

const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const {
Expand Down Expand Up @@ -283,16 +289,20 @@ const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const {
const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId(
int64 bookmark_id) const {
const auto iter = offline_pages_.find(bookmark_id);
return iter != offline_pages_.end() ? &(iter->second) : nullptr;
return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion()
? &(iter->second)
: nullptr;
}

const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL(
const GURL& offline_url) const {
for (auto iter = offline_pages_.begin();
iter != offline_pages_.end();
++iter) {
if (iter->second.GetOfflineURL() == offline_url)
if (iter->second.GetOfflineURL() == offline_url &&
!iter->second.IsMarkedForDeletion()) {
return &(iter->second);
}
}
return nullptr;
}
Expand All @@ -301,7 +311,7 @@ const OfflinePageItem* OfflinePageModel::GetPageByOnlineURL(
const GURL& online_url) const {
for (auto iter = offline_pages_.begin(); iter != offline_pages_.end();
++iter) {
if (iter->second.url == online_url)
if (iter->second.url == online_url && !iter->second.IsMarkedForDeletion())
return &(iter->second);
}
return nullptr;
Expand Down
7 changes: 7 additions & 0 deletions components/offline_pages/offline_page_model_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ TEST_F(OfflinePageModelTest, MarkPageForDeletion) {
base::Bind(&OfflinePageModelTest::OnSavePageDone, AsWeakPtr()));
PumpLoop();

GURL offline_url = model()->GetAllPages().begin()->GetOfflineURL();

// Delete the page with undo tiggerred.
model()->MarkPageForDeletion(
kTestPageBookmarkId1,
Expand All @@ -610,6 +612,11 @@ TEST_F(OfflinePageModelTest, MarkPageForDeletion) {
const std::vector<OfflinePageItem>& offline_pages = model()->GetAllPages();
EXPECT_EQ(0UL, offline_pages.size());

EXPECT_FALSE(model()->HasOfflinePages());
EXPECT_EQ(nullptr, model()->GetPageByOnlineURL(kTestUrl));
EXPECT_EQ(nullptr, model()->GetPageByBookmarkId(kTestPageBookmarkId1));
EXPECT_EQ(nullptr, model()->GetPageByOfflineURL(offline_url));

// Undo the deletion.
model()->UndoPageDeletion(kTestPageBookmarkId1);
base::RunLoop().RunUntilIdle();
Expand Down

0 comments on commit 130d38b

Please sign in to comment.