| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/bookmarks/browser/bookmark_node_data.h" | 5 #include "components/bookmarks/browser/bookmark_node_data.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/pickle.h" | 10 #include "base/pickle.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 id_(node->id()) { | 30 id_(node->id()) { |
| 31 if (node->GetMetaInfoMap()) | 31 if (node->GetMetaInfoMap()) |
| 32 meta_info_map = *node->GetMetaInfoMap(); | 32 meta_info_map = *node->GetMetaInfoMap(); |
| 33 for (int i = 0; i < node->child_count(); ++i) | 33 for (int i = 0; i < node->child_count(); ++i) |
| 34 children.push_back(Element(node->GetChild(i))); | 34 children.push_back(Element(node->GetChild(i))); |
| 35 } | 35 } |
| 36 | 36 |
| 37 BookmarkNodeData::Element::~Element() { | 37 BookmarkNodeData::Element::~Element() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 void BookmarkNodeData::Element::WriteToPickle(Pickle* pickle) const { | 40 void BookmarkNodeData::Element::WriteToPickle(base::Pickle* pickle) const { |
| 41 pickle->WriteBool(is_url); | 41 pickle->WriteBool(is_url); |
| 42 pickle->WriteString(url.spec()); | 42 pickle->WriteString(url.spec()); |
| 43 pickle->WriteString16(title); | 43 pickle->WriteString16(title); |
| 44 pickle->WriteInt64(id_); | 44 pickle->WriteInt64(id_); |
| 45 pickle->WriteSizeT(meta_info_map.size()); | 45 pickle->WriteSizeT(meta_info_map.size()); |
| 46 for (BookmarkNode::MetaInfoMap::const_iterator it = meta_info_map.begin(); | 46 for (BookmarkNode::MetaInfoMap::const_iterator it = meta_info_map.begin(); |
| 47 it != meta_info_map.end(); ++it) { | 47 it != meta_info_map.end(); ++it) { |
| 48 pickle->WriteString(it->first); | 48 pickle->WriteString(it->first); |
| 49 pickle->WriteString(it->second); | 49 pickle->WriteString(it->second); |
| 50 } | 50 } |
| 51 if (!is_url) { | 51 if (!is_url) { |
| 52 pickle->WriteSizeT(children.size()); | 52 pickle->WriteSizeT(children.size()); |
| 53 for (std::vector<Element>::const_iterator i = children.begin(); | 53 for (std::vector<Element>::const_iterator i = children.begin(); |
| 54 i != children.end(); ++i) { | 54 i != children.end(); ++i) { |
| 55 i->WriteToPickle(pickle); | 55 i->WriteToPickle(pickle); |
| 56 } | 56 } |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 | 59 |
| 60 bool BookmarkNodeData::Element::ReadFromPickle(PickleIterator* iterator) { | 60 bool BookmarkNodeData::Element::ReadFromPickle(base::PickleIterator* iterator) { |
| 61 std::string url_spec; | 61 std::string url_spec; |
| 62 if (!iterator->ReadBool(&is_url) || | 62 if (!iterator->ReadBool(&is_url) || |
| 63 !iterator->ReadString(&url_spec) || | 63 !iterator->ReadString(&url_spec) || |
| 64 !iterator->ReadString16(&title) || | 64 !iterator->ReadString16(&title) || |
| 65 !iterator->ReadInt64(&id_)) { | 65 !iterator->ReadInt64(&id_)) { |
| 66 return false; | 66 return false; |
| 67 } | 67 } |
| 68 url = GURL(url_spec); | 68 url = GURL(url_spec); |
| 69 date_added = base::Time(); | 69 date_added = base::Time(); |
| 70 date_folder_modified = base::Time(); | 70 date_folder_modified = base::Time(); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 const base::string16 title = elements[i].title; | 187 const base::string16 title = elements[i].title; |
| 188 text += title; | 188 text += title; |
| 189 } else { | 189 } else { |
| 190 const base::string16 url = base::UTF8ToUTF16(elements[i].url.spec()); | 190 const base::string16 url = base::UTF8ToUTF16(elements[i].url.spec()); |
| 191 text += url; | 191 text += url; |
| 192 } | 192 } |
| 193 } | 193 } |
| 194 scw.WriteText(text); | 194 scw.WriteText(text); |
| 195 } | 195 } |
| 196 | 196 |
| 197 Pickle pickle; | 197 base::Pickle pickle; |
| 198 WriteToPickle(base::FilePath(), &pickle); | 198 WriteToPickle(base::FilePath(), &pickle); |
| 199 scw.WritePickledData(pickle, | 199 scw.WritePickledData(pickle, |
| 200 ui::Clipboard::GetFormatType(kClipboardFormatString)); | 200 ui::Clipboard::GetFormatType(kClipboardFormatString)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 bool BookmarkNodeData::ReadFromClipboard(ui::ClipboardType type) { | 203 bool BookmarkNodeData::ReadFromClipboard(ui::ClipboardType type) { |
| 204 DCHECK_EQ(type, ui::CLIPBOARD_TYPE_COPY_PASTE); | 204 DCHECK_EQ(type, ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 205 std::string data; | 205 std::string data; |
| 206 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | 206 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 207 clipboard->ReadData(ui::Clipboard::GetFormatType(kClipboardFormatString), | 207 clipboard->ReadData(ui::Clipboard::GetFormatType(kClipboardFormatString), |
| 208 &data); | 208 &data); |
| 209 | 209 |
| 210 if (!data.empty()) { | 210 if (!data.empty()) { |
| 211 Pickle pickle(data.data(), static_cast<int>(data.size())); | 211 base::Pickle pickle(data.data(), static_cast<int>(data.size())); |
| 212 if (ReadFromPickle(&pickle)) | 212 if (ReadFromPickle(&pickle)) |
| 213 return true; | 213 return true; |
| 214 } | 214 } |
| 215 | 215 |
| 216 base::string16 title; | 216 base::string16 title; |
| 217 std::string url; | 217 std::string url; |
| 218 clipboard->ReadBookmark(&title, &url); | 218 clipboard->ReadBookmark(&title, &url); |
| 219 if (!url.empty()) { | 219 if (!url.empty()) { |
| 220 Element element; | 220 Element element; |
| 221 element.is_url = true; | 221 element.is_url = true; |
| 222 element.url = GURL(url); | 222 element.url = GURL(url); |
| 223 element.title = title; | 223 element.title = title; |
| 224 | 224 |
| 225 elements.clear(); | 225 elements.clear(); |
| 226 elements.push_back(element); | 226 elements.push_back(element); |
| 227 return true; | 227 return true; |
| 228 } | 228 } |
| 229 | 229 |
| 230 return false; | 230 return false; |
| 231 } | 231 } |
| 232 #endif | 232 #endif |
| 233 | 233 |
| 234 void BookmarkNodeData::WriteToPickle(const base::FilePath& profile_path, | 234 void BookmarkNodeData::WriteToPickle(const base::FilePath& profile_path, |
| 235 Pickle* pickle) const { | 235 base::Pickle* pickle) const { |
| 236 profile_path.WriteToPickle(pickle); | 236 profile_path.WriteToPickle(pickle); |
| 237 pickle->WriteSizeT(size()); | 237 pickle->WriteSizeT(size()); |
| 238 | 238 |
| 239 for (size_t i = 0; i < size(); ++i) | 239 for (size_t i = 0; i < size(); ++i) |
| 240 elements[i].WriteToPickle(pickle); | 240 elements[i].WriteToPickle(pickle); |
| 241 } | 241 } |
| 242 | 242 |
| 243 bool BookmarkNodeData::ReadFromPickle(Pickle* pickle) { | 243 bool BookmarkNodeData::ReadFromPickle(base::Pickle* pickle) { |
| 244 PickleIterator data_iterator(*pickle); | 244 base::PickleIterator data_iterator(*pickle); |
| 245 size_t element_count; | 245 size_t element_count; |
| 246 if (profile_path_.ReadFromPickle(&data_iterator) && | 246 if (profile_path_.ReadFromPickle(&data_iterator) && |
| 247 data_iterator.ReadSizeT(&element_count)) { | 247 data_iterator.ReadSizeT(&element_count)) { |
| 248 std::vector<Element> tmp_elements; | 248 std::vector<Element> tmp_elements; |
| 249 tmp_elements.resize(element_count); | 249 tmp_elements.resize(element_count); |
| 250 for (size_t i = 0; i < element_count; ++i) { | 250 for (size_t i = 0; i < element_count; ++i) { |
| 251 if (!tmp_elements[i].ReadFromPickle(&data_iterator)) { | 251 if (!tmp_elements[i].ReadFromPickle(&data_iterator)) { |
| 252 return false; | 252 return false; |
| 253 } | 253 } |
| 254 } | 254 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 profile_path_ = profile_path; | 295 profile_path_ = profile_path; |
| 296 } | 296 } |
| 297 | 297 |
| 298 bool BookmarkNodeData::IsFromProfilePath( | 298 bool BookmarkNodeData::IsFromProfilePath( |
| 299 const base::FilePath& profile_path) const { | 299 const base::FilePath& profile_path) const { |
| 300 // An empty path means the data is not associated with any profile. | 300 // An empty path means the data is not associated with any profile. |
| 301 return !profile_path_.empty() && profile_path_ == profile_path; | 301 return !profile_path_.empty() && profile_path_ == profile_path; |
| 302 } | 302 } |
| 303 | 303 |
| 304 } // namespace bookmarks | 304 } // namespace bookmarks |
| OLD | NEW |