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