| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_STORAGE_H_ | |
| 6 #define COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_STORAGE_H_ | |
| 7 | |
| 8 #include "base/files/file_path.h" | |
| 9 #include "base/files/important_file_writer.h" | |
| 10 #include "base/memory/ref_counted.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "components/bookmarks/core/browser/bookmark_node.h" | |
| 13 | |
| 14 class BookmarkIndex; | |
| 15 class BookmarkModel; | |
| 16 | |
| 17 namespace base { | |
| 18 class SequencedTaskRunner; | |
| 19 } | |
| 20 | |
| 21 // BookmarkLoadDetails is used by BookmarkStorage when loading bookmarks. | |
| 22 // BookmarkModel creates a BookmarkLoadDetails and passes it (including | |
| 23 // ownership) to BookmarkStorage. BookmarkStorage loads the bookmarks (and | |
| 24 // index) in the background thread, then calls back to the BookmarkModel (on | |
| 25 // the main thread) when loading is done, passing ownership back to the | |
| 26 // BookmarkModel. While loading BookmarkModel does not maintain references to | |
| 27 // the contents of the BookmarkLoadDetails, this ensures we don't have any | |
| 28 // threading problems. | |
| 29 class BookmarkLoadDetails { | |
| 30 public: | |
| 31 BookmarkLoadDetails(BookmarkPermanentNode* bb_node, | |
| 32 BookmarkPermanentNode* other_folder_node, | |
| 33 BookmarkPermanentNode* mobile_folder_node, | |
| 34 BookmarkIndex* index, | |
| 35 int64 max_id); | |
| 36 ~BookmarkLoadDetails(); | |
| 37 | |
| 38 BookmarkPermanentNode* bb_node() { return bb_node_.get(); } | |
| 39 BookmarkPermanentNode* release_bb_node() { return bb_node_.release(); } | |
| 40 BookmarkPermanentNode* mobile_folder_node() { | |
| 41 return mobile_folder_node_.get(); | |
| 42 } | |
| 43 BookmarkPermanentNode* release_mobile_folder_node() { | |
| 44 return mobile_folder_node_.release(); | |
| 45 } | |
| 46 BookmarkPermanentNode* other_folder_node() { | |
| 47 return other_folder_node_.get(); | |
| 48 } | |
| 49 BookmarkPermanentNode* release_other_folder_node() { | |
| 50 return other_folder_node_.release(); | |
| 51 } | |
| 52 BookmarkIndex* index() { return index_.get(); } | |
| 53 BookmarkIndex* release_index() { return index_.release(); } | |
| 54 | |
| 55 const BookmarkNode::MetaInfoMap& model_meta_info_map() const { | |
| 56 return model_meta_info_map_; | |
| 57 } | |
| 58 void set_model_meta_info_map(const BookmarkNode::MetaInfoMap& meta_info_map) { | |
| 59 model_meta_info_map_ = meta_info_map; | |
| 60 } | |
| 61 | |
| 62 int64 model_sync_transaction_version() const { | |
| 63 return model_sync_transaction_version_; | |
| 64 } | |
| 65 void set_model_sync_transaction_version(int64 sync_transaction_version) { | |
| 66 model_sync_transaction_version_ = sync_transaction_version; | |
| 67 } | |
| 68 | |
| 69 // Max id of the nodes. | |
| 70 void set_max_id(int64 max_id) { max_id_ = max_id; } | |
| 71 int64 max_id() const { return max_id_; } | |
| 72 | |
| 73 // Computed checksum. | |
| 74 void set_computed_checksum(const std::string& value) { | |
| 75 computed_checksum_ = value; | |
| 76 } | |
| 77 const std::string& computed_checksum() const { return computed_checksum_; } | |
| 78 | |
| 79 // Stored checksum. | |
| 80 void set_stored_checksum(const std::string& value) { | |
| 81 stored_checksum_ = value; | |
| 82 } | |
| 83 const std::string& stored_checksum() const { return stored_checksum_; } | |
| 84 | |
| 85 // Whether ids were reassigned. IDs are reassigned during decoding if the | |
| 86 // checksum of the file doesn't match, some IDs are missing or not | |
| 87 // unique. Basically, if the user modified the bookmarks directly we'll | |
| 88 // reassign the ids to ensure they are unique. | |
| 89 void set_ids_reassigned(bool value) { ids_reassigned_ = value; } | |
| 90 bool ids_reassigned() const { return ids_reassigned_; } | |
| 91 | |
| 92 private: | |
| 93 scoped_ptr<BookmarkPermanentNode> bb_node_; | |
| 94 scoped_ptr<BookmarkPermanentNode> other_folder_node_; | |
| 95 scoped_ptr<BookmarkPermanentNode> mobile_folder_node_; | |
| 96 scoped_ptr<BookmarkIndex> index_; | |
| 97 BookmarkNode::MetaInfoMap model_meta_info_map_; | |
| 98 int64 model_sync_transaction_version_; | |
| 99 int64 max_id_; | |
| 100 std::string computed_checksum_; | |
| 101 std::string stored_checksum_; | |
| 102 bool ids_reassigned_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails); | |
| 105 }; | |
| 106 | |
| 107 // BookmarkStorage handles reading/write the bookmark bar model. The | |
| 108 // BookmarkModel uses the BookmarkStorage to load bookmarks from disk, as well | |
| 109 // as notifying the BookmarkStorage every time the model changes. | |
| 110 // | |
| 111 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write. | |
| 112 class BookmarkStorage : public base::ImportantFileWriter::DataSerializer, | |
| 113 public base::RefCountedThreadSafe<BookmarkStorage> { | |
| 114 public: | |
| 115 // Creates a BookmarkStorage for the specified model. The data will be loaded | |
| 116 // from and saved to a location derived from |profile_path|. The IO code will | |
| 117 // be executed as a task in |sequenced_task_runner|. | |
| 118 BookmarkStorage(BookmarkModel* model, | |
| 119 const base::FilePath& profile_path, | |
| 120 base::SequencedTaskRunner* sequenced_task_runner); | |
| 121 | |
| 122 // Loads the bookmarks into the model, notifying the model when done. This | |
| 123 // takes ownership of |details| and send the |OnLoadFinished| callback from | |
| 124 // a task in |task_runner|. See BookmarkLoadDetails for details. | |
| 125 void LoadBookmarks( | |
| 126 scoped_ptr<BookmarkLoadDetails> details, | |
| 127 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
| 128 | |
| 129 // Schedules saving the bookmark bar model to disk. | |
| 130 void ScheduleSave(); | |
| 131 | |
| 132 // Notification the bookmark bar model is going to be deleted. If there is | |
| 133 // a pending save, it is saved immediately. | |
| 134 void BookmarkModelDeleted(); | |
| 135 | |
| 136 // Callback from backend after loading the bookmark file. | |
| 137 void OnLoadFinished(); | |
| 138 | |
| 139 // ImportantFileWriter::DataSerializer implementation. | |
| 140 virtual bool SerializeData(std::string* output) OVERRIDE; | |
| 141 | |
| 142 private: | |
| 143 friend class base::RefCountedThreadSafe<BookmarkStorage>; | |
| 144 | |
| 145 virtual ~BookmarkStorage(); | |
| 146 | |
| 147 // Serializes the data and schedules save using ImportantFileWriter. | |
| 148 // Returns true on successful serialization. | |
| 149 bool SaveNow(); | |
| 150 | |
| 151 // The model. The model is NULL once BookmarkModelDeleted has been invoked. | |
| 152 BookmarkModel* model_; | |
| 153 | |
| 154 // Helper to write bookmark data safely. | |
| 155 base::ImportantFileWriter writer_; | |
| 156 | |
| 157 // See class description of BookmarkLoadDetails for details on this. | |
| 158 scoped_ptr<BookmarkLoadDetails> details_; | |
| 159 | |
| 160 // Sequenced task runner where file I/O operations will be performed at. | |
| 161 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | |
| 162 | |
| 163 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); | |
| 164 }; | |
| 165 | |
| 166 #endif // COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_STORAGE_H_ | |
| OLD | NEW |