OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // BookmarkCodec is responsible for encoding and decoding the BookmarkModel | 5 // BookmarkCodec is responsible for encoding and decoding the BookmarkModel |
6 // into JSON values. The encoded values are written to disk via the | 6 // into JSON values. The encoded values are written to disk via the |
7 // BookmarkService. | 7 // BookmarkService. |
8 | 8 |
9 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ | 9 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ |
10 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ | 10 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ |
11 #pragma once | 11 #pragma once |
12 | 12 |
13 #include <set> | 13 #include <set> |
14 #include <string> | 14 #include <string> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/md5.h" | 17 #include "base/md5.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 | 19 |
20 class BookmarkModel; | 20 class BookmarkModel; |
21 class BookmarkNode; | 21 class BookmarkNode; |
| 22 |
| 23 namespace base { |
22 class DictionaryValue; | 24 class DictionaryValue; |
23 class ListValue; | 25 class ListValue; |
24 class Value; | 26 class Value; |
| 27 } |
25 | 28 |
26 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON | 29 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON |
27 // values. BookmarkCodec is used by BookmarkService. | 30 // values. BookmarkCodec is used by BookmarkService. |
28 | 31 |
29 class BookmarkCodec { | 32 class BookmarkCodec { |
30 public: | 33 public: |
31 // Creates an instance of the codec. During decoding, if the IDs in the file | 34 // Creates an instance of the codec. During decoding, if the IDs in the file |
32 // are not unique, we will reassign IDs to make them unique. There are no | 35 // are not unique, we will reassign IDs to make them unique. There are no |
33 // guarantees on how the IDs are reassigned or about doing minimal | 36 // guarantees on how the IDs are reassigned or about doing minimal |
34 // reassignments to achieve uniqueness. | 37 // reassignments to achieve uniqueness. |
35 BookmarkCodec(); | 38 BookmarkCodec(); |
36 ~BookmarkCodec(); | 39 ~BookmarkCodec(); |
37 | 40 |
38 // Encodes the model to a JSON value. It's up to the caller to delete the | 41 // Encodes the model to a JSON value. It's up to the caller to delete the |
39 // returned object. This is invoked to encode the contents of the bookmark bar | 42 // returned object. This is invoked to encode the contents of the bookmark bar |
40 // model and is currently a convenience to invoking Encode that takes the | 43 // model and is currently a convenience to invoking Encode that takes the |
41 // bookmark bar node and other folder node. | 44 // bookmark bar node and other folder node. |
42 Value* Encode(BookmarkModel* model); | 45 base::Value* Encode(BookmarkModel* model); |
43 | 46 |
44 // Encodes the bookmark bar and other folders returning the JSON value. It's | 47 // Encodes the bookmark bar and other folders returning the JSON value. It's |
45 // up to the caller to delete the returned object. | 48 // up to the caller to delete the returned object. |
46 // This method is public for use by StarredURLDatabase in migrating the | 49 // This method is public for use by StarredURLDatabase in migrating the |
47 // bookmarks out of the database. | 50 // bookmarks out of the database. |
48 Value* Encode(const BookmarkNode* bookmark_bar_node, | 51 base::Value* Encode(const BookmarkNode* bookmark_bar_node, |
49 const BookmarkNode* other_folder_node, | 52 const BookmarkNode* other_folder_node, |
50 const BookmarkNode* synced_folder_node); | 53 const BookmarkNode* synced_folder_node); |
51 | 54 |
52 // Decodes the previously encoded value to the specified nodes as well as | 55 // Decodes the previously encoded value to the specified nodes as well as |
53 // setting |max_node_id| to the greatest node id. Returns true on success, | 56 // setting |max_node_id| to the greatest node id. Returns true on success, |
54 // false otherwise. If there is an error (such as unexpected version) all | 57 // false otherwise. If there is an error (such as unexpected version) all |
55 // children are removed from the bookmark bar and other folder nodes. On exit | 58 // children are removed from the bookmark bar and other folder nodes. On exit |
56 // |max_node_id| is set to the max id of the nodes. | 59 // |max_node_id| is set to the max id of the nodes. |
57 bool Decode(BookmarkNode* bb_node, | 60 bool Decode(BookmarkNode* bb_node, |
58 BookmarkNode* other_folder_node, | 61 BookmarkNode* other_folder_node, |
59 BookmarkNode* synced_folder_node, | 62 BookmarkNode* synced_folder_node, |
60 int64* max_node_id, | 63 int64* max_node_id, |
61 const Value& value); | 64 const base::Value& value); |
62 | 65 |
63 // Returns the checksum computed during last encoding/decoding call. | 66 // Returns the checksum computed during last encoding/decoding call. |
64 const std::string& computed_checksum() const { return computed_checksum_; } | 67 const std::string& computed_checksum() const { return computed_checksum_; } |
65 | 68 |
66 // Returns the checksum that's stored in the file. After a call to Encode, | 69 // Returns the checksum that's stored in the file. After a call to Encode, |
67 // the computed and stored checksums are the same since the computed checksum | 70 // the computed and stored checksums are the same since the computed checksum |
68 // is stored to the file. After a call to decode, the computed checksum can | 71 // is stored to the file. After a call to decode, the computed checksum can |
69 // differ from the stored checksum if the file contents were changed by the | 72 // differ from the stored checksum if the file contents were changed by the |
70 // user. | 73 // user. |
71 const std::string& stored_checksum() const { return stored_checksum_; } | 74 const std::string& stored_checksum() const { return stored_checksum_; } |
(...skipping 17 matching lines...) Expand all Loading... |
89 static const char* kDateModifiedKey; | 92 static const char* kDateModifiedKey; |
90 static const char* kChildrenKey; | 93 static const char* kChildrenKey; |
91 | 94 |
92 // Possible values for kTypeKey. | 95 // Possible values for kTypeKey. |
93 static const char* kTypeURL; | 96 static const char* kTypeURL; |
94 static const char* kTypeFolder; | 97 static const char* kTypeFolder; |
95 | 98 |
96 private: | 99 private: |
97 // Encodes node and all its children into a Value object and returns it. | 100 // Encodes node and all its children into a Value object and returns it. |
98 // The caller takes ownership of the returned object. | 101 // The caller takes ownership of the returned object. |
99 Value* EncodeNode(const BookmarkNode* node); | 102 base::Value* EncodeNode(const BookmarkNode* node); |
100 | 103 |
101 // Helper to perform decoding. | 104 // Helper to perform decoding. |
102 bool DecodeHelper(BookmarkNode* bb_node, | 105 bool DecodeHelper(BookmarkNode* bb_node, |
103 BookmarkNode* other_folder_node, | 106 BookmarkNode* other_folder_node, |
104 BookmarkNode* synced_folder_node, | 107 BookmarkNode* synced_folder_node, |
105 const Value& value); | 108 const base::Value& value); |
106 | 109 |
107 // Decodes the children of the specified node. Returns true on success. | 110 // Decodes the children of the specified node. Returns true on success. |
108 bool DecodeChildren(const ListValue& child_value_list, | 111 bool DecodeChildren(const base::ListValue& child_value_list, |
109 BookmarkNode* parent); | 112 BookmarkNode* parent); |
110 | 113 |
111 // Reassigns bookmark IDs for all nodes. | 114 // Reassigns bookmark IDs for all nodes. |
112 void ReassignIDs(BookmarkNode* bb_node, | 115 void ReassignIDs(BookmarkNode* bb_node, |
113 BookmarkNode* other_node, | 116 BookmarkNode* other_node, |
114 BookmarkNode* synced_node); | 117 BookmarkNode* synced_node); |
115 | 118 |
116 // Helper to recursively reassign IDs. | 119 // Helper to recursively reassign IDs. |
117 void ReassignIDsHelper(BookmarkNode* node); | 120 void ReassignIDsHelper(BookmarkNode* node); |
118 | 121 |
119 // Decodes the supplied node from the supplied value. Child nodes are | 122 // Decodes the supplied node from the supplied value. Child nodes are |
120 // created appropriately by way of DecodeChildren. If node is NULL a new | 123 // created appropriately by way of DecodeChildren. If node is NULL a new |
121 // node is created and added to parent (parent must then be non-NULL), | 124 // node is created and added to parent (parent must then be non-NULL), |
122 // otherwise node is used. | 125 // otherwise node is used. |
123 bool DecodeNode(const DictionaryValue& value, | 126 bool DecodeNode(const base::DictionaryValue& value, |
124 BookmarkNode* parent, | 127 BookmarkNode* parent, |
125 BookmarkNode* node); | 128 BookmarkNode* node); |
126 | 129 |
127 // Updates the check-sum with the given string. | 130 // Updates the check-sum with the given string. |
128 void UpdateChecksum(const std::string& str); | 131 void UpdateChecksum(const std::string& str); |
129 void UpdateChecksum(const string16& str); | 132 void UpdateChecksum(const string16& str); |
130 | 133 |
131 // Updates the check-sum with the given contents of URL/folder bookmark node. | 134 // Updates the check-sum with the given contents of URL/folder bookmark node. |
132 // NOTE: These functions take in individual properties of a bookmark node | 135 // NOTE: These functions take in individual properties of a bookmark node |
133 // instead of taking in a BookmarkNode for efficiency so that we don't convert | 136 // instead of taking in a BookmarkNode for efficiency so that we don't convert |
(...skipping 28 matching lines...) Expand all Loading... |
162 std::string computed_checksum_; | 165 std::string computed_checksum_; |
163 std::string stored_checksum_; | 166 std::string stored_checksum_; |
164 | 167 |
165 // Maximum ID assigned when decoding data. | 168 // Maximum ID assigned when decoding data. |
166 int64 maximum_id_; | 169 int64 maximum_id_; |
167 | 170 |
168 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); | 171 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); |
169 }; | 172 }; |
170 | 173 |
171 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ | 174 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ |
OLD | NEW |