diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-05 01:34:30 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-05 01:34:30 +0000 |
commit | 3ee3ffd1d79a0c1544f3bfd70c7de7606df9482f (patch) | |
tree | 0943a0925f5974478463e5d00886be29109858e6 /base/scoped_clipboard_writer.cc | |
parent | 38733063b7e1cf0b6e67c0b590773ff4f1aaa18d (diff) | |
download | chromium_src-3ee3ffd1d79a0c1544f3bfd70c7de7606df9482f.zip chromium_src-3ee3ffd1d79a0c1544f3bfd70c7de7606df9482f.tar.gz chromium_src-3ee3ffd1d79a0c1544f3bfd70c7de7606df9482f.tar.bz2 |
Rewrote the clipboard API to be more concurrent. Added a helper class to make it more foolproof. Updated all clients and unittests. Mac port by jeremy@chromium.org
Review URL: http://codereview.chromium.org/9154
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@4719 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/scoped_clipboard_writer.cc')
-rw-r--r-- | base/scoped_clipboard_writer.cc | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/base/scoped_clipboard_writer.cc b/base/scoped_clipboard_writer.cc new file mode 100644 index 0000000..a570981 --- /dev/null +++ b/base/scoped_clipboard_writer.cc @@ -0,0 +1,130 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "base/scoped_clipboard_writer.h" + +#include "base/clipboard.h" +#include "base/string_util.h" + +ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard) + : clipboard_(clipboard) { +} + +ScopedClipboardWriter::~ScopedClipboardWriter() { + if (!objects_.empty() && clipboard_) + clipboard_->WriteObjects(objects_); +} + +void ScopedClipboardWriter::WriteText(const std::wstring& text) { + if (text.empty()) + return; + + std::string utf8_text = WideToUTF8(text); + + Clipboard::ObjectMapParams params; + params.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), + utf8_text.end())); + objects_[Clipboard::CBF_TEXT] = params; +} + +void ScopedClipboardWriter::WriteHTML(const std::wstring& markup, + const std::string& src_url) { + if (markup.empty()) + return; + + std::string utf8_markup = WideToUTF8(markup); + + Clipboard::ObjectMapParams params; + params.push_back( + Clipboard::ObjectMapParam(utf8_markup.begin(), + utf8_markup.end())); + if (!src_url.empty()) { + params.push_back(Clipboard::ObjectMapParam(src_url.begin(), + src_url.end())); + } + + objects_[Clipboard::CBF_HTML] = params; +} + +void ScopedClipboardWriter::WriteBookmark(const std::wstring& title, + const std::string& url) { + if (title.empty() || url.empty()) + return; + + std::string utf8_markup = WideToUTF8(title); + + Clipboard::ObjectMapParams params; + params.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), + utf8_markup.end())); + params.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); + objects_[Clipboard::CBF_BOOKMARK] = params; +} + +void ScopedClipboardWriter::WriteHyperlink(const std::wstring& title, + const std::string& url) { + if (title.empty() || url.empty()) + return; + + std::string utf8_markup = WideToUTF8(title); + + Clipboard::ObjectMapParams params; + params.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), + utf8_markup.end())); + params.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); + objects_[Clipboard::CBF_LINK] = params; +} + +void ScopedClipboardWriter::WriteFile(const std::wstring& file) { + WriteFiles(std::vector<std::wstring>(1, file)); +} + +// Save the filenames as a string separated by nulls and terminated with an +// extra null. +void ScopedClipboardWriter::WriteFiles(const std::vector<std::wstring>& files) { + if (files.empty()) + return; + + Clipboard::ObjectMapParam param; + + for (std::vector<std::wstring>::const_iterator iter = files.begin(); + iter != files.end(); ++iter) { + std::string filename = WideToUTF8(*iter); + for (std::string::const_iterator filename_iter = filename.begin(); + filename_iter != filename.end(); ++filename_iter) { + param.push_back(*filename_iter); + } + param.push_back('\0'); + } + + param.push_back('\0'); + + Clipboard::ObjectMapParams params; + params.push_back(param); + objects_[Clipboard::CBF_FILES] = params; +} + +#if defined(OS_WIN) +void ScopedClipboardWriter::WriteWebSmartPaste() { + objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams(); +} + +void ScopedClipboardWriter::WriteBitmapFromPixels(const void* pixels, + const gfx::Size& size) { + Clipboard::ObjectMapParam param1, param2; + const char* pixels_data = reinterpret_cast<const char*>(pixels); + size_t pixels_len = 4 * size.width() * size.height(); + for (size_t i = 0; i < pixels_len; i++) + param1.push_back(pixels_data[i]); + + const char* size_data = reinterpret_cast<const char*>(&size); + size_t size_len = sizeof(gfx::Size); + for (size_t i = 0; i < size_len; i++) + param2.push_back(size_data[i]); + + Clipboard::ObjectMapParams params; + params.push_back(param1); + params.push_back(param2); + objects_[Clipboard::CBF_BITMAP] = params; +} +#endif // defined(OS_WIN) |