File: bookmark_editor.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 (153 lines) | stat: -rw-r--r-- 5,401 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
// Copyright 2013 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_EDITOR_H_
#define CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_EDITOR_H_

#include <optional>
#include <string>
#include <utility>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "ui/gfx/native_widget_types.h"

class GURL;
class Profile;

namespace bookmarks {
class BookmarkModel;
}

// Small, cross platform interface that shows the correct platform specific
// bookmark editor dialog.
class BookmarkEditor {
 public:
  // A callback which is run when clicking on the Save button in the editor.
  using OnSaveCallback = base::OnceClosure;

  // An enumeration of the possible configurations offered.
  enum Configuration {
    // If Configuration is SHOW_TREE, a tree is shown allowing the user to
    // choose the parent of the node.
    SHOW_TREE,
    NO_TREE
  };

  // Describes what the user is editing.
  class EditDetails {
   public:
    struct BookmarkData {
      BookmarkData();
      BookmarkData(BookmarkData const& other);
      ~BookmarkData();

      std::u16string title;

      // Exactly one of the following should be non-empty.
      std::optional<GURL> url;
      std::vector<BookmarkData> children;
    };

    // Returns whether the existing/new node has an URL that can be modified.
    bool CanChangeUrl() const;

    // Returns the resource id for the string resource to use on the window
    // title for this edit operation.
    int GetWindowTitleId() const;

    // Returns an EditDetails instance for the user editing the given bookmark.
    static EditDetails EditNode(const bookmarks::BookmarkNode* node);

    // Returns an EditDetails instance for the user moving the given selection
    // of bookmarks. The initial parent node will be computed based on the
    // `nodes` storages and their parents similarity.
    static EditDetails MoveNodes(
        bookmarks::BookmarkModel* model,
        const std::vector<
            raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>& nodes);

    // Returns an EditDetails instance for the user adding a bookmark within
    // a given parent node with a specified index.
    static EditDetails AddNodeInFolder(
        const bookmarks::BookmarkNode* parent_node,
        size_t index,
        const GURL& url,
        const std::u16string& title);

    // Returns an EditDetails instance for the user adding a folder within a
    // given parent node with a specified index.
    static EditDetails AddFolder(const bookmarks::BookmarkNode* parent_node,
                                 size_t index);

    enum Type {
      // The user is editing an existing node in the model. The node the user
      // is editing is set in |existing_node|.
      EXISTING_NODE,

      // A new bookmark should be created if the user accepts the edit.
      // |existing_node| is null in this case.
      NEW_URL,

      // A new folder bookmark should be created if the user accepts the edit.
      // The contents of the folder should be that of |urls|.
      // |existing_node| is null in this case.
      NEW_FOLDER,

      // The user is moving one or multiple existing nodes in the model. The
      // nodes the user is moving are set in `existing_nodes_to_move`.
      MOVE
    };

    EditDetails(const EditDetails& other);
    ~EditDetails();

    // See description of enum value for details.
    const Type type;

    // If type == EXISTING_NODE this gives the existing node.
    raw_ptr<const bookmarks::BookmarkNode> existing_node = nullptr;

    // If type == MOVE this gives the existing nodes to move.
    base::flat_set<raw_ptr<const bookmarks::BookmarkNode, VectorExperimental>>
        existing_nodes_to_move;

    // This gives the initial parent node to place the node(s) in.
    raw_ptr<const bookmarks::BookmarkNode> parent_node = nullptr;

    // If type == NEW_URL or type == NEW_FOLDER this gives the index to insert
    // the new node at.
    std::optional<size_t> index;

    // If type == NEW_URL this contains the URL/title. If type == NEW_FOLDER,
    // this contains the folder title and any urls/title pairs or nested
    // folders it should contain.
    BookmarkData bookmark_data;

   private:
    explicit EditDetails(Type node_type);
  };

  // Shows the bookmark editor. The bookmark editor allows editing an existing
  // node, moving one or multiple existing nodes, or creating a new bookmark
  // node (as determined by |details.type|). |details.parent_node| is only used
  // if |details.existing_node| is null.
  static void Show(gfx::NativeWindow parent_window,
                   Profile* profile,
                   const EditDetails& details,
                   Configuration configuration,
                   OnSaveCallback on_save_callback = base::DoNothing());

  // Modifies a bookmark node.
  static void ApplyEdits(bookmarks::BookmarkModel* model,
                         const bookmarks::BookmarkNode* new_parent,
                         const EditDetails& details,
                         const std::u16string& new_title,
                         const GURL& new_url);
};

#endif  // CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_EDITOR_H_