OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_CODEC_H_ | |
6 #define COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_CODEC_H_ | |
7 | |
8 #include <set> | |
9 #include <string> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/md5.h" | |
13 #include "base/strings/string16.h" | |
14 #include "components/bookmarks/core/browser/bookmark_node.h" | |
15 | |
16 class BookmarkModel; | |
17 | |
18 namespace base { | |
19 class DictionaryValue; | |
20 class ListValue; | |
21 class Value; | |
22 } | |
23 | |
24 // BookmarkCodec is responsible for encoding and decoding the BookmarkModel | |
25 // into JSON values. The encoded values are written to disk via the | |
26 // BookmarkStorage. | |
27 class BookmarkCodec { | |
28 public: | |
29 // Creates an instance of the codec. During decoding, if the IDs in the file | |
30 // are not unique, we will reassign IDs to make them unique. There are no | |
31 // guarantees on how the IDs are reassigned or about doing minimal | |
32 // reassignments to achieve uniqueness. | |
33 BookmarkCodec(); | |
34 ~BookmarkCodec(); | |
35 | |
36 // Encodes the model to a JSON value. It's up to the caller to delete the | |
37 // returned object. This is invoked to encode the contents of the bookmark bar | |
38 // model and is currently a convenience to invoking Encode that takes the | |
39 // bookmark bar node and other folder node. | |
40 base::Value* Encode(BookmarkModel* model); | |
41 | |
42 // Encodes the bookmark bar and other folders returning the JSON value. It's | |
43 // up to the caller to delete the returned object. | |
44 base::Value* Encode(const BookmarkNode* bookmark_bar_node, | |
45 const BookmarkNode* other_folder_node, | |
46 const BookmarkNode* mobile_folder_node, | |
47 const BookmarkNode::MetaInfoMap* model_meta_info_map, | |
48 int64 sync_transaction_version); | |
49 | |
50 // Decodes the previously encoded value to the specified nodes as well as | |
51 // setting |max_node_id| to the greatest node id. Returns true on success, | |
52 // false otherwise. If there is an error (such as unexpected version) all | |
53 // children are removed from the bookmark bar and other folder nodes. On exit | |
54 // |max_node_id| is set to the max id of the nodes. | |
55 bool Decode(BookmarkNode* bb_node, | |
56 BookmarkNode* other_folder_node, | |
57 BookmarkNode* mobile_folder_node, | |
58 int64* max_node_id, | |
59 const base::Value& value); | |
60 | |
61 // Returns the checksum computed during last encoding/decoding call. | |
62 const std::string& computed_checksum() const { return computed_checksum_; } | |
63 | |
64 // Returns the checksum that's stored in the file. After a call to Encode, | |
65 // the computed and stored checksums are the same since the computed checksum | |
66 // is stored to the file. After a call to decode, the computed checksum can | |
67 // differ from the stored checksum if the file contents were changed by the | |
68 // user. | |
69 const std::string& stored_checksum() const { return stored_checksum_; } | |
70 | |
71 // Return meta info of bookmark model root. | |
72 const BookmarkNode::MetaInfoMap& model_meta_info_map() const { | |
73 return model_meta_info_map_; | |
74 } | |
75 | |
76 // Return the sync transaction version of the bookmark model root. | |
77 int64 model_sync_transaction_version() const { | |
78 return model_sync_transaction_version_; | |
79 } | |
80 | |
81 // Returns whether the IDs were reassigned during decoding. Always returns | |
82 // false after encoding. | |
83 bool ids_reassigned() const { return ids_reassigned_; } | |
84 | |
85 // Names of the various keys written to the Value. | |
86 static const char* kRootsKey; | |
87 static const char* kRootFolderNameKey; | |
88 static const char* kOtherBookmarkFolderNameKey; | |
89 static const char* kMobileBookmarkFolderNameKey; | |
90 static const char* kVersionKey; | |
91 static const char* kChecksumKey; | |
92 static const char* kIdKey; | |
93 static const char* kTypeKey; | |
94 static const char* kNameKey; | |
95 static const char* kDateAddedKey; | |
96 static const char* kURLKey; | |
97 static const char* kDateModifiedKey; | |
98 static const char* kChildrenKey; | |
99 static const char* kMetaInfo; | |
100 static const char* kSyncTransactionVersion; | |
101 | |
102 // Possible values for kTypeKey. | |
103 static const char* kTypeURL; | |
104 static const char* kTypeFolder; | |
105 | |
106 private: | |
107 // Encodes node and all its children into a Value object and returns it. | |
108 // The caller takes ownership of the returned object. | |
109 base::Value* EncodeNode(const BookmarkNode* node); | |
110 | |
111 // Encodes the given meta info into a Value object and returns it. The caller | |
112 // takes ownership of the returned object. | |
113 base::Value* EncodeMetaInfo(const BookmarkNode::MetaInfoMap& meta_info_map); | |
114 | |
115 // Helper to perform decoding. | |
116 bool DecodeHelper(BookmarkNode* bb_node, | |
117 BookmarkNode* other_folder_node, | |
118 BookmarkNode* mobile_folder_node, | |
119 const base::Value& value); | |
120 | |
121 // Decodes the children of the specified node. Returns true on success. | |
122 bool DecodeChildren(const base::ListValue& child_value_list, | |
123 BookmarkNode* parent); | |
124 | |
125 // Reassigns bookmark IDs for all nodes. | |
126 void ReassignIDs(BookmarkNode* bb_node, | |
127 BookmarkNode* other_node, | |
128 BookmarkNode* mobile_node); | |
129 | |
130 // Helper to recursively reassign IDs. | |
131 void ReassignIDsHelper(BookmarkNode* node); | |
132 | |
133 // Decodes the supplied node from the supplied value. Child nodes are | |
134 // created appropriately by way of DecodeChildren. If node is NULL a new | |
135 // node is created and added to parent (parent must then be non-NULL), | |
136 // otherwise node is used. | |
137 bool DecodeNode(const base::DictionaryValue& value, | |
138 BookmarkNode* parent, | |
139 BookmarkNode* node); | |
140 | |
141 // Decodes the meta info from the supplied value. If the meta info contains | |
142 // a "sync.transaction_version" key, the value of that field will be stored | |
143 // in the sync_transaction_version variable, then deleted. This is for | |
144 // backward-compatibility reasons. | |
145 // meta_info_map and sync_transaction_version must not be NULL. | |
146 bool DecodeMetaInfo(const base::DictionaryValue& value, | |
147 BookmarkNode::MetaInfoMap* meta_info_map, | |
148 int64* sync_transaction_version); | |
149 | |
150 // Decodes the meta info from the supplied sub-node dictionary. The values | |
151 // found will be inserted in meta_info_map with the given prefix added to the | |
152 // start of their keys. | |
153 void DecodeMetaInfoHelper(const base::DictionaryValue& dict, | |
154 const std::string& prefix, | |
155 BookmarkNode::MetaInfoMap* meta_info_map); | |
156 | |
157 // Updates the check-sum with the given string. | |
158 void UpdateChecksum(const std::string& str); | |
159 void UpdateChecksum(const base::string16& str); | |
160 | |
161 // Updates the check-sum with the given contents of URL/folder bookmark node. | |
162 // NOTE: These functions take in individual properties of a bookmark node | |
163 // instead of taking in a BookmarkNode for efficiency so that we don't convert | |
164 // various data-types to UTF16 strings multiple times - once for serializing | |
165 // and once for computing the check-sum. | |
166 // The url parameter should be a valid UTF8 string. | |
167 void UpdateChecksumWithUrlNode(const std::string& id, | |
168 const base::string16& title, | |
169 const std::string& url); | |
170 void UpdateChecksumWithFolderNode(const std::string& id, | |
171 const base::string16& title); | |
172 | |
173 // Initializes/Finalizes the checksum. | |
174 void InitializeChecksum(); | |
175 void FinalizeChecksum(); | |
176 | |
177 // Whether or not IDs were reassigned by the codec. | |
178 bool ids_reassigned_; | |
179 | |
180 // Whether or not IDs are valid. This is initially true, but set to false | |
181 // if an id is missing or not unique. | |
182 bool ids_valid_; | |
183 | |
184 // Contains the id of each of the nodes found in the file. Used to determine | |
185 // if we have duplicates. | |
186 std::set<int64> ids_; | |
187 | |
188 // MD5 context used to compute MD5 hash of all bookmark data. | |
189 base::MD5Context md5_context_; | |
190 | |
191 // Checksums. | |
192 std::string computed_checksum_; | |
193 std::string stored_checksum_; | |
194 | |
195 // Maximum ID assigned when decoding data. | |
196 int64 maximum_id_; | |
197 | |
198 // Meta info set on bookmark model root. | |
199 BookmarkNode::MetaInfoMap model_meta_info_map_; | |
200 | |
201 // Sync transaction version set on bookmark model root. | |
202 int64 model_sync_transaction_version_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); | |
205 }; | |
206 | |
207 #endif // COMPONENTS_BOOKMARKS_CORE_BROWSER_BOOKMARK_CODEC_H_ | |
OLD | NEW |