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/strings/string_number_conversions.h" | |
| 16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "components/bookmarks/browser/bookmark_model.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
| 19 #include "components/bookmarks/browser/bookmark_node.h" | 20 #include "components/bookmarks/browser/bookmark_node.h" |
| 20 #include "components/offline_pages/offline_page_item.h" | 21 #include "components/offline_pages/offline_page_item.h" |
| 22 #include "components/offline_pages/proto/offline_pages.pb.h" | |
| 21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 22 | 24 |
| 23 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; | 25 using ArchiverResult = offline_pages::OfflinePageArchiver::ArchiverResult; |
| 24 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; | 26 using SavePageResult = offline_pages::OfflinePageModel::SavePageResult; |
| 25 | 27 |
| 26 namespace offline_pages { | 28 namespace offline_pages { |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // This enum is used in an UMA histogram. Hence the entries here shouldn't | 32 // 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 | 144 |
| 143 void OfflinePageModel::AddObserver(Observer* observer) { | 145 void OfflinePageModel::AddObserver(Observer* observer) { |
| 144 observers_.AddObserver(observer); | 146 observers_.AddObserver(observer); |
| 145 } | 147 } |
| 146 | 148 |
| 147 void OfflinePageModel::RemoveObserver(Observer* observer) { | 149 void OfflinePageModel::RemoveObserver(Observer* observer) { |
| 148 observers_.RemoveObserver(observer); | 150 observers_.RemoveObserver(observer); |
| 149 } | 151 } |
| 150 | 152 |
| 151 void OfflinePageModel::SavePage(const GURL& url, | 153 void OfflinePageModel::SavePage(const GURL& url, |
| 152 int64_t bookmark_id, | 154 int64_t offline_id, |
| 155 const ClientId& client_id, | |
| 153 scoped_ptr<OfflinePageArchiver> archiver, | 156 scoped_ptr<OfflinePageArchiver> archiver, |
| 154 const SavePageCallback& callback) { | 157 const SavePageCallback& callback) { |
| 155 DCHECK(is_loaded_); | 158 DCHECK(is_loaded_); |
| 156 | 159 |
| 157 // Skip saving the page that is not intended to be saved, like local file | 160 // Skip saving the page that is not intended to be saved, like local file |
| 158 // page. | 161 // page. |
| 159 if (!CanSavePage(url)) { | 162 if (!CanSavePage(url)) { |
| 160 InformSavePageDone(callback, SavePageResult::SKIPPED); | 163 InformSavePageDone(callback, SavePageResult::SKIPPED); |
| 161 return; | 164 return; |
| 162 } | 165 } |
| 163 | 166 |
| 164 DCHECK(archiver.get()); | 167 DCHECK(archiver.get()); |
| 165 archiver->CreateArchive(archives_dir_, | 168 archiver->CreateArchive( |
| 166 base::Bind(&OfflinePageModel::OnCreateArchiveDone, | 169 archives_dir_, base::Bind(&OfflinePageModel::OnCreateArchiveDone, |
| 167 weak_ptr_factory_.GetWeakPtr(), url, | 170 weak_ptr_factory_.GetWeakPtr(), url, offline_id, |
| 168 bookmark_id, base::Time::Now(), callback)); | 171 client_id, base::Time::Now(), 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, base::Bind(&DeleteArchiveFiles, paths_to_delete, success), |
| 242 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), | |
| 243 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, | 245 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, |
| 244 weak_ptr_factory_.GetWeakPtr(), | 246 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback, |
| 245 bookmark_ids, | |
| 246 callback, | |
| 247 base::Owned(success))); | 247 base::Owned(success))); |
| 248 } | 248 } |
| 249 | 249 |
| 250 void OfflinePageModel::ClearAll(const base::Closure& callback) { | 250 void OfflinePageModel::ClearAll(const base::Closure& callback) { |
| 251 DCHECK(is_loaded_); | 251 DCHECK(is_loaded_); |
| 252 | 252 |
| 253 std::vector<int64_t> bookmark_ids; | 253 std::vector<int64_t> offline_ids; |
| 254 for (const auto& id_page_pair : offline_pages_) | 254 for (const auto& id_page_pair : offline_pages_) |
| 255 bookmark_ids.push_back(id_page_pair.first); | 255 offline_ids.push_back(id_page_pair.first); |
| 256 DeletePagesByBookmarkId( | 256 DeletePagesByOfflineId( |
| 257 bookmark_ids, | 257 offline_ids, |
| 258 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, | 258 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, |
| 259 weak_ptr_factory_.GetWeakPtr(), | 259 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 260 callback)); | |
| 261 } | 260 } |
| 262 | 261 |
| 263 bool OfflinePageModel::HasOfflinePages() const { | 262 bool OfflinePageModel::HasOfflinePages() const { |
| 264 DCHECK(is_loaded_); | 263 DCHECK(is_loaded_); |
| 265 // Check that at least one page is not marked for deletion. Because we have | 264 // 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()|. | 265 // pages marked for deletion, we cannot simply invert result of |empty()|. |
| 267 for (const auto& id_page_pair : offline_pages_) { | 266 for (const auto& id_page_pair : offline_pages_) { |
| 268 if (!id_page_pair.second.IsMarkedForDeletion()) | 267 if (!id_page_pair.second.IsMarkedForDeletion()) |
| 269 return true; | 268 return true; |
| 270 } | 269 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 288 base::Time now = base::Time::Now(); | 287 base::Time now = base::Time::Now(); |
| 289 for (const auto& id_page_pair : offline_pages_) { | 288 for (const auto& id_page_pair : offline_pages_) { |
| 290 if (!id_page_pair.second.IsMarkedForDeletion() && | 289 if (!id_page_pair.second.IsMarkedForDeletion() && |
| 291 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { | 290 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { |
| 292 offline_pages.push_back(id_page_pair.second); | 291 offline_pages.push_back(id_page_pair.second); |
| 293 } | 292 } |
| 294 } | 293 } |
| 295 return offline_pages; | 294 return offline_pages; |
| 296 } | 295 } |
| 297 | 296 |
| 298 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( | 297 const std::vector<int64_t> OfflinePageModel::GetOfflineIdsForClientId( |
| 299 int64_t bookmark_id) const { | 298 const ClientId& cid) const { |
| 300 const auto iter = offline_pages_.find(bookmark_id); | 299 std::vector<int64_t> results; |
| 300 // TODO(bburns): actually use an index rather than linear scan. | |
| 301 const std::vector<OfflinePageItem> offline_pages = GetAllPages(); | |
| 302 for (size_t i = 0; i < offline_pages.size(); i++) { | |
| 303 if (offline_pages[i].client_id.name_space == cid.name_space && | |
| 304 offline_pages[i].client_id.id == cid.id) { | |
| 305 results.push_back(offline_pages[i].offline_id); | |
| 306 } | |
| 307 } | |
| 308 return results; | |
| 309 } | |
| 310 | |
| 311 const OfflinePageItem* OfflinePageModel::GetPageByOfflineId( | |
| 312 int64_t offline_id) const { | |
| 313 const auto iter = offline_pages_.find(offline_id); | |
| 301 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() | 314 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() |
| 302 ? &(iter->second) | 315 ? &(iter->second) |
| 303 : nullptr; | 316 : nullptr; |
| 304 } | 317 } |
| 305 | 318 |
| 306 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( | 319 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( |
| 307 const GURL& offline_url) const { | 320 const GURL& offline_url) const { |
| 308 for (auto iter = offline_pages_.begin(); | 321 for (auto iter = offline_pages_.begin(); |
| 309 iter != offline_pages_.end(); | 322 iter != offline_pages_.end(); |
| 310 ++iter) { | 323 ++iter) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, | 356 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, |
| 344 weak_ptr_factory_.GetWeakPtr(), | 357 weak_ptr_factory_.GetWeakPtr(), |
| 345 base::Owned(ids_of_pages_missing_archive_file))); | 358 base::Owned(ids_of_pages_missing_archive_file))); |
| 346 } | 359 } |
| 347 | 360 |
| 348 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { | 361 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { |
| 349 return store_.get(); | 362 return store_.get(); |
| 350 } | 363 } |
| 351 | 364 |
| 352 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, | 365 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, |
| 353 int64_t bookmark_id, | 366 int64_t offline_id, |
| 367 const ClientId& client_id, | |
| 354 const base::Time& start_time, | 368 const base::Time& start_time, |
| 355 const SavePageCallback& callback, | 369 const SavePageCallback& callback, |
| 356 OfflinePageArchiver* archiver, | 370 OfflinePageArchiver* archiver, |
| 357 ArchiverResult archiver_result, | 371 ArchiverResult archiver_result, |
| 358 const GURL& url, | 372 const GURL& url, |
| 359 const base::FilePath& file_path, | 373 const base::FilePath& file_path, |
| 360 int64_t file_size) { | 374 int64_t file_size) { |
| 361 if (requested_url != url) { | 375 if (requested_url != url) { |
| 362 DVLOG(1) << "Saved URL does not match requested URL."; | 376 DVLOG(1) << "Saved URL does not match requested URL."; |
| 363 // TODO(fgorski): We have created an archive for a wrong URL. It should be | 377 // TODO(fgorski): We have created an archive for a wrong URL. It should be |
| 364 // deleted from here, once archiver has the right functionality. | 378 // deleted from here, once archiver has the right functionality. |
| 365 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED); | 379 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED); |
| 366 DeletePendingArchiver(archiver); | 380 DeletePendingArchiver(archiver); |
| 367 return; | 381 return; |
| 368 } | 382 } |
| 369 | 383 |
| 370 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { | 384 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { |
| 371 SavePageResult result = ToSavePageResult(archiver_result); | 385 SavePageResult result = ToSavePageResult(archiver_result); |
| 372 InformSavePageDone(callback, result); | 386 InformSavePageDone(callback, result); |
| 373 DeletePendingArchiver(archiver); | 387 DeletePendingArchiver(archiver); |
| 374 return; | 388 return; |
| 375 } | 389 } |
| 376 | 390 |
| 377 OfflinePageItem offline_page_item(url, bookmark_id, file_path, file_size, | 391 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path, |
| 378 start_time); | 392 file_size, start_time); |
| 379 store_->AddOrUpdateOfflinePage( | 393 store_->AddOrUpdateOfflinePage( |
| 380 offline_page_item, | 394 offline_page_item, |
| 381 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, | 395 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, |
| 382 weak_ptr_factory_.GetWeakPtr(), archiver, callback, | 396 weak_ptr_factory_.GetWeakPtr(), archiver, callback, |
| 383 offline_page_item)); | 397 offline_page_item)); |
| 384 } | 398 } |
| 385 | 399 |
| 386 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, | 400 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, |
| 387 const SavePageCallback& callback, | 401 const SavePageCallback& callback, |
| 388 const OfflinePageItem& offline_page, | 402 const OfflinePageItem& offline_page, |
| 389 bool success) { | 403 bool success) { |
| 390 SavePageResult result; | 404 SavePageResult result; |
| 391 if (success) { | 405 if (success) { |
| 392 offline_pages_[offline_page.bookmark_id] = offline_page; | 406 offline_pages_[offline_page.offline_id] = offline_page; |
| 393 result = SavePageResult::SUCCESS; | 407 result = SavePageResult::SUCCESS; |
| 394 UMA_HISTOGRAM_TIMES( | 408 UMA_HISTOGRAM_TIMES( |
| 395 "OfflinePages.SavePageTime", | 409 "OfflinePages.SavePageTime", |
| 396 base::Time::Now() - offline_page.creation_time); | 410 base::Time::Now() - offline_page.creation_time); |
| 397 UMA_HISTOGRAM_MEMORY_KB( | 411 UMA_HISTOGRAM_MEMORY_KB( |
| 398 "OfflinePages.PageSize", offline_page.file_size / 1024); | 412 "OfflinePages.PageSize", offline_page.file_size / 1024); |
| 399 } else { | 413 } else { |
| 400 result = SavePageResult::STORE_FAILURE; | 414 result = SavePageResult::STORE_FAILURE; |
| 401 } | 415 } |
| 402 InformSavePageDone(callback, result); | 416 InformSavePageDone(callback, result); |
| 403 DeletePendingArchiver(archiver); | 417 DeletePendingArchiver(archiver); |
| 404 | 418 |
| 405 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 419 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 406 } | 420 } |
| 407 | 421 |
| 408 void OfflinePageModel::OnMarkPageAccesseDone( | 422 void OfflinePageModel::OnMarkPageAccesseDone( |
| 409 const OfflinePageItem& offline_page_item, bool success) { | 423 const OfflinePageItem& offline_page_item, bool success) { |
| 410 // Update the item in the cache only upon success. | 424 // Update the item in the cache only upon success. |
| 411 if (success) | 425 if (success) |
| 412 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | 426 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 413 | 427 |
| 414 // No need to fire OfflinePageModelChanged event since updating access info | 428 // No need to fire OfflinePageModelChanged event since updating access info |
| 415 // should not have any impact to the UI. | 429 // should not have any impact to the UI. |
| 416 } | 430 } |
| 417 | 431 |
| 418 void OfflinePageModel::OnMarkPageForDeletionDone( | 432 void OfflinePageModel::OnMarkPageForDeletionDone( |
| 419 const OfflinePageItem& offline_page_item, | 433 const OfflinePageItem& offline_page_item, |
| 420 const DeletePageCallback& callback, | 434 const DeletePageCallback& callback, |
| 421 bool success) { | 435 bool success) { |
| 422 // Update the item in the cache only upon success. | 436 // Update the item in the cache only upon success. |
| 423 if (success) | 437 if (success) |
| 424 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; | 438 offline_pages_[offline_page_item.offline_id] = offline_page_item; |
| 425 | 439 |
| 426 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS | 440 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS |
| 427 : DeletePageResult::STORE_FAILURE); | 441 : DeletePageResult::STORE_FAILURE); |
| 428 | 442 |
| 429 if (!success) | 443 if (!success) |
| 430 return; | 444 return; |
| 431 | 445 |
| 432 // Schedule to do the final deletion. | 446 // Schedule to do the final deletion. |
| 433 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 447 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 434 FROM_HERE, | 448 FROM_HERE, |
| 435 base::Bind(&OfflinePageModel::FinalizePageDeletion, | 449 base::Bind(&OfflinePageModel::FinalizePageDeletion, |
| 436 weak_ptr_factory_.GetWeakPtr()), | 450 weak_ptr_factory_.GetWeakPtr()), |
| 437 kFinalDeletionDelay); | 451 kFinalDeletionDelay); |
| 438 | 452 |
| 439 FOR_EACH_OBSERVER( | 453 FOR_EACH_OBSERVER(Observer, observers_, |
| 440 Observer, observers_, OfflinePageDeleted(offline_page_item.bookmark_id)); | 454 OfflinePageDeleted(offline_page_item.offline_id)); |
| 441 } | 455 } |
| 442 | 456 |
| 443 void OfflinePageModel::OnUndoOfflinePageDone( | 457 void OfflinePageModel::OnUndoOfflinePageDone( |
| 444 const OfflinePageItem& offline_page, bool success) { | 458 const OfflinePageItem& offline_page, bool success) { |
| 445 if (!success) | 459 if (!success) |
| 446 return; | 460 return; |
| 447 offline_pages_[offline_page.bookmark_id] = offline_page; | 461 offline_pages_[offline_page.offline_id] = offline_page; |
| 448 | 462 |
| 449 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); | 463 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); |
| 450 } | 464 } |
| 451 | 465 |
| 452 void OfflinePageModel::FinalizePageDeletion() { | 466 void OfflinePageModel::FinalizePageDeletion() { |
| 453 std::vector<int64_t> bookmark_ids_pending_deletion; | 467 std::vector<int64_t> offline_ids_pending_deletion; |
| 454 for (const auto& id_page_pair : offline_pages_) { | 468 for (const auto& id_page_pair : offline_pages_) { |
| 455 if (!id_page_pair.second.IsMarkedForDeletion()) | 469 if (!id_page_pair.second.IsMarkedForDeletion()) |
| 456 continue; | 470 continue; |
| 457 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); | 471 offline_ids_pending_deletion.push_back(id_page_pair.second.offline_id); |
| 458 } | 472 } |
| 459 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); | 473 DeletePagesByOfflineId(offline_ids_pending_deletion, DeletePageCallback()); |
| 460 } | 474 } |
| 461 | 475 |
| 462 void OfflinePageModel::UndoPageDeletion(int64_t bookmark_id) { | 476 void OfflinePageModel::UndoPageDeletion(int64_t offline_id) { |
| 463 auto iter = offline_pages_.find(bookmark_id); | 477 auto iter = offline_pages_.find(offline_id); |
| 464 if (iter == offline_pages_.end()) | 478 if (iter == offline_pages_.end()) |
| 465 return; | 479 return; |
| 466 | 480 |
| 467 // Make a copy of the cached item and update it. The cached item should only | 481 // Make a copy of the cached item and update it. The cached item should only |
| 468 // be updated upon the successful store operation. | 482 // be updated upon the successful store operation. |
| 469 OfflinePageItem offline_page_item = iter->second; | 483 OfflinePageItem offline_page_item = iter->second; |
| 470 if (!offline_page_item.IsMarkedForDeletion()) | 484 if (!offline_page_item.IsMarkedForDeletion()) |
| 471 return; | 485 return; |
| 472 | 486 |
| 473 // Clear the flag to bring it back. | 487 // Clear the flag to bring it back. |
| 474 offline_page_item.ClearMarkForDeletion(); | 488 offline_page_item.ClearMarkForDeletion(); |
| 475 store_->AddOrUpdateOfflinePage( | 489 store_->AddOrUpdateOfflinePage( |
| 476 offline_page_item, | 490 offline_page_item, |
| 477 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, | 491 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, |
| 478 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); | 492 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); |
| 479 } | 493 } |
| 480 | 494 |
| 481 void OfflinePageModel::BookmarkModelChanged() { | 495 void OfflinePageModel::BookmarkModelChanged() { |
| 482 } | 496 } |
| 483 | 497 |
| 484 void OfflinePageModel::BookmarkNodeAdded(bookmarks::BookmarkModel* model, | 498 void OfflinePageModel::BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
| 485 const bookmarks::BookmarkNode* parent, | 499 const bookmarks::BookmarkNode* parent, |
| 486 int index) { | 500 int index) { |
| 487 const bookmarks::BookmarkNode* node = parent->GetChild(index); | 501 const bookmarks::BookmarkNode* node = parent->GetChild(index); |
| 488 DCHECK(node); | 502 DCHECK(node); |
| 489 UndoPageDeletion(node->id()); | 503 ClientId cid; |
| 504 cid.name_space = "bookmark"; | |
|
Dmitry Titov
2016/02/23 20:35:20
This should be pulled into a constant. You already
bburns
2016/02/23 20:58:30
Done.
| |
| 505 cid.id = base::Int64ToString(node->id()); | |
| 506 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid); | |
| 507 for (size_t i = 0; i < ids.size(); i++) { | |
| 508 UndoPageDeletion(ids[i]); | |
| 509 } | |
| 490 } | 510 } |
| 491 | 511 |
| 492 void OfflinePageModel::BookmarkNodeRemoved( | 512 void OfflinePageModel::BookmarkNodeRemoved( |
| 493 bookmarks::BookmarkModel* model, | 513 bookmarks::BookmarkModel* model, |
| 494 const bookmarks::BookmarkNode* parent, | 514 const bookmarks::BookmarkNode* parent, |
| 495 int old_index, | 515 int old_index, |
| 496 const bookmarks::BookmarkNode* node, | 516 const bookmarks::BookmarkNode* node, |
| 497 const std::set<GURL>& removed_urls) { | 517 const std::set<GURL>& removed_urls) { |
| 518 ClientId cid; | |
| 519 cid.name_space = "bookmark"; | |
|
Dmitry Titov
2016/02/23 20:35:20
same as above
bburns
2016/02/23 20:58:30
Done.
| |
| 520 cid.id = base::Int64ToString(node->id()); | |
| 521 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid); | |
| 498 if (!is_loaded_) { | 522 if (!is_loaded_) { |
| 499 delayed_tasks_.push_back( | 523 for (size_t i = 0; i < ids.size(); i++) { |
| 500 base::Bind(&OfflinePageModel::MarkPageForDeletion, | 524 delayed_tasks_.push_back( |
| 501 weak_ptr_factory_.GetWeakPtr(), | 525 base::Bind(&OfflinePageModel::MarkPageForDeletion, |
| 502 node->id(), | 526 weak_ptr_factory_.GetWeakPtr(), ids[i], |
| 503 base::Bind(&EmptyDeleteCallback))); | 527 base::Bind(&EmptyDeleteCallback))); |
| 528 } | |
| 504 return; | 529 return; |
| 505 } | 530 } |
| 506 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); | 531 for (size_t i = 0; i < ids.size(); i++) { |
| 532 MarkPageForDeletion(ids[i], base::Bind(&EmptyDeleteCallback)); | |
| 533 } | |
| 507 } | 534 } |
| 508 | 535 |
| 509 void OfflinePageModel::BookmarkNodeChanged( | 536 void OfflinePageModel::BookmarkNodeChanged( |
| 510 bookmarks::BookmarkModel* model, | 537 bookmarks::BookmarkModel* model, |
| 511 const bookmarks::BookmarkNode* node) { | 538 const bookmarks::BookmarkNode* node) { |
| 512 // BookmarkNodeChanged could be triggered if title or URL gets changed. If | 539 // BookmarkNodeChanged could be triggered if title or URL gets changed. If |
| 513 // the latter, we need to invalidate the offline copy. | 540 // the latter, we need to invalidate the offline copy. |
| 514 auto iter = offline_pages_.find(node->id()); | 541 ClientId cid; |
| 515 if (iter != offline_pages_.end() && iter->second.url != node->url()) | 542 cid.name_space = "bookmark"; |
|
Dmitry Titov
2016/02/23 20:35:20
same ^^
bburns
2016/02/23 20:58:30
Done.
| |
| 516 DeletePageByBookmarkId(node->id(), DeletePageCallback()); | 543 cid.id = base::Int64ToString(node->id()); |
| 544 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid); | |
| 545 for (size_t i = 0; i < ids.size(); i++) { | |
| 546 auto iter = offline_pages_.find(ids[i]); | |
| 547 if (iter != offline_pages_.end() && iter->second.url != node->url()) | |
| 548 DeletePageByOfflineId(ids[i], DeletePageCallback()); | |
| 549 } | |
| 517 } | 550 } |
| 518 | 551 |
| 519 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() { | 552 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() { |
| 520 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, | 553 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, |
| 521 weak_ptr_factory_.GetWeakPtr())); | 554 weak_ptr_factory_.GetWeakPtr())); |
| 522 } | 555 } |
| 523 | 556 |
| 524 void OfflinePageModel::OnLoadDone( | 557 void OfflinePageModel::OnLoadDone( |
| 525 OfflinePageMetadataStore::LoadStatus load_status, | 558 OfflinePageMetadataStore::LoadStatus load_status, |
| 526 const std::vector<OfflinePageItem>& offline_pages) { | 559 const std::vector<OfflinePageItem>& offline_pages) { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 554 static_cast<int>(SavePageResult::RESULT_COUNT)); | 587 static_cast<int>(SavePageResult::RESULT_COUNT)); |
| 555 callback.Run(result); | 588 callback.Run(result); |
| 556 } | 589 } |
| 557 | 590 |
| 558 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { | 591 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { |
| 559 pending_archivers_.erase(std::find( | 592 pending_archivers_.erase(std::find( |
| 560 pending_archivers_.begin(), pending_archivers_.end(), archiver)); | 593 pending_archivers_.begin(), pending_archivers_.end(), archiver)); |
| 561 } | 594 } |
| 562 | 595 |
| 563 void OfflinePageModel::OnDeleteArchiveFilesDone( | 596 void OfflinePageModel::OnDeleteArchiveFilesDone( |
| 564 const std::vector<int64_t>& bookmark_ids, | 597 const std::vector<int64_t>& offline_ids, |
| 565 const DeletePageCallback& callback, | 598 const DeletePageCallback& callback, |
| 566 const bool* success) { | 599 const bool* success) { |
| 567 DCHECK(success); | 600 DCHECK(success); |
| 568 | 601 |
| 569 if (!*success) { | 602 if (!*success) { |
| 570 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); | 603 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); |
| 571 return; | 604 return; |
| 572 } | 605 } |
| 573 | 606 |
| 574 store_->RemoveOfflinePages( | 607 store_->RemoveOfflinePages( |
| 575 bookmark_ids, | 608 offline_ids, |
| 576 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 609 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
| 577 weak_ptr_factory_.GetWeakPtr(), bookmark_ids, callback)); | 610 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback)); |
| 578 } | 611 } |
| 579 | 612 |
| 580 void OfflinePageModel::OnRemoveOfflinePagesDone( | 613 void OfflinePageModel::OnRemoveOfflinePagesDone( |
| 581 const std::vector<int64_t>& bookmark_ids, | 614 const std::vector<int64_t>& offline_ids, |
| 582 const DeletePageCallback& callback, | 615 const DeletePageCallback& callback, |
| 583 bool success) { | 616 bool success) { |
| 584 // Delete the offline page from the in memory cache regardless of success in | 617 // Delete the offline page from the in memory cache regardless of success in |
| 585 // store. | 618 // store. |
| 586 base::Time now = base::Time::Now(); | 619 base::Time now = base::Time::Now(); |
| 587 int64_t total_size = 0; | 620 int64_t total_size = 0; |
| 588 for (int64_t bookmark_id : bookmark_ids) { | 621 for (int64_t offline_id : offline_ids) { |
| 589 auto iter = offline_pages_.find(bookmark_id); | 622 auto iter = offline_pages_.find(offline_id); |
| 590 if (iter == offline_pages_.end()) | 623 if (iter == offline_pages_.end()) |
| 591 continue; | 624 continue; |
| 592 total_size += iter->second.file_size; | 625 total_size += iter->second.file_size; |
| 593 UMA_HISTOGRAM_CUSTOM_COUNTS( | 626 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 594 "OfflinePages.PageLifetime", | 627 "OfflinePages.PageLifetime", |
| 595 (now - iter->second.creation_time).InMinutes(), | 628 (now - iter->second.creation_time).InMinutes(), |
| 596 1, | 629 1, |
| 597 base::TimeDelta::FromDays(365).InMinutes(), | 630 base::TimeDelta::FromDays(365).InMinutes(), |
| 598 100); | 631 100); |
| 599 UMA_HISTOGRAM_CUSTOM_COUNTS( | 632 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 610 base::TimeDelta::FromDays(365).InMinutes(), | 643 base::TimeDelta::FromDays(365).InMinutes(), |
| 611 100); | 644 100); |
| 612 UMA_HISTOGRAM_MEMORY_KB( | 645 UMA_HISTOGRAM_MEMORY_KB( |
| 613 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); | 646 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); |
| 614 UMA_HISTOGRAM_COUNTS( | 647 UMA_HISTOGRAM_COUNTS( |
| 615 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); | 648 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); |
| 616 // If the page is not marked for deletion at this point, the model has not | 649 // 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. | 650 // yet informed the observer that the offline page is deleted. |
| 618 if (!iter->second.IsMarkedForDeletion()) { | 651 if (!iter->second.IsMarkedForDeletion()) { |
| 619 FOR_EACH_OBSERVER(Observer, observers_, | 652 FOR_EACH_OBSERVER(Observer, observers_, |
| 620 OfflinePageDeleted(iter->second.bookmark_id)); | 653 OfflinePageDeleted(iter->second.offline_id)); |
| 621 } | 654 } |
| 622 offline_pages_.erase(iter); | 655 offline_pages_.erase(iter); |
| 623 } | 656 } |
| 624 if (bookmark_ids.size() > 1) { | 657 if (offline_ids.size() > 1) { |
| 625 UMA_HISTOGRAM_COUNTS( | 658 UMA_HISTOGRAM_COUNTS("OfflinePages.BatchDelete.Count", offline_ids.size()); |
| 626 "OfflinePages.BatchDelete.Count", bookmark_ids.size()); | |
| 627 UMA_HISTOGRAM_MEMORY_KB( | 659 UMA_HISTOGRAM_MEMORY_KB( |
| 628 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); | 660 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); |
| 629 } | 661 } |
| 630 // Deleting multiple pages always succeeds when it gets to this point. | 662 // Deleting multiple pages always succeeds when it gets to this point. |
| 631 InformDeletePageDone( | 663 InformDeletePageDone(callback, (success || offline_ids.size() > 1) |
| 632 callback, | 664 ? DeletePageResult::SUCCESS |
| 633 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS | 665 : DeletePageResult::STORE_FAILURE); |
| 634 : DeletePageResult::STORE_FAILURE); | |
| 635 } | 666 } |
| 636 | 667 |
| 637 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, | 668 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, |
| 638 DeletePageResult result) { | 669 DeletePageResult result) { |
| 639 UMA_HISTOGRAM_ENUMERATION( | 670 UMA_HISTOGRAM_ENUMERATION( |
| 640 "OfflinePages.DeletePageResult", | 671 "OfflinePages.DeletePageResult", |
| 641 static_cast<int>(result), | 672 static_cast<int>(result), |
| 642 static_cast<int>(DeletePageResult::RESULT_COUNT)); | 673 static_cast<int>(DeletePageResult::RESULT_COUNT)); |
| 643 if (!callback.is_null()) | 674 if (!callback.is_null()) |
| 644 callback.Run(result); | 675 callback.Run(result); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 657 | 688 |
| 658 store_->RemoveOfflinePages( | 689 store_->RemoveOfflinePages( |
| 659 *ids_of_pages_missing_archive_file, | 690 *ids_of_pages_missing_archive_file, |
| 660 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, | 691 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, |
| 661 weak_ptr_factory_.GetWeakPtr(), | 692 weak_ptr_factory_.GetWeakPtr(), |
| 662 *ids_of_pages_missing_archive_file, | 693 *ids_of_pages_missing_archive_file, |
| 663 done_callback)); | 694 done_callback)); |
| 664 } | 695 } |
| 665 | 696 |
| 666 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( | 697 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( |
| 667 const std::vector<int64_t>& bookmark_ids, | 698 const std::vector<int64_t>& offline_ids, |
| 668 OfflinePageModel::DeletePageResult /* result */) { | 699 OfflinePageModel::DeletePageResult /* result */) { |
| 669 for (int64_t bookmark_id : bookmark_ids) { | 700 for (int64_t offline_id : offline_ids) { |
| 670 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); | 701 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(offline_id)); |
| 671 } | 702 } |
| 672 } | 703 } |
| 673 | 704 |
| 674 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( | 705 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( |
| 675 const base::Closure& callback, | 706 const base::Closure& callback, |
| 676 DeletePageResult result) { | 707 DeletePageResult result) { |
| 677 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, | 708 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, |
| 678 weak_ptr_factory_.GetWeakPtr(), | 709 weak_ptr_factory_.GetWeakPtr(), |
| 679 callback)); | 710 callback)); |
| 680 } | 711 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 704 CLEAR_ALL_STATUS_COUNT); | 735 CLEAR_ALL_STATUS_COUNT); |
| 705 | 736 |
| 706 CacheLoadedData(offline_pages); | 737 CacheLoadedData(offline_pages); |
| 707 callback.Run(); | 738 callback.Run(); |
| 708 } | 739 } |
| 709 | 740 |
| 710 void OfflinePageModel::CacheLoadedData( | 741 void OfflinePageModel::CacheLoadedData( |
| 711 const std::vector<OfflinePageItem>& offline_pages) { | 742 const std::vector<OfflinePageItem>& offline_pages) { |
| 712 offline_pages_.clear(); | 743 offline_pages_.clear(); |
| 713 for (const auto& offline_page : offline_pages) | 744 for (const auto& offline_page : offline_pages) |
| 714 offline_pages_[offline_page.bookmark_id] = offline_page; | 745 offline_pages_[offline_page.offline_id] = offline_page; |
| 715 } | 746 } |
| 716 | 747 |
| 717 } // namespace offline_pages | 748 } // namespace offline_pages |
| OLD | NEW |