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.
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_STORAGE_H_
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_STORAGE_H_
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"
15 class BookmarkPermanentNode
;
18 class SequencedTaskRunner
;
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
{
35 BookmarkLoadDetails(BookmarkPermanentNode
* bb_node
,
36 BookmarkPermanentNode
* other_folder_node
,
37 BookmarkPermanentNode
* mobile_folder_node
,
40 ~BookmarkLoadDetails();
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();
47 BookmarkPermanentNode
* release_mobile_folder_node() {
48 return mobile_folder_node_
.release();
50 BookmarkPermanentNode
* other_folder_node() {
51 return other_folder_node_
.get();
53 BookmarkPermanentNode
* release_other_folder_node() {
54 return other_folder_node_
.release();
56 BookmarkIndex
* index() { return index_
.get(); }
57 BookmarkIndex
* release_index() { return index_
.release(); }
59 const BookmarkNode::MetaInfoMap
& model_meta_info_map() const {
60 return model_meta_info_map_
;
62 void set_model_meta_info_map(const BookmarkNode::MetaInfoMap
& meta_info_map
) {
63 model_meta_info_map_
= meta_info_map
;
66 int64
model_sync_transaction_version() const {
67 return model_sync_transaction_version_
;
69 void set_model_sync_transaction_version(int64 sync_transaction_version
) {
70 model_sync_transaction_version_
= sync_transaction_version
;
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_
; }
78 void set_computed_checksum(const std::string
& value
) {
79 computed_checksum_
= value
;
81 const std::string
& computed_checksum() const { return computed_checksum_
; }
84 void set_stored_checksum(const std::string
& value
) {
85 stored_checksum_
= value
;
87 const std::string
& stored_checksum() const { return stored_checksum_
; }
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_
; }
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_
;
104 std::string computed_checksum_
;
105 std::string stored_checksum_
;
106 bool ids_reassigned_
;
108 DISALLOW_COPY_AND_ASSIGN(BookmarkLoadDetails
);
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.
115 // Internally BookmarkStorage uses BookmarkCodec to do the actual read/write.
116 class BookmarkStorage
: public base::ImportantFileWriter::DataSerializer
,
117 public base::RefCountedThreadSafe
<BookmarkStorage
> {
119 // Creates a BookmarkStorage for the specified model
120 BookmarkStorage(content::BrowserContext
* context
,
121 BookmarkModel
* model
,
122 base::SequencedTaskRunner
* sequenced_task_runner
);
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
);
128 // Schedules saving the bookmark bar model to disk.
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();
135 // Callback from backend after loading the bookmark file.
136 void OnLoadFinished();
138 // ImportantFileWriter::DataSerializer implementation.
139 virtual bool SerializeData(std::string
* output
) OVERRIDE
;
142 friend class base::RefCountedThreadSafe
<BookmarkStorage
>;
144 virtual ~BookmarkStorage();
146 // Serializes the data and schedules save using ImportantFileWriter.
147 // Returns true on successful serialization.
150 // The model. The model is NULL once BookmarkModelDeleted has been invoked.
151 BookmarkModel
* model_
;
153 // Helper to write bookmark data safely.
154 base::ImportantFileWriter writer_
;
156 // See class description of BookmarkLoadDetails for details on this.
157 scoped_ptr
<BookmarkLoadDetails
> details_
;
159 // Sequenced task runner where file I/O operations will be performed at.
160 scoped_refptr
<base::SequencedTaskRunner
> sequenced_task_runner_
;
162 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage
);
165 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_STORAGE_H_