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 #pragma once 8 9 #include "base/file_path.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "chrome/browser/bookmarks/bookmark_index.h" 13 #include "chrome/common/important_file_writer.h" 14 #include "content/common/notification_observer.h" 15 #include "content/common/notification_registrar.h" 16 17 class BookmarkModel; 18 class BookmarkNode; 19 class Profile; 20 21 // BookmarkLoadDetails is used by BookmarkStorage when loading bookmarks. 22 // BookmarkModel creates a BookmarkLoadDetails and passes it (including 23 // ownership) to BookmarkStorage. BoomarkStorage loads the bookmarks (and index) 24 // in the background thread, then calls back to the BookmarkModel (on the main 25 // thread) when loading is done, passing ownership back to the BookmarkModel. 26 // While loading BookmarkModel does not maintain references to the contents 27 // of the BookmarkLoadDetails, this ensures we don't have any threading 28 // problems. 29 class BookmarkLoadDetails { 30 public: 31 BookmarkLoadDetails(BookmarkNode* bb_node, 32 BookmarkNode* other_folder_node, 33 BookmarkIndex* index, 34 int64 max_id); 35 ~BookmarkLoadDetails(); 36 bb_node()37 BookmarkNode* bb_node() { return bb_node_.get(); } release_bb_node()38 BookmarkNode* release_bb_node() { return bb_node_.release(); } other_folder_node()39 BookmarkNode* other_folder_node() { return other_folder_node_.get(); } release_other_folder_node()40 BookmarkNode* release_other_folder_node() { 41 return other_folder_node_.release(); 42 } index()43 BookmarkIndex* index() { return index_.get(); } release_index()44 BookmarkIndex* release_index() { return index_.release(); } 45 46 // Max id of the nodes. set_max_id(int64 max_id)47 void set_max_id(int64 max_id) { max_id_ = max_id; } max_id()48 int64 max_id() const { return max_id_; } 49 50 // Computed checksum. set_computed_checksum(const std::string & value)51 void set_computed_checksum(const std::string& value) { 52 computed_checksum_ = value; 53 } computed_checksum()54 const std::string& computed_checksum() const { return computed_checksum_; } 55 56 // Stored checksum. set_stored_checksum(const std::string & value)57 void set_stored_checksum(const std::string& value) { 58 stored_checksum_ = value; 59 } stored_checksum()60 const std::string& stored_checksum() const { return stored_checksum_; } 61 62 // Whether ids were reassigned. set_ids_reassigned(bool value)63 void set_ids_reassigned(bool value) { ids_reassigned_ = value; } ids_reassigned()64 bool ids_reassigned() const { return ids_reassigned_; } 65 66 private: 67 scoped_ptr<BookmarkNode> bb_node_; 68 scoped_ptr<BookmarkNode> other_folder_node_; 69 scoped_ptr<BookmarkIndex> index_; 70 int64 max_id_; 71 std::string computed_checksum_; 72 std::string stored_checksum_; 73 bool ids_reassigned_; 74 75 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails); 76 }; 77 78 // BookmarkStorage handles reading/write the bookmark bar model. The 79 // BookmarkModel uses the BookmarkStorage to load bookmarks from disk, as well 80 // as notifying the BookmarkStorage every time the model changes. 81 // 82 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write. 83 class BookmarkStorage : public NotificationObserver, 84 public ImportantFileWriter::DataSerializer, 85 public base::RefCountedThreadSafe<BookmarkStorage> { 86 public: 87 // Creates a BookmarkStorage for the specified model 88 BookmarkStorage(Profile* profile, BookmarkModel* model); 89 90 // Loads the bookmarks into the model, notifying the model when done. This 91 // takes ownership of |details|. See BookmarkLoadDetails for details. 92 void LoadBookmarks(BookmarkLoadDetails* details); 93 94 // Schedules saving the bookmark bar model to disk. 95 void ScheduleSave(); 96 97 // Notification the bookmark bar model is going to be deleted. If there is 98 // a pending save, it is saved immediately. 99 void BookmarkModelDeleted(); 100 101 // ImportantFileWriter::DataSerializer 102 virtual bool SerializeData(std::string* output); 103 104 private: 105 friend class base::RefCountedThreadSafe<BookmarkStorage>; 106 107 ~BookmarkStorage(); 108 109 class LoadTask; 110 111 // Callback from backend with the results of the bookmark file. 112 // This may be called multiple times, with different paths. This happens when 113 // we migrate bookmark data from database. 114 void OnLoadFinished(bool file_exists, 115 const FilePath& path); 116 117 // Loads bookmark data from |file| and notifies the model when finished. 118 void DoLoadBookmarks(const FilePath& file); 119 120 // Load bookmarks data from the file written by history (StarredURLDatabase). 121 void MigrateFromHistory(); 122 123 // Called when history has written the file with bookmarks data. Loads data 124 // from that file. 125 void OnHistoryFinishedWriting(); 126 127 // Called after we loaded file generated by history. Saves the data, deletes 128 // the temporary file, and notifies the model. 129 void FinishHistoryMigration(); 130 131 // NotificationObserver 132 virtual void Observe(NotificationType type, 133 const NotificationSource& source, 134 const NotificationDetails& details); 135 136 // Serializes the data and schedules save using ImportantFileWriter. 137 // Returns true on successful serialization. 138 bool SaveNow(); 139 140 // Keep the pointer to profile, we may need it for migration from history. 141 Profile* profile_; 142 143 // The model. The model is NULL once BookmarkModelDeleted has been invoked. 144 BookmarkModel* model_; 145 146 // Helper to write bookmark data safely. 147 ImportantFileWriter writer_; 148 149 // Helper to ensure that we unregister from notifications on destruction. 150 NotificationRegistrar notification_registrar_; 151 152 // Path to temporary file created during migrating bookmarks from history. 153 const FilePath tmp_history_path_; 154 155 // See class description of BookmarkLoadDetails for details on this. 156 scoped_ptr<BookmarkLoadDetails> details_; 157 158 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); 159 }; 160 161 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_STORAGE_H_ 162