1 // Copyright (c) 2012 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.
5 #include "webkit/support/mock_webclipboard_impl.h"
9 #include "base/logging.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "third_party/WebKit/public/platform/WebCommon.h"
14 #include "third_party/WebKit/public/platform/WebDragData.h"
15 #include "third_party/WebKit/public/platform/WebImage.h"
16 #include "third_party/WebKit/public/platform/WebURL.h"
17 #include "ui/base/clipboard/clipboard.h"
18 #include "ui/gfx/codec/png_codec.h"
19 #include "ui/gfx/size.h"
20 #include "webkit/glue/webkit_glue.h"
21 #include "webkit/renderer/clipboard_utils.h"
23 using WebKit::WebDragData
;
24 using WebKit::WebString
;
26 using WebKit::WebVector
;
28 MockWebClipboardImpl::MockWebClipboardImpl() {
31 MockWebClipboardImpl::~MockWebClipboardImpl() {
34 bool MockWebClipboardImpl::isFormatAvailable(Format format
, Buffer buffer
) {
37 return !m_plainText
.isNull();
40 return !m_htmlText
.isNull();
42 case FormatSmartPaste
:
43 return m_writeSmartPaste
;
54 #if defined(OS_POSIX) && !defined(OS_MACOSX)
65 WebVector
<WebString
> MockWebClipboardImpl::readAvailableTypes(
66 Buffer buffer
, bool* containsFilenames
) {
67 *containsFilenames
= false;
68 std::vector
<WebString
> results
;
69 if (!m_plainText
.isEmpty()) {
70 results
.push_back(WebString("text/plain"));
72 if (!m_htmlText
.isEmpty()) {
73 results
.push_back(WebString("text/html"));
75 if (!m_image
.isNull()) {
76 results
.push_back(WebString("image/png"));
78 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
80 it
!= m_customData
.end(); ++it
) {
81 CHECK(std::find(results
.begin(), results
.end(), it
->first
) ==
83 results
.push_back(it
->first
);
88 WebKit::WebString
MockWebClipboardImpl::readPlainText(
89 WebKit::WebClipboard::Buffer buffer
) {
93 // TODO(wtc): set output argument *url.
94 WebKit::WebString
MockWebClipboardImpl::readHTML(
95 WebKit::WebClipboard::Buffer buffer
, WebKit::WebURL
* url
,
96 unsigned* fragmentStart
, unsigned* fragmentEnd
) {
98 *fragmentEnd
= static_cast<unsigned>(m_htmlText
.length());
102 WebKit::WebData
MockWebClipboardImpl::readImage(
103 WebKit::WebClipboard::Buffer buffer
) {
105 std::vector
<unsigned char> encoded_image
;
106 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that
107 // for endianess reasons, it will be BGRA8888 on Windows.
108 const SkBitmap
& bitmap
= m_image
.getSkBitmap();
109 SkAutoLockPixels
lock(bitmap
);
110 gfx::PNGCodec::Encode(static_cast<unsigned char*>(bitmap
.getPixels()),
111 #if defined(OS_ANDROID)
112 gfx::PNGCodec::FORMAT_RGBA
,
114 gfx::PNGCodec::FORMAT_BGRA
,
116 gfx::Size(bitmap
.width(), bitmap
.height()),
118 false /* discard_transparency */,
119 std::vector
<gfx::PNGCodec::Comment
>(),
121 data
.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image
)),
122 encoded_image
.size());
126 WebKit::WebString
MockWebClipboardImpl::readCustomData(
127 WebKit::WebClipboard::Buffer buffer
,
128 const WebKit::WebString
& type
) {
129 std::map
<base::string16
, base::string16
>::const_iterator it
=
130 m_customData
.find(type
);
131 if (it
!= m_customData
.end())
133 return WebKit::WebString();
136 void MockWebClipboardImpl::writeHTML(
137 const WebKit::WebString
& htmlText
, const WebKit::WebURL
& url
,
138 const WebKit::WebString
& plainText
, bool writeSmartPaste
) {
141 m_htmlText
= htmlText
;
142 m_plainText
= plainText
;
143 m_writeSmartPaste
= writeSmartPaste
;
146 void MockWebClipboardImpl::writePlainText(const WebKit::WebString
& plain_text
) {
149 m_plainText
= plain_text
;
152 void MockWebClipboardImpl::writeURL(
153 const WebKit::WebURL
& url
, const WebKit::WebString
& title
) {
156 m_htmlText
= WebString::fromUTF8(webkit_clipboard::URLToMarkup(url
, title
));
157 m_plainText
= url
.spec().utf16();
160 void MockWebClipboardImpl::writeImage(const WebKit::WebImage
& image
,
161 const WebKit::WebURL
& url
, const WebKit::WebString
& title
) {
162 if (!image
.isNull()) {
165 m_plainText
= m_htmlText
;
166 m_htmlText
= WebString::fromUTF8(
167 webkit_clipboard::URLToImageMarkup(url
, title
));
172 void MockWebClipboardImpl::writeDataObject(const WebDragData
& data
) {
175 const WebVector
<WebDragData::Item
>& itemList
= data
.items();
176 for (size_t i
= 0; i
< itemList
.size(); ++i
) {
177 const WebDragData::Item
& item
= itemList
[i
];
178 switch (item
.storageType
) {
179 case WebDragData::Item::StorageTypeString
: {
180 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeText
)) {
181 m_plainText
= item
.stringData
;
184 if (EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeHTML
)) {
185 m_htmlText
= item
.stringData
;
188 m_customData
.insert(std::make_pair(item
.stringType
, item
.stringData
));
191 case WebDragData::Item::StorageTypeFilename
:
192 case WebDragData::Item::StorageTypeBinaryData
:
193 NOTREACHED(); // Currently unused by the clipboard implementation.
198 void MockWebClipboardImpl::clear() {
199 m_plainText
= WebString();
200 m_htmlText
= WebString();
202 m_customData
.clear();
203 m_writeSmartPaste
= false;