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