bookmark_codec.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2009 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. 4 5// BookmarkCodec is responsible for encoding and decoding the BookmarkModel 6// into JSON values. The encoded values are written to disk via the 7// BookmarkService. 8 9#ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ 10#define CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ 11 12#include <set> 13#include <string> 14 15#include "base/basictypes.h" 16#include "base/scoped_ptr.h" 17#include "base/md5.h" 18#include "base/string16.h" 19 20class BookmarkModel; 21class BookmarkNode; 22class DictionaryValue; 23class ListValue; 24class Value; 25 26// BookmarkCodec is responsible for encoding/decoding bookmarks into JSON 27// values. BookmarkCodec is used by BookmarkService. 28 29class BookmarkCodec { 30 public: 31 // 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 33 // guarantees on how the IDs are reassigned or about doing minimal 34 // reassignments to achieve uniqueness. 35 BookmarkCodec(); 36 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 Value* Encode(BookmarkModel* model); 42 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 // This method is public for use by StarredURLDatabase in migrating the 46 // bookmarks out of the database. 47 Value* Encode(const BookmarkNode* bookmark_bar_node, 48 const BookmarkNode* other_folder_node); 49 50 // Decodes the previously encoded value to the specified nodes as well as 51 // setting |max_node_id| to the greatest node id. Returns true on success, 52 // false otherwise. If there is an error (such as unexpected version) all 53 // children are removed from the bookmark bar and other folder nodes. On exit 54 // |max_node_id| is set to the max id of the nodes. 55 bool Decode(BookmarkNode* bb_node, 56 BookmarkNode* other_folder_node, 57 int64* max_node_id, 58 const Value& value); 59 60 // Returns the checksum computed during last encoding/decoding call. 61 const std::string& computed_checksum() const { return computed_checksum_; } 62 63 // Returns the checksum that's stored in the file. After a call to Encode, 64 // the computed and stored checksums are the same since the computed checksum 65 // is stored to the file. After a call to decode, the computed checksum can 66 // differ from the stored checksum if the file contents were changed by the 67 // user. 68 const std::string& stored_checksum() const { return stored_checksum_; } 69 70 // Returns whether the IDs were reassigned during decoding. Always returns 71 // false after encoding. 72 bool ids_reassigned() const { return ids_reassigned_; } 73 74 // Names of the various keys written to the Value. 75 static const wchar_t* kRootsKey; 76 static const wchar_t* kRootFolderNameKey; 77 static const wchar_t* kOtherBookmarkFolderNameKey; 78 static const wchar_t* kVersionKey; 79 static const wchar_t* kChecksumKey; 80 static const wchar_t* kIdKey; 81 static const wchar_t* kTypeKey; 82 static const wchar_t* kNameKey; 83 static const wchar_t* kDateAddedKey; 84 static const wchar_t* kURLKey; 85 static const wchar_t* kDateModifiedKey; 86 static const wchar_t* kChildrenKey; 87 88 // Possible values for kTypeKey. 89 static const char* kTypeURL; 90 static const char* kTypeFolder; 91 92 private: 93 // Encodes node and all its children into a Value object and returns it. 94 // The caller takes ownership of the returned object. 95 Value* EncodeNode(const BookmarkNode* node); 96 97 // Helper to perform decoding. 98 bool DecodeHelper(BookmarkNode* bb_node, 99 BookmarkNode* other_folder_node, 100 const Value& value); 101 102 // Decodes the children of the specified node. Returns true on success. 103 bool DecodeChildren(const ListValue& child_value_list, 104 BookmarkNode* parent); 105 106 // Reassigns bookmark IDs for all nodes. 107 void ReassignIDs(BookmarkNode* bb_node, BookmarkNode* other_node); 108 109 // Helper to recursively reassign IDs. 110 void ReassignIDsHelper(BookmarkNode* node); 111 112 // Decodes the supplied node from the supplied value. Child nodes are 113 // created appropriately by way of DecodeChildren. If node is NULL a new 114 // node is created and added to parent (parent must then be non-NULL), 115 // otherwise node is used. 116 bool DecodeNode(const DictionaryValue& value, 117 BookmarkNode* parent, 118 BookmarkNode* node); 119 120 // Updates the check-sum with the given string. 121 void UpdateChecksum(const std::string& str); 122 void UpdateChecksum(const string16& str); 123 124 // Updates the check-sum with the given contents of URL/folder bookmark node. 125 // NOTE: These functions take in individual properties of a bookmark node 126 // instead of taking in a BookmarkNode for efficiency so that we don't convert 127 // various data-types to UTF16 strings multiple times - once for serializing 128 // and once for computing the check-sum. 129 // The url parameter should be a valid UTF8 string. 130 void UpdateChecksumWithUrlNode(const std::string& id, 131 const string16& title, 132 const std::string& url); 133 void UpdateChecksumWithFolderNode(const std::string& id, 134 const string16& title); 135 136 // Initializes/Finalizes the checksum. 137 void InitializeChecksum(); 138 void FinalizeChecksum(); 139 140 // Whether or not IDs were reassigned by the codec. 141 bool ids_reassigned_; 142 143 // Whether or not IDs are valid. This is initially true, but set to false 144 // if an id is missing or not unique. 145 bool ids_valid_; 146 147 // Contains the id of each of the nodes found in the file. Used to determine 148 // if we have duplicates. 149 std::set<int64> ids_; 150 151 // MD5 context used to compute MD5 hash of all bookmark data. 152 MD5Context md5_context_; 153 154 // Checksums. 155 std::string computed_checksum_; 156 std::string stored_checksum_; 157 158 // Maximum ID assigned when decoding data. 159 int64 maximum_id_; 160 161 DISALLOW_COPY_AND_ASSIGN(BookmarkCodec); 162}; 163 164#endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_CODEC_H_ 165