| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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 #include "components/sync/engine_impl/loopback_server/persistent_bookmark_entity
.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/guid.h" |
| 13 #include "components/sync/base/model_type.h" |
| 14 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h" |
| 15 #include "components/sync/protocol/sync.pb.h" |
| 16 |
| 17 using std::string; |
| 18 |
| 19 namespace syncer { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Returns true if and only if |client_entity| is a bookmark. |
| 24 bool IsBookmark(const sync_pb::SyncEntity& client_entity) { |
| 25 return syncer::GetModelType(client_entity) == syncer::BOOKMARKS; |
| 26 } |
| 27 |
| 28 } // namespace |
| 29 |
| 30 PersistentBookmarkEntity::~PersistentBookmarkEntity() {} |
| 31 |
| 32 // static |
| 33 std::unique_ptr<LoopbackServerEntity> PersistentBookmarkEntity::CreateNew( |
| 34 const sync_pb::SyncEntity& client_entity, |
| 35 const string& parent_id, |
| 36 const string& client_guid) { |
| 37 CHECK(client_entity.version() == 0) << "New entities must have version = 0."; |
| 38 CHECK(IsBookmark(client_entity)) << "The given entity must be a bookmark."; |
| 39 |
| 40 const string id = |
| 41 LoopbackServerEntity::CreateId(syncer::BOOKMARKS, base::GenerateGUID()); |
| 42 const string originator_cache_guid = client_guid; |
| 43 const string originator_client_item_id = client_entity.id_string(); |
| 44 |
| 45 return std::unique_ptr<LoopbackServerEntity>(new PersistentBookmarkEntity( |
| 46 id, 0, client_entity.name(), originator_cache_guid, |
| 47 originator_client_item_id, client_entity.unique_position(), |
| 48 client_entity.specifics(), client_entity.folder(), parent_id, |
| 49 client_entity.ctime(), client_entity.mtime())); |
| 50 } |
| 51 |
| 52 // static |
| 53 std::unique_ptr<LoopbackServerEntity> |
| 54 PersistentBookmarkEntity::CreateUpdatedVersion( |
| 55 const sync_pb::SyncEntity& client_entity, |
| 56 const LoopbackServerEntity& current_server_entity, |
| 57 const string& parent_id) { |
| 58 CHECK(client_entity.version() != 0) << "Existing entities must not have a " |
| 59 << "version = 0."; |
| 60 CHECK(IsBookmark(client_entity)) << "The given entity must be a bookmark."; |
| 61 |
| 62 const PersistentBookmarkEntity& current_bookmark_entity = |
| 63 static_cast<const PersistentBookmarkEntity&>(current_server_entity); |
| 64 const string originator_cache_guid = |
| 65 current_bookmark_entity.originator_cache_guid_; |
| 66 const string originator_client_item_id = |
| 67 current_bookmark_entity.originator_client_item_id_; |
| 68 |
| 69 return std::unique_ptr<LoopbackServerEntity>(new PersistentBookmarkEntity( |
| 70 client_entity.id_string(), 0, client_entity.name(), originator_cache_guid, |
| 71 originator_client_item_id, client_entity.unique_position(), |
| 72 client_entity.specifics(), client_entity.folder(), parent_id, |
| 73 client_entity.ctime(), client_entity.mtime())); |
| 74 } |
| 75 |
| 76 // static |
| 77 std::unique_ptr<LoopbackServerEntity> |
| 78 PersistentBookmarkEntity::CreateFromEntity( |
| 79 const sync_pb::SyncEntity& client_entity) { |
| 80 CHECK(IsBookmark(client_entity)) << "The given entity must be a bookmark."; |
| 81 |
| 82 return std::unique_ptr<LoopbackServerEntity>(new PersistentBookmarkEntity( |
| 83 client_entity.id_string(), client_entity.version(), client_entity.name(), |
| 84 client_entity.originator_cache_guid(), |
| 85 client_entity.originator_client_item_id(), |
| 86 client_entity.unique_position(), client_entity.specifics(), |
| 87 client_entity.folder(), client_entity.parent_id_string(), |
| 88 client_entity.ctime(), client_entity.mtime())); |
| 89 } |
| 90 PersistentBookmarkEntity::PersistentBookmarkEntity( |
| 91 const string& id, |
| 92 int64_t version, |
| 93 const string& name, |
| 94 const string& originator_cache_guid, |
| 95 const string& originator_client_item_id, |
| 96 const sync_pb::UniquePosition& unique_position, |
| 97 const sync_pb::EntitySpecifics& specifics, |
| 98 bool is_folder, |
| 99 const string& parent_id, |
| 100 int64_t creation_time, |
| 101 int64_t last_modified_time) |
| 102 : LoopbackServerEntity(id, syncer::BOOKMARKS, version, name), |
| 103 originator_cache_guid_(originator_cache_guid), |
| 104 originator_client_item_id_(originator_client_item_id), |
| 105 unique_position_(unique_position), |
| 106 is_folder_(is_folder), |
| 107 parent_id_(parent_id), |
| 108 creation_time_(creation_time), |
| 109 last_modified_time_(last_modified_time) { |
| 110 SetSpecifics(specifics); |
| 111 } |
| 112 |
| 113 void PersistentBookmarkEntity::SetParentId(const string& parent_id) { |
| 114 parent_id_ = parent_id; |
| 115 } |
| 116 |
| 117 bool PersistentBookmarkEntity::RequiresParentId() const { |
| 118 // Bookmarks are stored as a hierarchy. All bookmarks must have a parent ID. |
| 119 return true; |
| 120 } |
| 121 |
| 122 string PersistentBookmarkEntity::GetParentId() const { |
| 123 return parent_id_; |
| 124 } |
| 125 |
| 126 sync_pb::LoopbackServerEntity_Type |
| 127 PersistentBookmarkEntity::GetLoopbackServerEntityType() const { |
| 128 return sync_pb::LoopbackServerEntity_Type_BOOKMARK; |
| 129 } |
| 130 |
| 131 void PersistentBookmarkEntity::SerializeAsProto( |
| 132 sync_pb::SyncEntity* proto) const { |
| 133 LoopbackServerEntity::SerializeBaseProtoFields(proto); |
| 134 |
| 135 proto->set_originator_cache_guid(originator_cache_guid_); |
| 136 proto->set_originator_client_item_id(originator_client_item_id_); |
| 137 |
| 138 proto->set_ctime(creation_time_); |
| 139 proto->set_mtime(last_modified_time_); |
| 140 |
| 141 sync_pb::UniquePosition* unique_position = proto->mutable_unique_position(); |
| 142 unique_position->CopyFrom(unique_position_); |
| 143 } |
| 144 |
| 145 bool PersistentBookmarkEntity::IsFolder() const { |
| 146 return is_folder_; |
| 147 } |
| 148 |
| 149 } // namespace syncer |
| OLD | NEW |