Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_node_data.h
blob9b91ded428a790a047b4e7123df3957ecca9aaa8
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_
8 #include <vector>
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"
15 #include "url/gurl.h"
17 #if defined(TOOLKIT_VIEWS)
18 #include "ui/base/clipboard/clipboard.h"
19 #endif
21 namespace base {
22 class Pickle;
23 class PickleIterator;
26 #if defined(TOOLKIT_VIEWS)
27 namespace ui {
28 class OSExchangeData;
30 #endif
32 namespace bookmarks {
34 class BookmarkModel;
36 // BookmarkNodeData is used to represent the following:
38 // . A single URL.
39 // . A single node from the bookmark model.
40 // . A set of nodes from the bookmark model.
42 // BookmarkNodeData is used by bookmark related views to represent a dragged
43 // bookmark or bookmarks.
45 // Typical usage when writing data for a drag is:
46 // BookmarkNodeData data(node_user_is_dragging);
47 // data.Write(os_exchange_data_for_drag);
49 // Typical usage to read is:
50 // BookmarkNodeData data;
51 // if (data.Read(os_exchange_data))
52 // // data is valid, contents are in elements.
54 struct BookmarkNodeData {
55 // Element represents a single node.
56 struct Element {
57 Element();
58 explicit Element(const BookmarkNode* node);
59 ~Element();
61 // If true, this element represents a URL.
62 bool is_url;
64 // The URL, only valid if is_url is true.
65 GURL url;
67 // Title of the entry, used for both urls and folders.
68 base::string16 title;
70 // Date of when this node was created.
71 base::Time date_added;
73 // Date of the last modification. Only used for folders.
74 base::Time date_folder_modified;
76 // Children, only used for non-URL nodes.
77 std::vector<Element> children;
79 // Meta info for the bookmark node.
80 BookmarkNode::MetaInfoMap meta_info_map;
82 int64 id() const { return id_; }
84 private:
85 friend struct BookmarkNodeData;
87 // For reading/writing this Element.
88 void WriteToPickle(base::Pickle* pickle) const;
89 bool ReadFromPickle(base::PickleIterator* iterator);
91 // ID of the node.
92 int64 id_;
95 // The MIME type for the clipboard format for BookmarkNodeData.
96 static const char* kClipboardFormatString;
98 BookmarkNodeData();
100 // Created a BookmarkNodeData populated from the arguments.
101 explicit BookmarkNodeData(const BookmarkNode* node);
102 explicit BookmarkNodeData(const std::vector<const BookmarkNode*>& nodes);
104 ~BookmarkNodeData();
106 #if defined(TOOLKIT_VIEWS)
107 static const ui::Clipboard::FormatType& GetBookmarkFormatType();
108 #endif
110 static bool ClipboardContainsBookmarks();
112 // Reads bookmarks from the given vector.
113 bool ReadFromVector(const std::vector<const BookmarkNode*>& nodes);
115 // Creates a single-bookmark DragData from url/title pair.
116 bool ReadFromTuple(const GURL& url, const base::string16& title);
118 // Writes bookmarks to the specified clipboard.
119 void WriteToClipboard(ui::ClipboardType type);
121 // Reads bookmarks from the specified clipboard. Prefers data written via
122 // WriteToClipboard() but will also attempt to read a plain bookmark.
123 bool ReadFromClipboard(ui::ClipboardType type);
125 #if defined(TOOLKIT_VIEWS)
126 // Writes elements to data. If there is only one element and it is a URL
127 // the URL and title are written to the clipboard in a format other apps can
128 // use.
129 // |profile_path| is used to identify which profile the data came from. Use an
130 // empty path to indicate that the data is not associated with any profile.
131 void Write(const base::FilePath& profile_path,
132 ui::OSExchangeData* data) const;
134 // Restores this data from the clipboard, returning true on success.
135 bool Read(const ui::OSExchangeData& data);
136 #endif
138 // Writes the data for a drag to |pickle|.
139 void WriteToPickle(const base::FilePath& profile_path,
140 base::Pickle* pickle) const;
142 // Reads the data for a drag from a |pickle|.
143 bool ReadFromPickle(base::Pickle* pickle);
145 // Returns the nodes represented by this DragData. If this DragData was
146 // created from the same profile then the nodes from the model are returned.
147 // If the nodes can't be found (may have been deleted), an empty vector is
148 // returned.
149 std::vector<const BookmarkNode*> GetNodes(
150 BookmarkModel* model,
151 const base::FilePath& profile_path) const;
153 // Convenience for getting the first node. Returns NULL if the data doesn't
154 // match any nodes or there is more than one node.
155 const BookmarkNode* GetFirstNode(BookmarkModel* model,
156 const base::FilePath& profile_path) const;
158 // Do we contain valid data?
159 bool is_valid() const { return !elements.empty(); }
161 // Returns true if there is a single url.
162 bool has_single_url() const { return size() == 1 && elements[0].is_url; }
164 // Number of elements.
165 size_t size() const { return elements.size(); }
167 // Clears the data.
168 void Clear();
170 // Sets |profile_path_|. This is useful for the constructors/readers that
171 // don't set it. This should only be called if the profile path is not
172 // already set.
173 void SetOriginatingProfilePath(const base::FilePath& profile_path);
175 // Returns true if this data is from the specified profile path.
176 bool IsFromProfilePath(const base::FilePath& profile_path) const;
178 // The actual elements written to the clipboard.
179 std::vector<Element> elements;
181 private:
182 // Path of the profile we originated from.
183 base::FilePath profile_path_;
186 } // namespace bookmarks
188 #endif // COMPONENTS_BOOKMARKS_BROWSER_BOOKMARK_NODE_DATA_H_