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
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/ozone/platform/x11/x11_clipboard_ozone.h"
#include <iterator>
#include <memory>
#include <set>
#include <utility>
#include "base/functional/bind.h"
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/x/x11_clipboard_helper.h"
namespace ui {
X11ClipboardOzone::X11ClipboardOzone()
: helper_(std::make_unique<XClipboardHelper>(
base::BindRepeating(&X11ClipboardOzone::OnSelectionChanged,
base::Unretained(this)))) {
DCHECK(helper_);
}
X11ClipboardOzone::~X11ClipboardOzone() = default;
void X11ClipboardOzone::OfferClipboardData(
ClipboardBuffer buffer,
const PlatformClipboard::DataMap& data_map,
PlatformClipboard::OfferDataClosure callback) {
DCHECK(!callback.is_null());
helper_->CreateNewClipboardData();
for (const auto& item : data_map)
helper_->InsertMapping(item.first, item.second);
helper_->TakeOwnershipOfSelection(buffer);
std::move(callback).Run();
}
void X11ClipboardOzone::RequestClipboardData(
ClipboardBuffer buffer,
const std::string& mime_type,
PlatformClipboard::RequestDataClosure callback) {
DCHECK(!callback.is_null());
auto atoms = mime_type == kMimeTypePlainText
? helper_->GetTextAtoms()
: helper_->GetAtomsForFormat(
ClipboardFormatType::CustomPlatformType(mime_type));
auto selection_data = helper_->Read(buffer, atoms);
std::move(callback).Run(selection_data.TakeBytes());
}
void X11ClipboardOzone::GetAvailableMimeTypes(
ClipboardBuffer buffer,
PlatformClipboard::GetMimeTypesClosure callback) {
DCHECK(!callback.is_null());
// This is the only function clients may use to request available formats, so
// include both standard and platform-specific (atom names) values.
// TODO(crbug.com/40054419): Consider adding a way of filtering mime types and
// querying availability of specific formats, so implementations can optimize
// it, if possible. E.g: Avoid multiple roundtrips to check if a given format
// is available. See ClipboardX11::IsFormatAvailable for example.
auto types = helper_->GetAvailableTypes(buffer);
auto atoms = helper_->GetAvailableAtomNames(buffer);
std::set<std::string> uniq(types.begin(), types.end());
uniq.insert(atoms.begin(), atoms.end());
std::move(callback).Run({uniq.begin(), uniq.end()});
}
bool X11ClipboardOzone::IsSelectionOwner(ClipboardBuffer buffer) {
return helper_->IsSelectionOwner(buffer);
}
void X11ClipboardOzone::SetClipboardDataChangedCallback(
ClipboardDataChangedCallback data_changed_callback) {
clipboard_changed_callback_ = std::move(data_changed_callback);
}
bool X11ClipboardOzone::IsSelectionBufferAvailable() const {
return true;
}
void X11ClipboardOzone::OnSelectionChanged(ClipboardBuffer buffer) {
if (clipboard_changed_callback_)
clipboard_changed_callback_.Run(buffer);
}
} // namespace ui
|