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 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 153 } |
154 | 154 |
155 #if !defined(OS_MACOSX) | 155 #if !defined(OS_MACOSX) |
156 void BookmarkNodeData::WriteToClipboard(ui::ClipboardType clipboard_type) { | 156 void BookmarkNodeData::WriteToClipboard(ui::ClipboardType clipboard_type) { |
157 DCHECK(clipboard_type == ui::CLIPBOARD_TYPE_COPY_PASTE || | 157 DCHECK(clipboard_type == ui::CLIPBOARD_TYPE_COPY_PASTE || |
158 clipboard_type == ui::CLIPBOARD_TYPE_SELECTION); | 158 clipboard_type == ui::CLIPBOARD_TYPE_SELECTION); |
159 ui::ScopedClipboardWriter scw(clipboard_type); | 159 ui::ScopedClipboardWriter scw(clipboard_type); |
160 | 160 |
161 // If there is only one element and it is a URL, write the URL to the | 161 // If there is only one element and it is a URL, write the URL to the |
162 // clipboard. | 162 // clipboard. |
163 if (elements.size() == 1 && elements[0].is_url) { | 163 if (has_single_url()) { |
164 const base::string16& title = elements[0].title; | 164 const base::string16& title = elements[0].title; |
165 const std::string url = elements[0].url.spec(); | 165 const std::string url = elements[0].url.spec(); |
166 | 166 |
167 scw.WriteBookmark(title, url); | 167 scw.WriteBookmark(title, url); |
168 | 168 |
169 // Don't call scw.WriteHyperlink() here, since some rich text editors will | 169 // Don't call scw.WriteHyperlink() here, since some rich text editors will |
170 // change fonts when such data is pasted in; besides, most such editors | 170 // change fonts when such data is pasted in; besides, most such editors |
171 // auto-linkify at some point anyway. | 171 // auto-linkify at some point anyway. |
172 | 172 |
173 // Also write the URL to the clipboard as text so that it can be pasted | 173 // 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 | 174 // 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 | 175 // 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 | 176 // on Linux (on Windows and Mac, there is no difference between these |
177 // functions). | 177 // functions). |
178 scw.WriteText(base::UTF8ToUTF16(url)); | 178 scw.WriteText(base::UTF8ToUTF16(url)); |
179 } else { | 179 } else { |
180 // We have either more than one URL, a folder, or a combination of URLs | 180 // We have either more than one URL, a folder, or a combination of URLs |
181 // and folders. | 181 // and folders. |
182 base::string16 text; | 182 base::string16 text; |
183 for (size_t i = 0; i < elements.size(); i++) { | 183 for (size_t i = 0; i < size(); i++) { |
184 text += i == 0 ? base::ASCIIToUTF16("") : base::ASCIIToUTF16("\n"); | 184 text += i == 0 ? base::ASCIIToUTF16("") : base::ASCIIToUTF16("\n"); |
185 if (!elements[i].is_url) { | 185 if (!elements[i].is_url) { |
186 // Then it's a folder. Only copy the name of the folder. | 186 // Then it's a folder. Only copy the name of the folder. |
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 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 Pickle* pickle) const { |
236 profile_path.WriteToPickle(pickle); | 236 profile_path.WriteToPickle(pickle); |
237 pickle->WriteSizeT(elements.size()); | 237 pickle->WriteSizeT(size()); |
238 | 238 |
239 for (size_t i = 0; i < elements.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(Pickle* pickle) { |
244 PickleIterator data_iterator(*pickle); | 244 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 } |
255 elements.swap(tmp_elements); | 255 elements.swap(tmp_elements); |
256 } | 256 } |
257 | 257 |
258 return true; | 258 return true; |
259 } | 259 } |
260 | 260 |
261 std::vector<const BookmarkNode*> BookmarkNodeData::GetNodes( | 261 std::vector<const BookmarkNode*> BookmarkNodeData::GetNodes( |
262 BookmarkModel* model, | 262 BookmarkModel* model, |
263 const base::FilePath& profile_path) const { | 263 const base::FilePath& profile_path) const { |
264 std::vector<const BookmarkNode*> nodes; | 264 std::vector<const BookmarkNode*> nodes; |
265 | 265 |
266 if (!IsFromProfilePath(profile_path)) | 266 if (!IsFromProfilePath(profile_path)) |
267 return nodes; | 267 return nodes; |
268 | 268 |
269 for (size_t i = 0; i < elements.size(); ++i) { | 269 for (size_t i = 0; i < size(); ++i) { |
270 const BookmarkNode* node = GetBookmarkNodeByID(model, elements[i].id_); | 270 const BookmarkNode* node = GetBookmarkNodeByID(model, elements[i].id_); |
271 if (!node) { | 271 if (!node) { |
272 nodes.clear(); | 272 nodes.clear(); |
273 return nodes; | 273 return nodes; |
274 } | 274 } |
275 nodes.push_back(node); | 275 nodes.push_back(node); |
276 } | 276 } |
277 return nodes; | 277 return nodes; |
278 } | 278 } |
279 | 279 |
(...skipping 15 matching lines...) Expand all 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 |