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> |
12 #include <string> | 13 #include <string> |
13 | 14 |
14 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
15 #include "base/md5.h" | 16 #include "base/md5.h" |
16 | 17 |
17 class BookmarkModel; | 18 class BookmarkModel; |
18 class BookmarkNode; | 19 class BookmarkNode; |
19 class DictionaryValue; | 20 class DictionaryValue; |
20 class ListValue; | 21 class ListValue; |
21 class Value; | 22 class Value; |
22 | 23 |
| 24 // Utility class to help assign unique integer IDs. |
| 25 class UniqueIDGenerator { |
| 26 public: |
| 27 UniqueIDGenerator(); |
| 28 |
| 29 // Checks whether the given ID can be used as a unique ID or not. If it can, |
| 30 // returns the id itself, otherwise generates a new unique id in a simple way |
| 31 // and returns that. |
| 32 // NOTE that if id is 0, a new unique id is returned. |
| 33 int GetUniqueID(int id); |
| 34 |
| 35 // Resets the ID generator to initial state. |
| 36 void Reset(); |
| 37 |
| 38 // Returns the current maximum. |
| 39 int current_max() const { return current_max_; } |
| 40 |
| 41 private: |
| 42 // Maximum value we have seen so far. |
| 43 int current_max_; |
| 44 // All IDs assigned so far. |
| 45 std::set<int> assigned_ids_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(UniqueIDGenerator); |
| 48 }; |
| 49 |
23 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON | 50 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON |
24 // values. BookmarkCodec is used by BookmarkService. | 51 // values. BookmarkCodec is used by BookmarkService. |
25 | 52 |
26 class BookmarkCodec { | 53 class BookmarkCodec { |
27 public: | 54 public: |
28 BookmarkCodec() {} | 55 // Creates an instance of the codec. Encodes/decodes bookmark IDs also if |
| 56 // persist_ids is true. The default constructor will not encode/decode IDs. |
| 57 // During decoding, if persist_ids is true and if the IDs in the file are not |
| 58 // unique, we will reassign IDs to make them unique. There are no guarantees |
| 59 // on how the IDs are reassigned or about doing minimal reassignments to |
| 60 // achieve uniqueness. |
| 61 BookmarkCodec(); |
| 62 explicit BookmarkCodec(bool persist_ids); |
29 | 63 |
30 // Encodes the model to a JSON value. It's up to the caller to delete the | 64 // Encodes the model to a JSON value. It's up to the caller to delete the |
31 // returned object. This is invoked to encode the contents of the bookmark bar | 65 // returned object. This is invoked to encode the contents of the bookmark bar |
32 // model and is currently a convenience to invoking Encode that takes the | 66 // model and is currently a convenience to invoking Encode that takes the |
33 // bookmark bar node and other folder node. | 67 // bookmark bar node and other folder node. |
34 Value* Encode(BookmarkModel* model); | 68 Value* Encode(BookmarkModel* model); |
35 | 69 |
36 // Encodes the bookmark bar and other folders returning the JSON value. It's | 70 // Encodes the bookmark bar and other folders returning the JSON value. It's |
37 // up to the caller to delete the returned object. | 71 // up to the caller to delete the returned object. |
38 // This method is public for use by StarredURLDatabase in migrating the | 72 // This method is public for use by StarredURLDatabase in migrating the |
(...skipping 16 matching lines...) Expand all Loading... |
55 // differ from the stored checksum if the file contents were changed by the | 89 // differ from the stored checksum if the file contents were changed by the |
56 // user. | 90 // user. |
57 const std::string& stored_checksum() const { return stored_checksum_; } | 91 const std::string& stored_checksum() const { return stored_checksum_; } |
58 | 92 |
59 // Names of the various keys written to the Value. | 93 // Names of the various keys written to the Value. |
60 static const wchar_t* kRootsKey; | 94 static const wchar_t* kRootsKey; |
61 static const wchar_t* kRootFolderNameKey; | 95 static const wchar_t* kRootFolderNameKey; |
62 static const wchar_t* kOtherBookmarFolderNameKey; | 96 static const wchar_t* kOtherBookmarFolderNameKey; |
63 static const wchar_t* kVersionKey; | 97 static const wchar_t* kVersionKey; |
64 static const wchar_t* kChecksumKey; | 98 static const wchar_t* kChecksumKey; |
| 99 static const wchar_t* kIdKey; |
65 static const wchar_t* kTypeKey; | 100 static const wchar_t* kTypeKey; |
66 static const wchar_t* kNameKey; | 101 static const wchar_t* kNameKey; |
67 static const wchar_t* kDateAddedKey; | 102 static const wchar_t* kDateAddedKey; |
68 static const wchar_t* kURLKey; | 103 static const wchar_t* kURLKey; |
69 static const wchar_t* kDateModifiedKey; | 104 static const wchar_t* kDateModifiedKey; |
70 static const wchar_t* kChildrenKey; | 105 static const wchar_t* kChildrenKey; |
71 | 106 |
72 // Possible values for kTypeKey. | 107 // Possible values for kTypeKey. |
73 static const wchar_t* kTypeURL; | 108 static const wchar_t* kTypeURL; |
74 static const wchar_t* kTypeFolder; | 109 static const wchar_t* kTypeFolder; |
(...skipping 21 matching lines...) Expand all Loading... |
96 | 131 |
97 // Updates the check-sum with the given string. | 132 // Updates the check-sum with the given string. |
98 void UpdateChecksum(const std::string& str); | 133 void UpdateChecksum(const std::string& str); |
99 void UpdateChecksum(const std::wstring& str); | 134 void UpdateChecksum(const std::wstring& str); |
100 | 135 |
101 // Updates the check-sum with the given contents of URL/folder bookmark node. | 136 // Updates the check-sum with the given contents of URL/folder bookmark node. |
102 // NOTE: These functions take in individual properties of a bookmark node | 137 // NOTE: These functions take in individual properties of a bookmark node |
103 // instead of taking in a BookmarkNode for efficiency so that we don't convert | 138 // instead of taking in a BookmarkNode for efficiency so that we don't convert |
104 // varous data-types to wide strings multiple times - once for serializing | 139 // varous data-types to wide strings multiple times - once for serializing |
105 // and once for computing the check-sum. | 140 // and once for computing the check-sum. |
106 void UpdateChecksumWithUrlNode(const std::wstring& title, | 141 void UpdateChecksumWithUrlNode(const std::string& id, |
| 142 const std::wstring& title, |
107 const std::wstring& url); | 143 const std::wstring& url); |
108 void UpdateChecksumWithFolderNode(const std::wstring& title); | 144 void UpdateChecksumWithFolderNode(const std::string& id, |
| 145 const std::wstring& title); |
109 | 146 |
110 // Initializes/Finalizes the checksum. | 147 // Initializes/Finalizes the checksum. |
111 void InitializeChecksum(); | 148 void InitializeChecksum(); |
112 void FinalizeChecksum(); | 149 void FinalizeChecksum(); |
113 | 150 |
| 151 // Whether to persist IDs or not. |
| 152 bool persist_ids_; |
| 153 // Unique ID generator used during decoding. |
| 154 UniqueIDGenerator id_generator_; |
| 155 |
114 // MD5 context used to compute MD5 hash of all bookmark data. | 156 // MD5 context used to compute MD5 hash of all bookmark data. |
115 MD5Context md5_context_; | 157 MD5Context md5_context_; |
116 | 158 |
117 // Checksums. | 159 // Checksums. |
118 std::string computed_checksum_; | 160 std::string computed_checksum_; |
119 std::string stored_checksum_; | 161 std::string stored_checksum_; |
120 | 162 |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); | 163 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); |
123 }; | 164 }; |
124 | 165 |
125 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ | 166 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ |
OLD | NEW |