bookmark_storage.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_
  5. #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_
  6. #include <stdint.h>
  7. #include "base/callback_forward.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/important_file_writer.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/memory/scoped_refptr.h"
  13. #include "base/time/time.h"
  14. #include "components/bookmarks/browser/bookmark_node.h"
  15. #include "components/bookmarks/browser/titled_url_index.h"
  16. namespace base {
  17. class SequencedTaskRunner;
  18. }
  19. namespace bookmarks {
  20. class BookmarkModel;
  21. // BookmarkStorage handles writing bookmark model to disk (as opposed to
  22. // ModelLoader which takes care of loading).
  23. //
  24. // Internally BookmarkStorage uses BookmarkCodec to do the actual write.
  25. class BookmarkStorage
  26. : public base::ImportantFileWriter::BackgroundDataSerializer {
  27. public:
  28. // How often the file is saved at most.
  29. static constexpr base::TimeDelta kSaveDelay = base::Milliseconds(2500);
  30. // Creates a BookmarkStorage for the specified model. The data will saved to a
  31. // file using the specified |file_path|. A backup file may be generated using
  32. // a name derived from |file_path| (appending suffix kBackupExtension). All
  33. // disk writes will be executed as a task in a backend task runner.
  34. BookmarkStorage(BookmarkModel* model, const base::FilePath& file_path);
  35. BookmarkStorage(const BookmarkStorage&) = delete;
  36. BookmarkStorage& operator=(const BookmarkStorage&) = delete;
  37. ~BookmarkStorage() override;
  38. // Schedules saving the bookmark bar model to disk.
  39. void ScheduleSave();
  40. // Notification the bookmark bar model is going to be deleted. If there is
  41. // a pending save, it is saved immediately.
  42. void BookmarkModelDeleted();
  43. // ImportantFileWriter::BackgroundDataSerializer implementation.
  44. base::ImportantFileWriter::BackgroundDataProducerCallback
  45. GetSerializedDataProducerForBackgroundSequence() override;
  46. // Returns whether there is still a pending write.
  47. bool HasScheduledSaveForTesting() const;
  48. private:
  49. // The state of the bookmark file backup. We lazily backup this file in order
  50. // to reduce disk writes until absolutely necessary. Will also leave the
  51. // backup unchanged if the browser starts & quits w/o changing bookmarks.
  52. enum BackupState {
  53. // No attempt has yet been made to backup the bookmarks file.
  54. BACKUP_NONE,
  55. // A request to backup the bookmarks file has been posted, but not yet
  56. // fulfilled.
  57. BACKUP_DISPATCHED,
  58. // The bookmarks file has been backed up (or at least attempted).
  59. BACKUP_ATTEMPTED
  60. };
  61. // Serializes the data and schedules save using ImportantFileWriter.
  62. // Returns true on successful serialization.
  63. bool SaveNow();
  64. // The model. The model is NULL once BookmarkModelDeleted has been invoked.
  65. raw_ptr<BookmarkModel> model_;
  66. // Sequenced task runner where disk writes will be performed at.
  67. scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
  68. // Helper to write bookmark data safely.
  69. base::ImportantFileWriter writer_;
  70. // The state of the backup file creation which is created lazily just before
  71. // the first scheduled save.
  72. bool backup_triggered_ = false;
  73. // Used to track the frequency of saves starting from the first save.
  74. base::TimeTicks last_scheduled_save_;
  75. };
  76. } // namespace bookmarks
  77. #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_