| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ |
| 6 #define CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 16 #include "chrome/browser/sync/syncable/model_type.h" |
| 17 #include "googleurl/src/gurl.h" |
| 18 |
| 19 // Forward declarations of internal class types so that sync API objects |
| 20 // may have opaque pointers to these types. |
| 21 namespace base { |
| 22 class DictionaryValue; |
| 23 } |
| 24 |
| 25 namespace syncable { |
| 26 class BaseTransaction; |
| 27 class Entry; |
| 28 } |
| 29 |
| 30 namespace sync_pb { |
| 31 class AppSpecifics; |
| 32 class AutofillSpecifics; |
| 33 class AutofillProfileSpecifics; |
| 34 class BookmarkSpecifics; |
| 35 class EntitySpecifics; |
| 36 class ExtensionSpecifics; |
| 37 class SessionSpecifics; |
| 38 class NigoriSpecifics; |
| 39 class PreferenceSpecifics; |
| 40 class PasswordSpecificsData; |
| 41 class ThemeSpecifics; |
| 42 class TypedUrlSpecifics; |
| 43 } |
| 44 |
| 45 namespace sync_api { |
| 46 |
| 47 class BaseTransaction; |
| 48 |
| 49 // A valid BaseNode will never have an ID of zero. |
| 50 static const int64 kInvalidId = 0; |
| 51 |
| 52 // BaseNode wraps syncable::Entry, and corresponds to a single object's state. |
| 53 // This, like syncable::Entry, is intended for use on the stack. A valid |
| 54 // transaction is necessary to create a BaseNode or any of its children. |
| 55 // Unlike syncable::Entry, a sync API BaseNode is identified primarily by its |
| 56 // int64 metahandle, which we call an ID here. |
| 57 class BaseNode { |
| 58 public: |
| 59 // All subclasses of BaseNode must provide a way to initialize themselves by |
| 60 // doing an ID lookup. Returns false on failure. An invalid or deleted |
| 61 // ID will result in failure. |
| 62 virtual bool InitByIdLookup(int64 id) = 0; |
| 63 |
| 64 // All subclasses of BaseNode must also provide a way to initialize themselves |
| 65 // by doing a client tag lookup. Returns false on failure. A deleted node |
| 66 // will return FALSE. |
| 67 virtual bool InitByClientTagLookup(syncable::ModelType model_type, |
| 68 const std::string& tag) = 0; |
| 69 |
| 70 // Each object is identified by a 64-bit id (internally, the syncable |
| 71 // metahandle). These ids are strictly local handles. They will persist |
| 72 // on this client, but the same object on a different client may have a |
| 73 // different ID value. |
| 74 virtual int64 GetId() const; |
| 75 |
| 76 // Returns the modification time of the object (in TimeTicks internal format). |
| 77 int64 GetModificationTime() const; |
| 78 |
| 79 // Nodes are hierarchically arranged into a single-rooted tree. |
| 80 // InitByRootLookup on ReadNode allows access to the root. GetParentId is |
| 81 // how you find a node's parent. |
| 82 int64 GetParentId() const; |
| 83 |
| 84 // Nodes are either folders or not. This corresponds to the IS_DIR property |
| 85 // of syncable::Entry. |
| 86 bool GetIsFolder() const; |
| 87 |
| 88 // Returns the title of the object. |
| 89 // Uniqueness of the title is not enforced on siblings -- it is not an error |
| 90 // for two children to share a title. |
| 91 std::string GetTitle() const; |
| 92 |
| 93 // Returns the model type of this object. The model type is set at node |
| 94 // creation time and is expected never to change. |
| 95 syncable::ModelType GetModelType() const; |
| 96 |
| 97 // Getter specific to the BOOKMARK datatype. Returns protobuf |
| 98 // data. Can only be called if GetModelType() == BOOKMARK. |
| 99 const sync_pb::BookmarkSpecifics& GetBookmarkSpecifics() const; |
| 100 |
| 101 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. |
| 102 // Returns the URL of a bookmark object. |
| 103 // TODO(ncarter): Remove this datatype-specific accessor. |
| 104 GURL GetURL() const; |
| 105 |
| 106 // Legacy, bookmark-specific getter that wraps GetBookmarkSpecifics() above. |
| 107 // Fill in a vector with the byte data of this node's favicon. Assumes |
| 108 // that the node is a bookmark. |
| 109 // Favicons are expected to be PNG images, and though no verification is |
| 110 // done on the syncapi client of this, the server may reject favicon updates |
| 111 // that are invalid for whatever reason. |
| 112 // TODO(ncarter): Remove this datatype-specific accessor. |
| 113 void GetFaviconBytes(std::vector<unsigned char>* output) const; |
| 114 |
| 115 // Getter specific to the APPS datatype. Returns protobuf |
| 116 // data. Can only be called if GetModelType() == APPS. |
| 117 const sync_pb::AppSpecifics& GetAppSpecifics() const; |
| 118 |
| 119 // Getter specific to the AUTOFILL datatype. Returns protobuf |
| 120 // data. Can only be called if GetModelType() == AUTOFILL. |
| 121 const sync_pb::AutofillSpecifics& GetAutofillSpecifics() const; |
| 122 |
| 123 virtual const sync_pb::AutofillProfileSpecifics& |
| 124 GetAutofillProfileSpecifics() const; |
| 125 |
| 126 // Getter specific to the NIGORI datatype. Returns protobuf |
| 127 // data. Can only be called if GetModelType() == NIGORI. |
| 128 const sync_pb::NigoriSpecifics& GetNigoriSpecifics() const; |
| 129 |
| 130 // Getter specific to the PASSWORD datatype. Returns protobuf |
| 131 // data. Can only be called if GetModelType() == PASSWORD. |
| 132 const sync_pb::PasswordSpecificsData& GetPasswordSpecifics() const; |
| 133 |
| 134 // Getter specific to the PREFERENCE datatype. Returns protobuf |
| 135 // data. Can only be called if GetModelType() == PREFERENCE. |
| 136 const sync_pb::PreferenceSpecifics& GetPreferenceSpecifics() const; |
| 137 |
| 138 // Getter specific to the THEME datatype. Returns protobuf |
| 139 // data. Can only be called if GetModelType() == THEME. |
| 140 const sync_pb::ThemeSpecifics& GetThemeSpecifics() const; |
| 141 |
| 142 // Getter specific to the TYPED_URLS datatype. Returns protobuf |
| 143 // data. Can only be called if GetModelType() == TYPED_URLS. |
| 144 const sync_pb::TypedUrlSpecifics& GetTypedUrlSpecifics() const; |
| 145 |
| 146 // Getter specific to the EXTENSIONS datatype. Returns protobuf |
| 147 // data. Can only be called if GetModelType() == EXTENSIONS. |
| 148 const sync_pb::ExtensionSpecifics& GetExtensionSpecifics() const; |
| 149 |
| 150 // Getter specific to the SESSIONS datatype. Returns protobuf |
| 151 // data. Can only be called if GetModelType() == SESSIONS. |
| 152 const sync_pb::SessionSpecifics& GetSessionSpecifics() const; |
| 153 |
| 154 const sync_pb::EntitySpecifics& GetEntitySpecifics() const; |
| 155 |
| 156 // Returns the local external ID associated with the node. |
| 157 int64 GetExternalId() const; |
| 158 |
| 159 // Return the ID of the node immediately before this in the sibling order. |
| 160 // For the first node in the ordering, return 0. |
| 161 int64 GetPredecessorId() const; |
| 162 |
| 163 // Return the ID of the node immediately after this in the sibling order. |
| 164 // For the last node in the ordering, return 0. |
| 165 virtual int64 GetSuccessorId() const; |
| 166 |
| 167 // Return the ID of the first child of this node. If this node has no |
| 168 // children, return 0. |
| 169 virtual int64 GetFirstChildId() const; |
| 170 |
| 171 // These virtual accessors provide access to data members of derived classes. |
| 172 virtual const syncable::Entry* GetEntry() const = 0; |
| 173 virtual const BaseTransaction* GetTransaction() const = 0; |
| 174 |
| 175 // Dumps a summary of node info into a DictionaryValue and returns it. |
| 176 // Transfers ownership of the DictionaryValue to the caller. |
| 177 base::DictionaryValue* GetSummaryAsValue() const; |
| 178 |
| 179 // Dumps all node details into a DictionaryValue and returns it. |
| 180 // Transfers ownership of the DictionaryValue to the caller. |
| 181 base::DictionaryValue* GetDetailsAsValue() const; |
| 182 |
| 183 protected: |
| 184 BaseNode(); |
| 185 virtual ~BaseNode(); |
| 186 // The server has a size limit on client tags, so we generate a fixed length |
| 187 // hash locally. This also ensures that ModelTypes have unique namespaces. |
| 188 static std::string GenerateSyncableHash(syncable::ModelType model_type, |
| 189 const std::string& client_tag); |
| 190 |
| 191 // Determines whether part of the entry is encrypted, and if so attempts to |
| 192 // decrypt it. Unless decryption is necessary and fails, this will always |
| 193 // return |true|. If the contents are encrypted, the decrypted data will be |
| 194 // stored in |unencrypted_data_|. |
| 195 // This method is invoked once when the BaseNode is initialized. |
| 196 bool DecryptIfNecessary(); |
| 197 |
| 198 // Returns the unencrypted specifics associated with |entry|. If |entry| was |
| 199 // not encrypted, it directly returns |entry|'s EntitySpecifics. Otherwise, |
| 200 // returns |unencrypted_data_|. |
| 201 const sync_pb::EntitySpecifics& GetUnencryptedSpecifics( |
| 202 const syncable::Entry* entry) const; |
| 203 |
| 204 // Copy |specifics| into |unencrypted_data_|. |
| 205 void SetUnencryptedSpecifics(const sync_pb::EntitySpecifics& specifics); |
| 206 |
| 207 private: |
| 208 void* operator new(size_t size); // Node is meant for stack use only. |
| 209 |
| 210 // A holder for the unencrypted data stored in an encrypted node. |
| 211 sync_pb::EntitySpecifics unencrypted_data_; |
| 212 |
| 213 // Same as |unencrypted_data_|, but for legacy password encryption. |
| 214 scoped_ptr<sync_pb::PasswordSpecificsData> password_data_; |
| 215 |
| 216 friend class SyncApiTest; |
| 217 FRIEND_TEST_ALL_PREFIXES(SyncApiTest, GenerateSyncableHash); |
| 218 |
| 219 DISALLOW_COPY_AND_ASSIGN(BaseNode); |
| 220 }; |
| 221 |
| 222 } // namespace sync_api |
| 223 |
| 224 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_BASE_NODE_H_ |
| OLD | NEW |