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

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

Issue 1694863003: Refactor the offline page storage to include client namespace and id. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments. Created 4 years, 10 months 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
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"
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
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(archives_dir_,
166 base::Bind(&OfflinePageModel::OnCreateArchiveDone, 169 base::Bind(&OfflinePageModel::OnCreateArchiveDone,
167 weak_ptr_factory_.GetWeakPtr(), url, 170 weak_ptr_factory_.GetWeakPtr(), url,
168 bookmark_id, base::Time::Now(), callback)); 171 offline_id, client_id, base::Time::Now(),
172 callback));
169 pending_archivers_.push_back(std::move(archiver)); 173 pending_archivers_.push_back(std::move(archiver));
170 } 174 }
171 175
172 void OfflinePageModel::MarkPageAccessed(int64_t bookmark_id) { 176 void OfflinePageModel::MarkPageAccessed(int64_t offline_id) {
173 DCHECK(is_loaded_); 177 DCHECK(is_loaded_);
174 auto iter = offline_pages_.find(bookmark_id); 178 auto iter = offline_pages_.find(offline_id);
175 if (iter == offline_pages_.end()) 179 if (iter == offline_pages_.end())
176 return; 180 return;
177 181
178 // MarkPageAccessed should not be called for a page that is being marked for 182 // MarkPageAccessed should not be called for a page that is being marked for
179 // deletion. 183 // deletion.
180 DCHECK(!iter->second.IsMarkedForDeletion()); 184 DCHECK(!iter->second.IsMarkedForDeletion());
181 185
182 // Make a copy of the cached item and update it. The cached item should only 186 // Make a copy of the cached item and update it. The cached item should only
183 // be updated upon the successful store operation. 187 // be updated upon the successful store operation.
184 OfflinePageItem offline_page_item = iter->second; 188 OfflinePageItem offline_page_item = iter->second;
185 offline_page_item.last_access_time = base::Time::Now(); 189 offline_page_item.last_access_time = base::Time::Now();
186 offline_page_item.access_count++; 190 offline_page_item.access_count++;
187 store_->AddOrUpdateOfflinePage( 191 store_->AddOrUpdateOfflinePage(
188 offline_page_item, 192 offline_page_item,
189 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone, 193 base::Bind(&OfflinePageModel::OnMarkPageAccesseDone,
190 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); 194 weak_ptr_factory_.GetWeakPtr(), offline_page_item));
191 } 195 }
192 196
193 void OfflinePageModel::MarkPageForDeletion(int64_t bookmark_id, 197 void OfflinePageModel::MarkPageForDeletion(int64_t offline_id,
194 const DeletePageCallback& callback) { 198 const DeletePageCallback& callback) {
195 DCHECK(is_loaded_); 199 DCHECK(is_loaded_);
196 auto iter = offline_pages_.find(bookmark_id); 200 auto iter = offline_pages_.find(offline_id);
197 if (iter == offline_pages_.end()) { 201 if (iter == offline_pages_.end()) {
198 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); 202 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND);
199 return; 203 return;
200 } 204 }
201 205
202 // Make a copy of the cached item and update it. The cached item should only 206 // Make a copy of the cached item and update it. The cached item should only
203 // be updated upon the successful store operation. 207 // be updated upon the successful store operation.
204 OfflinePageItem offline_page_item = iter->second; 208 OfflinePageItem offline_page_item = iter->second;
205 offline_page_item.MarkForDeletion(); 209 offline_page_item.MarkForDeletion();
206 store_->AddOrUpdateOfflinePage( 210 store_->AddOrUpdateOfflinePage(
207 offline_page_item, 211 offline_page_item,
208 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone, 212 base::Bind(&OfflinePageModel::OnMarkPageForDeletionDone,
209 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback)); 213 weak_ptr_factory_.GetWeakPtr(), offline_page_item, callback));
210 } 214 }
211 215
212 void OfflinePageModel::DeletePageByBookmarkId( 216 void OfflinePageModel::DeletePageByOfflineId(
213 int64_t bookmark_id, 217 int64_t offline_id,
214 const DeletePageCallback& callback) { 218 const DeletePageCallback& callback) {
215 DCHECK(is_loaded_); 219 DCHECK(is_loaded_);
216 std::vector<int64_t> bookmark_ids_to_delete; 220 std::vector<int64_t> offline_ids_to_delete;
217 bookmark_ids_to_delete.push_back(bookmark_id); 221 offline_ids_to_delete.push_back(offline_id);
218 DeletePagesByBookmarkId(bookmark_ids_to_delete, callback); 222 DeletePagesByOfflineId(offline_ids_to_delete, callback);
219 } 223 }
220 224
221 void OfflinePageModel::DeletePagesByBookmarkId( 225 void OfflinePageModel::DeletePagesByOfflineId(
222 const std::vector<int64_t>& bookmark_ids, 226 const std::vector<int64_t>& offline_ids,
223 const DeletePageCallback& callback) { 227 const DeletePageCallback& callback) {
224 DCHECK(is_loaded_); 228 DCHECK(is_loaded_);
225 229
226 std::vector<base::FilePath> paths_to_delete; 230 std::vector<base::FilePath> paths_to_delete;
227 for (const auto& bookmark_id : bookmark_ids) { 231 for (const auto& offline_id : offline_ids) {
228 auto iter = offline_pages_.find(bookmark_id); 232 auto iter = offline_pages_.find(offline_id);
229 if (iter != offline_pages_.end()) { 233 if (iter != offline_pages_.end()) {
230 paths_to_delete.push_back(iter->second.file_path); 234 paths_to_delete.push_back(iter->second.file_path);
231 } 235 }
232 } 236 }
233 237
234 if (paths_to_delete.empty()) { 238 if (paths_to_delete.empty()) {
235 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND); 239 InformDeletePageDone(callback, DeletePageResult::NOT_FOUND);
236 return; 240 return;
237 } 241 }
238 242
239 bool* success = new bool(false); 243 bool* success = new bool(false);
240 task_runner_->PostTaskAndReply( 244 task_runner_->PostTaskAndReply(
241 FROM_HERE, 245 FROM_HERE,
242 base::Bind(&DeleteArchiveFiles, paths_to_delete, success), 246 base::Bind(&DeleteArchiveFiles, paths_to_delete, success),
243 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone, 247 base::Bind(&OfflinePageModel::OnDeleteArchiveFilesDone,
244 weak_ptr_factory_.GetWeakPtr(), 248 weak_ptr_factory_.GetWeakPtr(),
245 bookmark_ids, 249 offline_ids,
246 callback, 250 callback,
247 base::Owned(success))); 251 base::Owned(success)));
248 } 252 }
249 253
250 void OfflinePageModel::ClearAll(const base::Closure& callback) { 254 void OfflinePageModel::ClearAll(const base::Closure& callback) {
251 DCHECK(is_loaded_); 255 DCHECK(is_loaded_);
252 256
253 std::vector<int64_t> bookmark_ids; 257 std::vector<int64_t> offline_ids;
254 for (const auto& id_page_pair : offline_pages_) 258 for (const auto& id_page_pair : offline_pages_)
255 bookmark_ids.push_back(id_page_pair.first); 259 offline_ids.push_back(id_page_pair.first);
256 DeletePagesByBookmarkId( 260 DeletePagesByOfflineId(
257 bookmark_ids, 261 offline_ids,
258 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll, 262 base::Bind(&OfflinePageModel::OnRemoveAllFilesDoneForClearAll,
259 weak_ptr_factory_.GetWeakPtr(), 263 weak_ptr_factory_.GetWeakPtr(),
260 callback)); 264 callback));
261 } 265 }
262 266
263 bool OfflinePageModel::HasOfflinePages() const { 267 bool OfflinePageModel::HasOfflinePages() const {
264 DCHECK(is_loaded_); 268 DCHECK(is_loaded_);
265 // Check that at least one page is not marked for deletion. Because we have 269 // 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()|. 270 // pages marked for deletion, we cannot simply invert result of |empty()|.
267 for (const auto& id_page_pair : offline_pages_) { 271 for (const auto& id_page_pair : offline_pages_) {
(...skipping 20 matching lines...) Expand all
288 base::Time now = base::Time::Now(); 292 base::Time now = base::Time::Now();
289 for (const auto& id_page_pair : offline_pages_) { 293 for (const auto& id_page_pair : offline_pages_) {
290 if (!id_page_pair.second.IsMarkedForDeletion() && 294 if (!id_page_pair.second.IsMarkedForDeletion() &&
291 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) { 295 now - id_page_pair.second.last_access_time > kPageCleanUpThreshold) {
292 offline_pages.push_back(id_page_pair.second); 296 offline_pages.push_back(id_page_pair.second);
293 } 297 }
294 } 298 }
295 return offline_pages; 299 return offline_pages;
296 } 300 }
297 301
298 const OfflinePageItem* OfflinePageModel::GetPageByBookmarkId( 302 const std::vector<int64_t> OfflinePageModel::GetOfflineIdsForClientId(
299 int64_t bookmark_id) const { 303 const ClientId& cid) const {
300 const auto iter = offline_pages_.find(bookmark_id); 304 std::vector<int64_t> results;
305 // TODO(bburns): actually use an index rather than linear scan.
306 const std::vector<OfflinePageItem> offline_pages = GetAllPages();
307 for (size_t i = 0; i < offline_pages.size(); i++) {
308 if (offline_pages[i].client_id_namespace == cid.space &&
309 offline_pages[i].client_id == cid.id) {
310 results.push_back(offline_pages[i].offline_id);
311 }
312 }
313 return results;
314 }
315
316 const OfflinePageItem* OfflinePageModel::GetPageByOfflineId(
317 int64_t offline_id) const {
318 const auto iter = offline_pages_.find(offline_id);
301 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion() 319 return iter != offline_pages_.end() && !iter->second.IsMarkedForDeletion()
302 ? &(iter->second) 320 ? &(iter->second)
303 : nullptr; 321 : nullptr;
304 } 322 }
305 323
306 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL( 324 const OfflinePageItem* OfflinePageModel::GetPageByOfflineURL(
307 const GURL& offline_url) const { 325 const GURL& offline_url) const {
308 for (auto iter = offline_pages_.begin(); 326 for (auto iter = offline_pages_.begin();
309 iter != offline_pages_.end(); 327 iter != offline_pages_.end();
310 ++iter) { 328 ++iter) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile, 361 base::Bind(&OfflinePageModel::OnFindPagesMissingArchiveFile,
344 weak_ptr_factory_.GetWeakPtr(), 362 weak_ptr_factory_.GetWeakPtr(),
345 base::Owned(ids_of_pages_missing_archive_file))); 363 base::Owned(ids_of_pages_missing_archive_file)));
346 } 364 }
347 365
348 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() { 366 OfflinePageMetadataStore* OfflinePageModel::GetStoreForTesting() {
349 return store_.get(); 367 return store_.get();
350 } 368 }
351 369
352 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url, 370 void OfflinePageModel::OnCreateArchiveDone(const GURL& requested_url,
353 int64_t bookmark_id, 371 int64_t offline_id,
372 const ClientId& client_id,
354 const base::Time& start_time, 373 const base::Time& start_time,
355 const SavePageCallback& callback, 374 const SavePageCallback& callback,
356 OfflinePageArchiver* archiver, 375 OfflinePageArchiver* archiver,
357 ArchiverResult archiver_result, 376 ArchiverResult archiver_result,
358 const GURL& url, 377 const GURL& url,
359 const base::FilePath& file_path, 378 const base::FilePath& file_path,
360 int64_t file_size) { 379 int64_t file_size) {
361 if (requested_url != url) { 380 if (requested_url != url) {
362 DVLOG(1) << "Saved URL does not match requested URL."; 381 DVLOG(1) << "Saved URL does not match requested URL.";
363 // TODO(fgorski): We have created an archive for a wrong URL. It should be 382 // TODO(fgorski): We have created an archive for a wrong URL. It should be
364 // deleted from here, once archiver has the right functionality. 383 // deleted from here, once archiver has the right functionality.
365 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED); 384 InformSavePageDone(callback, SavePageResult::ARCHIVE_CREATION_FAILED);
366 DeletePendingArchiver(archiver); 385 DeletePendingArchiver(archiver);
367 return; 386 return;
368 } 387 }
369 388
370 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) { 389 if (archiver_result != ArchiverResult::SUCCESSFULLY_CREATED) {
371 SavePageResult result = ToSavePageResult(archiver_result); 390 SavePageResult result = ToSavePageResult(archiver_result);
372 InformSavePageDone(callback, result); 391 InformSavePageDone(callback, result);
373 DeletePendingArchiver(archiver); 392 DeletePendingArchiver(archiver);
374 return; 393 return;
375 } 394 }
376 395
377 OfflinePageItem offline_page_item(url, bookmark_id, file_path, file_size, 396 OfflinePageItem offline_page_item(url, offline_id, client_id, file_path,
378 start_time); 397 file_size, start_time);
379 store_->AddOrUpdateOfflinePage( 398 store_->AddOrUpdateOfflinePage(
380 offline_page_item, 399 offline_page_item,
381 base::Bind(&OfflinePageModel::OnAddOfflinePageDone, 400 base::Bind(&OfflinePageModel::OnAddOfflinePageDone,
382 weak_ptr_factory_.GetWeakPtr(), archiver, callback, 401 weak_ptr_factory_.GetWeakPtr(), archiver, callback,
383 offline_page_item)); 402 offline_page_item));
384 } 403 }
385 404
386 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver, 405 void OfflinePageModel::OnAddOfflinePageDone(OfflinePageArchiver* archiver,
387 const SavePageCallback& callback, 406 const SavePageCallback& callback,
388 const OfflinePageItem& offline_page, 407 const OfflinePageItem& offline_page,
389 bool success) { 408 bool success) {
390 SavePageResult result; 409 SavePageResult result;
391 if (success) { 410 if (success) {
392 offline_pages_[offline_page.bookmark_id] = offline_page; 411 offline_pages_[offline_page.offline_id] = offline_page;
393 result = SavePageResult::SUCCESS; 412 result = SavePageResult::SUCCESS;
394 UMA_HISTOGRAM_TIMES( 413 UMA_HISTOGRAM_TIMES(
395 "OfflinePages.SavePageTime", 414 "OfflinePages.SavePageTime",
396 base::Time::Now() - offline_page.creation_time); 415 base::Time::Now() - offline_page.creation_time);
397 UMA_HISTOGRAM_MEMORY_KB( 416 UMA_HISTOGRAM_MEMORY_KB(
398 "OfflinePages.PageSize", offline_page.file_size / 1024); 417 "OfflinePages.PageSize", offline_page.file_size / 1024);
399 } else { 418 } else {
400 result = SavePageResult::STORE_FAILURE; 419 result = SavePageResult::STORE_FAILURE;
401 } 420 }
402 InformSavePageDone(callback, result); 421 InformSavePageDone(callback, result);
403 DeletePendingArchiver(archiver); 422 DeletePendingArchiver(archiver);
404 423
405 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); 424 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this));
406 } 425 }
407 426
408 void OfflinePageModel::OnMarkPageAccesseDone( 427 void OfflinePageModel::OnMarkPageAccesseDone(
409 const OfflinePageItem& offline_page_item, bool success) { 428 const OfflinePageItem& offline_page_item, bool success) {
410 // Update the item in the cache only upon success. 429 // Update the item in the cache only upon success.
411 if (success) 430 if (success)
412 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; 431 offline_pages_[offline_page_item.offline_id] = offline_page_item;
413 432
414 // No need to fire OfflinePageModelChanged event since updating access info 433 // No need to fire OfflinePageModelChanged event since updating access info
415 // should not have any impact to the UI. 434 // should not have any impact to the UI.
416 } 435 }
417 436
418 void OfflinePageModel::OnMarkPageForDeletionDone( 437 void OfflinePageModel::OnMarkPageForDeletionDone(
419 const OfflinePageItem& offline_page_item, 438 const OfflinePageItem& offline_page_item,
420 const DeletePageCallback& callback, 439 const DeletePageCallback& callback,
421 bool success) { 440 bool success) {
422 // Update the item in the cache only upon success. 441 // Update the item in the cache only upon success.
423 if (success) 442 if (success)
424 offline_pages_[offline_page_item.bookmark_id] = offline_page_item; 443 offline_pages_[offline_page_item.offline_id] = offline_page_item;
425 444
426 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS 445 InformDeletePageDone(callback, success ? DeletePageResult::SUCCESS
427 : DeletePageResult::STORE_FAILURE); 446 : DeletePageResult::STORE_FAILURE);
428 447
429 if (!success) 448 if (!success)
430 return; 449 return;
431 450
432 // Schedule to do the final deletion. 451 // Schedule to do the final deletion.
433 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 452 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
434 FROM_HERE, 453 FROM_HERE,
435 base::Bind(&OfflinePageModel::FinalizePageDeletion, 454 base::Bind(&OfflinePageModel::FinalizePageDeletion,
436 weak_ptr_factory_.GetWeakPtr()), 455 weak_ptr_factory_.GetWeakPtr()),
437 kFinalDeletionDelay); 456 kFinalDeletionDelay);
438 457
439 FOR_EACH_OBSERVER( 458 FOR_EACH_OBSERVER(
440 Observer, observers_, OfflinePageDeleted(offline_page_item.bookmark_id)); 459 Observer, observers_, OfflinePageDeleted(offline_page_item.offline_id));
441 } 460 }
442 461
443 void OfflinePageModel::OnUndoOfflinePageDone( 462 void OfflinePageModel::OnUndoOfflinePageDone(
444 const OfflinePageItem& offline_page, bool success) { 463 const OfflinePageItem& offline_page, bool success) {
445 if (!success) 464 if (!success)
446 return; 465 return;
447 offline_pages_[offline_page.bookmark_id] = offline_page; 466 offline_pages_[offline_page.offline_id] = offline_page;
448 467
449 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this)); 468 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageModelChanged(this));
450 } 469 }
451 470
452 void OfflinePageModel::FinalizePageDeletion() { 471 void OfflinePageModel::FinalizePageDeletion() {
453 std::vector<int64_t> bookmark_ids_pending_deletion; 472 std::vector<int64_t> offline_ids_pending_deletion;
454 for (const auto& id_page_pair : offline_pages_) { 473 for (const auto& id_page_pair : offline_pages_) {
455 if (!id_page_pair.second.IsMarkedForDeletion()) 474 if (!id_page_pair.second.IsMarkedForDeletion())
456 continue; 475 continue;
457 bookmark_ids_pending_deletion.push_back(id_page_pair.second.bookmark_id); 476 offline_ids_pending_deletion.push_back(id_page_pair.second.offline_id);
458 } 477 }
459 DeletePagesByBookmarkId(bookmark_ids_pending_deletion, DeletePageCallback()); 478 DeletePagesByOfflineId(offline_ids_pending_deletion, DeletePageCallback());
460 } 479 }
461 480
462 void OfflinePageModel::UndoPageDeletion(int64_t bookmark_id) { 481 void OfflinePageModel::UndoPageDeletion(int64_t offline_id) {
463 auto iter = offline_pages_.find(bookmark_id); 482 auto iter = offline_pages_.find(offline_id);
464 if (iter == offline_pages_.end()) 483 if (iter == offline_pages_.end())
465 return; 484 return;
466 485
467 // Make a copy of the cached item and update it. The cached item should only 486 // Make a copy of the cached item and update it. The cached item should only
468 // be updated upon the successful store operation. 487 // be updated upon the successful store operation.
469 OfflinePageItem offline_page_item = iter->second; 488 OfflinePageItem offline_page_item = iter->second;
470 if (!offline_page_item.IsMarkedForDeletion()) 489 if (!offline_page_item.IsMarkedForDeletion())
471 return; 490 return;
472 491
473 // Clear the flag to bring it back. 492 // Clear the flag to bring it back.
474 offline_page_item.ClearMarkForDeletion(); 493 offline_page_item.ClearMarkForDeletion();
475 store_->AddOrUpdateOfflinePage( 494 store_->AddOrUpdateOfflinePage(
476 offline_page_item, 495 offline_page_item,
477 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone, 496 base::Bind(&OfflinePageModel::OnUndoOfflinePageDone,
478 weak_ptr_factory_.GetWeakPtr(), offline_page_item)); 497 weak_ptr_factory_.GetWeakPtr(), offline_page_item));
479 } 498 }
480 499
481 void OfflinePageModel::BookmarkModelChanged() { 500 void OfflinePageModel::BookmarkModelChanged() {
482 } 501 }
483 502
484 void OfflinePageModel::BookmarkNodeAdded(bookmarks::BookmarkModel* model, 503 void OfflinePageModel::BookmarkNodeAdded(bookmarks::BookmarkModel* model,
485 const bookmarks::BookmarkNode* parent, 504 const bookmarks::BookmarkNode* parent,
486 int index) { 505 int index) {
487 const bookmarks::BookmarkNode* node = parent->GetChild(index); 506 const bookmarks::BookmarkNode* node = parent->GetChild(index);
488 DCHECK(node); 507 DCHECK(node);
489 UndoPageDeletion(node->id()); 508 ClientId cid;
509 cid.space = "bookmark";
510 cid.id = base::Int64ToString(node->id());
511 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid);
512 for (size_t i = 0; i < ids.size(); i++) {
513 UndoPageDeletion(ids[i]);
514 }
490 } 515 }
491 516
492 void OfflinePageModel::BookmarkNodeRemoved( 517 void OfflinePageModel::BookmarkNodeRemoved(
493 bookmarks::BookmarkModel* model, 518 bookmarks::BookmarkModel* model,
494 const bookmarks::BookmarkNode* parent, 519 const bookmarks::BookmarkNode* parent,
495 int old_index, 520 int old_index,
496 const bookmarks::BookmarkNode* node, 521 const bookmarks::BookmarkNode* node,
497 const std::set<GURL>& removed_urls) { 522 const std::set<GURL>& removed_urls) {
523 ClientId cid;
524 cid.space = "bookmark";
525 cid.id = base::Int64ToString(node->id());
526 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid);
498 if (!is_loaded_) { 527 if (!is_loaded_) {
499 delayed_tasks_.push_back( 528 for (size_t i = 0; i < ids.size(); i++) {
500 base::Bind(&OfflinePageModel::MarkPageForDeletion, 529 delayed_tasks_.push_back(
501 weak_ptr_factory_.GetWeakPtr(), 530 base::Bind(&OfflinePageModel::MarkPageForDeletion,
502 node->id(), 531 weak_ptr_factory_.GetWeakPtr(),
503 base::Bind(&EmptyDeleteCallback))); 532 ids[i],
533 base::Bind(&EmptyDeleteCallback)));
534 }
504 return; 535 return;
505 } 536 }
506 MarkPageForDeletion(node->id(), base::Bind(&EmptyDeleteCallback)); 537 for (size_t i = 0; i < ids.size(); i++) {
538 MarkPageForDeletion(ids[i], base::Bind(&EmptyDeleteCallback));
539 }
507 } 540 }
508 541
509 void OfflinePageModel::BookmarkNodeChanged( 542 void OfflinePageModel::BookmarkNodeChanged(
510 bookmarks::BookmarkModel* model, 543 bookmarks::BookmarkModel* model,
511 const bookmarks::BookmarkNode* node) { 544 const bookmarks::BookmarkNode* node) {
512 // BookmarkNodeChanged could be triggered if title or URL gets changed. If 545 // BookmarkNodeChanged could be triggered if title or URL gets changed. If
513 // the latter, we need to invalidate the offline copy. 546 // the latter, we need to invalidate the offline copy.
514 auto iter = offline_pages_.find(node->id()); 547 ClientId cid;
515 if (iter != offline_pages_.end() && iter->second.url != node->url()) 548 cid.space = "bookmark";
516 DeletePageByBookmarkId(node->id(), DeletePageCallback()); 549 cid.id = base::Int64ToString(node->id());
550 std::vector<int64_t> ids = GetOfflineIdsForClientId(cid);
551 for (size_t i = 0; i < ids.size(); i++) {
552 auto iter = offline_pages_.find(ids[i]);
553 if (iter != offline_pages_.end() && iter->second.url != node->url())
554 DeletePageByOfflineId(ids[i], DeletePageCallback());
555 }
517 } 556 }
518 557
519 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() { 558 void OfflinePageModel::OnEnsureArchivesDirCreatedDone() {
520 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone, 559 store_->Load(base::Bind(&OfflinePageModel::OnLoadDone,
521 weak_ptr_factory_.GetWeakPtr())); 560 weak_ptr_factory_.GetWeakPtr()));
522 } 561 }
523 562
524 void OfflinePageModel::OnLoadDone( 563 void OfflinePageModel::OnLoadDone(
525 OfflinePageMetadataStore::LoadStatus load_status, 564 OfflinePageMetadataStore::LoadStatus load_status,
526 const std::vector<OfflinePageItem>& offline_pages) { 565 const std::vector<OfflinePageItem>& offline_pages) {
(...skipping 27 matching lines...) Expand all
554 static_cast<int>(SavePageResult::RESULT_COUNT)); 593 static_cast<int>(SavePageResult::RESULT_COUNT));
555 callback.Run(result); 594 callback.Run(result);
556 } 595 }
557 596
558 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) { 597 void OfflinePageModel::DeletePendingArchiver(OfflinePageArchiver* archiver) {
559 pending_archivers_.erase(std::find( 598 pending_archivers_.erase(std::find(
560 pending_archivers_.begin(), pending_archivers_.end(), archiver)); 599 pending_archivers_.begin(), pending_archivers_.end(), archiver));
561 } 600 }
562 601
563 void OfflinePageModel::OnDeleteArchiveFilesDone( 602 void OfflinePageModel::OnDeleteArchiveFilesDone(
564 const std::vector<int64_t>& bookmark_ids, 603 const std::vector<int64_t>& offline_ids,
565 const DeletePageCallback& callback, 604 const DeletePageCallback& callback,
566 const bool* success) { 605 const bool* success) {
567 DCHECK(success); 606 DCHECK(success);
568 607
569 if (!*success) { 608 if (!*success) {
570 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE); 609 InformDeletePageDone(callback, DeletePageResult::DEVICE_FAILURE);
571 return; 610 return;
572 } 611 }
573 612
574 store_->RemoveOfflinePages( 613 store_->RemoveOfflinePages(
575 bookmark_ids, 614 offline_ids,
576 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, 615 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
577 weak_ptr_factory_.GetWeakPtr(), bookmark_ids, callback)); 616 weak_ptr_factory_.GetWeakPtr(), offline_ids, callback));
578 } 617 }
579 618
580 void OfflinePageModel::OnRemoveOfflinePagesDone( 619 void OfflinePageModel::OnRemoveOfflinePagesDone(
581 const std::vector<int64_t>& bookmark_ids, 620 const std::vector<int64_t>& offline_ids,
582 const DeletePageCallback& callback, 621 const DeletePageCallback& callback,
583 bool success) { 622 bool success) {
584 // Delete the offline page from the in memory cache regardless of success in 623 // Delete the offline page from the in memory cache regardless of success in
585 // store. 624 // store.
586 base::Time now = base::Time::Now(); 625 base::Time now = base::Time::Now();
587 int64_t total_size = 0; 626 int64_t total_size = 0;
588 for (int64_t bookmark_id : bookmark_ids) { 627 for (int64_t offline_id : offline_ids) {
589 auto iter = offline_pages_.find(bookmark_id); 628 auto iter = offline_pages_.find(offline_id);
590 if (iter == offline_pages_.end()) 629 if (iter == offline_pages_.end())
591 continue; 630 continue;
592 total_size += iter->second.file_size; 631 total_size += iter->second.file_size;
593 UMA_HISTOGRAM_CUSTOM_COUNTS( 632 UMA_HISTOGRAM_CUSTOM_COUNTS(
594 "OfflinePages.PageLifetime", 633 "OfflinePages.PageLifetime",
595 (now - iter->second.creation_time).InMinutes(), 634 (now - iter->second.creation_time).InMinutes(),
596 1, 635 1,
597 base::TimeDelta::FromDays(365).InMinutes(), 636 base::TimeDelta::FromDays(365).InMinutes(),
598 100); 637 100);
599 UMA_HISTOGRAM_CUSTOM_COUNTS( 638 UMA_HISTOGRAM_CUSTOM_COUNTS(
(...skipping 10 matching lines...) Expand all
610 base::TimeDelta::FromDays(365).InMinutes(), 649 base::TimeDelta::FromDays(365).InMinutes(),
611 100); 650 100);
612 UMA_HISTOGRAM_MEMORY_KB( 651 UMA_HISTOGRAM_MEMORY_KB(
613 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024); 652 "OfflinePages.DeletePage.PageSize", iter->second.file_size / 1024);
614 UMA_HISTOGRAM_COUNTS( 653 UMA_HISTOGRAM_COUNTS(
615 "OfflinePages.DeletePage.AccessCount", iter->second.access_count); 654 "OfflinePages.DeletePage.AccessCount", iter->second.access_count);
616 // If the page is not marked for deletion at this point, the model has not 655 // 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. 656 // yet informed the observer that the offline page is deleted.
618 if (!iter->second.IsMarkedForDeletion()) { 657 if (!iter->second.IsMarkedForDeletion()) {
619 FOR_EACH_OBSERVER(Observer, observers_, 658 FOR_EACH_OBSERVER(Observer, observers_,
620 OfflinePageDeleted(iter->second.bookmark_id)); 659 OfflinePageDeleted(iter->second.offline_id));
621 } 660 }
622 offline_pages_.erase(iter); 661 offline_pages_.erase(iter);
623 } 662 }
624 if (bookmark_ids.size() > 1) { 663 if (offline_ids.size() > 1) {
625 UMA_HISTOGRAM_COUNTS( 664 UMA_HISTOGRAM_COUNTS(
626 "OfflinePages.BatchDelete.Count", bookmark_ids.size()); 665 "OfflinePages.BatchDelete.Count", offline_ids.size());
627 UMA_HISTOGRAM_MEMORY_KB( 666 UMA_HISTOGRAM_MEMORY_KB(
628 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024); 667 "OfflinePages.BatchDelete.TotalPageSize", total_size / 1024);
629 } 668 }
630 // Deleting multiple pages always succeeds when it gets to this point. 669 // Deleting multiple pages always succeeds when it gets to this point.
631 InformDeletePageDone( 670 InformDeletePageDone(
632 callback, 671 callback,
633 (success || bookmark_ids.size() > 1) ? DeletePageResult::SUCCESS 672 (success || offline_ids.size() > 1) ? DeletePageResult::SUCCESS
634 : DeletePageResult::STORE_FAILURE); 673 : DeletePageResult::STORE_FAILURE);
635 } 674 }
636 675
637 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback, 676 void OfflinePageModel::InformDeletePageDone(const DeletePageCallback& callback,
638 DeletePageResult result) { 677 DeletePageResult result) {
639 UMA_HISTOGRAM_ENUMERATION( 678 UMA_HISTOGRAM_ENUMERATION(
640 "OfflinePages.DeletePageResult", 679 "OfflinePages.DeletePageResult",
641 static_cast<int>(result), 680 static_cast<int>(result),
642 static_cast<int>(DeletePageResult::RESULT_COUNT)); 681 static_cast<int>(DeletePageResult::RESULT_COUNT));
643 if (!callback.is_null()) 682 if (!callback.is_null())
644 callback.Run(result); 683 callback.Run(result);
(...skipping 12 matching lines...) Expand all
657 696
658 store_->RemoveOfflinePages( 697 store_->RemoveOfflinePages(
659 *ids_of_pages_missing_archive_file, 698 *ids_of_pages_missing_archive_file,
660 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone, 699 base::Bind(&OfflinePageModel::OnRemoveOfflinePagesDone,
661 weak_ptr_factory_.GetWeakPtr(), 700 weak_ptr_factory_.GetWeakPtr(),
662 *ids_of_pages_missing_archive_file, 701 *ids_of_pages_missing_archive_file,
663 done_callback)); 702 done_callback));
664 } 703 }
665 704
666 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone( 705 void OfflinePageModel::OnRemoveOfflinePagesMissingArchiveFileDone(
667 const std::vector<int64_t>& bookmark_ids, 706 const std::vector<int64_t>& offline_ids,
668 OfflinePageModel::DeletePageResult /* result */) { 707 OfflinePageModel::DeletePageResult /* result */) {
669 for (int64_t bookmark_id : bookmark_ids) { 708 for (int64_t offline_id : offline_ids) {
670 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(bookmark_id)); 709 FOR_EACH_OBSERVER(Observer, observers_, OfflinePageDeleted(offline_id));
671 } 710 }
672 } 711 }
673 712
674 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll( 713 void OfflinePageModel::OnRemoveAllFilesDoneForClearAll(
675 const base::Closure& callback, 714 const base::Closure& callback,
676 DeletePageResult result) { 715 DeletePageResult result) {
677 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll, 716 store_->Reset(base::Bind(&OfflinePageModel::OnResetStoreDoneForClearAll,
678 weak_ptr_factory_.GetWeakPtr(), 717 weak_ptr_factory_.GetWeakPtr(),
679 callback)); 718 callback));
680 } 719 }
(...skipping 23 matching lines...) Expand all
704 CLEAR_ALL_STATUS_COUNT); 743 CLEAR_ALL_STATUS_COUNT);
705 744
706 CacheLoadedData(offline_pages); 745 CacheLoadedData(offline_pages);
707 callback.Run(); 746 callback.Run();
708 } 747 }
709 748
710 void OfflinePageModel::CacheLoadedData( 749 void OfflinePageModel::CacheLoadedData(
711 const std::vector<OfflinePageItem>& offline_pages) { 750 const std::vector<OfflinePageItem>& offline_pages) {
712 offline_pages_.clear(); 751 offline_pages_.clear();
713 for (const auto& offline_page : offline_pages) 752 for (const auto& offline_page : offline_pages)
714 offline_pages_[offline_page.bookmark_id] = offline_page; 753 offline_pages_[offline_page.offline_id] = offline_page;
715 } 754 }
716 755
717 } // namespace offline_pages 756 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698