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