| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ | 5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ |
| 6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ | 6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ |
| 7 | 7 |
| 8 #include "base/callback_forward.h" | 8 #include "base/callback_forward.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/important_file_writer.h" | 10 #include "base/files/important_file_writer.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 14 #include "components/bookmarks/browser/bookmark_node.h" | 15 #include "components/bookmarks/browser/bookmark_node.h" |
| 15 | 16 |
| 16 class BookmarkModel; | 17 class BookmarkModel; |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace bookmarks { | 23 namespace bookmarks { |
| 23 | 24 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 bool ids_reassigned_; | 127 bool ids_reassigned_; |
| 127 | 128 |
| 128 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails); | 129 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails); |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 // BookmarkStorage handles reading/write the bookmark bar model. The | 132 // BookmarkStorage handles reading/write the bookmark bar model. The |
| 132 // BookmarkModel uses the BookmarkStorage to load bookmarks from disk, as well | 133 // BookmarkModel uses the BookmarkStorage to load bookmarks from disk, as well |
| 133 // as notifying the BookmarkStorage every time the model changes. | 134 // as notifying the BookmarkStorage every time the model changes. |
| 134 // | 135 // |
| 135 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write. | 136 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write. |
| 136 class BookmarkStorage : public base::ImportantFileWriter::DataSerializer, | 137 class BookmarkStorage : public base::ImportantFileWriter::DataSerializer { |
| 137 public base::RefCountedThreadSafe<BookmarkStorage> { | |
| 138 public: | 138 public: |
| 139 // Creates a BookmarkStorage for the specified model. The data will be loaded | 139 // Creates a BookmarkStorage for the specified model. The data will be loaded |
| 140 // from and saved to a location derived from |profile_path|. The IO code will | 140 // from and saved to a location derived from |profile_path|. The IO code will |
| 141 // be executed as a task in |sequenced_task_runner|. | 141 // be executed as a task in |sequenced_task_runner|. |
| 142 BookmarkStorage(BookmarkModel* model, | 142 BookmarkStorage(BookmarkModel* model, |
| 143 const base::FilePath& profile_path, | 143 const base::FilePath& profile_path, |
| 144 base::SequencedTaskRunner* sequenced_task_runner); | 144 base::SequencedTaskRunner* sequenced_task_runner); |
| 145 virtual ~BookmarkStorage(); |
| 145 | 146 |
| 146 // Loads the bookmarks into the model, notifying the model when done. This | 147 // Loads the bookmarks into the model, notifying the model when done. This |
| 147 // takes ownership of |details| and send the |OnLoadFinished| callback from | 148 // takes ownership of |details| and send the |OnLoadFinished| callback from |
| 148 // a task in |task_runner|. See BookmarkLoadDetails for details. | 149 // a task in |task_runner|. See BookmarkLoadDetails for details. |
| 149 void LoadBookmarks( | 150 void LoadBookmarks( |
| 150 scoped_ptr<BookmarkLoadDetails> details, | 151 scoped_ptr<BookmarkLoadDetails> details, |
| 151 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | 152 const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 152 | 153 |
| 153 // Schedules saving the bookmark bar model to disk. | 154 // Schedules saving the bookmark bar model to disk. |
| 154 void ScheduleSave(); | 155 void ScheduleSave(); |
| 155 | 156 |
| 156 // Notification the bookmark bar model is going to be deleted. If there is | 157 // Notification the bookmark bar model is going to be deleted. If there is |
| 157 // a pending save, it is saved immediately. | 158 // a pending save, it is saved immediately. |
| 158 void BookmarkModelDeleted(); | 159 void BookmarkModelDeleted(); |
| 159 | 160 |
| 160 // Callback from backend after loading the bookmark file. | 161 // Callback from backend after loading the bookmark file. |
| 161 void OnLoadFinished(); | 162 void OnLoadFinished(); |
| 162 | 163 |
| 163 // ImportantFileWriter::DataSerializer implementation. | 164 // ImportantFileWriter::DataSerializer implementation. |
| 164 virtual bool SerializeData(std::string* output) OVERRIDE; | 165 virtual bool SerializeData(std::string* output) OVERRIDE; |
| 165 | 166 |
| 166 private: | 167 private: |
| 167 friend class base::RefCountedThreadSafe<BookmarkStorage>; | |
| 168 | |
| 169 virtual ~BookmarkStorage(); | |
| 170 | |
| 171 // Serializes the data and schedules save using ImportantFileWriter. | 168 // Serializes the data and schedules save using ImportantFileWriter. |
| 172 // Returns true on successful serialization. | 169 // Returns true on successful serialization. |
| 173 bool SaveNow(); | 170 bool SaveNow(); |
| 174 | 171 |
| 175 // The model. The model is NULL once BookmarkModelDeleted has been invoked. | 172 // The model. The model is NULL once BookmarkModelDeleted has been invoked. |
| 176 BookmarkModel* model_; | 173 BookmarkModel* model_; |
| 177 | 174 |
| 178 // Helper to write bookmark data safely. | 175 // Helper to write bookmark data safely. |
| 179 base::ImportantFileWriter writer_; | 176 base::ImportantFileWriter writer_; |
| 180 | 177 |
| 181 // See class description of BookmarkLoadDetails for details on this. | 178 // See class description of BookmarkLoadDetails for details on this. |
| 182 scoped_ptr<BookmarkLoadDetails> details_; | 179 scoped_ptr<BookmarkLoadDetails> details_; |
| 183 | 180 |
| 184 // Sequenced task runner where file I/O operations will be performed at. | 181 // Sequenced task runner where file I/O operations will be performed at. |
| 185 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 182 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 186 | 183 |
| 184 base::WeakPtrFactory<BookmarkStorage> weak_factory_; |
| 185 |
| 187 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); | 186 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); |
| 188 }; | 187 }; |
| 189 | 188 |
| 190 } // namespace bookmarks | 189 } // namespace bookmarks |
| 191 | 190 |
| 192 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ | 191 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_STORAGE_H_ |
| OLD | NEW |