Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / bookmarks / core / browser / bookmark_node_data_views.cc
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.
4
5 #include "components/bookmarks/core/browser/bookmark_node_data.h"
6
7 #include "base/logging.h"
8 #include "base/pickle.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "ui/base/clipboard/clipboard.h"
11
12 namespace {
13
14 const char kJavaScriptScheme[] = "javascript";
15
16 }  // namespace
17
18 // static
19 const ui::OSExchangeData::CustomFormat&
20 BookmarkNodeData::GetBookmarkCustomFormat() {
21   CR_DEFINE_STATIC_LOCAL(
22       ui::OSExchangeData::CustomFormat,
23       format,
24       (ui::Clipboard::GetFormatType(BookmarkNodeData::kClipboardFormatString)));
25
26   return format;
27 }
28
29 void BookmarkNodeData::Write(const base::FilePath& profile_path,
30                              ui::OSExchangeData* data) const {
31   DCHECK(data);
32
33   // If there is only one element and it is a URL, write the URL to the
34   // clipboard.
35   if (elements.size() == 1 && elements[0].is_url) {
36     if (elements[0].url.SchemeIs(kJavaScriptScheme)) {
37       data->SetString(base::UTF8ToUTF16(elements[0].url.spec()));
38     } else {
39       data->SetURL(elements[0].url, elements[0].title);
40     }
41   }
42
43   Pickle data_pickle;
44   WriteToPickle(profile_path, &data_pickle);
45
46   data->SetPickledData(GetBookmarkCustomFormat(), data_pickle);
47 }
48
49 bool BookmarkNodeData::Read(const ui::OSExchangeData& data) {
50   elements.clear();
51
52   profile_path_.clear();
53
54   if (data.HasCustomFormat(GetBookmarkCustomFormat())) {
55     Pickle drag_data_pickle;
56     if (data.GetPickledData(GetBookmarkCustomFormat(), &drag_data_pickle)) {
57       if (!ReadFromPickle(&drag_data_pickle))
58         return false;
59     }
60   } else {
61     // See if there is a URL on the clipboard.
62     Element element;
63     GURL url;
64     base::string16 title;
65     if (data.GetURLAndTitle(
66             ui::OSExchangeData::CONVERT_FILENAMES, &url, &title))
67       ReadFromTuple(url, title);
68   }
69
70   return is_valid();
71 }