Home | History | Annotate | Download | only in bookmarks
      1 // Copyright 2013 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 "chrome/browser/bookmarks/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 "content/public/common/url_constants.h"
     11 #include "ui/base/clipboard/clipboard.h"
     12 
     13 // static
     14 const ui::OSExchangeData::CustomFormat&
     15 BookmarkNodeData::GetBookmarkCustomFormat() {
     16   CR_DEFINE_STATIC_LOCAL(
     17       ui::OSExchangeData::CustomFormat,
     18       format,
     19       (ui::Clipboard::GetFormatType(BookmarkNodeData::kClipboardFormatString)));
     20 
     21   return format;
     22 }
     23 
     24 void BookmarkNodeData::Write(Profile* profile, ui::OSExchangeData* data) const {
     25   DCHECK(data);
     26 
     27   // If there is only one element and it is a URL, write the URL to the
     28   // clipboard.
     29   if (elements.size() == 1 && elements[0].is_url) {
     30     if (elements[0].url.SchemeIs(chrome::kJavaScriptScheme)) {
     31       data->SetString(UTF8ToUTF16(elements[0].url.spec()));
     32     } else {
     33       data->SetURL(elements[0].url, elements[0].title);
     34     }
     35   }
     36 
     37   Pickle data_pickle;
     38   WriteToPickle(profile, &data_pickle);
     39 
     40   data->SetPickledData(GetBookmarkCustomFormat(), data_pickle);
     41 }
     42 
     43 bool BookmarkNodeData::Read(const ui::OSExchangeData& data) {
     44   elements.clear();
     45 
     46   profile_path_.clear();
     47 
     48   if (data.HasCustomFormat(GetBookmarkCustomFormat())) {
     49     Pickle drag_data_pickle;
     50     if (data.GetPickledData(GetBookmarkCustomFormat(), &drag_data_pickle)) {
     51       if (!ReadFromPickle(&drag_data_pickle))
     52         return false;
     53     }
     54   } else {
     55     // See if there is a URL on the clipboard.
     56     Element element;
     57     GURL url;
     58     string16 title;
     59     if (data.GetURLAndTitle(&url, &title))
     60       ReadFromTuple(url, title);
     61   }
     62 
     63   return is_valid();
     64 }
     65