1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
6 #define COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_
10 #include "base/files/file_path.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "components/bookmarks/browser/bookmark_node.h"
14 #include "ui/base/clipboard/clipboard_types.h"
17 #if defined(TOOLKIT_VIEWS)
18 #include "ui/base/dragdrop/os_exchange_data.h"
28 // BookmarkNodeData is used to represent the following:
31 // . A single node from the bookmark model.
32 // . A set of nodes from the bookmark model.
34 // BookmarkNodeData is used by bookmark related views to represent a dragged
35 // bookmark or bookmarks.
37 // Typical usage when writing data for a drag is:
38 // BookmarkNodeData data(node_user_is_dragging);
39 // data.Write(os_exchange_data_for_drag);
41 // Typical usage to read is:
42 // BookmarkNodeData data;
43 // if (data.Read(os_exchange_data))
44 // // data is valid, contents are in elements.
46 struct BookmarkNodeData
{
47 // Element represents a single node.
50 explicit Element(const BookmarkNode
* node
);
53 // If true, this element represents a URL.
56 // The URL, only valid if is_url is true.
59 // Title of the entry, used for both urls and folders.
62 // Date of when this node was created.
63 base::Time date_added
;
65 // Date of the last modification. Only used for folders.
66 base::Time date_folder_modified
;
68 // Children, only used for non-URL nodes.
69 std::vector
<Element
> children
;
71 // Meta info for the bookmark node.
72 BookmarkNode::MetaInfoMap meta_info_map
;
74 int64
id() const { return id_
; }
77 friend struct BookmarkNodeData
;
79 // For reading/writing this Element.
80 void WriteToPickle(Pickle
* pickle
) const;
81 bool ReadFromPickle(PickleIterator
* iterator
);
87 // The MIME type for the clipboard format for BookmarkNodeData.
88 static const char* kClipboardFormatString
;
92 // Created a BookmarkNodeData populated from the arguments.
93 explicit BookmarkNodeData(const BookmarkNode
* node
);
94 explicit BookmarkNodeData(const std::vector
<const BookmarkNode
*>& nodes
);
98 #if defined(TOOLKIT_VIEWS)
99 static const ui::OSExchangeData::CustomFormat
& GetBookmarkCustomFormat();
102 static bool ClipboardContainsBookmarks();
104 // Reads bookmarks from the given vector.
105 bool ReadFromVector(const std::vector
<const BookmarkNode
*>& nodes
);
107 // Creates a single-bookmark DragData from url/title pair.
108 bool ReadFromTuple(const GURL
& url
, const base::string16
& title
);
110 // Writes bookmarks to the specified clipboard.
111 void WriteToClipboard(ui::ClipboardType type
);
113 // Reads bookmarks from the specified clipboard. Prefers data written via
114 // WriteToClipboard() but will also attempt to read a plain bookmark.
115 bool ReadFromClipboard(ui::ClipboardType type
);
117 #if defined(TOOLKIT_VIEWS)
118 // Writes elements to data. If there is only one element and it is a URL
119 // the URL and title are written to the clipboard in a format other apps can
121 // |profile_path| is used to identify which profile the data came from. Use an
122 // empty path to indicate that the data is not associated with any profile.
123 void Write(const base::FilePath
& profile_path
,
124 ui::OSExchangeData
* data
) const;
126 // Restores this data from the clipboard, returning true on success.
127 bool Read(const ui::OSExchangeData
& data
);
130 // Writes the data for a drag to |pickle|.
131 void WriteToPickle(const base::FilePath
& profile_path
, Pickle
* pickle
) const;
133 // Reads the data for a drag from a |pickle|.
134 bool ReadFromPickle(Pickle
* pickle
);
136 // Returns the nodes represented by this DragData. If this DragData was
137 // created from the same profile then the nodes from the model are returned.
138 // If the nodes can't be found (may have been deleted), an empty vector is
140 std::vector
<const BookmarkNode
*> GetNodes(
141 BookmarkModel
* model
,
142 const base::FilePath
& profile_path
) const;
144 // Convenience for getting the first node. Returns NULL if the data doesn't
145 // match any nodes or there is more than one node.
146 const BookmarkNode
* GetFirstNode(BookmarkModel
* model
,
147 const base::FilePath
& profile_path
) const;
149 // Do we contain valid data?
150 bool is_valid() const { return !elements
.empty(); }
152 // Returns true if there is a single url.
153 bool has_single_url() const { return size() == 1 && elements
[0].is_url
; }
155 // Number of elements.
156 size_t size() const { return elements
.size(); }
161 // Sets |profile_path_|. This is useful for the constructors/readers that
162 // don't set it. This should only be called if the profile path is not
164 void SetOriginatingProfilePath(const base::FilePath
& profile_path
);
166 // Returns true if this data is from the specified profile path.
167 bool IsFromProfilePath(const base::FilePath
& profile_path
) const;
169 // The actual elements written to the clipboard.
170 std::vector
<Element
> elements
;
173 // Path of the profile we originated from.
174 base::FilePath profile_path_
;
177 } // namespace bookmarks
179 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_