| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 11 |
| 12 #include <set> | |
| 13 #include <string> | 12 #include <string> |
| 14 | 13 |
| 15 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 16 #include "base/scoped_ptr.h" | 15 #include "base/scoped_ptr.h" |
| 17 #include "base/md5.h" | 16 #include "base/md5.h" |
| 18 | 17 |
| 19 class BookmarkModel; | 18 class BookmarkModel; |
| 20 class BookmarkNode; | 19 class BookmarkNode; |
| 21 class DictionaryValue; | 20 class DictionaryValue; |
| 22 class ListValue; | 21 class ListValue; |
| 23 class Value; | 22 class Value; |
| 24 | 23 |
| 25 // Utility class to help assign unique 64-bit IDs. | |
| 26 class UniqueIDGenerator { | |
| 27 public: | |
| 28 UniqueIDGenerator(); | |
| 29 | |
| 30 // Checks whether the given ID can be used as a unique ID or not. If it can, | |
| 31 // returns the id itself, otherwise generates a new unique id in a simple way | |
| 32 // and returns that. | |
| 33 // NOTE that if id is 0, a new unique id is returned. | |
| 34 int64 GetUniqueID(int64 id); | |
| 35 | |
| 36 // Resets the ID generator to initial state. | |
| 37 void Reset(); | |
| 38 | |
| 39 // Returns the current maximum. | |
| 40 int64 current_max() const { return current_max_; } | |
| 41 | |
| 42 private: | |
| 43 // Checks if the given ID is already assigned. | |
| 44 bool IsIdAssigned(int64 id) const; | |
| 45 | |
| 46 // Records the given ID as assigned. | |
| 47 void RecordId(int64 id); | |
| 48 | |
| 49 // Maximum value we have seen so far. | |
| 50 int64 current_max_; | |
| 51 | |
| 52 // All IDs assigned so far. | |
| 53 scoped_ptr<std::set<int64> > assigned_ids_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(UniqueIDGenerator); | |
| 56 }; | |
| 57 | |
| 58 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON | 24 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON |
| 59 // values. BookmarkCodec is used by BookmarkService. | 25 // values. BookmarkCodec is used by BookmarkService. |
| 60 | 26 |
| 61 class BookmarkCodec { | 27 class BookmarkCodec { |
| 62 public: | 28 public: |
| 63 // Creates an instance of the codec. During decoding, if the IDs in the file | 29 // Creates an instance of the codec. During decoding, if the IDs in the file |
| 64 // are not unique, we will reassign IDs to make them unique. There are no | 30 // are not unique, we will reassign IDs to make them unique. There are no |
| 65 // guarantees on how the IDs are reassigned or about doing minimal | 31 // guarantees on how the IDs are reassigned or about doing minimal |
| 66 // reassignments to achieve uniqueness. | 32 // reassignments to achieve uniqueness. |
| 67 BookmarkCodec(); | 33 BookmarkCodec(); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 94 |
| 129 // Helper to perform decoding. | 95 // Helper to perform decoding. |
| 130 bool DecodeHelper(BookmarkNode* bb_node, | 96 bool DecodeHelper(BookmarkNode* bb_node, |
| 131 BookmarkNode* other_folder_node, | 97 BookmarkNode* other_folder_node, |
| 132 const Value& value); | 98 const Value& value); |
| 133 | 99 |
| 134 // Decodes the children of the specified node. Returns true on success. | 100 // Decodes the children of the specified node. Returns true on success. |
| 135 bool DecodeChildren(const ListValue& child_value_list, | 101 bool DecodeChildren(const ListValue& child_value_list, |
| 136 BookmarkNode* parent); | 102 BookmarkNode* parent); |
| 137 | 103 |
| 104 // Reassigns bookmark IDs for all nodes. |
| 105 void ReassignIDs(BookmarkNode* bb_node, BookmarkNode* other_node); |
| 106 |
| 107 // Helper to recursively reassign IDs. |
| 108 void ReassignIDsHelper(BookmarkNode* node); |
| 109 |
| 138 // Decodes the supplied node from the supplied value. Child nodes are | 110 // Decodes the supplied node from the supplied value. Child nodes are |
| 139 // created appropriately by way of DecodeChildren. If node is NULL a new | 111 // created appropriately by way of DecodeChildren. If node is NULL a new |
| 140 // node is created and added to parent, otherwise node is used. | 112 // node is created and added to parent, otherwise node is used. |
| 141 bool DecodeNode(const DictionaryValue& value, | 113 bool DecodeNode(const DictionaryValue& value, |
| 142 BookmarkNode* parent, | 114 BookmarkNode* parent, |
| 143 BookmarkNode* node); | 115 BookmarkNode* node); |
| 144 | 116 |
| 145 // Updates the check-sum with the given string. | 117 // Updates the check-sum with the given string. |
| 146 void UpdateChecksum(const std::string& str); | 118 void UpdateChecksum(const std::string& str); |
| 147 void UpdateChecksum(const std::wstring& str); | 119 void UpdateChecksum(const std::wstring& str); |
| 148 | 120 |
| 149 // Updates the check-sum with the given contents of URL/folder bookmark node. | 121 // Updates the check-sum with the given contents of URL/folder bookmark node. |
| 150 // NOTE: These functions take in individual properties of a bookmark node | 122 // NOTE: These functions take in individual properties of a bookmark node |
| 151 // instead of taking in a BookmarkNode for efficiency so that we don't convert | 123 // instead of taking in a BookmarkNode for efficiency so that we don't convert |
| 152 // varous data-types to wide strings multiple times - once for serializing | 124 // varous data-types to wide strings multiple times - once for serializing |
| 153 // and once for computing the check-sum. | 125 // and once for computing the check-sum. |
| 154 void UpdateChecksumWithUrlNode(const std::string& id, | 126 void UpdateChecksumWithUrlNode(const std::string& id, |
| 155 const std::wstring& title, | 127 const std::wstring& title, |
| 156 const std::wstring& url); | 128 const std::wstring& url); |
| 157 void UpdateChecksumWithFolderNode(const std::string& id, | 129 void UpdateChecksumWithFolderNode(const std::string& id, |
| 158 const std::wstring& title); | 130 const std::wstring& title); |
| 159 | 131 |
| 160 // Initializes/Finalizes the checksum. | 132 // Initializes/Finalizes the checksum. |
| 161 void InitializeChecksum(); | 133 void InitializeChecksum(); |
| 162 void FinalizeChecksum(); | 134 void FinalizeChecksum(); |
| 163 | 135 |
| 164 // Unique ID generator used during decoding. | |
| 165 UniqueIDGenerator id_generator_; | |
| 166 | |
| 167 // Whether or not IDs were reassigned by the codec. | 136 // Whether or not IDs were reassigned by the codec. |
| 168 bool ids_reassigned_; | 137 bool ids_reassigned_; |
| 169 | 138 |
| 139 // Whether or not IDs were missing for some bookmark nodes during decoding. |
| 140 bool ids_missing_; |
| 141 |
| 170 // MD5 context used to compute MD5 hash of all bookmark data. | 142 // MD5 context used to compute MD5 hash of all bookmark data. |
| 171 MD5Context md5_context_; | 143 MD5Context md5_context_; |
| 172 | 144 |
| 173 // Checksums. | 145 // Checksums. |
| 174 std::string computed_checksum_; | 146 std::string computed_checksum_; |
| 175 std::string stored_checksum_; | 147 std::string stored_checksum_; |
| 176 | 148 |
| 149 // Maximum ID assigned when decoding data. |
| 150 int64 maximum_id_; |
| 151 |
| 177 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); | 152 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); |
| 178 }; | 153 }; |
| 179 | 154 |
| 180 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ | 155 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ |
| OLD | NEW |