File: bookmark_entity_builder.h

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (149 lines) | stat: -rw-r--r-- 5,653 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
// 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.

#ifndef COMPONENTS_SYNC_TEST_BOOKMARK_ENTITY_BUILDER_H_
#define COMPONENTS_SYNC_TEST_BOOKMARK_ENTITY_BUILDER_H_

#include <memory>
#include <string>

#include "base/uuid.h"
#include "components/sync/base/data_type.h"
#include "components/sync/engine/loopback_server/loopback_server_entity.h"
#include "components/sync/protocol/unique_position.pb.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"

namespace sync_pb {
class BookmarkSpecifics;
class EntitySpecifics;
}  // namespace sync_pb

namespace fake_server {

// Builder for BookmarkEntity objects.
class BookmarkEntityBuilder {
 public:
  // Represents different generations of bookmarks ordered by time. It doesn't
  // contain all generations and may reflect differences in specifics and
  // SyncEntity. Note that this distinction represents which version of the
  // browser last modified a bookmark, rather than when it was created.
  enum class BookmarkGeneration {
    // A bookmark which doesn't contain title and GUID in specifics.
    kWithoutTitleInSpecifics,
    // A bookmark which contains valid GUID in specifics and
    // `originator_client_item_id`. For bookmarks created after M52.
    kLegacyTitleWithoutGuidInSpecifics,
    // Contains legacy title and GUID in specifics which matches to
    // `originator_client_item_id` (see BookmarkSpecifics for details).
    // Introduced in M81.
    kValidGuidAndLegacyTitle,
    // Contains both legacy title and full title in specifics. Introduced in
    // M83.
    kValidGuidAndFullTitle,
    // Contains `unique_position`, `type` and `parent_guid` in specifics.
    // Introduced in M94.
    kHierarchyFieldsInSpecifics,
  };

  BookmarkEntityBuilder(const std::string& title,
                        const base::Uuid& uuid,
                        const std::string& originator_cache_guid);
  BookmarkEntityBuilder(const std::u16string& title,
                        const base::Uuid& uuid,
                        const std::string& originator_cache_guid);
  BookmarkEntityBuilder(const BookmarkEntityBuilder& other);
  ~BookmarkEntityBuilder();

  // Allows setting an originator item ID that is not the default (which is the
  // GUID).
  BookmarkEntityBuilder& SetOriginatorClientItemId(
      const std::string& originator_client_item_id);

  // Uses a client tag hash instead of the pair
  // originator_cache_guid/originator_client_item_id.
  BookmarkEntityBuilder& EnableClientTagHash();

  // Sets the ID for the bookmark to be built. The ID should be in the format
  // returned by LoopbackServerEntity::CreateId. If this is not called, a random
  // ID will be generated.
  void SetId(const std::string& id);

  // Sets the parent ID of the bookmark to be built. If this is not called,
  // the bookmark will be included in the bookmarks bar.
  BookmarkEntityBuilder& SetParentId(const std::string& parent_id);

  // Set parent GUID to populate in specifics for generations above
  // `kHierarchyFieldsInSpecifics`. The GUID must be valid.
  BookmarkEntityBuilder& SetParentGuid(const base::Uuid& parent_guid);

  // Sets the index of the bookmark to be built. If this is not called,
  // the bookmark will be placed at index 0.
  BookmarkEntityBuilder& SetIndex(int index);

  // Update bookmark's generation, will be used to fill in the final entity
  // fields.
  BookmarkEntityBuilder& SetGeneration(BookmarkGeneration generation);

  BookmarkEntityBuilder& SetFavicon(const gfx::Image& favicon,
                                    const GURL& icon_url);

  // Builds and returns a LoopbackServerEntity representing a bookmark. Returns
  // null if the entity could not be built.
  std::unique_ptr<syncer::LoopbackServerEntity> BuildBookmark(const GURL& url);

  // Builds and returns a LoopbackServerEntity representing a bookmark folder.
  // Returns null if the entity could not be built.
  std::unique_ptr<syncer::LoopbackServerEntity> BuildFolder();

 private:
  sync_pb::UniquePosition GetUniquePosition() const;

  // Creates an EntitySpecifics and pre-populates its BookmarkSpecifics.
  sync_pb::EntitySpecifics CreateBaseEntitySpecifics(bool is_folder);

  // Builds the parts of a LoopbackServerEntity common to both normal bookmarks
  // and folders.
  std::unique_ptr<syncer::LoopbackServerEntity> Build(
      const sync_pb::EntitySpecifics& entity_specifics,
      bool is_folder);

  // Fill in favicon and icon URL in the specifics. `bookmark_specifics` must
  // not be nullptr.
  void FillWithFaviconIfNeeded(sync_pb::BookmarkSpecifics* bookmark_specifics);

  // The bookmark entity's title. This value is also used as the entity's name.
  const std::string title_;

  // The bookmark's GUID.
  const base::Uuid uuid_;

  // Information that associates the bookmark with its original client.
  const std::string originator_cache_guid_;
  std::string originator_client_item_id_;

  bool use_client_tag_hash_ = false;

  // The ID for the bookmark. This is only non-empty if it was explicitly set
  // via SetId(); otherwise a random ID will be generated on demand.
  std::string id_;

  // The ID of the parent bookmark folder.
  std::string parent_id_;
  base::Uuid parent_guid_;

  // The index of the bookmark folder within its siblings.
  int index_ = 0;

  // Information about the favicon of the bookmark.
  gfx::Image favicon_;
  GURL icon_url_;

  BookmarkGeneration bookmark_generation_ =
      BookmarkGeneration::kHierarchyFieldsInSpecifics;
};

}  // namespace fake_server

#endif  // COMPONENTS_SYNC_TEST_BOOKMARK_ENTITY_BUILDER_H_