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.
5 #include "sync/test/fake_server/bookmark_entity.h"
9 #include "base/basictypes.h"
10 #include "base/guid.h"
11 #include "sync/internal_api/public/base/model_type.h"
12 #include "sync/protocol/sync.pb.h"
13 #include "sync/test/fake_server/fake_server_entity.h"
17 namespace fake_server
{
21 // Returns true if and only if |client_entity| is a bookmark.
22 bool IsBookmark(const sync_pb::SyncEntity
& client_entity
) {
23 return syncer::GetModelType(client_entity
) == syncer::BOOKMARKS
;
28 BookmarkEntity::~BookmarkEntity() { }
31 scoped_ptr
<FakeServerEntity
> BookmarkEntity::CreateNew(
32 const sync_pb::SyncEntity
& client_entity
,
33 const string
& parent_id
,
34 const string
& client_guid
) {
35 CHECK(client_entity
.version() == 0) << "New entities must have version = 0.";
36 CHECK(IsBookmark(client_entity
)) << "The given entity must be a bookmark.";
39 FakeServerEntity::CreateId(syncer::BOOKMARKS
, base::GenerateGUID());
40 const string originator_cache_guid
= client_guid
;
41 const string originator_client_item_id
= client_entity
.id_string();
43 return scoped_ptr
<FakeServerEntity
>(new BookmarkEntity(
44 id
, client_entity
.version(), client_entity
.name(), originator_cache_guid
,
45 originator_client_item_id
, client_entity
.unique_position(),
46 client_entity
.specifics(), client_entity
.folder(), parent_id
,
47 client_entity
.ctime(), client_entity
.mtime()));
51 scoped_ptr
<FakeServerEntity
> BookmarkEntity::CreateUpdatedVersion(
52 const sync_pb::SyncEntity
& client_entity
,
53 const FakeServerEntity
& current_server_entity
,
54 const string
& parent_id
) {
55 CHECK(client_entity
.version() != 0) << "Existing entities must not have a "
57 CHECK(IsBookmark(client_entity
)) << "The given entity must be a bookmark.";
59 const BookmarkEntity
& current_bookmark_entity
=
60 static_cast<const BookmarkEntity
&>(current_server_entity
);
61 const string originator_cache_guid
=
62 current_bookmark_entity
.originator_cache_guid_
;
63 const string originator_client_item_id
=
64 current_bookmark_entity
.originator_client_item_id_
;
66 return scoped_ptr
<FakeServerEntity
>(new BookmarkEntity(
67 client_entity
.id_string(), client_entity
.version(), client_entity
.name(),
68 originator_cache_guid
, originator_client_item_id
,
69 client_entity
.unique_position(), client_entity
.specifics(),
70 client_entity
.folder(), parent_id
, client_entity
.ctime(),
71 client_entity
.mtime()));
74 BookmarkEntity::BookmarkEntity(
78 const string
& originator_cache_guid
,
79 const string
& originator_client_item_id
,
80 const sync_pb::UniquePosition
& unique_position
,
81 const sync_pb::EntitySpecifics
& specifics
,
83 const string
& parent_id
,
85 int64 last_modified_time
)
86 : FakeServerEntity(id
, syncer::BOOKMARKS
, version
, name
),
87 originator_cache_guid_(originator_cache_guid
),
88 originator_client_item_id_(originator_client_item_id
),
89 unique_position_(unique_position
),
90 is_folder_(is_folder
),
91 parent_id_(parent_id
),
92 creation_time_(creation_time
),
93 last_modified_time_(last_modified_time
) {
94 SetSpecifics(specifics
);
97 void BookmarkEntity::SetParentId(const string
& parent_id
) {
98 parent_id_
= parent_id
;
101 string
BookmarkEntity::GetParentId() const {
105 void BookmarkEntity::SerializeAsProto(sync_pb::SyncEntity
* proto
) const {
106 FakeServerEntity::SerializeBaseProtoFields(proto
);
108 proto
->set_originator_cache_guid(originator_cache_guid_
);
109 proto
->set_originator_client_item_id(originator_client_item_id_
);
111 proto
->set_parent_id_string(parent_id_
);
112 proto
->set_ctime(creation_time_
);
113 proto
->set_mtime(last_modified_time_
);
115 sync_pb::UniquePosition
* unique_position
= proto
->mutable_unique_position();
116 unique_position
->CopyFrom(unique_position_
);
119 bool BookmarkEntity::IsFolder() const {
123 } // namespace fake_server