File: bookmark_ui_operations_helper.h

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 (256 lines) | stat: -rw-r--r-- 10,182 bytes parent folder | download | duplicates (5)
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
253
254
255
256
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_UI_OPERATIONS_HELPER_H_
#define CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_UI_OPERATIONS_HELPER_H_

#include <cstddef>

#include "base/memory/raw_ptr.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/common/bookmark_metrics.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-forward.h"

class BookmarkMergedSurfaceService;
struct BookmarkParentFolder;
class Profile;
class Browser;

namespace bookmarks {
class BookmarkModel;
struct BookmarkNodeData;
}  // namespace bookmarks

namespace chrome {
enum class BookmarkReorderDropTarget;
}

namespace internal {

class BookmarkUIOperationsHelper {
 public:
  virtual ~BookmarkUIOperationsHelper();

  // Drops the bookmark nodes that are in `data` onto `target_parent()` at
  // `index`.
  // `copy` indicates the source operation: if true then the bookmarks in
  // `data` are copied, otherwise they are moved if they belong to the same
  // profile.
  // `browser` is needed if the user should be asked to confirm whether they
  // want to move a bookmark between local and account storage. Returns the drop
  // type used.
  ui::mojom::DragOperation DropBookmarks(
      Profile* profile,
      const bookmarks::BookmarkNodeData& data,
      size_t index,
      bool copy,
      chrome::BookmarkReorderDropTarget target,
      Browser* browser = nullptr);

  // Copies nodes onto the clipboard. The nodes are copied in such a way that if
  // pasted again new nodes can be created. Pass the calling context through as
  // `source`.
  static void CopyToClipboard(
      bookmarks::BookmarkModel* model,
      const std::vector<
          raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>& nodes,
      bookmarks::metrics::BookmarkEditSource source,
      bool is_off_the_record);

  // Copies nodes onto the clipboard then removes them from the bookmark model.
  // Pass the calling context through as `source`.
  static void CutToClipboard(
      bookmarks::BookmarkModel* model,
      const std::vector<
          raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>& nodes,
      bookmarks::metrics::BookmarkEditSource source,
      bool is_off_the_record);

  // Returns true if the user can paste from the clipboard a bookmark url/node
  // into `target_parent()`.
  bool CanPasteFromClipboard() const;

  // Pastes from the clipboard. The new nodes are added to `target_parent()`.
  // The nodes are inserted at `index`.
  void PasteFromClipboard(size_t index);

 protected:
  // Represents the target parent node for the operation.
  // For non-merged surfaces, it's a bookmark node.
  // For merged surfaces, it's either a bookmark node or a permanent folder.
  class TargetParent {
   public:
    virtual ~TargetParent();
    virtual bool IsManaged() const = 0;
    virtual bool IsPermanentNode() const = 0;
    virtual bool IsDirectChild(const bookmarks::BookmarkNode* node) const = 0;
    virtual bookmarks::BookmarkNode::Type GetType() const = 0;
    virtual const bookmarks::BookmarkNode* GetNodeAtIndex(
        size_t index) const = 0;
    virtual size_t GetChildrenCount() const = 0;
  };

  virtual bookmarks::BookmarkModel* model() = 0;
  virtual void AddNodesAsCopiesOfNodeData(
      const bookmarks::BookmarkNodeData& data,
      size_t index_to_add_at) = 0;
  virtual void MoveBookmarkNodeData(const bookmarks::BookmarkNodeData& data,
                                    const base::FilePath& profile_path,
                                    size_t index_to_add_at,
                                    Browser* browser) = 0;
  virtual const TargetParent* target_parent() const = 0;

 private:
  static void CopyOrCutToClipboard(
      bookmarks::BookmarkModel* model,
      const std::vector<
          raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>& nodes,
      bool remove_nodes,
      bookmarks::metrics::BookmarkEditSource source,
      bool is_off_the_record);

  // Updates `title` such that `url` and `title` pair are unique among the
  // children of `target_parent()`.
  void MakeTitleUnique(const GURL& url, std::u16string* title) const;
};

}  // namespace internal

