File: persistent_bookmark_entity.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (203 lines) | stat: -rw-r--r-- 7,224 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/sync/engine/loopback_server/persistent_bookmark_entity.h"

#include <memory>

#include "base/logging.h"
#include "base/uuid.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/protocol/loopback_server.pb.h"
#include "components/sync/protocol/sync_entity.pb.h"
#include "components/sync/protocol/unique_position.pb.h"

using std::string;

namespace syncer {

namespace {

// Returns true if and only if `client_entity` is a bookmark.
bool IsBookmark(const sync_pb::SyncEntity& client_entity) {
  return syncer::GetDataTypeFromSpecifics(client_entity.specifics()) ==
         syncer::BOOKMARKS;
}

}  // namespace

PersistentBookmarkEntity::~PersistentBookmarkEntity() = default;

// static
std::unique_ptr<LoopbackServerEntity> PersistentBookmarkEntity::CreateNew(
    const sync_pb::SyncEntity& client_entity,
    const string& parent_id,
    const string& originator_cache_guid) {
  if (!IsBookmark(client_entity)) {
    DLOG(WARNING) << "The given entity must be a bookmark.";
    return nullptr;
  }

  const std::string originator_client_item_id = client_entity.id_string();
  if (!base::Uuid::ParseLowercase(originator_client_item_id).is_valid()) {
    DLOG(WARNING) << "Invalid originator client item ID: "
                  << originator_client_item_id;
    return nullptr;
  }

  const string id = LoopbackServerEntity::CreateId(syncer::BOOKMARKS,
                                                   originator_client_item_id);

  return std::make_unique<PersistentBookmarkEntity>(
      id, 0, client_entity.name(), originator_cache_guid,
      originator_client_item_id, client_entity.client_tag_hash(),
      client_entity.unique_position(), client_entity.specifics(),
      client_entity.folder(), parent_id, client_entity.ctime(),
      client_entity.mtime());
}

// static
std::unique_ptr<LoopbackServerEntity>
PersistentBookmarkEntity::CreateUpdatedVersion(
    const sync_pb::SyncEntity& client_entity,
    const LoopbackServerEntity& current_server_entity,
    const string& parent_id,
    const std::string& updating_client_cache_guid) {
  if (client_entity.version() == 0) {
    DLOG(WARNING) << "Existing entities must not have a version = 0.";
    return nullptr;
  }

  if (!IsBookmark(client_entity)) {
    DLOG(WARNING) << "The given entity must be a bookmark.";
    return nullptr;
  }

  std::string originator_cache_guid;
  std::string originator_client_item_id;
  std::string client_tag_hash;

  if (current_server_entity.IsDeleted()) {
    // Handle undeletions.
    originator_cache_guid = updating_client_cache_guid;
    originator_client_item_id =
        LoopbackServerEntity::GetInnerIdFromId(client_entity.id_string());
    // An undeletion is similar to a creation, so let's honor the client tag
    // provided by the client (if any).
    client_tag_hash = client_entity.client_tag_hash();
  } else {
    // Regular case (non-undeletion).
    const PersistentBookmarkEntity& current_bookmark_entity =
        static_cast<const PersistentBookmarkEntity&>(current_server_entity);
    originator_cache_guid = current_bookmark_entity.originator_cache_guid_;
    originator_client_item_id =
        current_bookmark_entity.originator_client_item_id_;
    // Note that the client tag provided by the client in `client_entity` is
    // ignored during non-creations updates, since it's meant to be immutable.
    client_tag_hash = current_bookmark_entity.client_tag_hash_;
  }

  // Using a version of 0 is okay here as it'll be updated before this entity is
  // actually saved.
  return std::make_unique<PersistentBookmarkEntity>(
      client_entity.id_string(), 0, client_entity.name(), originator_cache_guid,
      originator_client_item_id, client_tag_hash,
      client_entity.unique_position(), client_entity.specifics(),
      client_entity.folder(), parent_id, client_entity.ctime(),
      client_entity.mtime());
}

// static
std::unique_ptr<LoopbackServerEntity>
PersistentBookmarkEntity::CreateFromEntity(
    const sync_pb::SyncEntity& client_entity) {
  if (!IsBookmark(client_entity)) {
    DLOG(WARNING) << "The given entity must be a bookmark.";
    return nullptr;
  }

  return std::make_unique<PersistentBookmarkEntity>(
      client_entity.id_string(), client_entity.version(), client_entity.name(),
      client_entity.originator_cache_guid(),
      client_entity.originator_client_item_id(),
      client_entity.client_tag_hash(), client_entity.unique_position(),
      client_entity.specifics(), client_entity.folder(),
      client_entity.parent_id_string(), client_entity.ctime(),
      client_entity.mtime());
}
PersistentBookmarkEntity::PersistentBookmarkEntity(
    const string& id,
    int64_t version,
    const string& name,
    const string& originator_cache_guid,
    const string& originator_client_item_id,
    const string& client_tag_hash,
    const sync_pb::UniquePosition& unique_position,
    const sync_pb::EntitySpecifics& specifics,
    bool is_folder,
    const string& parent_id,
    int64_t creation_time,
    int64_t last_modified_time)
    : LoopbackServerEntity(id, syncer::BOOKMARKS, version, name),
      originator_cache_guid_(originator_cache_guid),
      originator_client_item_id_(originator_client_item_id),
      client_tag_hash_(client_tag_hash),
      is_folder_(is_folder),
      unique_position_(unique_position),
      parent_id_(parent_id),
      creation_time_(creation_time),
      last_modified_time_(last_modified_time) {
  if (!client_tag_hash.empty() && !originator_client_item_id.empty()) {
    // This relies technically on a well-behaving client, but verifying here to
    // avoid issues with Local Sync, which uses LoopbackServer.
    DCHECK_EQ(
        ClientTagHash::FromHashed(client_tag_hash),
        ClientTagHash::FromUnhashed(BOOKMARKS, originator_client_item_id));
  }

  SetSpecifics(specifics);
}

void PersistentBookmarkEntity::SetParentId(const string& parent_id) {
  parent_id_ = parent_id;
}

bool PersistentBookmarkEntity::RequiresParentId() const {
  // Bookmarks are stored as a hierarchy. All bookmarks must have a parent ID.
  return true;
}

string PersistentBookmarkEntity::GetParentId() const {
  return parent_id_;
}

sync_pb::LoopbackServerEntity_Type
PersistentBookmarkEntity::GetLoopbackServerEntityType() const {
  return sync_pb::LoopbackServerEntity_Type_BOOKMARK;
}

void PersistentBookmarkEntity::SerializeAsProto(
    sync_pb::SyncEntity* proto) const {
  LoopbackServerEntity::SerializeBaseProtoFields(proto);

  if (!client_tag_hash_.empty()) {
    proto->set_client_tag_hash(client_tag_hash_);
  }

  proto->set_originator_cache_guid(originator_cache_guid_);
  proto->set_originator_client_item_id(originator_client_item_id_);

  proto->set_ctime(creation_time_);
  proto->set_mtime(last_modified_time_);

  sync_pb::UniquePosition* unique_position = proto->mutable_unique_position();
  unique_position->CopyFrom(unique_position_);
}

bool PersistentBookmarkEntity::IsFolder() const {
  return is_folder_;
}

}  // namespace syncer