| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/bookmarks/bookmark_node_data.h" | 5 #include "chrome/browser/bookmarks/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" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/bookmarks/bookmark_model.h" | 13 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 14 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 16 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 17 | 17 |
| 18 const char* BookmarkNodeData::kClipboardFormatString = | |
| 19 "chromium/x-bookmark-entries"; | |
| 20 | |
| 21 BookmarkNodeData::Element::Element() : is_url(false), id_(0) { | 18 BookmarkNodeData::Element::Element() : is_url(false), id_(0) { |
| 22 } | 19 } |
| 23 | 20 |
| 24 BookmarkNodeData::Element::Element(const BookmarkNode* node) | 21 BookmarkNodeData::Element::Element(const BookmarkNode* node) |
| 25 : is_url(node->is_url()), | 22 : is_url(node->is_url()), |
| 26 url(node->url()), | 23 url(node->url()), |
| 27 title(node->GetTitle()), | 24 title(node->GetTitle()), |
| 28 date_added(node->date_added()), | 25 date_added(node->date_added()), |
| 29 date_folder_modified(node->date_folder_modified()), | 26 date_folder_modified(node->date_folder_modified()), |
| 30 id_(node->id()) { | 27 id_(node->id()) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 children.push_back(Element()); | 89 children.push_back(Element()); |
| 93 if (!children.back().ReadFromPickle(pickle, iterator)) | 90 if (!children.back().ReadFromPickle(pickle, iterator)) |
| 94 return false; | 91 return false; |
| 95 } | 92 } |
| 96 } | 93 } |
| 97 return true; | 94 return true; |
| 98 } | 95 } |
| 99 | 96 |
| 100 // BookmarkNodeData ----------------------------------------------------------- | 97 // BookmarkNodeData ----------------------------------------------------------- |
| 101 | 98 |
| 99 // static |
| 100 const ui::Clipboard::FormatType& BookmarkNodeData::GetFormatType() { |
| 101 CR_DEFINE_STATIC_LOCAL( |
| 102 ui::Clipboard::FormatType, |
| 103 type, |
| 104 (ui::Clipboard::GetFormatType("chromium/x-bookmark-entries"))); |
| 105 return type; |
| 106 } |
| 107 |
| 102 BookmarkNodeData::BookmarkNodeData() { | 108 BookmarkNodeData::BookmarkNodeData() { |
| 103 } | 109 } |
| 104 | 110 |
| 105 BookmarkNodeData::BookmarkNodeData(const BookmarkNode* node) { | 111 BookmarkNodeData::BookmarkNodeData(const BookmarkNode* node) { |
| 106 elements.push_back(Element(node)); | 112 elements.push_back(Element(node)); |
| 107 } | 113 } |
| 108 | 114 |
| 109 BookmarkNodeData::BookmarkNodeData( | 115 BookmarkNodeData::BookmarkNodeData( |
| 110 const std::vector<const BookmarkNode*>& nodes) { | 116 const std::vector<const BookmarkNode*>& nodes) { |
| 111 ReadFromVector(nodes); | 117 ReadFromVector(nodes); |
| 112 } | 118 } |
| 113 | 119 |
| 114 BookmarkNodeData::~BookmarkNodeData() { | 120 BookmarkNodeData::~BookmarkNodeData() { |
| 115 } | 121 } |
| 116 | 122 |
| 117 #if !defined(OS_MACOSX) | 123 #if !defined(OS_MACOSX) |
| 118 // static | 124 // static |
| 119 bool BookmarkNodeData::ClipboardContainsBookmarks() { | 125 bool BookmarkNodeData::ClipboardContainsBookmarks() { |
| 120 return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable( | 126 return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable( |
| 121 ui::Clipboard::GetFormatType(kClipboardFormatString), | 127 GetFormatType(), |
| 122 ui::CLIPBOARD_TYPE_COPY_PASTE); | 128 ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 123 } | 129 } |
| 124 #endif | 130 #endif |
| 125 | 131 |
| 126 bool BookmarkNodeData::ReadFromVector( | 132 bool BookmarkNodeData::ReadFromVector( |
| 127 const std::vector<const BookmarkNode*>& nodes) { | 133 const std::vector<const BookmarkNode*>& nodes) { |
| 128 Clear(); | 134 Clear(); |
| 129 | 135 |
| 130 if (nodes.empty()) | 136 if (nodes.empty()) |
| 131 return false; | 137 return false; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // Also write the URL to the clipboard as text so that it can be pasted | 179 // Also write the URL to the clipboard as text so that it can be pasted |
| 174 // into text fields. We use WriteText instead of WriteURL because we don't | 180 // into text fields. We use WriteText instead of WriteURL because we don't |
| 175 // want to clobber the X clipboard when the user copies out of the omnibox | 181 // want to clobber the X clipboard when the user copies out of the omnibox |
| 176 // on Linux (on Windows and Mac, there is no difference between these | 182 // on Linux (on Windows and Mac, there is no difference between these |
| 177 // functions). | 183 // functions). |
| 178 scw.WriteText(base::UTF8ToUTF16(url)); | 184 scw.WriteText(base::UTF8ToUTF16(url)); |
| 179 } | 185 } |
| 180 | 186 |
| 181 Pickle pickle; | 187 Pickle pickle; |
| 182 WriteToPickle(NULL, &pickle); | 188 WriteToPickle(NULL, &pickle); |
| 183 scw.WritePickledData(pickle, | 189 scw.WritePickledData(pickle, GetFormatType()); |
| 184 ui::Clipboard::GetFormatType(kClipboardFormatString)); | |
| 185 } | 190 } |
| 186 | 191 |
| 187 bool BookmarkNodeData::ReadFromClipboard(ui::ClipboardType type) { | 192 bool BookmarkNodeData::ReadFromClipboard(ui::ClipboardType type) { |
| 188 DCHECK_EQ(type, ui::CLIPBOARD_TYPE_COPY_PASTE); | 193 DCHECK_EQ(type, ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 189 std::string data; | 194 std::string data; |
| 190 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); | 195 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 191 clipboard->ReadData(ui::Clipboard::GetFormatType(kClipboardFormatString), | 196 clipboard->ReadData(GetFormatType(), &data); |
| 192 &data); | |
| 193 | 197 |
| 194 if (!data.empty()) { | 198 if (!data.empty()) { |
| 195 Pickle pickle(data.data(), data.size()); | 199 Pickle pickle(data.data(), data.size()); |
| 196 if (ReadFromPickle(&pickle)) | 200 if (ReadFromPickle(&pickle)) |
| 197 return true; | 201 return true; |
| 198 } | 202 } |
| 199 | 203 |
| 200 base::string16 title; | 204 base::string16 title; |
| 201 std::string url; | 205 std::string url; |
| 202 clipboard->ReadBookmark(&title, &url); | 206 clipboard->ReadBookmark(&title, &url); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 DCHECK(profile_path_.empty()); | 279 DCHECK(profile_path_.empty()); |
| 276 | 280 |
| 277 if (profile) | 281 if (profile) |
| 278 profile_path_ = profile->GetPath(); | 282 profile_path_ = profile->GetPath(); |
| 279 } | 283 } |
| 280 | 284 |
| 281 bool BookmarkNodeData::IsFromProfile(Profile* profile) const { | 285 bool BookmarkNodeData::IsFromProfile(Profile* profile) const { |
| 282 // An empty path means the data is not associated with any profile. | 286 // An empty path means the data is not associated with any profile. |
| 283 return !profile_path_.empty() && profile_path_ == profile->GetPath(); | 287 return !profile_path_.empty() && profile_path_ == profile->GetPath(); |
| 284 } | 288 } |
| OLD | NEW |