| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/support/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 "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" | |
| 22 | |
| 23 using WebKit::WebDragData; | |
| 24 using WebKit::WebString; | |
| 25 using WebKit::WebURL; | |
| 26 using WebKit::WebVector; | |
| 27 | |
| 28 MockWebClipboardImpl::MockWebClipboardImpl() { | |
| 29 } | |
| 30 | |
| 31 MockWebClipboardImpl::~MockWebClipboardImpl() { | |
| 32 } | |
| 33 | |
| 34 bool MockWebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { | |
| 35 switch (format) { | |
| 36 case FormatPlainText: | |
| 37 return !m_plainText.isNull(); | |
| 38 | |
| 39 case FormatHTML: | |
| 40 return !m_htmlText.isNull(); | |
| 41 | |
| 42 case FormatSmartPaste: | |
| 43 return m_writeSmartPaste; | |
| 44 | |
| 45 default: | |
| 46 NOTREACHED(); | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 switch (buffer) { | |
| 51 case BufferStandard: | |
| 52 break; | |
| 53 case BufferSelection: | |
| 54 #if defined(OS_POSIX) && !defined(OS_MACOSX) | |
| 55 break; | |
| 56 #endif | |
| 57 default: | |
| 58 NOTREACHED(); | |
| 59 return false; | |
| 60 } | |
| 61 | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 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")); | |
| 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 | |
| 88 WebKit::WebString MockWebClipboardImpl::readPlainText( | |
| 89 WebKit::WebClipboard::Buffer buffer) { | |
| 90 return m_plainText; | |
| 91 } | |
| 92 | |
| 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) { | |
| 97 *fragmentStart = 0; | |
| 98 *fragmentEnd = static_cast<unsigned>(m_htmlText.length()); | |
| 99 return m_htmlText; | |
| 100 } | |
| 101 | |
| 102 WebKit::WebData MockWebClipboardImpl::readImage( | |
| 103 WebKit::WebClipboard::Buffer buffer) { | |
| 104 std::string data; | |
| 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, | |
| 113 #else | |
| 114 gfx::PNGCodec::FORMAT_BGRA, | |
| 115 #endif | |
| 116 gfx::Size(bitmap.width(), bitmap.height()), | |
| 117 bitmap.rowBytes(), | |
| 118 false /* discard_transparency */, | |
| 119 std::vector<gfx::PNGCodec::Comment>(), | |
| 120 &encoded_image); | |
| 121 data.assign(reinterpret_cast<char*>(vector_as_array(&encoded_image)), | |
| 122 encoded_image.size()); | |
| 123 return data; | |
| 124 } | |
| 125 | |
| 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()) | |
| 132 return it->second; | |
| 133 return WebKit::WebString(); | |
| 134 } | |
| 135 | |
| 136 void MockWebClipboardImpl::writeHTML( | |
| 137 const WebKit::WebString& htmlText, const WebKit::WebURL& url, | |
| 138 const WebKit::WebString& plainText, bool writeSmartPaste) { | |
| 139 clear(); | |
| 140 | |
| 141 m_htmlText = htmlText; | |
| 142 m_plainText = plainText; | |
| 143 m_writeSmartPaste = writeSmartPaste; | |
| 144 } | |
| 145 | |
| 146 void MockWebClipboardImpl::writePlainText(const WebKit::WebString& plain_text) { | |
| 147 clear(); | |
| 148 | |
| 149 m_plainText = plain_text; | |
| 150 } | |
| 151 | |
| 152 void MockWebClipboardImpl::writeURL( | |
| 153 const WebKit::WebURL& url, const WebKit::WebString& title) { | |
| 154 clear(); | |
| 155 | |
| 156 m_htmlText = WebString::fromUTF8(webkit_clipboard::URLToMarkup(url, title)); | |
| 157 m_plainText = url.spec().utf16(); | |
| 158 } | |
| 159 | |
| 160 void MockWebClipboardImpl::writeImage(const WebKit::WebImage& image, | |
| 161 const WebKit::WebURL& url, const WebKit::WebString& title) { | |
| 162 if (!image.isNull()) { | |
| 163 clear(); | |
| 164 | |
| 165 m_plainText = m_htmlText; | |
| 166 m_htmlText = WebString::fromUTF8( | |
| 167 webkit_clipboard::URLToImageMarkup(url, title)); | |
| 168 m_image = image; | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) { | |
| 173 clear(); | |
| 174 | |
| 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; | |
| 182 continue; | |
| 183 } | |
| 184 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) { | |
| 185 m_htmlText = item.stringData; | |
| 186 continue; | |
| 187 } | |
| 188 m_customData.insert(std::make_pair(item.stringType, item.stringData)); | |
| 189 continue; | |
| 190 } | |
| 191 case WebDragData::Item::StorageTypeFilename: | |
| 192 case WebDragData::Item::StorageTypeBinaryData: | |
| 193 NOTREACHED(); // Currently unused by the clipboard implementation. | |
| 194 } | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 void MockWebClipboardImpl::clear() { | |
| 199 m_plainText = WebString(); | |
| 200 m_htmlText = WebString(); | |
| 201 m_image.reset(); | |
| 202 m_customData.clear(); | |
| 203 m_writeSmartPaste = false; | |
| 204 } | |
| OLD | NEW |