Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / bookmarks / browser / bookmark_node_data_views.cc
bloba56c0d2d8423a967c7906e4c30749cd8f8a94b6b
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.
5 #include "components/bookmarks/browser/bookmark_node_data.h"
7 #include "base/logging.h"
8 #include "base/pickle.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/base/dragdrop/os_exchange_data.h"
11 #include "url/url_constants.h"
13 namespace bookmarks {
15 // static
16 const ui::Clipboard::FormatType& BookmarkNodeData::GetBookmarkFormatType() {
17 CR_DEFINE_STATIC_LOCAL(
18 ui::Clipboard::FormatType,
19 format,
20 (ui::Clipboard::GetFormatType(BookmarkNodeData::kClipboardFormatString)));
22 return format;
25 void BookmarkNodeData::Write(const base::FilePath& profile_path,
26 ui::OSExchangeData* data) const {
27 DCHECK(data);
29 // If there is only one element and it is a URL, write the URL to the
30 // clipboard.
31 if (has_single_url()) {
32 if (elements[0].url.SchemeIs(url::kJavaScriptScheme)) {
33 data->SetString(base::UTF8ToUTF16(elements[0].url.spec()));
34 } else {
35 data->SetURL(elements[0].url, elements[0].title);
39 base::Pickle data_pickle;
40 WriteToPickle(profile_path, &data_pickle);
42 data->SetPickledData(GetBookmarkFormatType(), data_pickle);
45 bool BookmarkNodeData::Read(const ui::OSExchangeData& data) {
46 elements.clear();
48 profile_path_.clear();
50 if (data.HasCustomFormat(GetBookmarkFormatType())) {
51 base::Pickle drag_data_pickle;
52 if (data.GetPickledData(GetBookmarkFormatType(), &drag_data_pickle)) {
53 if (!ReadFromPickle(&drag_data_pickle))
54 return false;
56 } else {
57 // See if there is a URL on the clipboard.
58 GURL url;
59 base::string16 title;
60 if (data.GetURLAndTitle(
61 ui::OSExchangeData::CONVERT_FILENAMES, &url, &title))
62 ReadFromTuple(url, title);
65 return is_valid();
68 } // namespace bookmarks