| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file implements the ScopedClipboardWriter class. Documentation on its | 5 // This file implements the ScopedClipboardWriter class. Documentation on its |
| 6 // purpose can be found in our header. Documentation on the format of the | 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. | 7 // parameters for each clipboard target can be found in clipboard.h. |
| 8 | 8 |
| 9 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 9 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 10 | 10 |
| 11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "net/base/escape.h" | 13 #include "net/base/escape.h" |
| 14 #include "ui/gfx/size.h" | 14 #include "ui/gfx/size.h" |
| 15 | 15 |
| 16 namespace ui { | 16 namespace ui { |
| 17 | 17 |
| 18 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard, | 18 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard, |
| 19 ClipboardType type) | 19 ClipboardType type) |
| 20 : clipboard_(clipboard), | 20 : clipboard_(clipboard), |
| 21 type_(type) { | 21 type_(type) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 ScopedClipboardWriter::~ScopedClipboardWriter() { | 24 ScopedClipboardWriter::~ScopedClipboardWriter() { |
| 25 if (!objects_.empty() && clipboard_) | 25 if (!objects_.empty() && clipboard_) |
| 26 clipboard_->WriteObjects(type_, objects_); | 26 clipboard_->WriteObjects(type_, objects_); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void ScopedClipboardWriter::WriteText(const string16& text) { | 29 void ScopedClipboardWriter::WriteText(const base::string16& text) { |
| 30 WriteTextOrURL(text, false); | 30 WriteTextOrURL(text, false); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void ScopedClipboardWriter::WriteURL(const string16& text) { | 33 void ScopedClipboardWriter::WriteURL(const base::string16& text) { |
| 34 WriteTextOrURL(text, true); | 34 WriteTextOrURL(text, true); |
| 35 } | 35 } |
| 36 | 36 |
| 37 void ScopedClipboardWriter::WriteHTML(const string16& markup, | 37 void ScopedClipboardWriter::WriteHTML(const base::string16& markup, |
| 38 const std::string& source_url) { | 38 const std::string& source_url) { |
| 39 std::string utf8_markup = UTF16ToUTF8(markup); | 39 std::string utf8_markup = UTF16ToUTF8(markup); |
| 40 | 40 |
| 41 Clipboard::ObjectMapParams parameters; | 41 Clipboard::ObjectMapParams parameters; |
| 42 parameters.push_back( | 42 parameters.push_back( |
| 43 Clipboard::ObjectMapParam(utf8_markup.begin(), | 43 Clipboard::ObjectMapParam(utf8_markup.begin(), |
| 44 utf8_markup.end())); | 44 utf8_markup.end())); |
| 45 if (!source_url.empty()) { | 45 if (!source_url.empty()) { |
| 46 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(), | 46 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(), |
| 47 source_url.end())); | 47 source_url.end())); |
| 48 } | 48 } |
| 49 | 49 |
| 50 objects_[Clipboard::CBF_HTML] = parameters; | 50 objects_[Clipboard::CBF_HTML] = parameters; |
| 51 } | 51 } |
| 52 | 52 |
| 53 void ScopedClipboardWriter::WriteRTF(const std::string& rtf_data) { | 53 void ScopedClipboardWriter::WriteRTF(const std::string& rtf_data) { |
| 54 Clipboard::ObjectMapParams parameters; | 54 Clipboard::ObjectMapParams parameters; |
| 55 parameters.push_back(Clipboard::ObjectMapParam(rtf_data.begin(), | 55 parameters.push_back(Clipboard::ObjectMapParam(rtf_data.begin(), |
| 56 rtf_data.end())); | 56 rtf_data.end())); |
| 57 objects_[Clipboard::CBF_RTF] = parameters; | 57 objects_[Clipboard::CBF_RTF] = parameters; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void ScopedClipboardWriter::WriteBookmark(const string16& bookmark_title, | 60 void ScopedClipboardWriter::WriteBookmark(const base::string16& bookmark_title, |
| 61 const std::string& url) { | 61 const std::string& url) { |
| 62 if (bookmark_title.empty() || url.empty()) | 62 if (bookmark_title.empty() || url.empty()) |
| 63 return; | 63 return; |
| 64 | 64 |
| 65 std::string utf8_markup = UTF16ToUTF8(bookmark_title); | 65 std::string utf8_markup = UTF16ToUTF8(bookmark_title); |
| 66 | 66 |
| 67 Clipboard::ObjectMapParams parameters; | 67 Clipboard::ObjectMapParams parameters; |
| 68 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), | 68 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), |
| 69 utf8_markup.end())); | 69 utf8_markup.end())); |
| 70 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); | 70 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); |
| 71 objects_[Clipboard::CBF_BOOKMARK] = parameters; | 71 objects_[Clipboard::CBF_BOOKMARK] = parameters; |
| 72 } | 72 } |
| 73 | 73 |
| 74 void ScopedClipboardWriter::WriteHyperlink(const string16& anchor_text, | 74 void ScopedClipboardWriter::WriteHyperlink(const base::string16& anchor_text, |
| 75 const std::string& url) { | 75 const std::string& url) { |
| 76 if (anchor_text.empty() || url.empty()) | 76 if (anchor_text.empty() || url.empty()) |
| 77 return; | 77 return; |
| 78 | 78 |
| 79 // Construct the hyperlink. | 79 // Construct the hyperlink. |
| 80 std::string html("<a href=\""); | 80 std::string html("<a href=\""); |
| 81 html.append(net::EscapeForHTML(url)); | 81 html.append(net::EscapeForHTML(url)); |
| 82 html.append("\">"); | 82 html.append("\">"); |
| 83 html.append(net::EscapeForHTML(UTF16ToUTF8(anchor_text))); | 83 html.append(net::EscapeForHTML(UTF16ToUTF8(anchor_text))); |
| 84 html.append("</a>"); | 84 html.append("</a>"); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 104 parameters.push_back(format_parameter); | 104 parameters.push_back(format_parameter); |
| 105 parameters.push_back(data_parameter); | 105 parameters.push_back(data_parameter); |
| 106 objects_[Clipboard::CBF_DATA] = parameters; | 106 objects_[Clipboard::CBF_DATA] = parameters; |
| 107 } | 107 } |
| 108 | 108 |
| 109 void ScopedClipboardWriter::Reset() { | 109 void ScopedClipboardWriter::Reset() { |
| 110 url_text_.clear(); | 110 url_text_.clear(); |
| 111 objects_.clear(); | 111 objects_.clear(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void ScopedClipboardWriter::WriteTextOrURL(const string16& text, bool is_url) { | 114 void ScopedClipboardWriter::WriteTextOrURL(const base::string16& text, |
| 115 bool is_url) { |
| 115 std::string utf8_text = UTF16ToUTF8(text); | 116 std::string utf8_text = UTF16ToUTF8(text); |
| 116 | 117 |
| 117 Clipboard::ObjectMapParams parameters; | 118 Clipboard::ObjectMapParams parameters; |
| 118 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), | 119 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), |
| 119 utf8_text.end())); | 120 utf8_text.end())); |
| 120 objects_[Clipboard::CBF_TEXT] = parameters; | 121 objects_[Clipboard::CBF_TEXT] = parameters; |
| 121 | 122 |
| 122 if (is_url) { | 123 if (is_url) { |
| 123 url_text_ = utf8_text; | 124 url_text_ = utf8_text; |
| 124 } else { | 125 } else { |
| 125 url_text_.clear(); | 126 url_text_.clear(); |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 } // namespace ui | 130 } // namespace ui |
| OLD | NEW |