Home | History | Annotate | Download | only in clipboard
      1 // Copyright (c) 2012 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 // This file implements the ScopedClipboardWriter class. Documentation on its
      6 // purpose can be found in our header. Documentation on the format of the
      7 // parameters for each clipboard target can be found in clipboard.h.
      8 
      9 #include "ui/base/clipboard/scoped_clipboard_writer.h"
     10 
     11 #include "base/pickle.h"
     12 #include "base/strings/utf_string_conversions.h"
     13 #include "net/base/escape.h"
     14 #include "ui/gfx/size.h"
     15 
     16 namespace ui {
     17 
     18 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard,
     19                                              ClipboardType type)
     20     : clipboard_(clipboard),
     21       type_(type) {
     22 }
     23 
     24 ScopedClipboardWriter::~ScopedClipboardWriter() {
     25   if (!objects_.empty() && clipboard_)
     26     clipboard_->WriteObjects(type_, objects_);
     27 }
     28 
     29 void ScopedClipboardWriter::WriteText(const base::string16& text) {
     30   WriteTextOrURL(text, false);
     31 }
     32 
     33 void ScopedClipboardWriter::WriteURL(const base::string16& text) {
     34   WriteTextOrURL(text, true);
     35 }
     36 
     37 void ScopedClipboardWriter::WriteHTML(const base::string16& markup,
     38                                       const std::string& source_url) {
     39   std::string utf8_markup = base::UTF16ToUTF8(markup);
     40 
     41   Clipboard::ObjectMapParams parameters;
     42   parameters.push_back(
     43       Clipboard::ObjectMapParam(utf8_markup.begin(),
     44                                 utf8_markup.end()));
     45   if (!source_url.empty()) {
     46     parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(),
     47                                                    source_url.end()));
     48   }
     49 
     50   objects_[Clipboard::CBF_HTML] = parameters;
     51 }
     52 
     53 void ScopedClipboardWriter::WriteRTF(const std::string& rtf_data) {
     54   Clipboard::ObjectMapParams parameters;
     55   parameters.push_back(Clipboard::ObjectMapParam(rtf_data.begin(),
     56                                                  rtf_data.end()));
     57   objects_[Clipboard::CBF_RTF] = parameters;
     58 }
     59 
     60 void ScopedClipboardWriter::WriteBookmark(const base::string16& bookmark_title,
     61                                           const std::string& url) {
     62   if (bookmark_title.empty() || url.empty())
     63     return;
     64 
     65   std::string utf8_markup = base::UTF16ToUTF8(bookmark_title);
     66 
     67   Clipboard::ObjectMapParams parameters;
     68   parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(),
     69                                                  utf8_markup.end()));
     70   parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end()));
     71   objects_[Clipboard::CBF_BOOKMARK] = parameters;
     72 }
     73 
     74 void ScopedClipboardWriter::WriteHyperlink(const base::string16& anchor_text,
     75                                            const std::string& url) {
     76   if (anchor_text.empty() || url.empty())
     77     return;
     78 
     79   // Construct the hyperlink.
     80   std::string html("<a href=\"");
     81   html.append(net::EscapeForHTML(url));
     82   html.append("\">");
     83   html.append(net::EscapeForHTML(base::UTF16ToUTF8(anchor_text)));
     84   html.append("</a>");
     85   WriteHTML(base::UTF8ToUTF16(html), std::string());
     86 }
     87 
     88 void ScopedClipboardWriter::WriteWebSmartPaste() {
     89   objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams();
     90 }
     91 
     92 void ScopedClipboardWriter::WritePickledData(
     93     const Pickle& pickle, const Clipboard::FormatType& format) {
     94   std::string format_string = format.Serialize();
     95   Clipboard::ObjectMapParam format_parameter(format_string.begin(),
     96                                              format_string.end());
     97   Clipboard::ObjectMapParam data_parameter;
     98 
     99   data_parameter.resize(pickle.size());
    100   memcpy(const_cast<char*>(&data_parameter.front()),
    101          pickle.data(), pickle.size());
    102 
    103   Clipboard::ObjectMapParams parameters;
    104   parameters.push_back(format_parameter);
    105   parameters.push_back(data_parameter);
    106   objects_[Clipboard::CBF_DATA] = parameters;
    107 }
    108 
    109 void ScopedClipboardWriter::Reset() {
    110   url_text_.clear();
    111   objects_.clear();
    112 }
    113 
    114 void ScopedClipboardWriter::WriteTextOrURL(const base::string16& text,
    115                                            bool is_url) {
    116   std::string utf8_text = base::UTF16ToUTF8(text);
    117 
    118   Clipboard::ObjectMapParams parameters;
    119   parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
    120                                                  utf8_text.end()));
    121   objects_[Clipboard::CBF_TEXT] = parameters;
    122 
    123   if (is_url) {
    124     url_text_ = utf8_text;
    125   } else {
    126     url_text_.clear();
    127   }
    128 }
    129 
    130 }  // namespace ui
    131