File: bookmark_model_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (252 lines) | stat: -rw-r--r-- 8,269 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2023 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_bookmarks/bookmark_model_view.h"

#include "base/check.h"
#include "components/bookmarks/browser/bookmark_client.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/common/bookmark_metrics.h"

namespace sync_bookmarks {

namespace {

const bookmarks::BookmarkNode* GetAncestorPermanentFolder(
    const bookmarks::BookmarkNode* node) {
  CHECK(node);

  const bookmarks::BookmarkNode* self_or_ancestor = node;

  while (!self_or_ancestor->is_permanent_node()) {
    self_or_ancestor = self_or_ancestor->parent();
    CHECK(self_or_ancestor);
  }

  return self_or_ancestor;
}

}  // namespace

BookmarkModelView::BookmarkModelView(bookmarks::BookmarkModel* bookmark_model)
    : bookmark_model_(bookmark_model->AsWeakPtr()) {
  CHECK(bookmark_model_);
}

BookmarkModelView::~BookmarkModelView() = default;

bool BookmarkModelView::IsNodeSyncable(
    const bookmarks::BookmarkNode* node) const {
  const bookmarks::BookmarkNode* ancestor_permanent_folder =
      GetAncestorPermanentFolder(node);
  CHECK(ancestor_permanent_folder);
  CHECK(ancestor_permanent_folder->is_permanent_node());
  CHECK_NE(ancestor_permanent_folder, root_node());

  // A node is considered syncable if it is a descendant of one of the syncable
  // permanent folder (e.g. excludes enterprise-managed nodes).
  return ancestor_permanent_folder == bookmark_bar_node() ||
         ancestor_permanent_folder == other_node() ||
         ancestor_permanent_folder == mobile_node();
}

bool BookmarkModelView::loaded() const {
  return bookmark_model_->loaded();
}

const bookmarks::BookmarkNode* BookmarkModelView::root_node() const {
  return bookmark_model_->root_node();
}

bool BookmarkModelView::is_permanent_node(
    const bookmarks::BookmarkNode* node) const {
  return bookmark_model_->is_permanent_node(node);
}

void BookmarkModelView::AddObserver(
    bookmarks::BookmarkModelObserver* observer) {
  bookmark_model_->AddObserver(observer);
}

void BookmarkModelView::RemoveObserver(
    bookmarks::BookmarkModelObserver* observer) {
  bookmark_model_->RemoveObserver(observer);
}

void BookmarkModelView::BeginExtensiveChanges() {
  bookmark_model_->BeginExtensiveChanges();
}

void BookmarkModelView::EndExtensiveChanges() {
  bookmark_model_->EndExtensiveChanges();
}

void BookmarkModelView::Remove(const bookmarks::BookmarkNode* node,
                               const base::Location& location) {
  bookmark_model_->Remove(node, bookmarks::metrics::BookmarkEditSource::kOther,
                          location);
}

void BookmarkModelView::Move(const bookmarks::BookmarkNode* node,
                             const bookmarks::BookmarkNode* new_parent,
                             size_t index) {
  bookmark_model_->Move(node, new_parent, index);
}

const gfx::Image& BookmarkModelView::GetFavicon(
    const bookmarks::BookmarkNode* node) {
  return bookmark_model_->GetFavicon(node);
}

void BookmarkModelView::SetTitle(const bookmarks::BookmarkNode* node,
                                 const std::u16string& title) {
  bookmark_model_->SetTitle(node, title,
                            bookmarks::metrics::BookmarkEditSource::kOther);
}

void BookmarkModelView::SetURL(const bookmarks::BookmarkNode* node,
                               const GURL& url) {
  bookmark_model_->SetURL(node, url,
                          bookmarks::metrics::BookmarkEditSource::kOther);
}

const bookmarks::BookmarkNode* BookmarkModelView::AddFolder(
    const bookmarks::BookmarkNode* parent,
    size_t index,
    const std::u16string& title,
    const bookmarks::BookmarkNode::MetaInfoMap* meta_info,
    std::optional<base::Time> creation_time,
    std::optional<base::Uuid> uuid) {
  return bookmark_model_->AddFolder(parent, index, title, meta_info,
                                    creation_time, uuid);
}

const bookmarks::BookmarkNode* BookmarkModelView::AddURL(
    const bookmarks::BookmarkNode* parent,
    size_t index,
    const std::u16string& title,
    const GURL& url,
    const bookmarks::BookmarkNode::MetaInfoMap* meta_info,
    std::optional<base::Time> creation_time,
    std::optional<base::Uuid> uuid) {
  return bookmark_model_->AddURL(parent, index, title, url, meta_info,
                                 creation_time, uuid);
}

void BookmarkModelView::ReorderChildren(
    const bookmarks::BookmarkNode* parent,
    const std::vector<const bookmarks::BookmarkNode*>& ordered_nodes) {
  bookmark_model_->ReorderChildren(parent, ordered_nodes);
}

void BookmarkModelView::UpdateLastUsedTime(const bookmarks::BookmarkNode* node,
                                           const base::Time time,
                                           bool just_opened) {
  bookmark_model_->UpdateLastUsedTime(node, time, just_opened);
}

void BookmarkModelView::SetNodeMetaInfoMap(
    const bookmarks::BookmarkNode* node,
    const bookmarks::BookmarkNode::MetaInfoMap& meta_info_map) {
  bookmark_model_->SetNodeMetaInfoMap(node, meta_info_map);
}

BookmarkModelViewUsingLocalOrSyncableNodes::
    BookmarkModelViewUsingLocalOrSyncableNodes(
        bookmarks::BookmarkModel* bookmark_model)
    : BookmarkModelView(bookmark_model) {}

BookmarkModelViewUsingLocalOrSyncableNodes::
    ~BookmarkModelViewUsingLocalOrSyncableNodes() = default;

const bookmarks::BookmarkNode*
BookmarkModelViewUsingLocalOrSyncableNodes::bookmark_bar_node() const {
  return underlying_model()->bookmark_bar_node();
}

const bookmarks::BookmarkNode*
BookmarkModelViewUsingLocalOrSyncableNodes::other_node() const {
  return underlying_model()->other_node();
}

const bookmarks::BookmarkNode*
BookmarkModelViewUsingLocalOrSyncableNodes::mobile_node() const {
  return underlying_model()->mobile_node();
}

void BookmarkModelViewUsingLocalOrSyncableNodes::EnsurePermanentNodesExist() {
  // Local-or-syncable permanent folders always exist, nothing to be done.
  CHECK(bookmark_bar_node());
  CHECK(other_node());
  CHECK(mobile_node());
}

void BookmarkModelViewUsingLocalOrSyncableNodes::RemoveAllSyncableNodes() {
  underlying_model()->BeginExtensiveChanges();

  for (const auto& permanent_node : root_node()->children()) {
    if (!IsNodeSyncable(permanent_node.get())) {
      continue;
    }

    while (!permanent_node->children().empty()) {
      underlying_model()->RemoveLastChild(
          permanent_node.get(), bookmarks::metrics::BookmarkEditSource::kOther,
          FROM_HERE);
    }
  }

  underlying_model()->EndExtensiveChanges();
}

const bookmarks::BookmarkNode*
BookmarkModelViewUsingLocalOrSyncableNodes::GetNodeByUuid(
    const base::Uuid& uuid) const {
  return underlying_model()->GetNodeByUuid(
      uuid,
      bookmarks::BookmarkModel::NodeTypeForUuidLookup::kLocalOrSyncableNodes);
}

BookmarkModelViewUsingAccountNodes::BookmarkModelViewUsingAccountNodes(
    bookmarks::BookmarkModel* bookmark_model)
    : BookmarkModelView(bookmark_model) {}

BookmarkModelViewUsingAccountNodes::~BookmarkModelViewUsingAccountNodes() =
    default;

const bookmarks::BookmarkNode*
BookmarkModelViewUsingAccountNodes::bookmark_bar_node() const {
  return underlying_model()->account_bookmark_bar_node();
}

const bookmarks::BookmarkNode* BookmarkModelViewUsingAccountNodes::other_node()
    const {
  return underlying_model()->account_other_node();
}

const bookmarks::BookmarkNode* BookmarkModelViewUsingAccountNodes::mobile_node()
    const {
  return underlying_model()->account_mobile_node();
}

void BookmarkModelViewUsingAccountNodes::EnsurePermanentNodesExist() {
  underlying_model()->CreateAccountPermanentFolders();
  CHECK(bookmark_bar_node());
  CHECK(other_node());
  CHECK(mobile_node());
}

void BookmarkModelViewUsingAccountNodes::RemoveAllSyncableNodes() {
  underlying_model()->RemoveAccountPermanentFolders();
}

const bookmarks::BookmarkNode*
BookmarkModelViewUsingAccountNodes::GetNodeByUuid(
    const base::Uuid& uuid) const {
  return underlying_model()->GetNodeByUuid(
      uuid, bookmarks::BookmarkModel::NodeTypeForUuidLookup::kAccountNodes);
}

}  // namespace sync_bookmarks