1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (C) 2007 Apple Inc. All rights reserved.
* Copyright (C) 2013 Google Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "third_party/blink/renderer/core/page/drag_data.h"
#include "third_party/blink/renderer/core/clipboard/data_object.h"
#include "third_party/blink/renderer/core/dom/document_fragment.h"
#include "third_party/blink/renderer/core/dom/range.h"
#include "third_party/blink/renderer/core/editing/serializers/serialization.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/platform/file_metadata.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "ui/base/clipboard/clipboard_constants.h"
namespace blink {
DragData::DragData(DataObject* data,
const gfx::PointF& client_position,
const gfx::PointF& global_position,
DragOperationsMask source_operation_mask,
bool force_default_action)
: client_position_(client_position),
global_position_(global_position),
platform_drag_data_(data),
dragging_source_operation_mask_(source_operation_mask),
force_default_action_(force_default_action) {}
bool DragData::ContainsHTML() const {
return platform_drag_data_->Types().Contains(ui::kMimeTypeHtml);
}
bool DragData::ContainsURL(FilenameConversionPolicy filename_policy) const {
return platform_drag_data_->Types().Contains(ui::kMimeTypeUriList) ||
(filename_policy == kConvertFilenames &&
platform_drag_data_->ContainsFilenames());
}
String DragData::AsURL(FilenameConversionPolicy filename_policy,
String* title) const {
String url;
if (platform_drag_data_->Types().Contains(ui::kMimeTypeUriList)) {
platform_drag_data_->UrlAndTitle(url, title);
} else if (filename_policy == kConvertFilenames && ContainsFiles()) {
url = FilePathToURL(platform_drag_data_->Filenames()[0]);
}
return url;
}
Vector<String> DragData::AsURLs(
FilenameConversionPolicy filename_policy) const {
Vector<String> result;
if (platform_drag_data_->Types().Contains(ui::kMimeTypeUriList)) {
const auto urls = platform_drag_data_->Urls();
result.reserve(urls.size());
for (const String& url : urls) {
result.push_back(url);
}
} else if (filename_policy == kConvertFilenames && ContainsFiles()) {
const auto filenames = platform_drag_data_->Filenames();
result.reserve(filenames.size());
for (const String& filename : filenames) {
result.push_back(FilePathToURL(filename));
}
}
return result;
}
bool DragData::ContainsFiles() const {
return platform_drag_data_->ContainsFilenames();
}
int DragData::GetModifiers() const {
return platform_drag_data_->GetModifiers();
}
bool DragData::ForceDefaultAction() const {
return force_default_action_;
}
void DragData::AsFilePaths(Vector<String>& result) const {
const Vector<String>& filenames = platform_drag_data_->Filenames();
for (wtf_size_t i = 0; i < filenames.size(); ++i) {
if (!filenames[i].empty())
result.push_back(filenames[i]);
}
}
unsigned DragData::NumberOfFiles() const {
return platform_drag_data_->Filenames().size();
}
bool DragData::ContainsPlainText() const {
return platform_drag_data_->Types().Contains(ui::kMimeTypePlainText);
}
String DragData::AsPlainText() const {
return platform_drag_data_->GetData(ui::kMimeTypePlainText);
}
bool DragData::CanSmartReplace() const {
// Mimic the situations in which mac allows drag&drop to do a smart replace.
// This is allowed whenever the drag data contains a 'range' (ie.,
// ClipboardWin::writeRange is called). For example, dragging a link
// should not result in a space being added.
return platform_drag_data_->Types().Contains(ui::kMimeTypePlainText) &&
!platform_drag_data_->Types().Contains(ui::kMimeTypeUriList);
}
bool DragData::ContainsCompatibleContent() const {
return ContainsPlainText() || ContainsURL() || ContainsHTML() ||
ContainsFiles();
}
DocumentFragment* DragData::AsFragment(LocalFrame* frame) const {
/*
* Order is richest format first. On OSX this is:
* * Web Archive
* * Filenames
* * HTML
* * RTF
* * TIFF
* * PICT
*/
if (ContainsFiles()) {
// FIXME: Implement this. Should be pretty simple to make some HTML
// and call createFragmentFromMarkup.
}
if (ContainsHTML()) {
String html;
KURL base_url;
platform_drag_data_->HtmlAndBaseURL(html, base_url);
DCHECK(frame->GetDocument());
if (DocumentFragment* fragment =
CreateStrictlyProcessedFragmentFromMarkupWithContext(
*frame->GetDocument(), html, 0, html.length(), base_url)) {
return fragment;
}
}
return nullptr;
}
String DragData::DroppedFileSystemId() const {
return platform_drag_data_->FilesystemId();
}
} // namespace blink
|