| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "webkit/glue/webdropdata.h" | 5 #include "webkit/glue/webdropdata.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/string_util.h" |
| 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebData.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebDragData.
h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebDragData.
h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
| 17 #include "ui/base/clipboard/clipboard.h" |
| 14 | 18 |
| 15 using WebKit::WebData; | 19 using WebKit::WebData; |
| 16 using WebKit::WebDragData; | 20 using WebKit::WebDragData; |
| 17 using WebKit::WebString; | 21 using WebKit::WebString; |
| 18 using WebKit::WebVector; | 22 using WebKit::WebVector; |
| 19 | 23 |
| 20 WebDropData::WebDropData(const WebDragData& drag_data) | 24 WebDropData::WebDropData(const WebDragData& drag_data) { |
| 21 : url(drag_data.url()), | 25 const WebVector<WebDragData::Item>& item_list = drag_data.items(); |
| 22 url_title(drag_data.urlTitle()), | 26 for (size_t i = 0; i < item_list.size(); ++i) { |
| 23 download_metadata(drag_data.downloadMetadata()), | 27 const WebDragData::Item& item = item_list[i]; |
| 24 plain_text(drag_data.plainText()), | 28 switch (item.storageType) { |
| 25 text_html(drag_data.htmlText()), | 29 case WebDragData::Item::StorageTypeString: { |
| 26 html_base_url(drag_data.htmlBaseURL()), | 30 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeText)) { |
| 27 file_description_filename(drag_data.fileContentFilename()) { | 31 plain_text = item.stringData; |
| 28 if (drag_data.containsFilenames()) { | 32 break; |
| 29 WebVector<WebString> filenames_copy; | 33 } |
| 30 drag_data.filenames(filenames_copy); | 34 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeURIList)) { |
| 31 for (size_t i = 0; i < filenames_copy.size(); ++i) | 35 url = GURL(item.stringData); |
| 32 filenames.push_back(filenames_copy[i]); | 36 url_title = item.title; |
| 33 } | 37 break; |
| 34 WebData contents = drag_data.fileContent(); | 38 } |
| 35 if (!contents.isEmpty()) | 39 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeDownloadURL)) { |
| 36 file_contents.assign(contents.data(), contents.size()); | 40 download_metadata = item.stringData; |
| 37 const WebVector<WebDragData::CustomData>& custom_data_alias = | 41 break; |
| 38 drag_data.customData(); | 42 } |
| 39 for (size_t i = 0; i < custom_data_alias.size(); ++i) { | 43 if (EqualsASCII(item.stringType, ui::Clipboard::kMimeTypeHTML)) { |
| 40 custom_data.insert(std::make_pair(custom_data_alias[i].type, | 44 text_html = item.stringData; |
| 41 custom_data_alias[i].data)); | 45 html_base_url = item.baseURL; |
| 46 break; |
| 47 } |
| 48 custom_data.insert(std::make_pair(item.stringType, item.stringData)); |
| 49 break; |
| 50 } |
| 51 case WebDragData::Item::StorageTypeBinaryData: |
| 52 file_contents.assign(item.binaryData.data(), |
| 53 item.binaryData.size()); |
| 54 file_description_filename = item.title; |
| 55 break; |
| 56 case WebDragData::Item::StorageTypeFilename: |
| 57 // We don't currently use this. |
| 58 NOTREACHED(); |
| 59 }; |
| 42 } | 60 } |
| 43 } | 61 } |
| 44 | 62 |
| 45 WebDropData::WebDropData() { | 63 WebDropData::WebDropData() { |
| 46 } | 64 } |
| 47 | 65 |
| 48 WebDropData::~WebDropData() { | 66 WebDropData::~WebDropData() { |
| 49 } | 67 } |
| 50 | 68 |
| 51 WebDragData WebDropData::ToDragData() const { | 69 WebDragData WebDropData::ToDragData() const { |
| 70 std::vector<WebDragData::Item> item_list; |
| 71 |
| 72 // These fields are currently unused when dragging into WebKit. |
| 73 DCHECK(download_metadata.empty()); |
| 74 DCHECK(file_contents.empty()); |
| 75 DCHECK(file_description_filename.empty()); |
| 76 |
| 77 // TODO(dcheng): We need a way to distinguish between null and empty strings. |
| 78 if (!plain_text.empty()) { |
| 79 WebDragData::Item item; |
| 80 item.storageType = WebDragData::Item::StorageTypeString; |
| 81 item.stringType = WebString::fromUTF8(ui::Clipboard::kMimeTypeText); |
| 82 item.stringData = plain_text; |
| 83 item_list.push_back(item); |
| 84 } |
| 85 |
| 86 if (!url.is_empty()) { |
| 87 WebDragData::Item item; |
| 88 item.storageType = WebDragData::Item::StorageTypeString; |
| 89 item.stringType = WebString::fromUTF8(ui::Clipboard::kMimeTypeURIList); |
| 90 item.stringData = WebString::fromUTF8(url.spec()); |
| 91 item.title = url_title; |
| 92 item_list.push_back(item); |
| 93 } |
| 94 |
| 95 if (!text_html.empty()) { |
| 96 WebDragData::Item item; |
| 97 item.storageType = WebDragData::Item::StorageTypeString; |
| 98 item.stringType = WebString::fromUTF8(ui::Clipboard::kMimeTypeHTML); |
| 99 item.stringData = text_html; |
| 100 item.baseURL = html_base_url; |
| 101 item_list.push_back(item); |
| 102 } |
| 103 |
| 104 for (std::vector<string16>::const_iterator it = filenames.begin(); |
| 105 it != filenames.end(); |
| 106 ++it) { |
| 107 WebDragData::Item item; |
| 108 item.storageType = WebDragData::Item::StorageTypeFilename; |
| 109 item.filenameData = *it; |
| 110 item_list.push_back(item); |
| 111 } |
| 112 |
| 113 for (std::map<string16, string16>::const_iterator it = custom_data.begin(); |
| 114 it != custom_data.end(); |
| 115 ++it) { |
| 116 WebDragData::Item item; |
| 117 item.storageType = WebDragData::Item::StorageTypeString; |
| 118 item.stringType = it->first; |
| 119 item.stringData = it->second; |
| 120 item_list.push_back(item); |
| 121 } |
| 122 |
| 52 WebDragData result; | 123 WebDragData result; |
| 53 result.initialize(); | 124 result.initialize(); |
| 54 result.setURL(url); | 125 result.setItems(item_list); |
| 55 result.setURLTitle(url_title); | |
| 56 result.setFilenames(filenames); | |
| 57 #ifdef WEBKIT_DRAG_DROP_SUPPORT_FILESYSTEM | 126 #ifdef WEBKIT_DRAG_DROP_SUPPORT_FILESYSTEM |
| 58 // TODO(kinuko): remove this ifdef once we update the WebKit API. | 127 // TODO(kinuko): remove this ifdef once we update the WebKit API. |
| 59 result.setFilesystemId(filesystem_id); | 128 result.setFilesystemId(filesystem_id); |
| 60 #endif | 129 #endif |
| 61 result.setPlainText(plain_text); | |
| 62 result.setHTMLText(text_html); | |
| 63 result.setHTMLBaseURL(html_base_url); | |
| 64 result.setFileContentFilename(file_description_filename); | |
| 65 result.setFileContent(WebData(file_contents.data(), file_contents.size())); | |
| 66 WebVector<WebDragData::CustomData> custom_data_vector(custom_data.size()); | |
| 67 size_t i = 0; | |
| 68 for (std::map<string16, string16>::const_iterator it = custom_data.begin(); | |
| 69 it != custom_data.end(); | |
| 70 ++it, ++i) { | |
| 71 WebDragData::CustomData data = {it->first, it->second}; | |
| 72 custom_data_vector[i] = data; | |
| 73 } | |
| 74 result.setCustomData(custom_data_vector); | |
| 75 return result; | 130 return result; |
| 76 } | 131 } |
| OLD | NEW |