Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_node_data.h
blobfbb975ea244b63142b2535309b7418da1560dd79
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_
8 #include <vector>
10 #include "base/files/file_path.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/bookmarks/bookmark_model.h"
14 #include "ui/base/clipboard/clipboard_types.h"
16 #include "url/gurl.h"
17 #if defined(TOOLKIT_VIEWS)
18 #include "ui/base/dragdrop/os_exchange_data.h"
19 #endif
21 class Pickle;
22 class PickleIterator;
23 class Profile;
25 // BookmarkNodeData is used to represent the following:
27 // . A single URL.
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.
45 struct Element {
46 Element();
47 explicit Element(const BookmarkNode* node);
48 ~Element();
50 // If true, this element represents a URL.
51 bool is_url;
53 // The URL, only valid if is_url is true.
54 GURL url;
56 // Title of the entry, used for both urls and folders.
57 base::string16 title;
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 // Meta info for the bookmark node.
69 BookmarkNode::MetaInfoMap meta_info_map;
71 int64 id() const { return id_; }
73 private:
74 friend struct BookmarkNodeData;
76 // For reading/writing this Element.
77 void WriteToPickle(Pickle* pickle) const;
78 bool ReadFromPickle(Pickle* pickle, PickleIterator* iterator);
80 // ID of the node.
81 int64 id_;
84 // The MIME type for the clipboard format for BookmarkNodeData.
85 static const char* kClipboardFormatString;
87 BookmarkNodeData();
89 // Created a BookmarkNodeData populated from the arguments.
90 explicit BookmarkNodeData(const BookmarkNode* node);
91 explicit BookmarkNodeData(const std::vector<const BookmarkNode*>& nodes);
93 ~BookmarkNodeData();
95 #if defined(TOOLKIT_VIEWS)
96 static const ui::OSExchangeData::CustomFormat& GetBookmarkCustomFormat();
97 #endif
99 static bool ClipboardContainsBookmarks();
101 // Reads bookmarks from the given vector.
102 bool ReadFromVector(const std::vector<const BookmarkNode*>& nodes);
104 // Creates a single-bookmark DragData from url/title pair.
105 bool ReadFromTuple(const GURL& url, const base::string16& title);
107 // Writes bookmarks to the specified clipboard.
108 void WriteToClipboard(ui::ClipboardType type);
110 // Reads bookmarks from the specified clipboard. Prefers data written via
111 // WriteToClipboard() but will also attempt to read a plain bookmark.
112 bool ReadFromClipboard(ui::ClipboardType type);
114 #if defined(TOOLKIT_VIEWS)
115 // Writes elements to data. If there is only one element and it is a URL
116 // the URL and title are written to the clipboard in a format other apps can
117 // use.
118 // |profile| is used to identify which profile the data came from. Use a
119 // value of null to indicate the data is not associated with any profile.
120 void Write(Profile* profile, ui::OSExchangeData* data) const;
122 // Restores this data from the clipboard, returning true on success.
123 bool Read(const ui::OSExchangeData& data);
124 #endif
126 // Writes the data for a drag to |pickle|.
127 void WriteToPickle(Profile* profile, Pickle* pickle) const;
129 // Reads the data for a drag from a |pickle|.
130 bool ReadFromPickle(Pickle* pickle);
132 // Returns the nodes represented by this DragData. If this DragData was
133 // created from the same profile then the nodes from the model are returned.
134 // If the nodes can't be found (may have been deleted), an empty vector is
135 // returned.
136 std::vector<const BookmarkNode*> GetNodes(Profile* profile) const;
138 // Convenience for getting the first node. Returns NULL if the data doesn't
139 // match any nodes or there is more than one node.
140 const BookmarkNode* GetFirstNode(Profile* profile) const;
142 // Do we contain valid data?
143 bool is_valid() const { return !elements.empty(); }
145 // Returns true if there is a single url.
146 bool has_single_url() const { return is_valid() && elements[0].is_url; }
148 // Number of elements.
149 size_t size() const { return elements.size(); }
151 // Clears the data.
152 void Clear();
154 // Sets |profile_path_| to that of |profile|. This is useful for the
155 // constructors/readers that don't set it. This should only be called if the
156 // profile path is not already set.
157 void SetOriginatingProfile(Profile* profile);
159 // Returns true if this data is from the specified profile.
160 bool IsFromProfile(Profile* profile) const;
162 // The actual elements written to the clipboard.
163 std::vector<Element> elements;
165 private:
166 // Path of the profile we originated from.
167 base::FilePath profile_path_;
170 #endif // CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_