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