1 // Copyright (c) 2012 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 CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_
6 #define CHROME_BROWSER_BOOKMARKS_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 "ui/base/clipboard/clipboard_types.h"
16 #if defined(TOOLKIT_VIEWS)
17 #include "ui/base/dragdrop/os_exchange_data.h"
25 // BookmarkNodeData is used to represent the following:
28 // . A single node from the bookmark model.
29 // . A set of nodes from the bookmark model.
31 // BookmarkNodeData is used by bookmark related views to represent a dragged
32 // bookmark or bookmarks.
34 // Typical usage when writing data for a drag is:
35 // BookmarkNodeData data(node_user_is_dragging);
36 // data.Write(os_exchange_data_for_drag);
38 // Typical usage to read is:
39 // BookmarkNodeData data;
40 // if (data.Read(os_exchange_data))
41 // // data is valid, contents are in elements.
43 struct BookmarkNodeData {
44 // Element represents a single node.
47 explicit Element(const BookmarkNode* node);
50 // If true, this element represents a URL.
53 // The URL, only valid if is_url is true.
56 // Title of the entry, used for both urls and folders.
59 // Date of when this node was created.
60 base::Time date_added;
62 // Date of the last modification. Only used for folders.
63 base::Time date_folder_modified;
65 // Children, only used for non-URL nodes.
66 std::vector<Element> children;
68 int64 id() const { return id_; }
71 friend struct BookmarkNodeData;
73 // For reading/writing this Element.
74 void WriteToPickle(Pickle* pickle) const;
75 bool ReadFromPickle(Pickle* pickle, PickleIterator* iterator);
81 // The MIME type for the clipboard format for BookmarkNodeData.
82 static const char* kClipboardFormatString;
86 // Created a BookmarkNodeData populated from the arguments.
87 explicit BookmarkNodeData(const BookmarkNode* node);
88 explicit BookmarkNodeData(const std::vector<const BookmarkNode*>& nodes);
92 #if defined(TOOLKIT_VIEWS)
93 static const ui::OSExchangeData::CustomFormat& GetBookmarkCustomFormat();
96 static bool ClipboardContainsBookmarks();
98 // Reads bookmarks from the given vector.
99 bool ReadFromVector(const std::vector<const BookmarkNode*>& nodes);
101 // Creates a single-bookmark DragData from url/title pair.
102 bool ReadFromTuple(const GURL& url, const string16& title);
104 // Writes bookmarks to the specified clipboard.
105 void WriteToClipboard(ui::ClipboardType type);
107 // Reads bookmarks from the specified clipboard. Prefers data written via
108 // WriteToClipboard() but will also attempt to read a plain bookmark.
109 bool ReadFromClipboard(ui::ClipboardType type);
111 #if defined(TOOLKIT_VIEWS)
112 // Writes elements to data. If there is only one element and it is a URL
113 // the URL and title are written to the clipboard in a format other apps can
115 // |profile| is used to identify which profile the data came from. Use a
116 // value of null to indicate the data is not associated with any profile.
117 void Write(Profile* profile, ui::OSExchangeData* data) const;
119 // Restores this data from the clipboard, returning true on success.
120 bool Read(const ui::OSExchangeData& data);
123 // Writes the data for a drag to |pickle|.
124 void WriteToPickle(Profile* profile, Pickle* pickle) const;
126 // Reads the data for a drag from a |pickle|.
127 bool ReadFromPickle(Pickle* pickle);
129 // Returns the nodes represented by this DragData. If this DragData was
130 // created from the same profile then the nodes from the model are returned.
131 // If the nodes can't be found (may have been deleted), an empty vector is
133 std::vector<const BookmarkNode*> GetNodes(Profile* profile) const;
135 // Convenience for getting the first node. Returns NULL if the data doesn't
136 // match any nodes or there is more than one node.
137 const BookmarkNode* GetFirstNode(Profile* profile) const;
139 // Do we contain valid data?
140 bool is_valid() const { return !elements.empty(); }
142 // Returns true if there is a single url.
143 bool has_single_url() const { return is_valid() && elements[0].is_url; }
145 // Number of elements.
146 size_t size() const { return elements.size(); }
151 // Sets |profile_path_| to that of |profile|. This is useful for the
152 // constructors/readers that don't set it. This should only be called if the
153 // profile path is not already set.
154 void SetOriginatingProfile(Profile* profile);
156 // Returns true if this data is from the specified profile.
157 bool IsFromProfile(Profile* profile) const;
159 // The actual elements written to the clipboard.
160 std::vector<Element> elements;
163 // Path of the profile we originated from.
164 base::FilePath profile_path_;
167 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_