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
|
// 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_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
#define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
#include <stddef.h>
#include <stdint.h>
#include <string>
#include <vector>
#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "ui/base/clipboard/clipboard_buffer.h"
#include "url/gurl.h"
#if defined(TOOLKIT_VIEWS)
#include "ui/base/clipboard/clipboard_format_type.h"
#endif
namespace base {
class Pickle;
class PickleIterator;
}
#if defined(TOOLKIT_VIEWS)
namespace ui {
class OSExchangeData;
}
#endif
namespace bookmarks {
class BookmarkModel;
// BookmarkNodeData is used to represent the following:
//
// . A single URL.
// . A single node from the bookmark model.
// . A set of nodes from the bookmark model.
//
// BookmarkNodeData is used by bookmark related views to represent a dragged
// bookmark or bookmarks.
//
// Typical usage when writing data for a drag is:
// BookmarkNodeData data(node_user_is_dragging);
// data.Write(os_exchange_data_for_drag);
//
// Typical usage to read is:
// BookmarkNodeData data;
// if (data.Read(os_exchange_data))
// // data is valid, contents are in elements.
struct BookmarkNodeData {
// Element represents a single node.
struct Element {
Element();
explicit Element(const BookmarkNode* node);
Element(const Element& other);
~Element();
// If true, this element represents a URL.
bool is_url;
// The URL, only valid if is_url is true.
GURL url;
// Title of the entry, used for both urls and folders.
std::u16string title;
// Date of when this node was created.
base::Time date_added;
// Date of the last modification. Only used for folders.
base::Time date_folder_modified;
// Children, only used for non-URL nodes.
std::vector<Element> children;
// Meta info for the bookmark node.
BookmarkNode::MetaInfoMap meta_info_map;
int64_t id() const { return id_; }
private:
friend struct BookmarkNodeData;
#if !BUILDFLAG(IS_APPLE)
// For reading/writing this Element.
void WriteToPickle(base::Pickle* pickle) const;
bool ReadFromPickle(base::PickleIterator* iterator);
#endif
// ID of the node.
int64_t id_;
};
#if !BUILDFLAG(IS_APPLE)
// The MIME type for the clipboard format for BookmarkNodeData. This type is
// not used on the Mac.
static const char kClipboardFormatString[];
#endif
BookmarkNodeData();
BookmarkNodeData(const BookmarkNodeData& other);
// Created a BookmarkNodeData populated from the arguments.
explicit BookmarkNodeData(const BookmarkNode* node);
explicit BookmarkNodeData(
const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>&
nodes);
~BookmarkNodeData();
#if defined(TOOLKIT_VIEWS)
static const ui::ClipboardFormatType& GetBookmarkFormatType();
#endif
static bool ClipboardContainsBookmarks();
// Reads bookmarks from the given vector.
bool ReadFromVector(
const std::vector<raw_ptr<const BookmarkNode, VectorExperimental>>&
nodes);
// Creates a single-bookmark DragData from url/title pair.
bool ReadFromTuple(const GURL& url, const std::u16string& title);
// Writes bookmarks to the specified clipboard.
void WriteToClipboard(bool is_off_the_record);
// Reads bookmarks from the specified clipboard. Prefers data written via
// WriteToClipboard() but will also attempt to read a plain bookmark.
bool ReadFromClipboard(ui::ClipboardBuffer buffer);
#if defined(TOOLKIT_VIEWS)
// Writes elements to data. If there is only one element and it is a URL
// the URL and title are written to the clipboard in a format other apps can
// use.
// `profile_path` is used to identify which profile the data came from. Use an
// empty path to indicate that the data is not associated with any profile.
void Write(const base::FilePath& profile_path,
ui::OSExchangeData* data) const;
// Restores this data from the clipboard, returning true on success.
bool Read(const ui::OSExchangeData& data);
#endif
#if !BUILDFLAG(IS_APPLE)
// Writes the data for a drag to `pickle`.
void WriteToPickle(const base::FilePath& profile_path,
base::Pickle* pickle) const;
// Reads the data for a drag from a `pickle`.
bool ReadFromPickle(base::Pickle* pickle);
#endif
// Returns the nodes represented by this DragData. If this DragData was
// created from the same profile then the nodes from the model are returned.
// If the nodes can't be found (may have been deleted), an empty vector is
// returned.
std::vector<raw_ptr<const BookmarkNode, VectorExperimental>> GetNodes(
BookmarkModel* model,
const base::FilePath& profile_path) const;
// Convenience for getting the first node. Returns NULL if the data doesn't
// match any nodes or there is more than one node.
const BookmarkNode* GetFirstNode(BookmarkModel* model,
const base::FilePath& profile_path) const;
// Do we contain valid data?
bool is_valid() const { return !elements.empty(); }
// Returns true if there is a single url.
bool has_single_url() const { return size() == 1 && elements[0].is_url; }
// Number of elements.
size_t size() const { return elements.size(); }
// Clears the data.
void Clear();
// Sets `profile_path_`. This is useful for the constructors/readers that
// don't set it. This should only be called if the profile path is not
// already set.
void SetOriginatingProfilePath(const base::FilePath& profile_path);
// Returns true if this data is from the specified profile path.
bool IsFromProfilePath(const base::FilePath& profile_path) const;
// The actual elements written to the clipboard.
std::vector<Element> elements;
private:
// Path of the profile we originated from.
base::FilePath profile_path_;
};
} // namespace bookmarks
#endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
|