Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: components/offline_pages/offline_page_model.cc

Issue 1475883003: [Offline pages] Fixing crashes caused by access to offline pages marked for deletion (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/offline_pages/offline_page_model.h" 5 #include "components/offline_pages/offline_page_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 bookmark_ids.push_back(id_page_pair.first); 246 bookmark_ids.push_back(id_page_pair.first);
247 DeletePagesByBookmarkId( 247 DeletePagesByBookmarkId(
248 bookmark_ids, 248 bookmark_ids,
249 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, 249 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll,
250 weak_ptr_factory_.GetWeakPtr(), 250 weak_ptr_factory_.GetWeakPtr(),
251 callback)); 251 callback));
252 } 252 }
253 253
254 bool OfflinePageModel::HasOfflinePages() const { 254 bool OfflinePageModel::HasOfflinePages() const {
255 DCHECK(is_loaded_); 255 DCHECK(is_loaded_);
256 return !offline_pages_.empty(); 256 // Check that at least one page is not marked for deletion. Because we have
257 // pages marked for deletion, we cannot simply invert result of |empty()|.
258 for (const auto& iter : offline_pages_) {
Michael Courage 2015/11/25 19:00:58 optional nit: seems like there's a pretty even spl
fgorski 2015/11/25 19:27:26 Done.
259 if (!iter.second.IsMarkedForDeletion())
260 return true;
261 }
262 return false;
257 } 263 }
258 264
259 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const { 265 const std::vector<OfflinePageItem> OfflinePageModel::GetAllPages() const {
260 DCHECK(is_loaded_); 266 DCHECK(is_loaded_);
261 std::vector<OfflinePageItem> offline_pages; 267 std::vector<OfflinePageItem> offline_pages;
262 for (const auto& id_page_pair : offline_pages_) { 268 for (const auto& id_page_pair : offline_pages_) {
263 if (id_page_pair.second.IsMarkedForDeletion()) 269 if (id_page_pair.second.IsMarkedForDeletion())
264 continue; 270 continue;
265 offline_pages.push_back(id_page_pair.second); 271 offline_pages.push_back(id_page_pair.second);
266 } 272 }
267 return offline_pages; 273 return offline_pages;
268 } 274 }
269 275
270 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const { 276 const std::vector<OfflinePageItem> OfflinePageModel::GetPagesToCleanUp() const {
271 DCHECK(is_loaded_); 277 DCHECK(is_loaded_);
272 std::vector<OfflinePageItem> offline_pages; 278 std::vector<OfflinePageItem> offline_pages;
273 base::Time now = base::Time::Now(); 279 base::Time now = base::Time::Now();
274 for (const auto& id_page_pair : offline_pages_) { 280 for (const auto& id_page_pair : offline_pages_) {
275 if (!id_page_pair.second.IsMarkedForDeletion() && 281 if (!id_page_pair.second.IsMarkedForDeletion() &&
276 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { 282 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) {
277 offline_pages.push_back(id_page_pair.second); 283 offline_pages.push_back(id_page_pair.second);
278 } 284 }
279 } 285 }
280 return offline_pages; 286 return offline_pages;
281 } 287 }
282 288
283 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( 289 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId(
284 int64 bookmark_id) const { 290 int64 bookmark_id) const {
285 const auto iter = offline_pages_.find(bookmark_id); 291 const auto iter = offline_pages_.find(bookmark_id);
286 return iter != offline_pages_.end() ? &(iter->second) : nullptr; 292 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion()
293 ? &(iter->second)
294 : nullptr;
287 } 295 }
288 296
289 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( 297 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL(
290 const GURL& offline_url) const { 298 const GURL& offline_url) const {
291 for (auto iter = offline_pages_.begin(); 299 for (auto iter = offline_pages_.begin();
292 iter != offline_pages_.end(); 300 iter != offline_pages_.end();
293 ++iter) { 301 ++iter) {
294 if (iter->second.GetOfflineURL() == offline_url) 302 if (iter->second.GetOfflineURL() == offline_url &&
303 !iter->second.IsMarkedForDeletion()) {
295 return &(iter->second); 304 return &(iter->second);
305 }
296 } 306 }
297 return nullptr; 307 return nullptr;
298 } 308 }
299 309
300 const OfflinePageItem* OfflinePageModel::GetPageByOnlineURL( 310 const OfflinePageItem* OfflinePageModel::GetPageByOnlineURL(
301 const GURL& online_url) const { 311 const GURL& online_url) const {
302 for (auto iter = offline_pages_.begin(); iter != offline_pages_.end(); 312 for (auto iter = offline_pages_.begin(); iter != offline_pages_.end();
303 ++iter) { 313 ++iter) {
304 if (iter->second.url == online_url) 314 if (iter->second.url == online_url && !iter->second.IsMarkedForDeletion()) {
305 return &(iter->second); 315 return &(iter->second);
316 }
306 } 317 }
307 return nullptr; 318 return nullptr;
308 } 319 }
309 320
310 void OfflinePageModel::CheckForExternalFileDeletion() { 321 void OfflinePageModel::CheckForExternalFileDeletion() {
311 DCHECK(is_loaded_); 322 DCHECK(is_loaded_);
312 323
313 std::vector<std::pair<int64, base::FilePath>> id_path_pairs; 324 std::vector<std::pair<int64, base::FilePath>> id_path_pairs;
314 for (const auto& id_page_pair : offline_pages_) { 325 for (const auto& id_page_pair : offline_pages_) {
315 id_path_pairs.push_back( 326 id_path_pairs.push_back(
(...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 } 700 }
690 701
691 void OfflinePageModel::CacheLoadedData( 702 void OfflinePageModel::CacheLoadedData(
692 const std::vector<OfflinePageItem>& offline_pages) { 703 const std::vector<OfflinePageItem>& offline_pages) {
693 offline_pages_.clear(); 704 offline_pages_.clear();
694 for (const auto& offline_page : offline_pages) 705 for (const auto& offline_page : offline_pages)
695 offline_pages_[offline_page.bookmark_id] = offline_page; 706 offline_pages_[offline_page.bookmark_id] = offline_page;
696 } 707 }
697 708
698 } // namespace offline_pages 709 } // namespace offline_pages
OLDNEW
« no previous file with comments | « no previous file | components/offline_pages/offline_page_model_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698