summaryrefslogtreecommitdiffstats
path: root/webkit/glue/webclipboard_impl.cc
diff options
context:
space:
mode:
authordarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-26 00:15:20 +0000
committerdarin@chromium.org <darin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-02-26 00:15:20 +0000
commitc62ce3e9f74a7b55b2d50c227555a3c198fd9cf3 (patch)
tree53fc0d34109ac909a7ba8f603835da8468abcd10 /webkit/glue/webclipboard_impl.cc
parentbb095ea448d990cf3ab9ae3af4c9fc11d6348f90 (diff)
downloadchromium_src-c62ce3e9f74a7b55b2d50c227555a3c198fd9cf3.zip
chromium_src-c62ce3e9f74a7b55b2d50c227555a3c198fd9cf3.tar.gz
chromium_src-c62ce3e9f74a7b55b2d50c227555a3c198fd9cf3.tar.bz2
Chromium changes to use new WebKit, WebKitClient, and WebClipboard interfaces.
A new WebKitClientImpl class is added to webkit/glue that consumers can use to help implement WebKitClient. In the future, consumers will likely subclass WebKitClientImpl. For now, that is not necessary. Since a WebImage may not hold a SkBitmap, I needed to modify ScopedClipboardWriterGlue to not deal in SkBitmaps. So, I just added a WriteBitmapFromPixels method in place of the WriteBitmap method. That method is actually named the same as the one from the base class, which is perhaps kind of nice since the purpose of ScopedClipboardWriterGlue is to override the default way of sending an image to the clipboard! R=dglazkov Review URL: http://codereview.chromium.org/28119 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10416 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/glue/webclipboard_impl.cc')
-rw-r--r--webkit/glue/webclipboard_impl.cc134
1 files changed, 134 insertions, 0 deletions
diff --git a/webkit/glue/webclipboard_impl.cc b/webkit/glue/webclipboard_impl.cc
new file mode 100644
index 0000000..4e9db87
--- /dev/null
+++ b/webkit/glue/webclipboard_impl.cc
@@ -0,0 +1,134 @@
+// Copyright (c) 2009 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 "webkit/glue/webclipboard_impl.h"
+
+#include "WebImage.h"
+#include "WebString.h"
+#include "WebURL.h"
+
+#include "base/clipboard.h"
+#include "base/logging.h"
+#include "base/string_util.h"
+#include "googleurl/src/gurl.h"
+#include "net/base/escape.h"
+#include "webkit/glue/scoped_clipboard_writer_glue.h"
+#include "webkit/glue/webkit_glue.h"
+
+using WebKit::WebClipboard;
+using WebKit::WebImage;
+using WebKit::WebString;
+using WebKit::WebURL;
+
+namespace webkit_glue {
+
+static std::string URLToMarkup(const WebURL& url, const WebString& title) {
+ std::string markup("<a href=\"");
+ markup.append(url.spec());
+ markup.append("\">");
+ // TODO(darin): HTML escape this
+ markup.append(EscapeForHTML(UTF16ToUTF8(title)));
+ markup.append("</a>");
+ return markup;
+}
+
+static std::string URLToImageMarkup(const WebURL& url,
+ const WebString& title) {
+ std::string markup("<img src=\"");
+ markup.append(url.spec());
+ markup.append("\"");
+ if (!title.isEmpty()) {
+ markup.append(" alt=\"");
+ markup.append(EscapeForHTML(UTF16ToUTF8(title)));
+ markup.append("\"");
+ }
+ markup.append("/>");
+ return markup;
+}
+
+bool WebClipboardImpl::isFormatAvailable(Format format) {
+ Clipboard::FormatType format_type;
+
+ switch (format) {
+ case FormatHTML:
+ format_type = Clipboard::GetHtmlFormatType();
+ break;
+ case FormatSmartPaste:
+ format_type = Clipboard::GetWebKitSmartPasteFormatType();
+ break;
+ case FormatBookmark:
+#if defined(OS_WIN) || defined(OS_MACOSX)
+ format_type = Clipboard::GetUrlWFormatType();
+ break;
+#endif
+ default:
+ NOTREACHED();
+ return false;
+ }
+
+ return ClipboardIsFormatAvailable(format_type);
+}
+
+WebString WebClipboardImpl::readPlainText() {
+ if (ClipboardIsFormatAvailable(Clipboard::GetPlainTextWFormatType())) {
+ std::wstring text;
+ ClipboardReadText(&text);
+ if (!text.empty())
+ return WideToUTF16(text);
+ }
+
+ if (ClipboardIsFormatAvailable(Clipboard::GetPlainTextFormatType())) {
+ std::string text;
+ ClipboardReadAsciiText(&text);
+ if (!text.empty())
+ return UTF8ToUTF16(text);
+ }
+
+ return WebString();
+}
+
+WebString WebClipboardImpl::readHTML(WebURL* source_url) {
+ std::wstring html_stdstr;
+ GURL gurl;
+ ClipboardReadHTML(&html_stdstr, &gurl);
+ *source_url = gurl;
+ return WideToUTF16(html_stdstr);
+}
+
+void WebClipboardImpl::writeHTML(
+ const WebString& html_text, const WebURL& source_url,
+ const WebString& plain_text, bool write_smart_paste) {
+ ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
+ scw.WriteHTML(UTF16ToWide(html_text), source_url.spec());
+ scw.WriteText(UTF16ToWide(plain_text));
+
+ if (write_smart_paste)
+ scw.WriteWebSmartPaste();
+}
+
+void WebClipboardImpl::writeURL(const WebURL& url, const WebString& title) {
+ ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
+
+ scw.WriteBookmark(UTF16ToWide(title), url.spec());
+ scw.WriteHTML(UTF8ToWide(URLToMarkup(url, title)), "");
+ scw.WriteText(UTF8ToWide(url.spec()));
+}
+
+void WebClipboardImpl::writeImage(
+ const WebImage& image, const WebURL& url, const WebString& title) {
+ ScopedClipboardWriterGlue scw(ClipboardGetClipboard());
+
+#if defined(OS_WIN)
+ if (!image.isNull())
+ scw.WriteBitmapFromPixels(image.pixels(), image.size());
+#endif
+
+ if (!url.isEmpty()) {
+ scw.WriteBookmark(UTF16ToWide(title), url.spec());
+ scw.WriteHTML(UTF8ToWide(URLToImageMarkup(url, title)), "");
+ scw.WriteText(UTF8ToWide(url.spec()));
+ }
+}
+
+} // namespace webkit_glue