Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_storage.h
blob6fbb8a5508fecb888687a6ab155d4d0674476b99
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.
5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback_forward.h"
12 #include "base/files/file_path.h"
13 #include "base/files/important_file_writer.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h"
18 #include "components/bookmarks/browser/bookmark_node.h"
20 namespace base {
21 class SequencedTaskRunner;
24 namespace bookmarks {
26 class BookmarkIndex;
27 class BookmarkModel;
29 // A list of BookmarkPermanentNodes that owns them.
30 typedef ScopedVector<BookmarkPermanentNode> BookmarkPermanentNodeList;
32 // A callback that generates a BookmarkPermanentNodeList, given a max ID to
33 // use. The max ID argument will be updated after any new nodes have been
34 // created and assigned IDs.
35 typedef base::Callback<BookmarkPermanentNodeList(int64*)> LoadExtraCallback;
37 // BookmarkLoadDetails is used by BookmarkStorage when loading bookmarks.
38 // BookmarkModel creates a BookmarkLoadDetails and passes it (including
39 // ownership) to BookmarkStorage. BookmarkStorage loads the bookmarks (and
40 // index) in the background thread, then calls back to the BookmarkModel (on
41 // the main thread) when loading is done, passing ownership back to the
42 // BookmarkModel. While loading BookmarkModel does not maintain references to
43 // the contents of the BookmarkLoadDetails, this ensures we don't have any
44 // threading problems.
45 class BookmarkLoadDetails {
46 public:
47 BookmarkLoadDetails(BookmarkPermanentNode* bb_node,
48 BookmarkPermanentNode* other_folder_node,
49 BookmarkPermanentNode* mobile_folder_node,
50 const LoadExtraCallback& load_extra_callback,
51 BookmarkIndex* index,
52 int64 max_id);
53 ~BookmarkLoadDetails();
55 void LoadExtraNodes();
57 BookmarkPermanentNode* bb_node() { return bb_node_.get(); }
58 BookmarkPermanentNode* release_bb_node() { return bb_node_.release(); }
59 BookmarkPermanentNode* mobile_folder_node() {
60 return mobile_folder_node_.get();
62 BookmarkPermanentNode* release_mobile_folder_node() {
63 return mobile_folder_node_.release();
65 BookmarkPermanentNode* other_folder_node() {
66 return other_folder_node_.get();
68 BookmarkPermanentNode* release_other_folder_node() {
69 return other_folder_node_.release();
71 const BookmarkPermanentNodeList& extra_nodes() {
72 return extra_nodes_;
74 void release_extra_nodes(std::vector<BookmarkPermanentNode*>* extra_nodes) {
75 extra_nodes_.release(extra_nodes);
77 BookmarkIndex* index() { return index_.get(); }
78 BookmarkIndex* release_index() { return index_.release(); }
80 const BookmarkNode::MetaInfoMap& model_meta_info_map() const {
81 return model_meta_info_map_;
83 void set_model_meta_info_map(const BookmarkNode::MetaInfoMap& meta_info_map) {
84 model_meta_info_map_ = meta_info_map;
87 int64 model_sync_transaction_version() const {
88 return model_sync_transaction_version_;
90 void set_model_sync_transaction_version(int64 sync_transaction_version) {
91 model_sync_transaction_version_ = sync_transaction_version;
94 // Max id of the nodes.
95 void set_max_id(int64 max_id) { max_id_ = max_id; }
96 int64 max_id() const { return max_id_; }
98 // Computed checksum.
99 void set_computed_checksum(const std::string& value) {
100 computed_checksum_ = value;
102 const std::string& computed_checksum() const { return computed_checksum_; }
104 // Stored checksum.
105 void set_stored_checksum(const std::string& value) {
106 stored_checksum_ = value;
108 const std::string& stored_checksum() const { return stored_checksum_; }
110 // Whether ids were reassigned. IDs are reassigned during decoding if the
111 // checksum of the file doesn't match, some IDs are missing or not
112 // unique. Basically, if the user modified the bookmarks directly we'll
113 // reassign the ids to ensure they are unique.
114 void set_ids_reassigned(bool value) { ids_reassigned_ = value; }
115 bool ids_reassigned() const { return ids_reassigned_; }
117 private:
118 scoped_ptr<BookmarkPermanentNode> bb_node_;
119 scoped_ptr<BookmarkPermanentNode> other_folder_node_;
120 scoped_ptr<BookmarkPermanentNode> mobile_folder_node_;
121 LoadExtraCallback load_extra_callback_;
122 BookmarkPermanentNodeList extra_nodes_;
123 scoped_ptr<BookmarkIndex> index_;
124 BookmarkNode::MetaInfoMap model_meta_info_map_;
125 int64 model_sync_transaction_version_;
126 int64 max_id_;
127 std::string computed_checksum_;
128 std::string stored_checksum_;
129 bool ids_reassigned_;
131 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails);
134 // BookmarkStorage handles reading/write the bookmark bar model. The
135 // BookmarkModel uses the BookmarkStorage to load bookmarks from disk, as well
136 // as notifying the BookmarkStorage every time the model changes.
138 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write.
139 class BookmarkStorage : public base::ImportantFileWriter::DataSerializer {
140 public:
141 // Creates a BookmarkStorage for the specified model. The data will be loaded
142 // from and saved to a location derived from |profile_path|. The IO code will
143 // be executed as a task in |sequenced_task_runner|.
144 BookmarkStorage(BookmarkModel* model,
145 const base::FilePath& profile_path,
146 base::SequencedTaskRunner* sequenced_task_runner);
147 ~BookmarkStorage() override;
149 // Loads the bookmarks into the model, notifying the model when done. This
150 // takes ownership of |details| and send the |OnLoadFinished| callback from
151 // a task in |task_runner|. See BookmarkLoadDetails for details.
152 void LoadBookmarks(
153 scoped_ptr<BookmarkLoadDetails> details,
154 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
156 // Schedules saving the bookmark bar model to disk.
157 void ScheduleSave();
159 // Notification the bookmark bar model is going to be deleted. If there is
160 // a pending save, it is saved immediately.
161 void BookmarkModelDeleted();
163 // Callback from backend after loading the bookmark file.
164 void OnLoadFinished(scoped_ptr<BookmarkLoadDetails> details);
166 // ImportantFileWriter::DataSerializer implementation.
167 bool SerializeData(std::string* output) override;
169 private:
170 // The state of the bookmark file backup. We lazily backup this file in order
171 // to reduce disk writes until absolutely necessary. Will also leave the
172 // backup unchanged if the browser starts & quits w/o changing bookmarks.
173 enum BackupState {
174 // No attempt has yet been made to backup the bookmarks file.
175 BACKUP_NONE,
176 // A request to backup the bookmarks file has been posted, but not yet
177 // fulfilled.
178 BACKUP_DISPATCHED,
179 // The bookmarks file has been backed up (or at least attempted).
180 BACKUP_ATTEMPTED
183 // Serializes the data and schedules save using ImportantFileWriter.
184 // Returns true on successful serialization.
185 bool SaveNow();
187 // Callback from backend after creation of backup file.
188 void OnBackupFinished();
190 // The model. The model is NULL once BookmarkModelDeleted has been invoked.
191 BookmarkModel* model_;
193 // Helper to write bookmark data safely.
194 base::ImportantFileWriter writer_;
196 // The state of the backup file creation which is created lazily just before
197 // the first scheduled save.
198 BackupState backup_state_ = BACKUP_NONE;
200 // Sequenced task runner where file I/O operations will be performed at.
201 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
203 base::WeakPtrFactory<BookmarkStorage> weak_factory_;
205 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage);
208 } // namespace bookmarks
210 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_