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