OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2008 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 "base/scoped_clipboard_writer.h" |
| 6 |
| 7 #include "base/clipboard.h" |
| 8 #include "base/string_util.h" |
| 9 |
| 10 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard) |
| 11 : clipboard_(clipboard) { |
| 12 } |
| 13 |
| 14 ScopedClipboardWriter::~ScopedClipboardWriter() { |
| 15 if (!objects_.empty() && clipboard_) |
| 16 clipboard_->WriteObjects(objects_); |
| 17 } |
| 18 |
| 19 void ScopedClipboardWriter::WriteText(const std::wstring& text) { |
| 20 if (text.empty()) |
| 21 return; |
| 22 |
| 23 std::string utf8_text = WideToUTF8(text); |
| 24 |
| 25 Clipboard::ObjectMapParams params; |
| 26 params.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), |
| 27 utf8_text.end())); |
| 28 objects_[Clipboard::CBF_TEXT] = params; |
| 29 } |
| 30 |
| 31 void ScopedClipboardWriter::WriteHTML(const std::wstring& markup, |
| 32 const std::string& src_url) { |
| 33 if (markup.empty()) |
| 34 return; |
| 35 |
| 36 std::string utf8_markup = WideToUTF8(markup); |
| 37 |
| 38 Clipboard::ObjectMapParams params; |
| 39 params.push_back( |
| 40 Clipboard::ObjectMapParam(utf8_markup.begin(), |
| 41 utf8_markup.end())); |
| 42 if (!src_url.empty()) { |
| 43 params.push_back(Clipboard::ObjectMapParam(src_url.begin(), |
| 44 src_url.end())); |
| 45 } |
| 46 |
| 47 objects_[Clipboard::CBF_HTML] = params; |
| 48 } |
| 49 |
| 50 void ScopedClipboardWriter::WriteBookmark(const std::wstring& title, |
| 51 const std::string& url) { |
| 52 if (title.empty() || url.empty()) |
| 53 return; |
| 54 |
| 55 std::string utf8_markup = WideToUTF8(title); |
| 56 |
| 57 Clipboard::ObjectMapParams params; |
| 58 params.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), |
| 59 utf8_markup.end())); |
| 60 params.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); |
| 61 objects_[Clipboard::CBF_BOOKMARK] = params; |
| 62 } |
| 63 |
| 64 void ScopedClipboardWriter::WriteHyperlink(const std::wstring& title, |
| 65 const std::string& url) { |
| 66 if (title.empty() || url.empty()) |
| 67 return; |
| 68 |
| 69 std::string utf8_markup = WideToUTF8(title); |
| 70 |
| 71 Clipboard::ObjectMapParams params; |
| 72 params.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), |
| 73 utf8_markup.end())); |
| 74 params.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); |
| 75 objects_[Clipboard::CBF_LINK] = params; |
| 76 } |
| 77 |
| 78 void ScopedClipboardWriter::WriteFile(const std::wstring& file) { |
| 79 WriteFiles(std::vector<std::wstring>(1, file)); |
| 80 } |
| 81 |
| 82 // Save the filenames as a string separated by nulls and terminated with an |
| 83 // extra null. |
| 84 void ScopedClipboardWriter::WriteFiles(const std::vector<std::wstring>& files) { |
| 85 if (files.empty()) |
| 86 return; |
| 87 |
| 88 Clipboard::ObjectMapParam param; |
| 89 |
| 90 for (std::vector<std::wstring>::const_iterator iter = files.begin(); |
| 91 iter != files.end(); ++iter) { |
| 92 std::string filename = WideToUTF8(*iter); |
| 93 for (std::string::const_iterator filename_iter = filename.begin(); |
| 94 filename_iter != filename.end(); ++filename_iter) { |
| 95 param.push_back(*filename_iter); |
| 96 } |
| 97 param.push_back('\0'); |
| 98 } |
| 99 |
| 100 param.push_back('\0'); |
| 101 |
| 102 Clipboard::ObjectMapParams params; |
| 103 params.push_back(param); |
| 104 objects_[Clipboard::CBF_FILES] = params; |
| 105 } |
| 106 |
| 107 #if defined(OS_WIN) |
| 108 void ScopedClipboardWriter::WriteWebSmartPaste() { |
| 109 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams(); |
| 110 } |
| 111 |
| 112 void ScopedClipboardWriter::WriteBitmapFromPixels(const void* pixels, |
| 113 const gfx::Size& size) { |
| 114 Clipboard::ObjectMapParam param1, param2; |
| 115 const char* pixels_data = reinterpret_cast<const char*>(pixels); |
| 116 size_t pixels_len = 4 * size.width() * size.height(); |
| 117 for (size_t i = 0; i < pixels_len; i++) |
| 118 param1.push_back(pixels_data[i]); |
| 119 |
| 120 const char* size_data = reinterpret_cast<const char*>(&size); |
| 121 size_t size_len = sizeof(gfx::Size); |
| 122 for (size_t i = 0; i < size_len; i++) |
| 123 param2.push_back(size_data[i]); |
| 124 |
| 125 Clipboard::ObjectMapParams params; |
| 126 params.push_back(param1); |
| 127 params.push_back(param2); |
| 128 objects_[Clipboard::CBF_BITMAP] = params; |
| 129 } |
| 130 #endif // defined(OS_WIN) |
OLD | NEW |