Home | History | Annotate | Download | only in fake_server
      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.
     22 bool IsBookmark(const sync_pb::SyncEntity& client_entity) {
     23   return syncer::GetModelType(client_entity) == syncer::BOOKMARKS;
     24 }
     25 
     26 }  // namespace
     27 
     28 BookmarkEntity::~BookmarkEntity() { }
     29 
     30 // static
     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
     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 
     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 
    107 string BookmarkEntity::GetParentId() const {
    108   return parent_id_;
    109 }
    110 
    111 void BookmarkEntity::SerializeAsProto(sync_pb::SyncEntity* proto) {
    112   FakeServerEntity::SerializeBaseProtoFields(proto);
    113 
    114   sync_pb::EntitySpecifics* specifics = proto->mutable_specifics();
    115   specifics->CopyFrom(specifics_);
    116 
    117   proto->set_originator_cache_guid(originator_cache_guid_);
    118   proto->set_originator_client_item_id(originator_client_item_id_);
    119 
    120   proto->set_parent_id_string(parent_id_);
    121   proto->set_ctime(creation_time_);
    122   proto->set_mtime(last_modified_time_);
    123 
    124   sync_pb::UniquePosition* unique_position = proto->mutable_unique_position();
    125   unique_position->CopyFrom(unique_position_);
    126 }
    127 
    128 bool BookmarkEntity::IsDeleted() const {
    129   return false;
    130 }
    131 
    132 bool BookmarkEntity::IsFolder() const {
    133   return is_folder_;
    134 }
    135 
    136 }  // namespace fake_server
    137