2013-10-18 18:33:56 +02:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
#include <string>
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "base/files/file_path.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
#include "libcef/browser/stream_impl.h"
|
2013-10-18 18:33:56 +02:00
|
|
|
#include "libcef/common/drag_data_impl.h"
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
#define CHECK_READONLY_RETURN_VOID() \
|
|
|
|
if (read_only_) { \
|
|
|
|
DCHECK(false) << "object is read only"; \
|
|
|
|
return; \
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
|
|
|
|
CefDragDataImpl::CefDragDataImpl(const content::DropData& data)
|
2017-05-17 11:29:28 +02:00
|
|
|
: data_(data), read_only_(false) {}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2017-05-11 20:22:21 +02:00
|
|
|
CefDragDataImpl::CefDragDataImpl(const content::DropData& data,
|
|
|
|
CefRefPtr<CefImage> image,
|
|
|
|
const CefPoint& image_hotspot)
|
|
|
|
: data_(data),
|
|
|
|
image_(image),
|
|
|
|
image_hotspot_(image_hotspot),
|
2017-05-17 11:29:28 +02:00
|
|
|
read_only_(false) {}
|
2017-05-11 20:22:21 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefDragDataImpl::CefDragDataImpl() : read_only_(false) {}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefDragData> CefDragData::Create() {
|
|
|
|
return new CefDragDataImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefDragData> CefDragDataImpl::Clone() {
|
2020-01-15 14:36:24 +01:00
|
|
|
CefDragDataImpl* drag_data = nullptr;
|
2014-07-01 00:30:29 +02:00
|
|
|
{
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2017-05-11 20:22:21 +02:00
|
|
|
drag_data = new CefDragDataImpl(data_, image_, image_hotspot_);
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
return drag_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::IsReadOnly() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
return read_only_;
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::IsLink() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2017-03-03 23:37:23 +01:00
|
|
|
return (data_.url.is_valid() &&
|
|
|
|
data_.file_contents_content_disposition.empty());
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::IsFragment() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2017-03-03 23:37:23 +01:00
|
|
|
return (!data_.url.is_valid() &&
|
|
|
|
data_.file_contents_content_disposition.empty() &&
|
2013-10-18 18:33:56 +02:00
|
|
|
data_.filenames.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::IsFile() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2017-03-03 23:37:23 +01:00
|
|
|
return (!data_.file_contents_content_disposition.empty() ||
|
|
|
|
!data_.filenames.empty());
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetLinkURL() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2013-10-18 18:33:56 +02:00
|
|
|
return data_.url.spec();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetLinkTitle() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2013-10-18 18:33:56 +02:00
|
|
|
return data_.url_title;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetLinkMetadata() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2013-10-18 18:33:56 +02:00
|
|
|
return data_.download_metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetFragmentText() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2020-07-08 19:23:29 +02:00
|
|
|
return data_.text ? CefString(*data_.text) : CefString();
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetFragmentHtml() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2020-07-08 19:23:29 +02:00
|
|
|
return data_.html ? CefString(*data_.html) : CefString();
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetFragmentBaseURL() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2013-10-18 18:33:56 +02:00
|
|
|
return data_.html_base_url.spec();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefDragDataImpl::GetFileName() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2020-07-08 19:23:29 +02:00
|
|
|
auto filename = data_.GetSafeFilenameForImageFileContents();
|
2017-03-03 23:37:23 +01:00
|
|
|
return filename ? CefString(filename->value()) : CefString();
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t CefDragDataImpl::GetFileContents(CefRefPtr<CefStreamWriter> writer) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (data_.file_contents.empty()) {
|
2014-07-01 00:30:29 +02:00
|
|
|
return 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
char* data = const_cast<char*>(data_.file_contents.c_str());
|
|
|
|
size_t size = data_.file_contents.size();
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!writer.get()) {
|
2014-07-01 00:30:29 +02:00
|
|
|
return size;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
return writer->Write(data, 1, size);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::GetFileNames(std::vector<CefString>& names) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (data_.filenames.empty()) {
|
2013-10-18 18:33:56 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
std::vector<ui::FileInfo>::const_iterator it = data_.filenames.begin();
|
2021-10-04 13:54:06 +02:00
|
|
|
for (; it != data_.filenames.end(); ++it) {
|
|
|
|
auto name = it->display_name.value();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (name.empty()) {
|
2023-09-22 13:04:16 +02:00
|
|
|
name = it->path.BaseName().value();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-10-04 13:54:06 +02:00
|
|
|
names.push_back(name);
|
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
2023-09-22 13:04:16 +02:00
|
|
|
bool CefDragDataImpl::GetFilePaths(std::vector<CefString>& paths) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
if (data_.filenames.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ui::FileInfo>::const_iterator it = data_.filenames.begin();
|
|
|
|
for (; it != data_.filenames.end(); ++it) {
|
|
|
|
auto path = it->path.value();
|
|
|
|
paths.push_back(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
void CefDragDataImpl::SetLinkURL(const CefString& url) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
data_.url = GURL(url.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::SetLinkTitle(const CefString& title) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
data_.url_title = title.ToString16();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::SetLinkMetadata(const CefString& data) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
data_.download_metadata = data.ToString16();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::SetFragmentText(const CefString& text) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2020-07-08 19:23:29 +02:00
|
|
|
data_.text = text.ToString16();
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::SetFragmentHtml(const CefString& fragment) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2020-07-08 19:23:29 +02:00
|
|
|
data_.html = fragment.ToString16();
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::SetFragmentBaseURL(const CefString& fragment) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
data_.html_base_url = GURL(fragment.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::ResetFileContents() {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
|
|
|
data_.file_contents.erase();
|
2017-03-03 23:37:23 +01:00
|
|
|
data_.file_contents_source_url = GURL();
|
|
|
|
data_.file_contents_filename_extension.erase();
|
|
|
|
data_.file_contents_content_disposition.erase();
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefDragDataImpl::AddFile(const CefString& path,
|
|
|
|
const CefString& display_name) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2014-07-01 00:30:29 +02:00
|
|
|
CHECK_READONLY_RETURN_VOID();
|
2024-01-20 23:48:57 +01:00
|
|
|
data_.filenames.emplace_back(base::FilePath(path),
|
|
|
|
base::FilePath(display_name));
|
2014-07-01 00:30:29 +02:00
|
|
|
}
|
|
|
|
|
2022-03-02 20:44:16 +01:00
|
|
|
void CefDragDataImpl::ClearFilenames() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
data_.filenames.clear();
|
|
|
|
}
|
|
|
|
|
2014-07-01 00:30:29 +02:00
|
|
|
void CefDragDataImpl::SetReadOnly(bool read_only) {
|
2014-07-15 00:18:51 +02:00
|
|
|
base::AutoLock lock_scope(lock_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (read_only_ == read_only) {
|
2014-07-01 00:30:29 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-07-01 00:30:29 +02:00
|
|
|
|
|
|
|
read_only_ = read_only;
|
|
|
|
}
|
2017-05-11 20:22:21 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefImage> CefDragDataImpl::GetImage() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return image_;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefPoint CefDragDataImpl::GetImageHotspot() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return image_hotspot_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefDragDataImpl::HasImage() {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
return image_ ? true : false;
|
2023-05-08 17:07:57 +02:00
|
|
|
}
|