- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / bookmarks / bookmark_node_data.h
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.
4
5 #ifndef CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_
6 #define CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_
7
8 #include <vector>
9
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"
14
15 #include "url/gurl.h"
16 #if defined(TOOLKIT_VIEWS)
17 #include "ui/base/dragdrop/os_exchange_data.h"
18 #endif
19
20 class BookmarkNode;
21 class Pickle;
22 class PickleIterator;
23 class Profile;
24
25 // BookmarkNodeData is used to represent the following:
26 //
27 // . A single URL.
28 // . A single node from the bookmark model.
29 // . A set of nodes from the bookmark model.
30 //
31 // BookmarkNodeData is used by bookmark related views to represent a dragged
32 // bookmark or bookmarks.
33 //
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);
37 //
38 // Typical usage to read is:
39 //   BookmarkNodeData data;
40 //   if (data.Read(os_exchange_data))
41 //     // data is valid, contents are in elements.
42
43 struct BookmarkNodeData {
44   // Element represents a single node.
45   struct Element {
46     Element();
47     explicit Element(const BookmarkNode* node);
48     ~Element();
49
50     // If true, this element represents a URL.
51     bool is_url;
52
53     // The URL, only valid if is_url is true.
54     GURL url;
55
56     // Title of the entry, used for both urls and folders.
57     string16 title;
58
59     // Date of when this node was created.
60     base::Time date_added;
61
62     // Date of the last modification. Only used for folders.
63     base::Time date_folder_modified;
64
65     // Children, only used for non-URL nodes.
66     std::vector<Element> children;
67
68     int64 id() const { return id_; }
69
70    private:
71     friend struct BookmarkNodeData;
72
73     // For reading/writing this Element.
74     void WriteToPickle(Pickle* pickle) const;
75     bool ReadFromPickle(Pickle* pickle, PickleIterator* iterator);
76
77     // ID of the node.
78     int64 id_;
79   };
80
81   // The MIME type for the clipboard format for BookmarkNodeData.
82   static const char* kClipboardFormatString;
83
84   BookmarkNodeData();
85
86   // Created a BookmarkNodeData populated from the arguments.
87   explicit BookmarkNodeData(const BookmarkNode* node);
88   explicit BookmarkNodeData(const std::vector<const BookmarkNode*>& nodes);
89
90   ~BookmarkNodeData();
91
92 #if defined(TOOLKIT_VIEWS)
93   static const ui::OSExchangeData::CustomFormat& GetBookmarkCustomFormat();
94 #endif
95
96   static bool ClipboardContainsBookmarks();
97
98   // Reads bookmarks from the given vector.
99   bool ReadFromVector(const std::vector<const BookmarkNode*>& nodes);
100
101   // Creates a single-bookmark DragData from url/title pair.
102   bool ReadFromTuple(const GURL& url, const string16& title);
103
104   // Writes bookmarks to the specified clipboard.
105   void WriteToClipboard(ui::ClipboardType type);
106
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);
110
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
114   // use.
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;
118
119   // Restores this data from the clipboard, returning true on success.
120   bool Read(const ui::OSExchangeData& data);
121 #endif
122
123   // Writes the data for a drag to |pickle|.
124   void WriteToPickle(Profile* profile, Pickle* pickle) const;
125
126   // Reads the data for a drag from a |pickle|.
127   bool ReadFromPickle(Pickle* pickle);
128
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
132   // returned.
133   std::vector<const BookmarkNode*> GetNodes(Profile* profile) const;
134
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;
138
139   // Do we contain valid data?
140   bool is_valid() const { return !elements.empty(); }
141
142   // Returns true if there is a single url.
143   bool has_single_url() const { return is_valid() && elements[0].is_url; }
144
145   // Number of elements.
146   size_t size() const { return elements.size(); }
147
148   // Clears the data.
149   void Clear();
150
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);
155
156   // Returns true if this data is from the specified profile.
157   bool IsFromProfile(Profile* profile) const;
158
159   // The actual elements written to the clipboard.
160   std::vector<Element> elements;
161
162  private:
163   // Path of the profile we originated from.
164   base::FilePath profile_path_;
165 };
166
167 #endif  // CHROME_BROWSER_BOOKMARKS_BOOKMARK_NODE_DATA_H_