1 // Copyright 2013 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 "content/test/mock_webclipboard_impl.h"
9 #include "base/stl_util.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "content/renderer/clipboard_utils.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/geometry/size.h"
21 using blink::WebDragData
;
22 using blink::WebString
;
24 using blink::WebVector
;
28 MockWebClipboardImpl::MockWebClipboardImpl()
29 : m_sequenceNumber(0),
30 m_writeSmartPaste(false) {}
32 MockWebClipboardImpl::~MockWebClipboardImpl() {}
34 uint64_t MockWebClipboardImpl::sequenceNumber(Buffer
) {
35 return m_sequenceNumber
;
38 bool MockWebClipboardImpl::isFormatAvailable(Format format
, Buffer buffer
) {
41 return !m_plainText
.is_null();
44 return !m_htmlText
.is_null();
46 case FormatSmartPaste
:
47 return m_writeSmartPaste
;
55 WebVector
<WebString
> MockWebClipboardImpl::readAvailableTypes(
57 bool* containsFilenames
) {
58 *containsFilenames
= false;
59 std::vector
<WebString
> results
;
60 if (!m_plainText
.string().empty()) {
61 results
.push_back(WebString("text/plain"));
63 if (!m_htmlText
.string().empty()) {
64 results
.push_back(WebString("text/html"));
66 if (!m_image
.isNull()) {
67 results
.push_back(WebString("image/png"));
69 for (std::map
<base::string16
, base::string16
>::const_iterator it
=
71 it
!= m_customData
.end(); ++it
) {
72 CHECK(std::find(results
.begin(), results
.end(), it
->first
) ==
74 results
.push_back(it
->first
);
79 blink::WebString
MockWebClipboardImpl::readPlainText(
80 blink::WebClipboard::Buffer buffer
) {
84 // TODO(wtc): set output argument *url.
85 blink::WebString
MockWebClipboardImpl::readHTML(
86 blink::WebClipboard::Buffer buffer
,
88 unsigned* fragmentStart
,
89 unsigned* fragmentEnd
) {
91 *fragmentEnd
= static_cast<unsigned>(m_htmlText
.string().length());
95 blink::WebData
MockWebClipboardImpl::readImage(
96 blink::WebClipboard::Buffer buffer
) {
98 std::vector
<unsigned char> encoded_image
;
99 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that
100 // for endianess reasons, it will be BGRA8888 on Windows.
101 const SkBitmap
& bitmap
= m_image
.getSkBitmap();
102 SkAutoLockPixels
lock(bitmap
);
103 gfx::PNGCodec::Encode(static_cast<unsigned char*>(bitmap
.getPixels()),
104 #if defined(OS_ANDROID)
105 gfx::PNGCodec::FORMAT_RGBA
,
107 gfx::PNGCodec::FORMAT_BGRA
,
109 gfx::Size(bitmap
.width(), bitmap
.height()),
110 static_cast<int>(bitmap
.rowBytes()),
111 false /* discard_transparency */,
112 std::vector
<gfx::PNGCodec::Comment
>(),
114 data
.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image
)),
115 encoded_image
.size());
119 blink::WebString
MockWebClipboardImpl::readCustomData(
120 blink::WebClipboard::Buffer buffer
,
121 const blink::WebString
& type
) {
122 std::map
<base::string16
, base::string16
>::const_iterator it
=
123 m_customData
.find(type
);
124 if (it
!= m_customData
.end())
126 return blink::WebString();
129 void MockWebClipboardImpl::writeHTML(const blink::WebString
& htmlText
,
130 const blink::WebURL
& url
,
131 const blink::WebString
& plainText
,
132 bool writeSmartPaste
) {
135 m_htmlText
= htmlText
;
136 m_plainText
= plainText
;
137 m_writeSmartPaste
= writeSmartPaste
;
141 void MockWebClipboardImpl::writePlainText(const blink::WebString
& plain_text
) {
144 m_plainText
= plain_text
;
148 void MockWebClipboardImpl::writeURL(const blink::WebURL
& url
,
149 const blink::WebString
& title
) {
152 m_htmlText
= WebString::fromUTF8(URLToMarkup(url
, title
));
153 m_plainText
= url
.spec().utf16();
157 void MockWebClipboardImpl::writeImage(const blink::WebImage
& image
,
158 const blink::WebURL
& url
,
159 const blink::WebString
& title
) {
160 if (!image
.isNull()) {
163 m_plainText
= m_htmlText
;
164 m_htmlText
= WebString::fromUTF8(URLToImageMarkup(url
, title
));
170 void MockWebClipboardImpl::writeDataObject(const WebDragData
& data
) {
173 const WebVector
<WebDragData::Item
>& itemList
= data
.items();
174 for (size_t i
= 0; i
< itemList
.size(); ++i
) {
175 const WebDragData::Item
& item
= itemList
[i
];
176 switch (item
.storageType
) {
177 case WebDragData::Item::StorageTypeString
: {
179 if (base::EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeText
)) {
180 m_plainText
= item
.stringData
;
183 if (base::EqualsASCII(item
.stringType
, ui::Clipboard::kMimeTypeHTML
)) {
184 m_htmlText
= item
.stringData
;
187 m_customData
.insert(std::make_pair(item
.stringType
, item
.stringData
));
191 // Currently other types are unused by the clipboard implementation.
197 void MockWebClipboardImpl::clear() {
198 m_plainText
= base::NullableString16();
199 m_htmlText
= base::NullableString16();
201 m_customData
.clear();
202 m_writeSmartPaste
= false;
205 } // namespace content