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