Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: chrome/browser/bookmarks/bookmark_codec.h

Issue 155456: Try the original CL "Always persist bookmark IDs" again with the fix to... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/bookmarks/bookmark_codec.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <set>
13 #include <string> 13 #include <string>
14 14
15 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "base/scoped_ptr.h" 16 #include "base/scoped_ptr.h"
17 #include "base/md5.h" 17 #include "base/md5.h"
18 18
19 class BookmarkModel; 19 class BookmarkModel;
20 class BookmarkNode; 20 class BookmarkNode;
21 class DictionaryValue; 21 class DictionaryValue;
22 class ListValue; 22 class ListValue;
23 class Value; 23 class Value;
24 24
25 // Utility class to help assign unique integer IDs. 25 // Utility class to help assign unique 64-bit IDs.
26 class UniqueIDGenerator { 26 class UniqueIDGenerator {
27 public: 27 public:
28 UniqueIDGenerator(); 28 UniqueIDGenerator();
29 29
30 // Checks whether the given ID can be used as a unique ID or not. If it can, 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 31 // returns the id itself, otherwise generates a new unique id in a simple way
32 // and returns that. 32 // and returns that.
33 // NOTE that if id is 0, a new unique id is returned. 33 // NOTE that if id is 0, a new unique id is returned.
34 int GetUniqueID(int id); 34 int64 GetUniqueID(int64 id);
35 35
36 // Resets the ID generator to initial state. 36 // Resets the ID generator to initial state.
37 void Reset(); 37 void Reset();
38 38
39 // Returns the current maximum. 39 // Returns the current maximum.
40 int current_max() const { return current_max_; } 40 int64 current_max() const { return current_max_; }
41 41
42 private: 42 private:
43 // Checks if the given ID is already assigned. 43 // Checks if the given ID is already assigned.
44 bool IsIdAssigned(int id) const; 44 bool IsIdAssigned(int64 id) const;
45 45
46 // Records the given ID as assigned. 46 // Records the given ID as assigned.
47 void RecordId(int id); 47 void RecordId(int64 id);
48 48
49 // Maximum value we have seen so far. 49 // Maximum value we have seen so far.
50 int current_max_; 50 int64 current_max_;
51
51 // All IDs assigned so far. 52 // All IDs assigned so far.
52 scoped_ptr<std::set<int> > assigned_ids_; 53 scoped_ptr<std::set<int64> > assigned_ids_;
53 54
54 DISALLOW_COPY_AND_ASSIGN(UniqueIDGenerator); 55 DISALLOW_COPY_AND_ASSIGN(UniqueIDGenerator);
55 }; 56 };
56 57
57 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON 58 // BookmarkCodec is responsible for encoding/decoding bookmarks into JSON
58 // values. BookmarkCodec is used by BookmarkService. 59 // values. BookmarkCodec is used by BookmarkService.
59 60
60 class BookmarkCodec { 61 class BookmarkCodec {
61 public: 62 public:
62 // Creates an instance of the codec. Encodes/decodes bookmark IDs also if 63 // Creates an instance of the codec. During decoding, if the IDs in the file
63 // persist_ids is true. The default constructor will not encode/decode IDs. 64 // are not unique, we will reassign IDs to make them unique. There are no
64 // During decoding, if persist_ids is true and if the IDs in the file are not 65 // guarantees on how the IDs are reassigned or about doing minimal
65 // unique, we will reassign IDs to make them unique. There are no guarantees 66 // reassignments to achieve uniqueness.
66 // on how the IDs are reassigned or about doing minimal reassignments to
67 // achieve uniqueness.
68 BookmarkCodec(); 67 BookmarkCodec();
69 explicit BookmarkCodec(bool persist_ids);
70 68
71 // Encodes the model to a JSON value. It's up to the caller to delete the 69 // Encodes the model to a JSON value. It's up to the caller to delete the
72 // returned object. This is invoked to encode the contents of the bookmark bar 70 // returned object. This is invoked to encode the contents of the bookmark bar
73 // model and is currently a convenience to invoking Encode that takes the 71 // model and is currently a convenience to invoking Encode that takes the
74 // bookmark bar node and other folder node. 72 // bookmark bar node and other folder node.
75 Value* Encode(BookmarkModel* model); 73 Value* Encode(BookmarkModel* model);
76 74
77 // Encodes the bookmark bar and other folders returning the JSON value. It's 75 // Encodes the bookmark bar and other folders returning the JSON value. It's
78 // up to the caller to delete the returned object. 76 // up to the caller to delete the returned object.
79 // This method is public for use by StarredURLDatabase in migrating the 77 // This method is public for use by StarredURLDatabase in migrating the
80 // bookmarks out of the database. 78 // bookmarks out of the database.
81 Value* Encode(const BookmarkNode* bookmark_bar_node, 79 Value* Encode(const BookmarkNode* bookmark_bar_node,
82 const BookmarkNode* other_folder_node); 80 const BookmarkNode* other_folder_node);
83 81
84 // Decodes the previously encoded value to the specified nodes as well as 82 // Decodes the previously encoded value to the specified nodes as well as
85 // setting |max_node_id| to the greatest node id. Returns true on success, 83 // setting |max_node_id| to the greatest node id. Returns true on success,
86 // false otherwise. If there is an error (such as unexpected version) all 84 // false otherwise. If there is an error (such as unexpected version) all
87 // children are removed from the bookmark bar and other folder nodes. On exit 85 // children are removed from the bookmark bar and other folder nodes. On exit
88 // |max_node_id| is set to the max id of the nodes. 86 // |max_node_id| is set to the max id of the nodes.
89 bool Decode(BookmarkNode* bb_node, 87 bool Decode(BookmarkNode* bb_node,
90 BookmarkNode* other_folder_node, 88 BookmarkNode* other_folder_node,
91 int* max_node_id, 89 int64* max_node_id,
92 const Value& value); 90 const Value& value);
93 91
94 // Returns the checksum computed during last encoding/decoding call. 92 // Returns the checksum computed during last encoding/decoding call.
95 const std::string& computed_checksum() const { return computed_checksum_; } 93 const std::string& computed_checksum() const { return computed_checksum_; }
96 94
97 // Returns the checksum that's stored in the file. After a call to Encode, 95 // Returns the checksum that's stored in the file. After a call to Encode,
98 // the computed and stored checksums are the same since the computed checksum 96 // the computed and stored checksums are the same since the computed checksum
99 // is stored to the file. After a call to decode, the computed checksum can 97 // is stored to the file. After a call to decode, the computed checksum can
100 // differ from the stored checksum if the file contents were changed by the 98 // differ from the stored checksum if the file contents were changed by the
101 // user. 99 // user.
102 const std::string& stored_checksum() const { return stored_checksum_; } 100 const std::string& stored_checksum() const { return stored_checksum_; }
103 101
102 // Returns whether the IDs were reassigned during decoding. Always returns
103 // false after encoding.
104 bool ids_reassigned() const { return ids_reassigned_; }
105
104 // Names of the various keys written to the Value. 106 // Names of the various keys written to the Value.
105 static const wchar_t* kRootsKey; 107 static const wchar_t* kRootsKey;
106 static const wchar_t* kRootFolderNameKey; 108 static const wchar_t* kRootFolderNameKey;
107 static const wchar_t* kOtherBookmarkFolderNameKey; 109 static const wchar_t* kOtherBookmarkFolderNameKey;
108 static const wchar_t* kVersionKey; 110 static const wchar_t* kVersionKey;
109 static const wchar_t* kChecksumKey; 111 static const wchar_t* kChecksumKey;
110 static const wchar_t* kIdKey; 112 static const wchar_t* kIdKey;
111 static const wchar_t* kTypeKey; 113 static const wchar_t* kTypeKey;
112 static const wchar_t* kNameKey; 114 static const wchar_t* kNameKey;
113 static const wchar_t* kDateAddedKey; 115 static const wchar_t* kDateAddedKey;
114 static const wchar_t* kURLKey; 116 static const wchar_t* kURLKey;
115 static const wchar_t* kDateModifiedKey; 117 static const wchar_t* kDateModifiedKey;
116 static const wchar_t* kChildrenKey; 118 static const wchar_t* kChildrenKey;
117 119
118 // Possible values for kTypeKey. 120 // Possible values for kTypeKey.
119 static const wchar_t* kTypeURL; 121 static const wchar_t* kTypeURL;
120 static const wchar_t* kTypeFolder; 122 static const wchar_t* kTypeFolder;
121 123
122 private: 124 private:
123 // Encodes node and all its children into a Value object and returns it. 125 // Encodes node and all its children into a Value object and returns it.
124 // The caller takes ownership of the returned object. 126 // The caller takes ownership of the returned object.
125 Value* EncodeNode(const BookmarkNode* node); 127 Value* EncodeNode(const BookmarkNode* node);
126 128
127 // Helper to perform decoding. 129 // Helper to perform decoding.
128 bool DecodeHelper(BookmarkNode* bb_node, 130 bool DecodeHelper(BookmarkNode* bb_node,
129 BookmarkNode* other_folder_node, 131 BookmarkNode* other_folder_node,
130 int* max_id,
131 const Value& value); 132 const Value& value);
132 133
133 // Decodes the children of the specified node. Returns true on success. 134 // Decodes the children of the specified node. Returns true on success.
134 bool DecodeChildren(const ListValue& child_value_list, 135 bool DecodeChildren(const ListValue& child_value_list,
135 BookmarkNode* parent); 136 BookmarkNode* parent);
136 137
137 // Decodes the supplied node from the supplied value. Child nodes are 138 // Decodes the supplied node from the supplied value. Child nodes are
138 // created appropriately by way of DecodeChildren. If node is NULL a new 139 // created appropriately by way of DecodeChildren. If node is NULL a new
139 // node is created and added to parent, otherwise node is used. 140 // node is created and added to parent, otherwise node is used.
140 bool DecodeNode(const DictionaryValue& value, 141 bool DecodeNode(const DictionaryValue& value,
(...skipping 12 matching lines...) Expand all
153 void UpdateChecksumWithUrlNode(const std::string& id, 154 void UpdateChecksumWithUrlNode(const std::string& id,
154 const std::wstring& title, 155 const std::wstring& title,
155 const std::wstring& url); 156 const std::wstring& url);
156 void UpdateChecksumWithFolderNode(const std::string& id, 157 void UpdateChecksumWithFolderNode(const std::string& id,
157 const std::wstring& title); 158 const std::wstring& title);
158 159
159 // Initializes/Finalizes the checksum. 160 // Initializes/Finalizes the checksum.
160 void InitializeChecksum(); 161 void InitializeChecksum();
161 void FinalizeChecksum(); 162 void FinalizeChecksum();
162 163
163 // Whether to persist IDs or not.
164 bool persist_ids_;
165 // Unique ID generator used during decoding. 164 // Unique ID generator used during decoding.
166 UniqueIDGenerator id_generator_; 165 UniqueIDGenerator id_generator_;
167 166
167 // Whether or not IDs were reassigned by the codec.
168 bool ids_reassigned_;
169
168 // MD5 context used to compute MD5 hash of all bookmark data. 170 // MD5 context used to compute MD5 hash of all bookmark data.
169 MD5Context md5_context_; 171 MD5Context md5_context_;
170 172
171 // Checksums. 173 // Checksums.
172 std::string computed_checksum_; 174 std::string computed_checksum_;
173 std::string stored_checksum_; 175 std::string stored_checksum_;
174 176
175 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); 177 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec);
176 }; 178 };
177 179
178 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ 180 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/bookmarks/bookmark_codec.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698