OLD | NEW |
| (Empty) |
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/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 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. The data will be loaded | |
120 // from and saved to a location derived from |profile_path|. The IO code will | |
121 // be executed as a task in |sequenced_task_runner|. | |
122 BookmarkStorage(BookmarkModel* model, | |
123 const base::FilePath& profile_path, | |
124 base::SequencedTaskRunner* sequenced_task_runner); | |
125 | |
126 // Loads the bookmarks into the model, notifying the model when done. This | |
127 // takes ownership of |details| and send the |OnLoadFinished| callback from | |
128 // a task in |task_runner|. See BookmarkLoadDetails for details. | |
129 void LoadBookmarks( | |
130 scoped_ptr<BookmarkLoadDetails> details, | |
131 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
132 | |
133 // Schedules saving the bookmark bar model to disk. | |
134 void ScheduleSave(); | |
135 | |
136 // Notification the bookmark bar model is going to be deleted. If there is | |
137 // a pending save, it is saved immediately. | |
138 void BookmarkModelDeleted(); | |
139 | |
140 // Callback from backend after loading the bookmark file. | |
141 void OnLoadFinished(); | |
142 | |
143 // ImportantFileWriter::DataSerializer implementation. | |
144 virtual bool SerializeData(std::string* output) OVERRIDE; | |
145 | |
146 private: | |
147 friend class base::RefCountedThreadSafe<BookmarkStorage>; | |
148 | |
149 virtual ~BookmarkStorage(); | |
150 | |
151 // Serializes the data and schedules save using ImportantFileWriter. | |
152 // Returns true on successful serialization. | |
153 bool SaveNow(); | |
154 | |
155 // The model. The model is NULL once BookmarkModelDeleted has been invoked. | |
156 BookmarkModel* model_; | |
157 | |
158 // Helper to write bookmark data safely. | |
159 base::ImportantFileWriter writer_; | |
160 | |
161 // See class description of BookmarkLoadDetails for details on this. | |
162 scoped_ptr<BookmarkLoadDetails> details_; | |
163 | |
164 // Sequenced task runner where file I/O operations will be performed at. | |
165 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(BookmarkStorage); | |
168 }; | |
169 | |
170 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_STORAGE_H_ | |
OLD | NEW |