summaryrefslogtreecommitdiff
path: root/chromium/ui/ozone/platform/wayland/host/wayland_data_device.cc
blob: f80738e5e341c5181fe040836d1ef275d8d65ddf (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/ozone/platform/wayland/host/wayland_data_device.h"

#include <memory>
#include <string>
#include <utility>

#include "base/bind.h"
#include "base/files/scoped_file.h"
#include "ui/base/clipboard/clipboard_buffer.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/ozone/platform/wayland/common/data_util.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
#include "ui/ozone/platform/wayland/common/wayland_util.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_data_offer.h"
#include "ui/ozone/platform/wayland/host/wayland_data_source.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"

namespace ui {

WaylandDataDevice::WaylandDataDevice(WaylandConnection* connection,
                                     wl_data_device* data_device)
    : WaylandDataDeviceBase(connection), data_device_(data_device) {
  static const struct wl_data_device_listener kDataDeviceListener = {
      WaylandDataDevice::OnOffer, WaylandDataDevice::OnEnter,
      WaylandDataDevice::OnLeave, WaylandDataDevice::OnMotion,
      WaylandDataDevice::OnDrop,  WaylandDataDevice::OnSelection};
  wl_data_device_add_listener(data_device_.get(), &kDataDeviceListener, this);
}

WaylandDataDevice::~WaylandDataDevice() = default;

void WaylandDataDevice::StartDrag(const WaylandDataSource& data_source,
                                  const WaylandWindow& origin_window,
                                  wl_surface* icon_surface,
                                  DragDelegate* delegate) {
  DCHECK(delegate);
  DCHECK(!drag_delegate_);
  drag_delegate_ = delegate;

  wl_data_device_start_drag(data_device_.get(), data_source.data_source(),
                            origin_window.root_surface()->surface(),
                            icon_surface, connection()->serial());
  drag_delegate_->DrawIcon();
  connection()->ScheduleFlush();
}

void WaylandDataDevice::ResetDragDelegate() {
  DCHECK(drag_delegate_);
  drag_delegate_ = nullptr;
}

void WaylandDataDevice::RequestData(WaylandDataOffer* offer,
                                    const std::string& mime_type,
                                    RequestDataCallback callback) {
  DCHECK(offer);
  DCHECK(wl::IsMimeTypeSupported(mime_type));

  base::ScopedFD fd = offer->Receive(mime_type);
  if (!fd.is_valid()) {
    LOG(ERROR) << "Failed to open file descriptor.";
    return;
  }

  // Ensure there is not pending operation to be performed by the compositor,
  // otherwise read(..) can block awaiting data to be sent to pipe.
  RegisterDeferredReadClosure(base::BindOnce(
      &WaylandDataDevice::ReadDragDataFromFD, base::Unretained(this),
      std::move(fd), std::move(callback)));
  RegisterDeferredReadCallback();
}

void WaylandDataDevice::SetSelectionSource(WaylandDataSource* source) {
  DCHECK(source);
  wl_data_device_set_selection(data_device_.get(), source->data_source(),
                               connection()->serial());
  connection()->ScheduleFlush();
}

void WaylandDataDevice::ReadDragDataFromFD(base::ScopedFD fd,
                                           RequestDataCallback callback) {
  std::vector<uint8_t> contents;
  wl::ReadDataFromFD(std::move(fd), &contents);
  std::move(callback).Run(scoped_refptr<base::RefCountedBytes>(
      base::RefCountedBytes::TakeVector(&contents)));
}

// static
void WaylandDataDevice::OnOffer(void* data,
                                wl_data_device* data_device,
                                wl_data_offer* offer) {
  auto* self = static_cast<WaylandDataDevice*>(data);

  self->connection()->clipboard()->UpdateSequenceNumber(
      ClipboardBuffer::kCopyPaste);

  DCHECK(!self->new_offer_);
  self->new_offer_ = std::make_unique<WaylandDataOffer>(offer);
}

void WaylandDataDevice::OnEnter(void* data,
                                wl_data_device* data_device,
                                uint32_t serial,
                                wl_surface* surface,
                                wl_fixed_t x,
                                wl_fixed_t y,
                                wl_data_offer* offer) {
  WaylandWindow* window = wl::RootWindowFromWlSurface(surface);
  if (!window) {
    LOG(ERROR) << "Failed to get window.";
    return;
  }

  auto* self = static_cast<WaylandDataDevice*>(data);

  // Null |drag_delegate_| here means that the DND session has been initiated by
  // an external application. In this case, use the default data drag delegate.
  if (!self->drag_delegate_)
    self->drag_delegate_ = self->connection()->data_drag_controller();

  DCHECK(self->new_offer_);
  self->drag_delegate_->OnDragOffer(std::move(self->new_offer_));

  gfx::PointF point(wl_fixed_to_double(x), wl_fixed_to_double(y));
  self->drag_delegate_->OnDragEnter(window, point, serial);
}

void WaylandDataDevice::OnMotion(void* data,
                                 wl_data_device* data_device,
                                 uint32_t time,
                                 wl_fixed_t x,
                                 wl_fixed_t y) {
  auto* self = static_cast<WaylandDataDevice*>(data);
  if (self->drag_delegate_) {
    gfx::PointF point(wl_fixed_to_double(x), wl_fixed_to_double(y));
    self->drag_delegate_->OnDragMotion(point);
  }
}

void WaylandDataDevice::OnDrop(void* data, wl_data_device* data_device) {
  auto* self = static_cast<WaylandDataDevice*>(data);
  if (self->drag_delegate_)
    self->drag_delegate_->OnDragDrop();
}

void WaylandDataDevice::OnLeave(void* data, wl_data_device* data_device) {
  auto* self = static_cast<WaylandDataDevice*>(data);
  if (self->drag_delegate_)
    self->drag_delegate_->OnDragLeave();

  // When in a DND session initiated by an external application,
  // |drag_delegate_| is set at OnEnter, and must be reset here to avoid
  // potential use-after-free. Above call to OnDragLeave() may result in
  // |drag_delegate_| being reset, so it must be checked here as well.
  if (self->drag_delegate_ && !self->drag_delegate_->IsDragSource())
    self->drag_delegate_ = nullptr;
}

void WaylandDataDevice::OnSelection(void* data,
                                    wl_data_device* data_device,
                                    wl_data_offer* offer) {
  auto* self = static_cast<WaylandDataDevice*>(data);
  DCHECK(self);

  // 'offer' will be null to indicate that the selection is no longer valid,
  // i.e. there is no longer clipboard data available to paste.
  if (!offer) {
    self->ResetDataOffer();

    // Clear Clipboard cache.
    self->connection()->clipboard()->SetData({}, {});
    return;
  }

  DCHECK(self->new_offer_);
  self->set_data_offer(std::move(self->new_offer_));

  self->data_offer()->EnsureTextMimeTypeIfNeeded();
}

}  // namespace ui