Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/test/mock_webclipboard_impl.h" | 5 #include "content/test/mock_webclipboard_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/renderer/clipboard_utils.h" | 13 #include "content/renderer/clipboard_utils.h" |
| 14 #include "third_party/WebKit/public/platform/WebCommon.h" | 14 #include "third_party/WebKit/public/platform/WebCommon.h" |
| 15 #include "third_party/WebKit/public/platform/WebDragData.h" | 15 #include "third_party/WebKit/public/platform/WebDragData.h" |
| 16 #include "third_party/WebKit/public/platform/WebImage.h" | 16 #include "third_party/WebKit/public/platform/WebImage.h" |
| 17 #include "third_party/WebKit/public/platform/WebURL.h" | 17 #include "third_party/WebKit/public/platform/WebURL.h" |
| 18 #include "ui/base/clipboard/clipboard.h" | 18 #include "ui/base/clipboard/clipboard.h" |
| 19 #include "ui/gfx/codec/png_codec.h" | 19 #include "ui/gfx/codec/png_codec.h" |
| 20 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
| 21 | 21 |
| 22 using blink::WebDragData; | 22 using blink::WebDragData; |
| 23 using blink::WebString; | 23 using blink::WebString; |
| 24 using blink::WebURL; | 24 using blink::WebURL; |
| 25 using blink::WebVector; | 25 using blink::WebVector; |
| 26 | 26 |
| 27 namespace { | |
| 28 std::string webStringToString(const blink::WebString& string) { | |
| 29 return std::string(string.utf8().c_str()); | |
| 30 } | |
| 31 } // namespace | |
| 32 | |
| 27 namespace content { | 33 namespace content { |
| 28 | 34 |
| 29 MockWebClipboardImpl::MockWebClipboardImpl() {} | 35 MockWebClipboardImpl::MockWebClipboardImpl() {} |
| 30 | 36 |
| 31 MockWebClipboardImpl::~MockWebClipboardImpl() {} | 37 MockWebClipboardImpl::~MockWebClipboardImpl() {} |
| 32 | 38 |
| 33 bool MockWebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { | 39 bool MockWebClipboardImpl::isFormatAvailable(Format format, Buffer buffer) { |
| 34 switch (format) { | 40 switch (format) { |
| 35 case FormatPlainText: | 41 case FormatPlainText: |
| 36 return !m_plainText.isNull(); | 42 return !m_plainText.empty(); |
|
dcheng
2014/01/27 19:47:04
The difference between empty and null is significa
| |
| 37 | 43 |
| 38 case FormatHTML: | 44 case FormatHTML: |
| 39 return !m_htmlText.isNull(); | 45 return !m_htmlText.empty(); |
| 40 | 46 |
| 41 case FormatSmartPaste: | 47 case FormatSmartPaste: |
| 42 return m_writeSmartPaste; | 48 return m_writeSmartPaste; |
| 43 | 49 |
| 44 default: | 50 default: |
| 45 NOTREACHED(); | 51 NOTREACHED(); |
| 46 return false; | 52 return false; |
| 47 } | 53 } |
| 48 | 54 |
| 49 switch (buffer) { | 55 switch (buffer) { |
| 50 case BufferStandard: | 56 case BufferStandard: |
| 51 break; | 57 break; |
| 52 case BufferSelection: | 58 case BufferSelection: |
| 53 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 59 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 54 break; | 60 break; |
| 55 #endif | 61 #endif |
| 56 default: | 62 default: |
| 57 NOTREACHED(); | 63 NOTREACHED(); |
| 58 return false; | 64 return false; |
| 59 } | 65 } |
| 60 | 66 |
| 61 return true; | 67 return true; |
| 62 } | 68 } |
| 63 | 69 |
| 64 WebVector<WebString> MockWebClipboardImpl::readAvailableTypes( | 70 WebVector<WebString> MockWebClipboardImpl::readAvailableTypes( |
| 65 Buffer buffer, | 71 Buffer buffer, |
| 66 bool* containsFilenames) { | 72 bool* containsFilenames) { |
| 67 *containsFilenames = false; | 73 *containsFilenames = false; |
| 68 std::vector<WebString> results; | 74 std::vector<WebString> results; |
| 69 if (!m_plainText.isEmpty()) { | 75 if (!m_plainText.empty()) { |
| 70 results.push_back(WebString("text/plain")); | 76 results.push_back(WebString("text/plain")); |
| 71 } | 77 } |
| 72 if (!m_htmlText.isEmpty()) { | 78 if (!m_htmlText.empty()) { |
| 73 results.push_back(WebString("text/html")); | 79 results.push_back(WebString("text/html")); |
| 74 } | 80 } |
| 75 if (!m_image.isNull()) { | 81 if (!m_image.isNull()) { |
| 76 results.push_back(WebString("image/png")); | 82 results.push_back(WebString("image/png")); |
| 77 } | 83 } |
| 78 for (std::map<base::string16, base::string16>::const_iterator it = | 84 for (std::map<base::string16, base::string16>::const_iterator it = |
| 79 m_customData.begin(); | 85 m_customData.begin(); |
| 80 it != m_customData.end(); ++it) { | 86 it != m_customData.end(); ++it) { |
| 81 CHECK(std::find(results.begin(), results.end(), it->first) == | 87 CHECK(std::find(results.begin(), results.end(), it->first) == |
| 82 results.end()); | 88 results.end()); |
| 83 results.push_back(it->first); | 89 results.push_back(it->first); |
| 84 } | 90 } |
| 85 return results; | 91 return results; |
| 86 } | 92 } |
| 87 | 93 |
| 88 blink::WebString MockWebClipboardImpl::readPlainText( | 94 blink::WebString MockWebClipboardImpl::readPlainText( |
| 89 blink::WebClipboard::Buffer buffer) { | 95 blink::WebClipboard::Buffer buffer) { |
| 90 return m_plainText; | 96 return blink::WebString::fromUTF8(m_plainText); |
| 91 } | 97 } |
| 92 | 98 |
| 93 // TODO(wtc): set output argument *url. | 99 // TODO(wtc): set output argument *url. |
| 94 blink::WebString MockWebClipboardImpl::readHTML( | 100 blink::WebString MockWebClipboardImpl::readHTML( |
| 95 blink::WebClipboard::Buffer buffer, | 101 blink::WebClipboard::Buffer buffer, |
| 96 blink::WebURL* url, | 102 blink::WebURL* url, |
| 97 unsigned* fragmentStart, | 103 unsigned* fragmentStart, |
| 98 unsigned* fragmentEnd) { | 104 unsigned* fragmentEnd) { |
| 99 *fragmentStart = 0; | 105 *fragmentStart = 0; |
| 100 *fragmentEnd = static_cast<unsigned>(m_htmlText.length()); | 106 *fragmentEnd = static_cast<unsigned>(m_htmlText.length()); |
| 101 return m_htmlText; | 107 return blink::WebString::fromUTF8(m_htmlText); |
| 102 } | 108 } |
| 103 | 109 |
| 104 blink::WebData MockWebClipboardImpl::readImage( | 110 blink::WebData MockWebClipboardImpl::readImage( |
| 105 blink::WebClipboard::Buffer buffer) { | 111 blink::WebClipboard::Buffer buffer) { |
| 106 std::string data; | 112 std::string data; |
| 107 std::vector<unsigned char> encoded_image; | 113 std::vector<unsigned char> encoded_image; |
| 108 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that | 114 // TODO(dcheng): Verify that we can assume the image is ARGB8888. Note that |
| 109 // for endianess reasons, it will be BGRA8888 on Windows. | 115 // for endianess reasons, it will be BGRA8888 on Windows. |
| 110 const SkBitmap& bitmap = m_image.getSkBitmap(); | 116 const SkBitmap& bitmap = m_image.getSkBitmap(); |
| 111 SkAutoLockPixels lock(bitmap); | 117 SkAutoLockPixels lock(bitmap); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 134 return it->second; | 140 return it->second; |
| 135 return blink::WebString(); | 141 return blink::WebString(); |
| 136 } | 142 } |
| 137 | 143 |
| 138 void MockWebClipboardImpl::writeHTML(const blink::WebString& htmlText, | 144 void MockWebClipboardImpl::writeHTML(const blink::WebString& htmlText, |
| 139 const blink::WebURL& url, | 145 const blink::WebURL& url, |
| 140 const blink::WebString& plainText, | 146 const blink::WebString& plainText, |
| 141 bool writeSmartPaste) { | 147 bool writeSmartPaste) { |
| 142 clear(); | 148 clear(); |
| 143 | 149 |
| 144 m_htmlText = htmlText; | 150 m_htmlText = webStringToString(htmlText); |
| 145 m_plainText = plainText; | 151 m_plainText = webStringToString(plainText); |
| 146 m_writeSmartPaste = writeSmartPaste; | 152 m_writeSmartPaste = writeSmartPaste; |
| 147 } | 153 } |
| 148 | 154 |
| 149 void MockWebClipboardImpl::writePlainText(const blink::WebString& plain_text) { | 155 void MockWebClipboardImpl::writePlainText(const blink::WebString& plain_text) { |
| 150 clear(); | 156 clear(); |
| 151 | 157 |
| 152 m_plainText = plain_text; | 158 m_plainText = webStringToString(plain_text); |
| 153 } | 159 } |
| 154 | 160 |
| 155 void MockWebClipboardImpl::writeURL(const blink::WebURL& url, | 161 void MockWebClipboardImpl::writeURL(const blink::WebURL& url, |
| 156 const blink::WebString& title) { | 162 const blink::WebString& title) { |
| 157 clear(); | 163 clear(); |
| 158 | 164 |
| 159 m_htmlText = WebString::fromUTF8(URLToMarkup(url, title)); | 165 m_htmlText = URLToMarkup(url, title); |
| 160 m_plainText = url.spec().utf16(); | 166 m_plainText = webStringToString(url.string()); |
| 161 } | 167 } |
| 162 | 168 |
| 163 void MockWebClipboardImpl::writeImage(const blink::WebImage& image, | 169 void MockWebClipboardImpl::writeImage(const blink::WebImage& image, |
| 164 const blink::WebURL& url, | 170 const blink::WebURL& url, |
| 165 const blink::WebString& title) { | 171 const blink::WebString& title) { |
| 166 if (!image.isNull()) { | 172 if (!image.isNull()) { |
| 167 clear(); | 173 clear(); |
| 168 | 174 |
| 169 m_plainText = m_htmlText; | 175 m_plainText = m_htmlText; |
| 170 m_htmlText = WebString::fromUTF8(URLToImageMarkup(url, title)); | 176 m_htmlText = URLToImageMarkup(url, title); |
| 171 m_image = image; | 177 m_image = image; |
| 172 } | 178 } |
| 173 } | 179 } |
| 174 | 180 |
| 175 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) { | 181 void MockWebClipboardImpl::writeDataObject(const WebDragData& data) { |
| 176 clear(); | 182 clear(); |
| 177 | 183 |
| 178 const WebVector<WebDragData::Item>& itemList = data.items(); | 184 const WebVector<WebDragData::Item>& itemList = data.items(); |
| 179 for (size_t i = 0; i < itemList.size(); ++i) { | 185 for (size_t i = 0; i < itemList.size(); ++i) { |
| 180 const WebDragData::Item& item = itemList[i]; | 186 const WebDragData::Item& item = itemList[i]; |
| 181 switch (item.storageType) { | 187 switch (item.storageType) { |
| 182 case WebDragData::Item::StorageTypeString: { | 188 case WebDragData::Item::StorageTypeString: { |
| 183 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeText)) { | 189 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeText)) { |
| 184 m_plainText = item.stringData; | 190 m_plainText = webStringToString(item.stringData); |
| 185 continue; | 191 continue; |
| 186 } | 192 } |
| 187 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) { | 193 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) { |
| 188 m_htmlText = item.stringData; | 194 m_htmlText = webStringToString(item.stringData); |
| 189 continue; | 195 continue; |
| 190 } | 196 } |
| 191 m_customData.insert(std::make_pair(item.stringType, item.stringData)); | 197 m_customData.insert(std::make_pair(item.stringType, item.stringData)); |
| 192 continue; | 198 continue; |
| 193 } | 199 } |
| 194 case WebDragData::Item::StorageTypeFilename: | 200 case WebDragData::Item::StorageTypeFilename: |
| 195 case WebDragData::Item::StorageTypeBinaryData: | 201 case WebDragData::Item::StorageTypeBinaryData: |
| 196 NOTREACHED(); // Currently unused by the clipboard implementation. | 202 NOTREACHED(); // Currently unused by the clipboard implementation. |
| 197 } | 203 } |
| 198 } | 204 } |
| 199 } | 205 } |
| 200 | 206 |
| 201 void MockWebClipboardImpl::clear() { | 207 void MockWebClipboardImpl::clear() { |
| 202 m_plainText = WebString(); | 208 m_plainText.clear(); |
| 203 m_htmlText = WebString(); | 209 m_htmlText.clear(); |
| 204 m_image.reset(); | 210 m_image.reset(); |
| 205 m_customData.clear(); | 211 m_customData.clear(); |
| 206 m_writeSmartPaste = false; | 212 m_writeSmartPaste = false; |
| 207 } | 213 } |
| 208 | 214 |
| 209 } // namespace content | 215 } // namespace content |
| OLD | NEW |