diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-23 17:35:11 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-10-23 17:35:11 +0000 |
commit | d1bf2aa00c309571470c9b6791b613dea520140e (patch) | |
tree | 7d90d8e54cae18db8f39278d90998bc581040aad /base | |
parent | 47b994fc2cac49493133da207e69cfe4529d40f0 (diff) | |
download | chromium_src-d1bf2aa00c309571470c9b6791b613dea520140e.zip chromium_src-d1bf2aa00c309571470c9b6791b613dea520140e.tar.gz chromium_src-d1bf2aa00c309571470c9b6791b613dea520140e.tar.bz2 |
Simultaneously store multiple formats on the linux clipboard.
Review URL: http://codereview.chromium.org/8038
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@3819 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base_lib.scons | 1 | ||||
-rw-r--r-- | base/base_unittests.scons | 1 | ||||
-rw-r--r-- | base/clipboard.h | 24 | ||||
-rw-r--r-- | base/clipboard_linux.cc | 200 | ||||
-rw-r--r-- | base/clipboard_unittest.cc | 35 |
5 files changed, 192 insertions, 69 deletions
diff --git a/base/base_lib.scons b/base/base_lib.scons index ee12592..17c3543 100644 --- a/base/base_lib.scons +++ b/base/base_lib.scons @@ -175,6 +175,7 @@ if env['PLATFORM'] == 'posix': input_files.extend([ 'atomicops_internals_x86_gcc.cc', 'base_paths_linux.cc', + 'clipboard_linux.cc', 'file_util_linux.cc', 'hmac_nss.cc', 'message_pump_glib.cc', diff --git a/base/base_unittests.scons b/base/base_unittests.scons index 5ff75ce..f015a8f 100644 --- a/base/base_unittests.scons +++ b/base/base_unittests.scons @@ -141,7 +141,6 @@ if env['PLATFORM'] in ('posix', 'darwin'): # Remove files that still need to be ported from the input_files list. # TODO(port): delete files from this list as they get ported. to_be_ported_files = [ - 'clipboard_unittest.cc', 'idletimer_unittest.cc', 'watchdog_unittest.cc', 'gfx/native_theme_unittest.cc', diff --git a/base/clipboard.h b/base/clipboard.h index f9f21b1..f6d2531 100644 --- a/base/clipboard.h +++ b/base/clipboard.h @@ -5,7 +5,9 @@ #ifndef BASE_CLIPBOARD_H_ #define BASE_CLIPBOARD_H_ +#include <map> #include <string> +#include <utility> #include <vector> #include "base/basictypes.h" @@ -29,6 +31,9 @@ class Clipboard { #elif defined(OS_LINUX) typedef struct _GdkAtom* FormatType; typedef struct _GtkClipboard GtkClipboard; + // A mapping from target (format) string to the relevant data (usually a + // string, but potentially arbitrary data). + typedef std::map<std::string, std::pair<uint8*, size_t> > TargetMap; #endif Clipboard(); @@ -98,7 +103,7 @@ class Clipboard { // out parameter. void ReadFile(std::wstring* file) const; void ReadFiles(std::vector<std::wstring>* files) const; - + // Get format Identifiers for various types. static FormatType GetUrlFormatType(); static FormatType GetUrlWFormatType(); @@ -135,6 +140,23 @@ class Clipboard { HWND clipboard_owner_; #elif defined(OS_LINUX) + // Write changes to gtk clipboard. + void SetGtkClipboard(); + // Free pointers in clipboard_data_ and clear() the map. + void FreeTargetMap(); + // Insert a mapping into clipboard_data_, or change an existing mapping. + uint8* InsertOrOverwrite(std::string target, uint8* data, size_t data_len); + + // We have to be able to store multiple formats on the clipboard + // simultaneously. The Chrome Clipboard class accomplishes this by + // expecting that consecutive calls to Write* (WriteHTML, WriteText, etc.) + // The GTK clipboard interface does not naturally support consecutive calls + // building on one another. So we keep all data in a map, and always pass the + // map when we are setting the gtk clipboard. Consecutive calls to Write* + // will write to the map and set the GTK clipboard, then add to the same + // map and set the GTK clipboard again. GTK thinks it is wiping out the + // clipboard but we are actually keeping the previous data. + TargetMap clipboard_data_; GtkClipboard* clipboard_; #endif diff --git a/base/clipboard_linux.cc b/base/clipboard_linux.cc index af929c3f..1e4686a 100644 --- a/base/clipboard_linux.cc +++ b/base/clipboard_linux.cc @@ -6,97 +6,132 @@ #include "base/string_util.h" #include <gtk/gtk.h> -#include <string.h> +#include <map> +#include <set> +#include <string> +#include <utility> namespace { static const char* kMimeHtml = "text/html"; +static const char* kMimeText = "text/plain"; -void GetHtml(GtkClipboard* clipboard, +// GtkClipboardGetFunc callback. +// GTK will call this when an application wants data we copied to the clipboard. +void GetData(GtkClipboard* clipboard, GtkSelectionData* selection_data, guint info, gpointer user_data) { - if (selection_data->target != gdk_atom_intern(kMimeHtml, false)) - return; + Clipboard::TargetMap* data_map = + reinterpret_cast<Clipboard::TargetMap*>(user_data); - gtk_selection_data_set(selection_data, gdk_atom_intern(kMimeHtml, false), - 8, - static_cast<guchar*>(user_data), - strlen(static_cast<char*>(user_data))); -} + Clipboard::TargetMap::iterator iter = + data_map->find(std::string(gdk_atom_name(selection_data->target))); -void ClearHtml(GtkClipboard* clipboard, - gpointer user_data) { - delete[] static_cast<char*>(user_data); -} + if (iter == data_map->end()) + return; -void GetNoOp(GtkClipboard* clipboard, - GtkSelectionData* selection_data, - guint info, - gpointer user_data) { + gtk_selection_data_set(selection_data, selection_data->target, 8, + iter->second.first, + iter->second.second); } -void ClearNoOp(GtkClipboard* clipboard, +// GtkClipboardClearFunc callback. +// GTK will call this when new data is set on the clipboard (whether or not we +// retain ownership) or when gtk_clipboard_clear() is called. We don't do +// anything because we don't want to clear the clipboard_data_ map if we call +// set data several times in a row. Instead we manually clear the +// clipboard_data_ map on Clear() and on Clipboard destruction. +void ClearData(GtkClipboard* clipboard, gpointer user_data) { } -} +} // namespace Clipboard::Clipboard() { clipboard_ = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); } Clipboard::~Clipboard() { - // TODO(estade): enable this (must first use gtk_clipboard_set_can_store()?) - // gtk_clipboard_store(clipboard_); + // TODO(estade): do we want to save clipboard data after we exit? + // gtk_clipboard_set_can_store and gtk_clipboard_store work + // but have strangely awful performance. + Clear(); } void Clipboard::Clear() { - // gtk_clipboard_clear() does not appear to be enough. We must first grab - // ownership of the clipboard by setting data. - GtkTargetEntry empty_target[] = {}; - gtk_clipboard_set_with_data(clipboard_, empty_target, 0, - GetNoOp, ClearNoOp, NULL); gtk_clipboard_clear(clipboard_); + FreeTargetMap(); } void Clipboard::WriteText(const std::wstring& text) { std::string utf8_text = WideToUTF8(text); + size_t text_len = utf8_text.size() + 1; + uint8* text_data = new uint8[text_len]; + memcpy(text_data, utf8_text.c_str(), text_len); - gtk_clipboard_set_text(clipboard_, utf8_text.c_str(), utf8_text.size()); + uint8* old_data = InsertOrOverwrite(kMimeText, text_data, text_len); + InsertOrOverwrite("TEXT", text_data, text_len); + InsertOrOverwrite("STRING", text_data, text_len); + InsertOrOverwrite("UTF8_STRING", text_data, text_len); + InsertOrOverwrite("COMPOUND_TEXT", text_data, text_len); + + if (old_data) + delete[] old_data; + + SetGtkClipboard(); } void Clipboard::WriteHTML(const std::wstring& markup, const std::string& src_url) { // TODO(estade): might not want to ignore src_url std::string html = WideToUTF8(markup); - char* html_data = new char[html.size() + 1]; - strcpy(html_data, html.c_str()); + size_t data_len = html.size() + 1; + uint8* html_data = new uint8[data_len]; + memcpy(html_data, html.c_str(), data_len); - char target_format[strlen(kMimeHtml) + 1]; - strcpy(target_format, kMimeHtml); - GtkTargetEntry target[] = { {target_format, 0} }; + uint8* old_data = InsertOrOverwrite(kMimeHtml, html_data, data_len); - if (!gtk_clipboard_set_with_data(clipboard_, target, 1, - GetHtml, ClearHtml, html_data)) { - delete[] html_data; - } + if (old_data) + delete[] old_data; + + SetGtkClipboard(); } +// We do not use gtk_clipboard_wait_is_target_available because of +// a bug with the gtk clipboard. It caches the available targets +// and does not always refresh the cache when it is appropriate. +// TODO(estade): When gnome bug 557315 is resolved, change this function +// to use gtk_clipboard_wait_is_target_available. Also, catch requests +// for plain text and change them to gtk_clipboard_wait_is_text_available +// (which checks for several standard text targets). bool Clipboard::IsFormatAvailable(Clipboard::FormatType format) const { - if (format == GetPlainTextFormatType()) { - return gtk_clipboard_wait_is_text_available(clipboard_); + bool retval = false; + GdkAtom* targets = NULL; + GtkSelectionData* data = + gtk_clipboard_wait_for_contents(clipboard_, + gdk_atom_intern("TARGETS", false)); + + if (!data) + return false; + + int num = 0; + gtk_selection_data_get_targets(data, &targets, &num); + + for (int i = 0; i < num; i++) { + if (targets[i] == format) { + retval = true; + break; + } } - return gtk_clipboard_wait_is_target_available(clipboard_, format); + gtk_selection_data_free(data); + g_free(targets); + + return retval; } void Clipboard::ReadText(std::wstring* result) const { - if (!result) { - NOTREACHED(); - return; - } - result->clear(); gchar* text = gtk_clipboard_wait_for_text(clipboard_); @@ -109,11 +144,6 @@ void Clipboard::ReadText(std::wstring* result) const { } void Clipboard::ReadAsciiText(std::string* result) const { - if (!result) { - NOTREACHED(); - return; - } - result->clear(); gchar* text = gtk_clipboard_wait_for_text(clipboard_); @@ -124,12 +154,8 @@ void Clipboard::ReadAsciiText(std::string* result) const { g_free(text); } +// TODO(estade): handle different charsets. void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const { - if (!markup) { - NOTREACHED(); - return; - } - markup->clear(); GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_, @@ -138,8 +164,10 @@ void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const { if (!data) return; - std::string html(reinterpret_cast<char*>(data->data)); - markup->assign(UTF8ToWide(html)); + UTF8ToWide(reinterpret_cast<char*>(data->data), + strlen(reinterpret_cast<char*>(data->data)), + markup); + gtk_selection_data_free(data); } // static @@ -149,7 +177,6 @@ Clipboard::FormatType Clipboard::GetPlainTextFormatType() { // static Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { - // all gtk clipboard strings are UTF8 return GetPlainTextFormatType(); } @@ -158,3 +185,62 @@ Clipboard::FormatType Clipboard::GetHtmlFormatType() { return gdk_atom_intern(kMimeHtml, false); } +// Take ownership of the GTK clipboard and inform it of the targets we support. +void Clipboard::SetGtkClipboard() { + GtkTargetEntry targets[clipboard_data_.size()]; + + int i = 0; + for (Clipboard::TargetMap::iterator iter = clipboard_data_.begin(); + iter != clipboard_data_.end(); iter++, i++) { + char* target_string = new char[iter->first.size() + 1]; + strcpy(target_string, iter->first.c_str()); + targets[i].target = target_string; + targets[i].flags = 0; + targets[i].info = i; + } + + gtk_clipboard_set_with_data(clipboard_, targets, + clipboard_data_.size(), + GetData, ClearData, + &clipboard_data_); + + for (size_t i = 0; i < clipboard_data_.size(); i++) + delete[] targets[i].target; +} + +// Free the pointers in the clipboard_data_ map and reset the map. +void Clipboard::FreeTargetMap() { + std::set<uint8*> ptrs; + + for (Clipboard::TargetMap::iterator iter = clipboard_data_.begin(); + iter != clipboard_data_.end(); iter++) + ptrs.insert(iter->second.first); + + for (std::set<uint8*>::iterator iter = ptrs.begin(); + iter != ptrs.end(); iter++) + delete[] *iter; + + clipboard_data_.clear(); +} + +// Insert the key/value pair in the clipboard_data structure. Overwrite +// and any old value that might have had that key. If we overwrote something, +// return the pointer to that something. Otherwise return null. +uint8* Clipboard::InsertOrOverwrite(std::string key, + uint8* data, size_t data_len) { + std::pair<std::string, std::pair<uint8*, size_t> > mapping = + std::make_pair(key, std::make_pair(data, data_len)); + + Clipboard::TargetMap::iterator iter = clipboard_data_.find(key); + uint8* retval = NULL; + + if (iter == clipboard_data_.end()) { + clipboard_data_.insert(mapping); + } else { + retval = iter->second.first; + iter->second = mapping.second; + } + + return retval; +} + diff --git a/base/clipboard_unittest.cc b/base/clipboard_unittest.cc index c035c9c0..066bd3e 100644 --- a/base/clipboard_unittest.cc +++ b/base/clipboard_unittest.cc @@ -42,6 +42,21 @@ TEST_F(ClipboardTest, TextTest) { EXPECT_EQ(WideToUTF8(text), ascii_text); } +TEST_F(ClipboardTest, OverwriteTest) { + Clipboard clipboard; + + std::wstring text1(L"first string"), text2(L"second string"), text_result; + + clipboard.Clear(); + clipboard.WriteText(text1); + clipboard.WriteText(text2); + + EXPECT_TRUE(clipboard.IsFormatAvailable( + Clipboard::GetPlainTextWFormatType())); + clipboard.ReadText(&text_result); + EXPECT_EQ(text2, text_result); +} + TEST_F(ClipboardTest, HTMLTest) { Clipboard clipboard; @@ -61,8 +76,6 @@ TEST_F(ClipboardTest, HTMLTest) { #endif } -// TODO(estade): Port the following tests. -#if !defined(OS_LINUX) TEST_F(ClipboardTest, TrickyHTMLTest) { Clipboard clipboard; @@ -75,15 +88,15 @@ TEST_F(ClipboardTest, TrickyHTMLTest) { Clipboard::GetHtmlFormatType())); clipboard.ReadHTML(&markup_result, &url_result); EXPECT_EQ(markup, markup_result); - -#if defined(OS_MACOSX) - // TODO(playmobil): It's not clear that the OS X clipboard needs to support +#if defined(OS_WIN) + // TODO(playmobil): It's not clear that non windows clipboards need to support // this. -#else EXPECT_EQ(url, url_result); #endif } +// TODO(estade): Port the following test (decide what target we use for urls) +#if !defined(OS_LINUX) TEST_F(ClipboardTest, BookmarkTest) { Clipboard clipboard; @@ -98,6 +111,7 @@ TEST_F(ClipboardTest, BookmarkTest) { EXPECT_EQ(title, title_result); EXPECT_EQ(url, url_result); } +#endif TEST_F(ClipboardTest, MultiFormatTest) { Clipboard clipboard; @@ -118,10 +132,9 @@ TEST_F(ClipboardTest, MultiFormatTest) { Clipboard::GetPlainTextFormatType())); clipboard.ReadHTML(&markup_result, &url_result); EXPECT_EQ(markup, markup_result); -#if defined(OS_MACOSX) - // TODO(playmobil): It's not clear that the OS X clipboard needs to support +#if defined(OS_WIN) + // TODO(playmobil): It's not clear that non windows clipboards need to support // this. -#else EXPECT_EQ(url, url_result); #endif clipboard.ReadText(&text_result); @@ -130,6 +143,8 @@ TEST_F(ClipboardTest, MultiFormatTest) { EXPECT_EQ(WideToUTF8(text), ascii_text); } +// TODO(estade): Port the following tests (decide what targets we use for files) +#if !defined(OS_LINUX) // Files for this test don't actually need to exist on the file system, just // don't try to use a non-existent file you've retrieved from the clipboard. TEST_F(ClipboardTest, FileTest) { @@ -176,6 +191,7 @@ TEST_F(ClipboardTest, MultipleFilesTest) { for (size_t i = 0; i < out_files.size(); ++i) EXPECT_EQ(files[i], out_files[i]); } +#endif // !defined(OS_LINUX) #if defined(OS_WIN) // Windows only tests. TEST_F(ClipboardTest, HyperlinkTest) { @@ -224,4 +240,3 @@ TEST_F(ClipboardTest, BitmapTest) { Clipboard::GetBitmapFormatType())); } #endif -#endif // !defined(OS_LINUX) |