GPU workaround to simulate Out of Memory errors with large textures
[chromium-blink-merge.git] / content / test / mock_webclipboard_impl.cc
blob8a6e7748e5bbfea432fb4a39b66d590d5b0c728d
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"
7 #include <algorithm>
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;
23 using blink::WebURL;
24 using blink::WebVector;
26 namespace content {
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) {
39 switch (format) {
40 case FormatPlainText:
41 return !m_plainText.is_null();
43 case FormatHTML:
44 return !m_htmlText.is_null();
46 case FormatSmartPaste:
47 return m_writeSmartPaste;
49 default:
50 NOTREACHED();
51 return false;
55 WebVector<WebString> MockWebClipboardImpl::readAvailableTypes(
56 Buffer buffer,
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 =
70 m_customData.begin();
71 it != m_customData.end(); ++it) {
72 CHECK(std::find(results.begin(), results.end(), it->first) ==
73 results.end());
74 results.push_back(it->first);
76 return results;
79 blink::WebString MockWebClipboardImpl::readPlainText(
80 blink::WebClipboard::Buffer buffer) {
81 return m_plainText;
84 // TODO(wtc): set output argument *url.
85 blink::WebString MockWebClipboardImpl::readHTML(
86 blink::WebClipboard::Buffer buffer,
87 blink::WebURL* url,
88 unsigned* fragmentStart,
89 unsigned* fragmentEnd) {
90 *fragmentStart = 0;
91 *fragmentEnd = static_cast<unsigned>(m_htmlText.string().length());
92 return m_htmlText;
95 blink::WebData MockWebClipboardImpl::readImage(
96 blink::WebClipboard::Buffer buffer) {
97 std::string data;
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,
106 #else
107 gfx::PNGCodec::FORMAT_BGRA,
108 #endif
109 gfx::Size(bitmap.width(), bitmap.height()),
110 static_cast<int>(bitmap.rowBytes()),
111 false /* discard_transparency */,
112 std::vector<gfx::PNGCodec::Comment>(),
113 &encoded_image);
114 data.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image)),
115 encoded_image.size());
116 return data;
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())
125 return it->second;
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) {
133 clear();
135 m_htmlText = htmlText;
136 m_plainText = plainText;
137 m_writeSmartPaste = writeSmartPaste;
138 ++m_sequenceNumber;
141 void MockWebClipboardImpl::writePlainText(const blink::WebString& plain_text) {
142 clear();
144 m_plainText = plain_text;
145 ++m_sequenceNumber;
148 void MockWebClipboardImpl::writeURL(const blink::WebURL& url,
149 const blink::WebString& title) {
150 clear();
152 m_htmlText = WebString::fromUTF8(URLToMarkup(url, title));
153 m_plainText = url.spec().utf16();
154 ++m_sequenceNumber;
157 void MockWebClipboardImpl::writeImage(const blink::WebImage& image,
158 const blink::WebURL& url,
159 const blink::WebString& title) {
160 if (!image.isNull()) {
161 clear();
163 m_plainText = m_htmlText;
164 m_htmlText = WebString::fromUTF8(URLToImageMarkup(url, title));
165 m_image = image;
166 ++m_sequenceNumber;
170 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) {
171 clear();
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: {
178 ++m_sequenceNumber;
179 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeText)) {
180 m_plainText = item.stringData;
181 continue;
183 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) {
184 m_htmlText = item.stringData;
185 continue;
187 m_customData.insert(std::make_pair(item.stringType, item.stringData));
188 continue;
190 default:
191 // Currently other types are unused by the clipboard implementation.
192 NOTREACHED();
197 void MockWebClipboardImpl::clear() {
198 m_plainText = base::NullableString16();
199 m_htmlText = base::NullableString16();
200 m_image.reset();
201 m_customData.clear();
202 m_writeSmartPaste = false;
205 } // namespace content