summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorericu@google.com <ericu@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-01 21:42:14 +0000
committerericu@google.com <ericu@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-01 21:42:14 +0000
commit0c4c3884b89e0c5fb3adbbb0f3cdf092cda84a10 (patch)
treee942cc54be97b815ae60fdd7bbd65a2aa624d843 /base
parent2ef2712ec860306a843c0e18afbdc339da4917cd (diff)
downloadchromium_src-0c4c3884b89e0c5fb3adbbb0f3cdf092cda84a10.zip
chromium_src-0c4c3884b89e0c5fb3adbbb0f3cdf092cda84a10.tar.gz
chromium_src-0c4c3884b89e0c5fb3adbbb0f3cdf092cda84a10.tar.bz2
The "Copy URL" link is always greyed out in the Chrome menu on popups [crbug.com/13488].
This turns out to be because it was never implemented. Tested manually on Windows; I'll test on Linux before submitting. BUG=13488 TEST=Tested manually on Windows and added a unit test for the new Clipboard function. Review URL: http://codereview.chromium.org/210042 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27772 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/clipboard.h9
-rw-r--r--base/clipboard_linux.cc8
-rw-r--r--base/clipboard_unittest.cc30
-rw-r--r--base/scoped_clipboard_writer.cc36
-rw-r--r--base/scoped_clipboard_writer.h12
5 files changed, 86 insertions, 9 deletions
diff --git a/base/clipboard.h b/base/clipboard.h
index b960676..e5cc9c2 100644
--- a/base/clipboard.h
+++ b/base/clipboard.h
@@ -116,6 +116,14 @@ class Clipboard {
// can use.
void WriteObjects(const ObjectMap& objects, base::ProcessHandle process);
+ // On Linux, we need to know when the clipboard is set to a URL. Most
+ // platforms don't care.
+#if !defined(OS_LINUX)
+ void DidWriteURL(const std::string& utf8_text) {}
+#else // !defined(OS_LINUX)
+ void DidWriteURL(const std::string& utf8_text);
+#endif
+
// Tests whether the clipboard contains a certain format
bool IsFormatAvailable(const FormatType& format, Buffer buffer) const;
@@ -190,6 +198,7 @@ class Clipboard {
void WriteFiles(const char* file_data, size_t file_len);
void WriteBitmap(const char* pixel_data, const char* size_data);
+
#if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_FREEBSD)
// |format_name| is an ASCII string and should be NULL-terminated.
// TODO(estade): port to mac.
diff --git a/base/clipboard_linux.cc b/base/clipboard_linux.cc
index 9a04d1c..f49da3f 100644
--- a/base/clipboard_linux.cc
+++ b/base/clipboard_linux.cc
@@ -119,6 +119,14 @@ void Clipboard::WriteObjects(const ObjectMap& objects) {
SetGtkClipboard();
}
+// When a URL is copied from a render view context menu (via "copy link
+// location", for example), we additionally stick it in the X clipboard. This
+// matches other linux browsers.
+void Clipboard::DidWriteURL(const std::string& utf8_text) {
+ gtk_clipboard_set_text(primary_selection_, utf8_text.c_str(),
+ utf8_text.length());
+}
+
// Take ownership of the GTK clipboard and inform it of the targets we support.
void Clipboard::SetGtkClipboard() {
scoped_array<GtkTargetEntry> targets(
diff --git a/base/clipboard_unittest.cc b/base/clipboard_unittest.cc
index d26ba00..e6238ba 100644
--- a/base/clipboard_unittest.cc
+++ b/base/clipboard_unittest.cc
@@ -227,6 +227,36 @@ TEST_F(ClipboardTest, MultipleFilesTest) {
}
#endif // !defined(OS_LINUX)
+TEST_F(ClipboardTest, URLTest) {
+ Clipboard clipboard;
+
+ string16 url(ASCIIToUTF16("http://www.google.com/"));
+
+ {
+ ScopedClipboardWriter clipboard_writer(&clipboard);
+ clipboard_writer.WriteURL(url);
+ }
+
+ EXPECT_TRUE(clipboard.IsFormatAvailable(
+ Clipboard::GetPlainTextWFormatType(), Clipboard::BUFFER_STANDARD));
+ EXPECT_TRUE(clipboard.IsFormatAvailable(Clipboard::GetPlainTextFormatType(),
+ Clipboard::BUFFER_STANDARD));
+ string16 text_result;
+ clipboard.ReadText(Clipboard::BUFFER_STANDARD, &text_result);
+
+ EXPECT_EQ(text_result, url);
+
+ std::string ascii_text;
+ clipboard.ReadAsciiText(Clipboard::BUFFER_STANDARD, &ascii_text);
+ EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
+
+#if defined(OS_LINUX)
+ ascii_text.clear();
+ clipboard.ReadAsciiText(Clipboard::BUFFER_SELECTION, &ascii_text);
+ EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
+#endif // defined(OS_LINUX)
+}
+
#if defined(OS_WIN) || defined(OS_LINUX)
TEST_F(ClipboardTest, DataTest) {
Clipboard clipboard;
diff --git a/base/scoped_clipboard_writer.cc b/base/scoped_clipboard_writer.cc
index dff3321..e464628 100644
--- a/base/scoped_clipboard_writer.cc
+++ b/base/scoped_clipboard_writer.cc
@@ -18,20 +18,19 @@ ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard)
}
ScopedClipboardWriter::~ScopedClipboardWriter() {
- if (!objects_.empty() && clipboard_)
+ if (!objects_.empty() && clipboard_) {
clipboard_->WriteObjects(objects_);
+ if (url_text_.length())
+ clipboard_->DidWriteURL(url_text_);
+ }
}
void ScopedClipboardWriter::WriteText(const string16& text) {
- if (text.empty())
- return;
-
- std::string utf8_text = UTF16ToUTF8(text);
+ WriteTextOrURL(text, false);
+}
- Clipboard::ObjectMapParams parameters;
- parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
- utf8_text.end()));
- objects_[Clipboard::CBF_TEXT] = parameters;
+void ScopedClipboardWriter::WriteURL(const string16& text) {
+ WriteTextOrURL(text, true);
}
void ScopedClipboardWriter::WriteHTML(const string16& markup,
@@ -156,3 +155,22 @@ void ScopedClipboardWriter::WritePickledData(const Pickle& pickle,
parameters.push_back(data_parameter);
objects_[Clipboard::CBF_DATA] = parameters;
}
+
+void ScopedClipboardWriter::WriteTextOrURL(const string16& text, bool is_url) {
+ if (text.empty())
+ return;
+
+ std::string utf8_text = UTF16ToUTF8(text);
+
+ Clipboard::ObjectMapParams parameters;
+ parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
+ utf8_text.end()));
+ objects_[Clipboard::CBF_TEXT] = parameters;
+
+ if (is_url) {
+ url_text_ = utf8_text;
+ } else {
+ url_text_.clear();
+ }
+}
+
diff --git a/base/scoped_clipboard_writer.h b/base/scoped_clipboard_writer.h
index ed63050..feea7cd 100644
--- a/base/scoped_clipboard_writer.h
+++ b/base/scoped_clipboard_writer.h
@@ -33,6 +33,10 @@ class ScopedClipboardWriter {
// Converts |text| to UTF-8 and adds it to the clipboard.
void WriteText(const string16& text);
+ // Converts the text of the URL to UTF-8 and adds it to the clipboard, then
+ // notifies the Clipboard that we just wrote a URL.
+ void WriteURL(const string16& text);
+
// Adds HTML to the clipboard. The url parameter is optional, but especially
// useful if the HTML fragment contains relative links.
void WriteHTML(const string16& markup, const std::string& source_url);
@@ -60,11 +64,19 @@ class ScopedClipboardWriter {
void WritePickledData(const Pickle& pickle, Clipboard::FormatType format);
protected:
+ // Converts |text| to UTF-8 and adds it to the clipboard. If it's a URL, we
+ // also notify the clipboard of that fact.
+ void WriteTextOrURL(const string16& text, bool is_url);
+
// We accumulate the data passed to the various targets in the |objects_|
// vector, and pass it to Clipboard::WriteObjects() during object destruction.
Clipboard::ObjectMap objects_;
Clipboard* clipboard_;
+ // We keep around the UTF-8 text of the URL in order to pass it to
+ // Clipboard::DidWriteURL().
+ std::string url_text_;
+
private:
DISALLOW_COPY_AND_ASSIGN(ScopedClipboardWriter);
};