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 "sync/test/fake_server/bookmark_entity_builder.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <string> | |
11 | |
12 #include "base/guid.h" | |
13 #include "base/memory/ptr_util.h" | |
14 #include "base/time/time.h" | |
15 #include "sync/internal_api/public/base/model_type.h" | |
16 #include "sync/internal_api/public/base/unique_position.h" | |
17 #include "sync/protocol/sync.pb.h" | |
18 #include "sync/syncable/syncable_util.h" | |
19 #include "sync/test/fake_server/bookmark_entity.h" | |
20 #include "sync/test/fake_server/fake_server_entity.h" | |
21 #include "sync/util/time.h" | |
22 #include "url/gurl.h" | |
23 | |
24 using std::string; | |
25 | |
26 using syncer::syncable::GenerateSyncableBookmarkHash; | |
27 | |
28 // A version must be passed when creating a FakeServerEntity, but this value | |
29 // is overrideen immediately when saving the entity in FakeServer. | |
30 const int64_t kUnusedVersion = 0L; | |
31 | |
32 // Default time (creation and last modified) used when creating entities. | |
33 const int64_t kDefaultTime = 1234L; | |
34 | |
35 namespace fake_server { | |
36 | |
37 BookmarkEntityBuilder::BookmarkEntityBuilder( | |
38 const string& title, | |
39 const string& originator_cache_guid, | |
40 const string& originator_client_item_id) | |
41 : title_(title), | |
42 originator_cache_guid_(originator_cache_guid), | |
43 originator_client_item_id_(originator_client_item_id) { | |
44 } | |
45 | |
46 BookmarkEntityBuilder::BookmarkEntityBuilder( | |
47 const BookmarkEntityBuilder& other) = default; | |
48 | |
49 BookmarkEntityBuilder::~BookmarkEntityBuilder() { | |
50 } | |
51 | |
52 void BookmarkEntityBuilder::SetParentId(const std::string& parent_id) { | |
53 parent_id_ = parent_id; | |
54 } | |
55 | |
56 std::unique_ptr<FakeServerEntity> BookmarkEntityBuilder::BuildBookmark( | |
57 const GURL& url) { | |
58 if (!url.is_valid()) { | |
59 return base::WrapUnique<FakeServerEntity>(NULL); | |
60 } | |
61 | |
62 sync_pb::EntitySpecifics entity_specifics = CreateBaseEntitySpecifics(); | |
63 entity_specifics.mutable_bookmark()->set_url(url.spec()); | |
64 const bool kIsNotFolder = false; | |
65 return Build(entity_specifics, kIsNotFolder); | |
66 } | |
67 | |
68 std::unique_ptr<FakeServerEntity> BookmarkEntityBuilder::BuildFolder() { | |
69 const bool kIsFolder = true; | |
70 return Build(CreateBaseEntitySpecifics(), kIsFolder); | |
71 } | |
72 | |
73 sync_pb::EntitySpecifics BookmarkEntityBuilder::CreateBaseEntitySpecifics() | |
74 const { | |
75 sync_pb::EntitySpecifics entity_specifics; | |
76 sync_pb::BookmarkSpecifics* bookmark_specifics = | |
77 entity_specifics.mutable_bookmark(); | |
78 bookmark_specifics->set_title(title_); | |
79 | |
80 return entity_specifics; | |
81 } | |
82 | |
83 std::unique_ptr<FakeServerEntity> BookmarkEntityBuilder::Build( | |
84 const sync_pb::EntitySpecifics& entity_specifics, | |
85 bool is_folder) { | |
86 sync_pb::UniquePosition unique_position; | |
87 // TODO(pvalenzuela): Allow caller customization of the position integer. | |
88 const string suffix = GenerateSyncableBookmarkHash( | |
89 originator_cache_guid_, | |
90 originator_client_item_id_); | |
91 syncer::UniquePosition::FromInt64(0, suffix).ToProto(&unique_position); | |
92 | |
93 if (parent_id_.empty()) { | |
94 parent_id_ = FakeServerEntity::CreateId(syncer::BOOKMARKS, "bookmark_bar"); | |
95 } | |
96 | |
97 const string id = FakeServerEntity::CreateId(syncer::BOOKMARKS, | |
98 base::GenerateGUID()); | |
99 | |
100 return base::WrapUnique<FakeServerEntity>(new BookmarkEntity( | |
101 id, kUnusedVersion, title_, originator_cache_guid_, | |
102 originator_client_item_id_, unique_position, entity_specifics, is_folder, | |
103 parent_id_, kDefaultTime, kDefaultTime)); | |
104 } | |
105 | |
106 } // namespace fake_server | |
OLD | NEW |