// Helper class for UI operations e.g. Drop, Copy, Paste bookmarks.
//  `BookmarkUIOperationsHelperNonMergedSurfaces` must be used in non bookmark
//  merged surfaces where there is a clear distinction between account and local
//  bookmarks.
class BookmarkUIOperationsHelperNonMergedSurfaces
    : public internal::BookmarkUIOperationsHelper {
 public:
  // `model` and `parent` must be not null and outlive `this`.
  // `parent` is the parent node where nodes are being dropped, copy/cut ->
  // paste into parent folder.
  BookmarkUIOperationsHelperNonMergedSurfaces(
      bookmarks::BookmarkModel* model,
      const bookmarks::BookmarkNode* parent);

  BookmarkUIOperationsHelperNonMergedSurfaces(
      const BookmarkUIOperationsHelperNonMergedSurfaces&) = delete;
  BookmarkUIOperationsHelperNonMergedSurfaces& operator=(
      const BookmarkUIOperationsHelperNonMergedSurfaces&) = delete;

  ~BookmarkUIOperationsHelperNonMergedSurfaces() override;

 protected:
  bookmarks::BookmarkModel* model() override;
  void AddNodesAsCopiesOfNodeData(const bookmarks::BookmarkNodeData& data,
                                  size_t index_to_add_at) override;
  void MoveBookmarkNodeData(const bookmarks::BookmarkNodeData& data,
                            const base::FilePath& profile_path,
                            size_t index_to_add_at,
                            Browser* browser) override;
  const internal::BookmarkUIOperationsHelper::TargetParent* target_parent()
      const override;

 private:
  class TargetParent
      : public internal::BookmarkUIOperationsHelper::TargetParent {
   public:
    static std::unique_ptr<TargetParent> CreateTargetParent(
        bookmarks::BookmarkModel* model,
        const bookmarks::BookmarkNode* parent);

    TargetParent(const bookmarks::BookmarkNode* parent, bool is_managed);
    ~TargetParent() override;

    const bookmarks::BookmarkNode* parent_node() const;

    // internal::BookmarkUIOperationsHelper::TargetParent
    bool IsManaged() const override;
    bool IsPermanentNode() const override;
    bool IsDirectChild(const bookmarks::BookmarkNode* node) const override;
    bookmarks::BookmarkNode::Type GetType() const override;
    const bookmarks::BookmarkNode* GetNodeAtIndex(size_t index) const override;
    size_t GetChildrenCount() const override;

   private:
    const raw_ptr<const bookmarks::BookmarkNode> parent_;
    const bool is_managed_;
  };

  const bookmarks::BookmarkNode* parent_node() const;

  const raw_ptr<bookmarks::BookmarkModel> model_;
  const std::unique_ptr<TargetParent> target_parent_;
};

// Helper class for UI operations e.g. Drop, Copy, Paste bookmarks.
//  `BookmarkUIOperationsHelperNonMergedSurfaces` must be used in bookmark
//  merged surfaces (Bookmark bar, bookmark menu and side panel).
class BookmarkUIOperationsHelperMergedSurfaces
    : public internal::BookmarkUIOperationsHelper {
 public:
  BookmarkUIOperationsHelperMergedSurfaces(
      BookmarkMergedSurfaceService* merged_surface_service,
      const BookmarkParentFolder* parent);

  BookmarkUIOperationsHelperMergedSurfaces(
      const BookmarkUIOperationsHelperMergedSurfaces&) = delete;
  BookmarkUIOperationsHelperMergedSurfaces& operator=(
      const BookmarkUIOperationsHelperMergedSurfaces&) = delete;

  ~BookmarkUIOperationsHelperMergedSurfaces() override;

  // Merged bookmark surfaces can trigger a non-merged bookmark UI e.g. bookmark
  // context menu can trigger the edit bookmark UI or the bookmark manager with
  // a choice of a highlighted node.
  // This function returns the default parent to use for non-merged surfaces UIs
  // triggered from merged surfaces.
  // This function treats managed nodes as some operations are allowed; e.g.
  // opening the bookmark manager with a managed node highlighted. This should
  // not be used by managed folders if intended to be used for adding new nodes,
  // since managed bookmarks do not support creating nodes.
  const bookmarks::BookmarkNode* GetDefaultParentForNonMergedSurfaces() const;

 protected:
  bookmarks::BookmarkModel* model() override;
  void AddNodesAsCopiesOfNodeData(const bookmarks::BookmarkNodeData& data,
                                  size_t index_to_add_at) override;
  void MoveBookmarkNodeData(const bookmarks::BookmarkNodeData& data,
                            const base::FilePath& profile_path,
                            size_t index_to_add_at,
                            Browser* browser) override;
  const internal::BookmarkUIOperationsHelper::TargetParent* target_parent()
      const override;

 private:
  class TargetParent
      : public internal::BookmarkUIOperationsHelper::TargetParent {
   public:
    static std::unique_ptr<TargetParent> CreateTargetParent(
        BookmarkMergedSurfaceService* merged_surface_service,
        const BookmarkParentFolder* parent);

    TargetParent(BookmarkMergedSurfaceService* merged_surface_service,
                 const BookmarkParentFolder* parent);
    ~TargetParent() override;

    const BookmarkParentFolder* parent_folder() const;

    // internal::BookmarkUIOperationsHelper::TargetParent
    bool IsManaged() const override;
    bool IsPermanentNode() const override;
    bool IsDirectChild(const bookmarks::BookmarkNode* node) const override;
    bookmarks::BookmarkNode::Type GetType() const override;
    const bookmarks::BookmarkNode* GetNodeAtIndex(size_t index) const override;
    size_t GetChildrenCount() const override;

   private:
    const raw_ptr<BookmarkMergedSurfaceService> merged_surface_service_;
    const raw_ptr<const BookmarkParentFolder> parent_;
  };

  const BookmarkParentFolder* parent_folder() const;

  const raw_ptr<BookmarkMergedSurfaceService> merged_surface_service_;
  const std::unique_ptr<TargetParent> target_parent_;
};

#endif  // CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_UI_OPERATIONS_HELPER_H_