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_builder.h"
      6 
      7 #include <string>
      8 
      9 #include "base/guid.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/time/time.h"
     12 #include "sync/internal_api/public/base/model_type.h"
     13 #include "sync/internal_api/public/base/unique_position.h"
     14 #include "sync/protocol/sync.pb.h"
     15 #include "sync/syncable/syncable_util.h"
     16 #include "sync/test/fake_server/bookmark_entity.h"
     17 #include "sync/test/fake_server/fake_server_entity.h"
     18 #include "sync/util/time.h"
     19 #include "url/gurl.h"
     20 
     21 using std::string;
     22 
     23 using syncer::syncable::GenerateSyncableBookmarkHash;
     24 
     25 // A version must be passed when creating a FakeServerEntity, but this value
     26 // is overrideen immediately when saving the entity in FakeServer.
     27 const int64 kUnusedVersion = 0L;
     28 
     29 // Default time (creation and last modified) used when creating entities.
     30 const int64 kDefaultTime = 1234L;
     31 
     32 namespace fake_server {
     33 
     34 BookmarkEntityBuilder::BookmarkEntityBuilder(
     35     const string& title,
     36     const GURL& url,
     37     const string& originator_cache_guid,
     38     const string& originator_client_item_id)
     39     : EntityBuilder(syncer::BOOKMARKS, title),
     40       url_(url),
     41       originator_cache_guid_(originator_cache_guid),
     42       originator_client_item_id_(originator_client_item_id) {
     43 }
     44 
     45 BookmarkEntityBuilder::~BookmarkEntityBuilder() {
     46 }
     47 
     48 scoped_ptr<FakeServerEntity> BookmarkEntityBuilder::Build() {
     49   if (!url_.is_valid()) {
     50     return make_scoped_ptr<FakeServerEntity>(NULL);
     51   }
     52 
     53   sync_pb::EntitySpecifics entity_specifics;
     54   sync_pb::BookmarkSpecifics* bookmark_specifics =
     55       entity_specifics.mutable_bookmark();
     56   bookmark_specifics->set_title(name_);
     57   bookmark_specifics->set_url(url_.spec());
     58 
     59   sync_pb::UniquePosition unique_position;
     60   // TODO(pvalenzuela): Allow caller customization of the position integer.
     61   string suffix = GenerateSyncableBookmarkHash(originator_cache_guid_,
     62                                                originator_client_item_id_);
     63   syncer::UniquePosition::FromInt64(0, suffix).ToProto(&unique_position);
     64 
     65   return make_scoped_ptr<FakeServerEntity>(
     66       new BookmarkEntity(id_,
     67                          kUnusedVersion,
     68                          name_,
     69                          originator_cache_guid_,
     70                          originator_client_item_id_,
     71                          unique_position,
     72                          entity_specifics,
     73                          // TODO(pvalenzuela): Support bookmark folders.
     74                          false,
     75                          // TODO(pvalenzuela): Support caller specification of
     76                          // the parent bookmark folder.
     77                          FakeServerEntity::CreateId(syncer::BOOKMARKS,
     78                                                     "bookmark_bar"),
     79                          kDefaultTime,
     80                          kDefaultTime));
     81 }
     82 
     83 }  // namespace fake_server
     84