15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/bookmarks/browser/bookmark_node_data.h"
6a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
7a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/logging.h"
8a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "base/pickle.h"
9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "ui/base/clipboard/clipboard.h"
11a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace bookmarks {
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
14a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochnamespace {
15a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
16a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochconst char kJavaScriptScheme[] = "javascript";
17a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
18a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch}  // namespace
19a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// static
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)const ui::OSExchangeData::CustomFormat&
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)BookmarkNodeData::GetBookmarkCustomFormat() {
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  CR_DEFINE_STATIC_LOCAL(
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      ui::OSExchangeData::CustomFormat,
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      format,
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      (ui::Clipboard::GetFormatType(BookmarkNodeData::kClipboardFormatString)));
27a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
28a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return format;
29a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
30a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid BookmarkNodeData::Write(const base::FilePath& profile_path,
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                             ui::OSExchangeData* data) const {
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  DCHECK(data);
34a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // If there is only one element and it is a URL, write the URL to the
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // clipboard.
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (elements.size() == 1 && elements[0].is_url) {
38a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    if (elements[0].url.SchemeIs(kJavaScriptScheme)) {
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      data->SetString(base::UTF8ToUTF16(elements[0].url.spec()));
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    } else {
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      data->SetURL(elements[0].url, elements[0].title);
42a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
43a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
45a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  Pickle data_pickle;
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  WriteToPickle(profile_path, &data_pickle);
47a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
48a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  data->SetPickledData(GetBookmarkCustomFormat(), data_pickle);
49a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
50a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
51a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)bool BookmarkNodeData::Read(const ui::OSExchangeData& data) {
52a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  elements.clear();
53a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
54a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  profile_path_.clear();
55a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
56a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (data.HasCustomFormat(GetBookmarkCustomFormat())) {
57a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    Pickle drag_data_pickle;
58a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (data.GetPickledData(GetBookmarkCustomFormat(), &drag_data_pickle)) {
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      if (!ReadFromPickle(&drag_data_pickle))
60a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)        return false;
61a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    }
62a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
63a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // See if there is a URL on the clipboard.
64a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    Element element;
65a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    GURL url;
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::string16 title;
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (data.GetURLAndTitle(
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            ui::OSExchangeData::CONVERT_FILENAMES, &url, &title))
69a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      ReadFromTuple(url, title);
70a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return is_valid();
73a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace bookmarks
76