Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "components/bookmarks/browser/bookmark_model.h" | 18 #include "components/bookmarks/browser/bookmark_model.h" |
| 19 #include "components/bookmarks/browser/bookmark_node.h" | 19 #include "components/bookmarks/browser/bookmark_node.h" |
| 20 #include "components/offline_pages/offline_page_item.h" | 20 #include "components/offline_pages/offline_page_item.h" |
| 21 #include "components/offline_pages/proto/offline_pages.pb.h" | |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 24 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
| 24 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; | 25 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
| 25 | 26 |
| 26 namespace offline_pages { | 27 namespace offline_pages { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 // This enum is used in an UMA histogram. Hence the entries here shouldn't | 31 // This enum is used in an UMA histogram. Hence the entries here shouldn't |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 142 | 143 |
| 143 void OfflinePageModel::AddObserver(Observer* observer) { | 144 void OfflinePageModel::AddObserver(Observer* observer) { |
| 144 observers_.AddObserver(observer); | 145 observers_.AddObserver(observer); |
| 145 } | 146 } |
| 146 | 147 |
| 147 void OfflinePageModel::RemoveObserver(Observer* observer) { | 148 void OfflinePageModel::RemoveObserver(Observer* observer) { |
| 148 observers_.RemoveObserver(observer); | 149 observers_.RemoveObserver(observer); |
| 149 } | 150 } |
| 150 | 151 |
| 151 void OfflinePageModel::SavePage(const GURL& url, | 152 void OfflinePageModel::SavePage(const GURL& url, |
| 152 int64_t bookmark_id, | 153 int64_t offline_id, |
| 154 const ClientId& client_id, | |
| 153 scoped_ptr<OfflinePageArchiver> archiver, | 155 scoped_ptr<OfflinePageArchiver> archiver, |
| 154 const SavePageCallback& callback) { | 156 const SavePageCallback& callback) { |
| 155 DCHECK(is_loaded_); | 157 DCHECK(is_loaded_); |
| 156 | 158 |
| 157 // Skip saving the page that is not intended to be saved, like local file | 159 // Skip saving the page that is not intended to be saved, like local file |
| 158 // page. | 160 // page. |
| 159 if (!CanSavePage(url)) { | 161 if (!CanSavePage(url)) { |
| 160 InformSavePageDone(callback, SavePageResult::SKIPPED); | 162 InformSavePageDone(callback, SavePageResult::SKIPPED); |
| 161 return; | 163 return; |
| 162 } | 164 } |
| 163 | 165 |
| 164 DCHECK(archiver.get()); | 166 DCHECK(archiver.get()); |
| 165 archiver->CreateArchive(archives_dir_, | 167 archiver->CreateArchive(archives_dir_, |
| 166 base::Bind(&OfflinePageModel::OnCreateArchiveDone, | 168 base::Bind(&OfflinePageModel::OnCreateArchiveDone, |
| 167 weak_ptr_factory_.GetWeakPtr(), url, | 169 weak_ptr_factory_.GetWeakPtr(), url, |
| 168 bookmark_id, base::Time::Now(), callback)); | 170 offline_id, client_id, base::Time::Now(), |
| 171 callback)); | |
| 169 pending_archivers_.push_back(std::move(archiver)); | 172 pending_archivers_.push_back(std::move(archiver)); |
| 170 } | 173 } |
| 171 | 174 |
| 172 void OfflinePageModel::MarkPageAccessed(int64_t bookmark_id) { | 175 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) { |
| 173 DCHECK(is_loaded_); | 176 DCHECK(is_loaded_); |
| 174 auto iter = offline_pages_.find(bookmark_id); | 177 auto iter = offline_pages_.find(offline_id); |
| 175 if (iter == offline_pages_.end()) | 178 if (iter == offline_pages_.end()) |
| 176 return; | 179 return; |
| 177 | 180 |
| 178 // MarkPageAccessed should not be called for a page that is being marked for | 181 // MarkPageAccessed should not be called for a page that is being marked for |
| 179 // deletion. | 182 // deletion. |
| 180 DCHECK(!iter->second.IsMarkedForDeletion()); | 183 DCHECK(!iter->second.IsMarkedForDeletion()); |
| 181 | 184 |
| 182 // Make a copy of the cached item and update it. The cached item should only | 185 // Make a copy of the cached item and update it. The cached item should only |
| 183 // be updated upon the successful store operation. | 186 // be updated upon the successful store operation. |
| 184 OfflinePageItem offline_page_item = iter->second; | 187 OfflinePageItem offline_page_item = iter->second; |
| 185 offline_page_item.last_access_time = base::Time::Now(); | 188 offline_page_item.last_access_time = base::Time::Now(); |
| 186 offline_page_item.access_count++; | 189 offline_page_item.access_count++; |
| 187 store_->AddOrUpdateOfflinePage( | 190 store_->AddOrUpdateOfflinePage( |
| 188 offline_page_item, | 191 offline_page_item, |
| 189 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, | 192 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, |
| 190 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 193 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
| 191 } | 194 } |
| 192 | 195 |
| 193 void OfflinePageModel::MarkPageForDeletion(int64_t bookmark_id, | 196 void OfflinePageModel::MarkPageForDeletion(int64_t offline_id, |
| 194 const DeletePageCallback& callback) { | 197 const DeletePageCallback& callback) { |
| 195 DCHECK(is_loaded_); | 198 DCHECK(is_loaded_); |
| 196 auto iter = offline_pages_.find(bookmark_id); | 199 auto iter = offline_pages_.find(offline_id); |
| 197 if (iter == offline_pages_.end()) { | 200 if (iter == offline_pages_.end()) { |
| 198 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | 201 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); |
| 199 return; | 202 return; |
| 200 } | 203 } |
| 201 | 204 |
| 202 // Make a copy of the cached item and update it. The cached item should only | 205 // Make a copy of the cached item and update it. The cached item should only |
| 203 // be updated upon the successful store operation. | 206 // be updated upon the successful store operation. |
| 204 OfflinePageItem offline_page_item = iter->second; | 207 OfflinePageItem offline_page_item = iter->second; |
| 205 offline_page_item.MarkForDeletion(); | 208 offline_page_item.MarkForDeletion(); |
| 206 store_->AddOrUpdateOfflinePage( | 209 store_->AddOrUpdateOfflinePage( |
| 207 offline_page_item, | 210 offline_page_item, |
| 208 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone, | 211 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone, |
| 209 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback)); | 212 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback)); |
| 210 } | 213 } |
| 211 | 214 |
| 212 void OfflinePageModel::DeletePageByBookmarkId( | 215 void OfflinePageModel::DeletePageByOfflineId( |
| 213 int64_t bookmark_id, | 216 int64_t offline_id, |
| 214 const DeletePageCallback& callback) { | 217 const DeletePageCallback& callback) { |
| 215 DCHECK(is_loaded_); | 218 DCHECK(is_loaded_); |
| 216 std::vector<int64_t> bookmark_ids_to_delete; | 219 std::vector<int64_t> offline_ids_to_delete; |
| 217 bookmark_ids_to_delete.push_back(bookmark_id); | 220 offline_ids_to_delete.push_back(offline_id); |
| 218 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); | 221 DeletePagesByOfflineId(offline_ids_to_delete, callback); |
| 219 } | 222 } |
| 220 | 223 |
| 221 void OfflinePageModel::DeletePagesByBookmarkId( | 224 void OfflinePageModel::DeletePagesByOfflineId( |
| 222 const std::vector<int64_t>& bookmark_ids, | 225 const std::vector<int64_t>& offline_ids, |
| 223 const DeletePageCallback& callback) { | 226 const DeletePageCallback& callback) { |
| 224 DCHECK(is_loaded_); | 227 DCHECK(is_loaded_); |
| 225 | 228 |
| 226 std::vector<base::FilePath> paths_to_delete; | 229 std::vector<base::FilePath> paths_to_delete; |
| 227 for (const auto& bookmark_id : bookmark_ids) { | 230 for (const auto& offline_id : offline_ids) { |
| 228 auto iter = offline_pages_.find(bookmark_id); | 231 auto iter = offline_pages_.find(offline_id); |
| 229 if (iter != offline_pages_.end()) { | 232 if (iter != offline_pages_.end()) { |
| 230 paths_to_delete.push_back(iter->second.file_path); | 233 paths_to_delete.push_back(iter->second.file_path); |
| 231 } | 234 } |
| 232 } | 235 } |
| 233 | 236 |
| 234 if (paths_to_delete.empty()) { | 237 if (paths_to_delete.empty()) { |
| 235 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); | 238 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); |
| 236 return; | 239 return; |
| 237 } | 240 } |
| 238 | 241 |
| 239 bool* success = new bool(false); | 242 bool* success = new bool(false); |
| 240 task_runner_->PostTaskAndReply( | 243 task_runner_->PostTaskAndReply( |
| 241 FROM_HERE, | 244 FROM_HERE, |
| 242 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), | 245 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), |
| 243 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 246 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
| 244 weak_ptr_factory_.GetWeakPtr(), | 247 weak_ptr_factory_.GetWeakPtr(), |
| 245 bookmark_ids, | 248 offline_ids, |
| 246 callback, | 249 callback, |
| 247 base::Owned(success))); | 250 base::Owned(success))); |
| 248 } | 251 } |
| 249 | 252 |
| 250 void OfflinePageModel::ClearAll(const base::Closure& callback) { | 253 void OfflinePageModel::ClearAll(const base::Closure& callback) { |
| 251 DCHECK(is_loaded_); | 254 DCHECK(is_loaded_); |
| 252 | 255 |
| 253 std::vector<int64_t> bookmark_ids; | 256 std::vector<int64_t> offline_ids; |
| 254 for (const auto& id_page_pair : offline_pages_) | 257 for (const auto& id_page_pair : offline_pages_) |
| 255 bookmark_ids.push_back(id_page_pair.first); | 258 offline_ids.push_back(id_page_pair.first); |
| 256 DeletePagesByBookmarkId( | 259 DeletePagesByOfflineId( |
| 257 bookmark_ids, | 260 offline_ids, |
| 258 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | 261 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, |
| 259 weak_ptr_factory_.GetWeakPtr(), | 262 weak_ptr_factory_.GetWeakPtr(), |
| 260 callback)); | 263 callback)); |
| 261 } | 264 } |
| 262 | 265 |
| 263 bool OfflinePageModel::HasOfflinePages() const { | 266 bool OfflinePageModel::HasOfflinePages() const { |
| 264 DCHECK(is_loaded_); | 267 DCHECK(is_loaded_); |
| 265 // Check that at least one page is not marked for deletion. Because we have | 268 // Check that at least one page is not marked for deletion. Because we have |
| 266 // pages marked for deletion, we cannot simply invert result of |empty()|. | 269 // pages marked for deletion, we cannot simply invert result of |empty()|. |
| 267 for (const auto& id_page_pair : offline_pages_) { | 270 for (const auto& id_page_pair : offline_pages_) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 288 base::Time now = base::Time::Now(); | 291 base::Time now = base::Time::Now(); |
| 289 for (const auto& id_page_pair : offline_pages_) { | 292 for (const auto& id_page_pair : offline_pages_) { |
| 290 if (!id_page_pair.second.IsMarkedForDeletion() && | 293 if (!id_page_pair.second.IsMarkedForDeletion() && |
| 291 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { | 294 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { |
| 292 offline_pages.push_back(id_page_pair.second); | 295 offline_pages.push_back(id_page_pair.second); |
| 293 } | 296 } |
| 294 } | 297 } |
| 295 return offline_pages; | 298 return offline_pages; |
| 296 } | 299 } |
| 297 | 300 |
| 298 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( | 301 const OfflinePageItem* OfflinePageModel::GetPageByOfflineId( |
| 299 int64_t bookmark_id) const { | 302 int64_t offline_id) const { |
| 300 const auto iter = offline_pages_.find(bookmark_id); | 303 const auto iter = offline_pages_.find(offline_id); |
| 301 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() | 304 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() |
| 302 ? &(iter->second) | 305 ? &(iter->second) |
| 303 : nullptr; | 306 : nullptr; |
| 304 } | 307 } |
| 305 | 308 |
| 306 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( | 309 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( |
| 307 const GURL& offline_url) const { | 310 const GURL& offline_url) const { |
| 308 for (auto iter = offline_pages_.begin(); | 311 for (auto iter = offline_pages_.begin(); |
| 309 iter != offline_pages_.end(); | 312 iter != offline_pages_.end(); |
| 310 ++iter) { | 313 ++iter) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, | 346 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, |
| 344 weak_ptr_factory_.GetWeakPtr(), | 347 weak_ptr_factory_.GetWeakPtr(), |
| 345 base::Owned(ids_of_pages_missing_archive_file))); | 348 base::Owned(ids_of_pages_missing_archive_file))); |
| 346 } | 349 } |
| 347 | 350 |
| 348 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 351 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { |
| 349 return store_.get(); | 352 return store_.get(); |
| 350 } | 353 } |
| 351 | 354 |
| 352 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 355 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, |
| 353 int64_t bookmark_id, | 356 int64_t offline_id, |
| 357 const ClientId& client_id, | |
| 354 const base::Time& start_time, | 358 const base::Time& start_time, |
| 355 const SavePageCallback& callback, | 359 const SavePageCallback& callback, |
| 356 OfflinePageArchiver* archiver, | 360 OfflinePageArchiver* archiver, |
| 357 ArchiverResult archiver_result, | 361 ArchiverResult archiver_result, |
| 358 const GURL& url, | 362 const GURL& url, |
| 359 const base::FilePath& file_path, | 363 const base::FilePath& file_path, |
| 360 int64_t file_size) { | 364 int64_t file_size) { |
| 361 if (requested_url != url) { | 365 if (requested_url != url) { |
| 362 DVLOG(1) << "Saved URL does not match requested URL."; | 366 DVLOG(1) << "Saved URL does not match requested URL."; |
| 363 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 367 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 364 // deleted from here, once archiver has the right functionality. | 368 // deleted from here, once archiver has the right functionality. |
| 365 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED); | 369 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED); |
| 366 DeletePendingArchiver(archiver); | 370 DeletePendingArchiver(archiver); |
| 367 return; | 371 return; |
| 368 } | 372 } |
| 369 | 373 |
| 370 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 374 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 371 SavePageResult result = ToSavePageResult(archiver_result); | 375 SavePageResult result = ToSavePageResult(archiver_result); |
| 372 InformSavePageDone(callback, result); | 376 InformSavePageDone(callback, result); |
| 373 DeletePendingArchiver(archiver); | 377 DeletePendingArchiver(archiver); |
| 374 return; | 378 return; |
| 375 } | 379 } |
| 376 | 380 |
| 377 OfflinePageItem offline_page_item(url, bookmark_id, file_path, file_size, | 381 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 378 start_time); | 382 file_size, start_time); |
| 379 store_->AddOrUpdateOfflinePage( | 383 store_->AddOrUpdateOfflinePage( |
| 380 offline_page_item, | 384 offline_page_item, |
| 381 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, | 385 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, |
| 382 weak_ptr_factory_.GetWeakPtr(), archiver, callback, | 386 weak_ptr_factory_.GetWeakPtr(), archiver, callback, |
| 383 offline_page_item)); | 387 offline_page_item)); |
| 384 } | 388 } |
| 385 | 389 |
| 386 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 390 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, |
| 387 const SavePageCallback& callback, | 391 const SavePageCallback& callback, |
| 388 const OfflinePageItem& offline_page, | 392 const OfflinePageItem& offline_page, |
| 389 bool success) { | 393 bool success) { |
| 390 SavePageResult result; | 394 SavePageResult result; |
| 391 if (success) { | 395 if (success) { |
| 392 offline_pages_[offline_page.bookmark_id] = offline_page; | 396 offline_pages_[offline_page.offline_id] = offline_page; |
| 393 result = SavePageResult::SUCCESS; | 397 result = SavePageResult::SUCCESS; |
| 394 UMA_HISTOGRAM_TIMES( | 398 UMA_HISTOGRAM_TIMES( |
| 395 "OfflinePages.SavePageTime", | 399 "OfflinePages.SavePageTime", |
| 396 base::Time::Now() - offline_page.creation_time); | 400 base::Time::Now() - offline_page.creation_time); |
| 397 UMA_HISTOGRAM_MEMORY_KB( | 401 UMA_HISTOGRAM_MEMORY_KB( |
| 398 "OfflinePages.PageSize", offline_page.file_size / 1024); | 402 "OfflinePages.PageSize", offline_page.file_size / 1024); |
| 399 } else { | 403 } else { |
| 400 result = SavePageResult::STORE_FAILURE; | 404 result = SavePageResult::STORE_FAILURE; |
| 401 } | 405 } |
| 402 InformSavePageDone(callback, result); | 406 InformSavePageDone(callback, result); |
| 403 DeletePendingArchiver(archiver); | 407 DeletePendingArchiver(archiver); |
| 404 | 408 |
| 405 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 409 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 406 } | 410 } |
| 407 | 411 |
| 408 void OfflinePageModel::OnMarkPageAccesseDone( | 412 void OfflinePageModel::OnMarkPageAccesseDone( |
| 409 const OfflinePageItem& offline_page_item, bool success) { | 413 const OfflinePageItem& offline_page_item, bool success) { |
| 410 // Update the item in the cache only upon success. | 414 // Update the item in the cache only upon success. |
| 411 if (success) | 415 if (success) |
| 412 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | 416 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 413 | 417 |
| 414 // No need to fire OfflinePageModelChanged event since updating access info | 418 // No need to fire OfflinePageModelChanged event since updating access info |
| 415 // should not have any impact to the UI. | 419 // should not have any impact to the UI. |
| 416 } | 420 } |
| 417 | 421 |
| 418 void OfflinePageModel::OnMarkPageForDeletionDone( | 422 void OfflinePageModel::OnMarkPageForDeletionDone( |
| 419 const OfflinePageItem& offline_page_item, | 423 const OfflinePageItem& offline_page_item, |
| 420 const DeletePageCallback& callback, | 424 const DeletePageCallback& callback, |
| 421 bool success) { | 425 bool success) { |
| 422 // Update the item in the cache only upon success. | 426 // Update the item in the cache only upon success. |
| 423 if (success) | 427 if (success) |
| 424 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | 428 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 425 | 429 |
| 426 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS | 430 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS |
| 427 : DeletePageResult::STORE_FAILURE); | 431 : DeletePageResult::STORE_FAILURE); |
| 428 | 432 |
| 429 if (!success) | 433 if (!success) |
| 430 return; | 434 return; |
| 431 | 435 |
| 432 // Schedule to do the final deletion. | 436 // Schedule to do the final deletion. |
| 433 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 437 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 434 FROM_HERE, | 438 FROM_HERE, |
| 435 base::Bind(&OfflinePageModel::FinalizePageDeletion, | 439 base::Bind(&OfflinePageModel::FinalizePageDeletion, |
| 436 weak_ptr_factory_.GetWeakPtr()), | 440 weak_ptr_factory_.GetWeakPtr()), |
| 437 kFinalDeletionDelay); | 441 kFinalDeletionDelay); |
| 438 | 442 |
| 439 FOR_EACH_OBSERVER( | 443 FOR_EACH_OBSERVER( |
| 440 Observer, observers_, OfflinePageDeleted(offline_page_item.bookmark_id)); | 444 Observer, observers_, OfflinePageDeleted(offline_page_item.offline_id)); |
| 441 } | 445 } |
| 442 | 446 |
| 443 void OfflinePageModel::OnUndoOfflinePageDone( | 447 void OfflinePageModel::OnUndoOfflinePageDone( |
| 444 const OfflinePageItem& offline_page, bool success) { | 448 const OfflinePageItem& offline_page, bool success) { |
| 445 if (!success) | 449 if (!success) |
| 446 return; | 450 return; |
| 447 offline_pages_[offline_page.bookmark_id] = offline_page; | 451 offline_pages_[offline_page.offline_id] = offline_page; |
| 448 | 452 |
| 449 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 453 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 450 } | 454 } |
| 451 | 455 |
| 452 void OfflinePageModel::FinalizePageDeletion() { | 456 void OfflinePageModel::FinalizePageDeletion() { |
| 453 std::vector<int64_t> bookmark_ids_pending_deletion; | 457 std::vector<int64_t> offline_ids_pending_deletion; |
| 454 for (const auto& id_page_pair : offline_pages_) { | 458 for (const auto& id_page_pair : offline_pages_) { |
| 455 if (!id_page_pair.second.IsMarkedForDeletion()) | 459 if (!id_page_pair.second.IsMarkedForDeletion()) |
| 456 continue; | 460 continue; |
| 457 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); | 461 offline_ids_pending_deletion.push_back(id_page_pair.second.offline_id); |
| 458 } | 462 } |
| 459 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); | 463 DeletePagesByOfflineId(offline_ids_pending_deletion, DeletePageCallback()); |
| 460 } | 464 } |
| 461 | 465 |
| 462 void OfflinePageModel::UndoPageDeletion(int64_t bookmark_id) { | 466 void OfflinePageModel::UndoPageDeletion(int64_t offline_id) { |
| 463 auto iter = offline_pages_.find(bookmark_id); | 467 auto iter = offline_pages_.find(offline_id); |
| 464 if (iter == offline_pages_.end()) | 468 if (iter == offline_pages_.end()) |
| 465 return; | 469 return; |
| 466 | 470 |
| 467 // Make a copy of the cached item and update it. The cached item should only | 471 // Make a copy of the cached item and update it. The cached item should only |
| 468 // be updated upon the successful store operation. | 472 // be updated upon the successful store operation. |
| 469 OfflinePageItem offline_page_item = iter->second; | 473 OfflinePageItem offline_page_item = iter->second; |
| 470 if (!offline_page_item.IsMarkedForDeletion()) | 474 if (!offline_page_item.IsMarkedForDeletion()) |
| 471 return; | 475 return; |
| 472 | 476 |
| 473 // Clear the flag to bring it back. | 477 // Clear the flag to bring it back. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 506 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); | 510 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); |
| 507 } | 511 } |
| 508 | 512 |
| 509 void OfflinePageModel::BookmarkNodeChanged( | 513 void OfflinePageModel::BookmarkNodeChanged( |
| 510 bookmarks::BookmarkModel* model, | 514 bookmarks::BookmarkModel* model, |
| 511 const bookmarks::BookmarkNode* node) { | 515 const bookmarks::BookmarkNode* node) { |
| 512 // BookmarkNodeChanged could be triggered if title or URL gets changed. If | 516 // BookmarkNodeChanged could be triggered if title or URL gets changed. If |
| 513 // the latter, we need to invalidate the offline copy. | 517 // the latter, we need to invalidate the offline copy. |
| 514 auto iter = offline_pages_.find(node->id()); | 518 auto iter = offline_pages_.find(node->id()); |
| 515 if (iter != offline_pages_.end() && iter->second.url != node->url()) | 519 if (iter != offline_pages_.end() && iter->second.url != node->url()) |
| 516 DeletePageByBookmarkId(node->id(), DeletePageCallback()); | 520 DeletePageByOfflineId(node->id(), DeletePageCallback()); |
| 517 } | 521 } |
| 518 | 522 |
| 519 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() { | 523 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() { |
| 520 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, | 524 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, |
| 521 weak_ptr_factory_.GetWeakPtr())); | 525 weak_ptr_factory_.GetWeakPtr())); |
| 522 } | 526 } |
| 523 | 527 |
| 524 void OfflinePageModel::OnLoadDone( | 528 void OfflinePageModel::OnLoadDone( |
| 525 OfflinePageMetadataStore::LoadStatus load_status, | 529 OfflinePageMetadataStore::LoadStatus load_status, |
| 526 const std::vector<OfflinePageItem>& offline_pages) { | 530 const std::vector<OfflinePageItem>& offline_pages) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 554 static_cast<int>(SavePageResult::RESULT_COUNT)); | 558 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 555 callback.Run(result); | 559 callback.Run(result); |
| 556 } | 560 } |
| 557 | 561 |
| 558 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 562 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { |
| 559 pending_archivers_.erase(std::find( | 563 pending_archivers_.erase(std::find( |
| 560 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 564 pending_archivers_.begin(), pending_archivers_.end(), archiver)); |
| 561 } | 565 } |
| 562 | 566 |
| 563 void OfflinePageModel::OnDeleteArchiveFilesDone( | 567 void OfflinePageModel::OnDeleteArchiveFilesDone( |
| 564 const std::vector<int64_t>& bookmark_ids, | 568 const std::vector<int64_t>& offline_ids, |
| 565 const DeletePageCallback& callback, | 569 const DeletePageCallback& callback, |
| 566 const bool* success) { | 570 const bool* success) { |
| 567 DCHECK(success); | 571 DCHECK(success); |
| 568 | 572 |
| 569 if (!*success) { | 573 if (!*success) { |
| 570 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); | 574 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); |
| 571 return; | 575 return; |
| 572 } | 576 } |
| 573 | 577 |
| 574 store_->RemoveOfflinePages( | 578 store_->RemoveOfflinePages( |
| 575 bookmark_ids, | 579 offline_ids, |
| 576 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 580 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
| 577 weak_ptr_factory_.GetWeakPtr(), bookmark_ids, callback)); | 581 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 578 } | 582 } |
| 579 | 583 |
| 580 void OfflinePageModel::OnRemoveOfflinePagesDone( | 584 void OfflinePageModel::OnRemoveOfflinePagesDone( |
| 581 const std::vector<int64_t>& bookmark_ids, | 585 const std::vector<int64_t>& offline_ids, |
| 582 const DeletePageCallback& callback, | 586 const DeletePageCallback& callback, |
| 583 bool success) { | 587 bool success) { |
| 584 // Delete the offline page from the in memory cache regardless of success in | 588 // Delete the offline page from the in memory cache regardless of success in |
| 585 // store. | 589 // store. |
| 586 base::Time now = base::Time::Now(); | 590 base::Time now = base::Time::Now(); |
| 587 int64_t total_size = 0; | 591 int64_t total_size = 0; |
| 588 for (int64_t bookmark_id : bookmark_ids) { | 592 for (int64_t offline_id : offline_ids) { |
| 589 auto iter = offline_pages_.find(bookmark_id); | 593 auto iter = offline_pages_.find(offline_id); |
| 590 if (iter == offline_pages_.end()) | 594 if (iter == offline_pages_.end()) |
| 591 continue; | 595 continue; |
| 592 total_size += iter->second.file_size; | 596 total_size += iter->second.file_size; |
| 593 UMA_HISTOGRAM_CUSTOM_COUNTS( | 597 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 594 "OfflinePages.PageLifetime", | 598 "OfflinePages.PageLifetime", |
| 595 (now - iter->second.creation_time).InMinutes(), | 599 (now - iter->second.creation_time).InMinutes(), |
| 596 1, | 600 1, |
| 597 base::TimeDelta::FromDays(365).InMinutes(), | 601 base::TimeDelta::FromDays(365).InMinutes(), |
| 598 100); | 602 100); |
| 599 UMA_HISTOGRAM_CUSTOM_COUNTS( | 603 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 610 base::TimeDelta::FromDays(365).InMinutes(), | 614 base::TimeDelta::FromDays(365).InMinutes(), |
| 611 100); | 615 100); |
| 612 UMA_HISTOGRAM_MEMORY_KB( | 616 UMA_HISTOGRAM_MEMORY_KB( |
| 613 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); | 617 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); |
| 614 UMA_HISTOGRAM_COUNTS( | 618 UMA_HISTOGRAM_COUNTS( |
| 615 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); | 619 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); |
| 616 // If the page is not marked for deletion at this point, the model has not | 620 // If the page is not marked for deletion at this point, the model has not |
| 617 // yet informed the observer that the offline page is deleted. | 621 // yet informed the observer that the offline page is deleted. |
| 618 if (!iter->second.IsMarkedForDeletion()) { | 622 if (!iter->second.IsMarkedForDeletion()) { |
| 619 FOR_EACH_OBSERVER(Observer, observers_, | 623 FOR_EACH_OBSERVER(Observer, observers_, |
| 620 OfflinePageDeleted(iter->second.bookmark_id)); | 624 OfflinePageDeleted(iter->second.offline_id)); |
| 621 } | 625 } |
| 622 offline_pages_.erase(iter); | 626 offline_pages_.erase(iter); |
| 623 } | 627 } |
| 624 if (bookmark_ids.size() > 1) { | 628 if (offline_ids.size() > 1) { |
| 625 UMA_HISTOGRAM_COUNTS( | 629 UMA_HISTOGRAM_COUNTS( |
| 626 "OfflinePages.BatchDelete.Count", bookmark_ids.size()); | 630 "OfflinePages.BatchDelete.Count", offline_ids.size()); |
| 627 UMA_HISTOGRAM_MEMORY_KB( | 631 UMA_HISTOGRAM_MEMORY_KB( |
| 628 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | 632 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); |
| 629 } | 633 } |
| 630 // Deleting multiple pages always succeeds when it gets to this point. | 634 // Deleting multiple pages always succeeds when it gets to this point. |
| 631 InformDeletePageDone( | 635 InformDeletePageDone( |
| 632 callback, | 636 callback, |
| 633 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 637 (success || offline_ids.size() > 1) ? DeletePageResult::SUCCESS |
| 634 : DeletePageResult::STORE_FAILURE); | 638 : DeletePageResult::STORE_FAILURE); |
|
fgorski
2016/02/12 21:43:41
nit: align
bburns
2016/02/20 01:14:18
Done.
| |
| 635 } | 639 } |
| 636 | 640 |
| 637 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 641 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
| 638 DeletePageResult result) { | 642 DeletePageResult result) { |
| 639 UMA_HISTOGRAM_ENUMERATION( | 643 UMA_HISTOGRAM_ENUMERATION( |
| 640 "OfflinePages.DeletePageResult", | 644 "OfflinePages.DeletePageResult", |
| 641 static_cast<int>(result), | 645 static_cast<int>(result), |
| 642 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 646 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 643 if (!callback.is_null()) | 647 if (!callback.is_null()) |
| 644 callback.Run(result); | 648 callback.Run(result); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 657 | 661 |
| 658 store_->RemoveOfflinePages( | 662 store_->RemoveOfflinePages( |
| 659 *ids_of_pages_missing_archive_file, | 663 *ids_of_pages_missing_archive_file, |
| 660 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 664 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
| 661 weak_ptr_factory_.GetWeakPtr(), | 665 weak_ptr_factory_.GetWeakPtr(), |
| 662 *ids_of_pages_missing_archive_file, | 666 *ids_of_pages_missing_archive_file, |
| 663 done_callback)); | 667 done_callback)); |
| 664 } | 668 } |
| 665 | 669 |
| 666 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 670 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( |
| 667 const std::vector<int64_t>& bookmark_ids, | 671 const std::vector<int64_t>& offline_ids, |
| 668 OfflinePageModel::DeletePageResult /* result */) { | 672 OfflinePageModel::DeletePageResult /* result */) { |
| 669 for (int64_t bookmark_id : bookmark_ids) { | 673 for (int64_t offline_id : offline_ids) { |
| 670 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 674 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(offline_id)); |
| 671 } | 675 } |
| 672 } | 676 } |
| 673 | 677 |
| 674 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | 678 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( |
| 675 const base::Closure& callback, | 679 const base::Closure& callback, |
| 676 DeletePageResult result) { | 680 DeletePageResult result) { |
| 677 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | 681 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, |
| 678 weak_ptr_factory_.GetWeakPtr(), | 682 weak_ptr_factory_.GetWeakPtr(), |
| 679 callback)); | 683 callback)); |
| 680 } | 684 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 704 CLEAR_ALL_STATUS_COUNT); | 708 CLEAR_ALL_STATUS_COUNT); |
| 705 | 709 |
| 706 CacheLoadedData(offline_pages); | 710 CacheLoadedData(offline_pages); |
| 707 callback.Run(); | 711 callback.Run(); |
| 708 } | 712 } |
| 709 | 713 |
| 710 void OfflinePageModel::CacheLoadedData( | 714 void OfflinePageModel::CacheLoadedData( |
| 711 const std::vector<OfflinePageItem>& offline_pages) { | 715 const std::vector<OfflinePageItem>& offline_pages) { |
| 712 offline_pages_.clear(); | 716 offline_pages_.clear(); |
| 713 for (const auto& offline_page : offline_pages) | 717 for (const auto& offline_page : offline_pages) |
| 714 offline_pages_[offline_page.bookmark_id] = offline_page; | 718 offline_pages_[offline_page.offline_id] = offline_page; |
| 715 } | 719 } |
| 716 | 720 |
| 717 } // namespace offline_pages | 721 } // namespace offline_pages |
| OLD | NEW |