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.
4
5 #include "content/test/mock_webclipboard_impl.h"
6
7 #include <algorithm>
8
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 "content/renderer/clipboard_utils.h"
14 #include "third_party/WebKit/public/platform/WebCommon.h"
15 #include "third_party/WebKit/public/platform/WebDragData.h"
16 #include "third_party/WebKit/public/platform/WebImage.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
18 #include "ui/base/clipboard/clipboard.h"
19 #include "ui/gfx/codec/png_codec.h"
20 #include "ui/gfx/size.h"
21
22 using blink::WebDragData;
23 using blink::WebString;
24 using blink::WebURL;
25 using blink::WebVector;
26
27 namespace content {
28
MockWebClipboardImpl()29 MockWebClipboardImpl::MockWebClipboardImpl() {}
30
~MockWebClipboardImpl()31 MockWebClipboardImpl::~MockWebClipboardImpl() {}
32
isFormatAvailable(Format format,Buffer buffer)33 bool MockWebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) {
34 switch (format) {
35 case FormatPlainText:
36 return !m_plainText.isNull();
37
38 case FormatHTML:
39 return !m_htmlText.isNull();
40
41 case FormatSmartPaste:
42 return m_writeSmartPaste;
43
44 default:
45 NOTREACHED();
46 return false;
47 }
48
49 switch (buffer) {
50 case BufferStandard:
51 break;
52 case BufferSelection:
53 #if defined(OS_POSIX) && !defined(OS_MACOSX)
54 break;
55 #endif
56 default:
57 NOTREACHED();
58 return false;
59 }
60
61 return true;
62 }
63
readAvailableTypes(Buffer buffer,bool * containsFilenames)64 WebVector<WebString> MockWebClipboardImpl::readAvailableTypes(
65 Buffer buffer,
66 bool* containsFilenames) {
67 *containsFilenames = false;
68 std::vector<WebString> results;
69 if (!m_plainText.isEmpty()) {
70 results.push_back(WebString("text/plain"));
71 }
72 if (!m_htmlText.isEmpty()) {
73 results.push_back(WebString("text/html"));
74 }
75 if (!m_image.isNull()) {
76 results.push_back(WebString("image/png"));
77 }
78 for (std::map<base::string16, base::string16>::const_iterator it =
79 m_customData.begin();
80 it != m_customData.end(); ++it) {
81 CHECK(std::find(results.begin(), results.end(), it->first) ==
82 results.end());
83 results.push_back(it->first);
84 }
85 return results;
86 }
87
readPlainText(blink::WebClipboard::Buffer buffer)88 blink::WebString MockWebClipboardImpl::readPlainText(
89 blink::WebClipboard::Buffer buffer) {
90 return m_plainText;
91 }
92
93 // TODO(wtc): set output argument *url.
readHTML(blink::WebClipboard::Buffer buffer,blink::WebURL * url,unsigned * fragmentStart,unsigned * fragmentEnd)94 blink::WebString MockWebClipboardImpl::readHTML(
95 blink::WebClipboard::Buffer buffer,
96 blink::WebURL* url,
97 unsigned* fragmentStart,
98 unsigned* fragmentEnd) {
99 *fragmentStart = 0;
100 *fragmentEnd = static_cast<unsigned>(m_htmlText.length());
101 return m_htmlText;
102 }
103
readImage(blink::WebClipboard::Buffer buffer)104 blink::WebData MockWebClipboardImpl::readImage(
105 blink::WebClipboard::Buffer buffer) {
106 std::string data;
107 std::vector<unsigned char> encoded_image;
108 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that
109 // for endianess reasons, it will be BGRA8888 on Windows.
110 const SkBitmap& bitmap = m_image.getSkBitmap();
111 SkAutoLockPixels lock(bitmap);
112 gfx::PNGCodec::Encode(static_cast<unsigned char*>(bitmap.getPixels()),
113 #if defined(OS_ANDROID)
114 gfx::PNGCodec::FORMAT_RGBA,
115 #else
116 gfx::PNGCodec::FORMAT_BGRA,
117 #endif
118 gfx::Size(bitmap.width(), bitmap.height()),
119 bitmap.rowBytes(),
120 false /* discard_transparency */,
121 std::vector<gfx::PNGCodec::Comment>(),
122 &encoded_image);
123 data.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image)),
124 encoded_image.size());
125 return data;
126 }
127
readCustomData(blink::WebClipboard::Buffer buffer,const blink::WebString & type)128 blink::WebString MockWebClipboardImpl::readCustomData(
129 blink::WebClipboard::Buffer buffer,
130 const blink::WebString& type) {
131 std::map<base::string16, base::string16>::const_iterator it =
132 m_customData.find(type);
133 if (it != m_customData.end())
134 return it->second;
135 return blink::WebString();
136 }
137
writeHTML(const blink::WebString & htmlText,const blink::WebURL & url,const blink::WebString & plainText,bool writeSmartPaste)138 void MockWebClipboardImpl::writeHTML(const blink::WebString& htmlText,
139 const blink::WebURL& url,
140 const blink::WebString& plainText,
141 bool writeSmartPaste) {
142 clear();
143
144 m_htmlText = htmlText;
145 m_plainText = plainText;
146 m_writeSmartPaste = writeSmartPaste;
147 }
148
writePlainText(const blink::WebString & plain_text)149 void MockWebClipboardImpl::writePlainText(const blink::WebString& plain_text) {
150 clear();
151
152 m_plainText = plain_text;
153 }
154
writeURL(const blink::WebURL & url,const blink::WebString & title)155 void MockWebClipboardImpl::writeURL(const blink::WebURL& url,
156 const blink::WebString& title) {
157 clear();
158
159 m_htmlText = WebString::fromUTF8(URLToMarkup(url, title));
160 m_plainText = url.spec().utf16();
161 }
162
writeImage(const blink::WebImage & image,const blink::WebURL & url,const blink::WebString & title)163 void MockWebClipboardImpl::writeImage(const blink::WebImage& image,
164 const blink::WebURL& url,
165 const blink::WebString& title) {
166 if (!image.isNull()) {
167 clear();
168
169 m_plainText = m_htmlText;
170 m_htmlText = WebString::fromUTF8(URLToImageMarkup(url, title));
171 m_image = image;
172 }
173 }
174
writeDataObject(const WebDragData & data)175 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) {
176 clear();
177
178 const WebVector<WebDragData::Item>& itemList = data.items();
179 for (size_t i = 0; i < itemList.size(); ++i) {
180 const WebDragData::Item& item = itemList[i];
181 switch (item.storageType) {
182 case WebDragData::Item::StorageTypeString: {
183 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeText)) {
184 m_plainText = item.stringData;
185 continue;
186 }
187 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) {
188 m_htmlText = item.stringData;
189 continue;
190 }
191 m_customData.insert(std::make_pair(item.stringType, item.stringData));
192 continue;
193 }
194 case WebDragData::Item::StorageTypeFilename:
195 case WebDragData::Item::StorageTypeBinaryData:
196 NOTREACHED(); // Currently unused by the clipboard implementation.
197 }
198 }
199 }
200
clear()201 void MockWebClipboardImpl::clear() {
202 m_plainText = WebString();
203 m_htmlText = WebString();
204 m_image.reset();
205 m_customData.clear();
206 m_writeSmartPaste = false;
207 }
208
209 } // namespace content
210