• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "sync/test/fake_server/bookmark_entity.h"
6 
7 #include <string>
8 
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"
14 
15 using std::string;
16 
17 namespace fake_server {
18 
19 namespace {
20 
21 // Returns true if and only if |client_entity| is a bookmark.
IsBookmark(const sync_pb::SyncEntity & client_entity)22 bool IsBookmark(const sync_pb::SyncEntity& client_entity) {
23   return syncer::GetModelType(client_entity) == syncer::BOOKMARKS;
24 }
25 
26 }  // namespace
27 
~BookmarkEntity()28 BookmarkEntity::~BookmarkEntity() { }
29 
30 // static
CreateNew(const sync_pb::SyncEntity & client_entity,const string & parent_id,const string & client_guid)31 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.";
37 
38   string id = FakeServerEntity::CreateId(syncer::BOOKMARKS,
39                                          base::GenerateGUID());
40   string originator_cache_guid = client_guid;
41   string originator_client_item_id = client_entity.id_string();
42 
43   return new BookmarkEntity(id,
44                             client_entity.version(),
45                             client_entity.name(),
46                             originator_cache_guid,
47                             originator_client_item_id,
48                             client_entity.unique_position(),
49                             client_entity.specifics(),
50                             client_entity.folder(),
51                             parent_id,
52                             client_entity.ctime(),
53                             client_entity.mtime());
54 }
55 
56 // static
CreateUpdatedVersion(const sync_pb::SyncEntity & client_entity,FakeServerEntity * current_server_entity,const string & parent_id)57 FakeServerEntity* BookmarkEntity::CreateUpdatedVersion(
58     const sync_pb::SyncEntity& client_entity,
59     FakeServerEntity* current_server_entity,
60     const string& parent_id) {
61   CHECK(client_entity.version() != 0) << "Existing entities must not have a "
62                                       << "version = 0.";
63   CHECK(IsBookmark(client_entity)) << "The given entity must be a bookmark.";
64 
65   BookmarkEntity* current_bookmark_entity =
66       static_cast<BookmarkEntity*>(current_server_entity);
67   string originator_cache_guid =
68       current_bookmark_entity->originator_cache_guid_;
69   string originator_client_item_id =
70       current_bookmark_entity->originator_client_item_id_;
71 
72   return new BookmarkEntity(client_entity.id_string(),
73                             client_entity.version(),
74                             client_entity.name(),
75                             originator_cache_guid,
76                             originator_client_item_id,
77                             client_entity.unique_position(),
78                             client_entity.specifics(),
79                             client_entity.folder(),
80                             parent_id,
81                             client_entity.ctime(),
82                             client_entity.mtime());
83 }
84 
BookmarkEntity(const string & id,int64 version,const string & name,const string & originator_cache_guid,const string & originator_client_item_id,const sync_pb::UniquePosition & unique_position,const sync_pb::EntitySpecifics & specifics,bool is_folder,const string & parent_id,int64 creation_time,int64 last_modified_time)85 BookmarkEntity::BookmarkEntity(
86     const string& id,
87     int64 version,
88     const string& name,
89     const string& originator_cache_guid,
90     const string& originator_client_item_id,
91     const sync_pb::UniquePosition& unique_position,
92     const sync_pb::EntitySpecifics& specifics,
93     bool is_folder,
94     const string& parent_id,
95     int64 creation_time,
96     int64 last_modified_time)
97     : FakeServerEntity(id, syncer::BOOKMARKS, version, name),
98       originator_cache_guid_(originator_cache_guid),
99       originator_client_item_id_(originator_client_item_id),
100       unique_position_(unique_position),
101       specifics_(specifics),
102       is_folder_(is_folder),
103       parent_id_(parent_id),
104       creation_time_(creation_time),
105       last_modified_time_(last_modified_time) { }
106 
GetParentId() const107 string BookmarkEntity::GetParentId() const {
108   return parent_id_;
109 }
110 
SerializeAsProto()111 sync_pb::SyncEntity* BookmarkEntity::SerializeAsProto() {
112   sync_pb::SyncEntity* sync_entity = new sync_pb::SyncEntity();
113   FakeServerEntity::SerializeBaseProtoFields(sync_entity);
114 
115   sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics();
116   specifics->CopyFrom(specifics_);
117 
118   sync_entity->set_originator_cache_guid(originator_cache_guid_);
119   sync_entity->set_originator_client_item_id(originator_client_item_id_);
120 
121   sync_entity->set_parent_id_string(parent_id_);
122   sync_entity->set_ctime(creation_time_);
123   sync_entity->set_mtime(last_modified_time_);
124 
125   sync_pb::UniquePosition* unique_position =
126       sync_entity->mutable_unique_position();
127   unique_position->CopyFrom(unique_position_);
128 
129   return sync_entity;
130 }
131 
IsDeleted() const132 bool BookmarkEntity::IsDeleted() const {
133   return false;
134 }
135 
IsFolder() const136 bool BookmarkEntity::IsFolder() const {
137   return is_folder_;
138 }
139 
140 }  // namespace fake_server
141