summaryrefslogtreecommitdiff
path: root/chromium/components/exo/drag_drop_operation.cc
blob: e7ad6b32e7d6e963f4781142b10f9e51cac44975 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2019 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 "components/exo/drag_drop_operation.h"

#include "base/barrier_closure.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "components/exo/data_offer.h"
#include "components/exo/data_source.h"
#include "components/exo/seat.h"
#include "components/exo/surface.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/transform_util.h"

#if defined(OS_CHROMEOS)
#include "ash/drag_drop/drag_drop_controller.h"
#endif  // defined(OS_CHROMEOS)

namespace exo {

namespace {

uint32_t DndActionsToDragOperations(const base::flat_set<DndAction>& actions) {
  uint32_t dnd_operations = 0;
  for (const DndAction action : actions) {
    switch (action) {
      case DndAction::kNone:
        FALLTHROUGH;
        // We don't support the ask action
      case DndAction::kAsk:
        break;
      case DndAction::kCopy:
        dnd_operations |= ui::DragDropTypes::DragOperation::DRAG_COPY;
        break;
      case DndAction::kMove:
        dnd_operations |= ui::DragDropTypes::DragOperation::DRAG_MOVE;
        break;
    }
  }
  return dnd_operations;
}

#if defined(OS_CHROMEOS)
DndAction DragOperationsToPreferredDndAction(int op) {
  if (op & ui::DragDropTypes::DragOperation::DRAG_COPY)
    return DndAction::kCopy;

  if (op & ui::DragDropTypes::DragOperation::DRAG_MOVE)
    return DndAction::kMove;

  return DndAction::kNone;
}
#endif

DndAction DragOperationToDndAction(int op) {
  switch (op) {
    case ui::DragDropTypes::DragOperation::DRAG_NONE:
      return DndAction::kNone;
    case ui::DragDropTypes::DragOperation::DRAG_MOVE:
      return DndAction::kMove;
    case ui::DragDropTypes::DragOperation::DRAG_COPY:
      return DndAction::kCopy;
    default:
      NOTREACHED();
      return DndAction::kNone;
  }
}

}  // namespace

base::WeakPtr<DragDropOperation> DragDropOperation::Create(
    DataSource* source,
    Surface* origin,
    Surface* icon,
    const gfx::Point& drag_start_point,
    ui::DragDropTypes::DragEventSource event_source) {
  auto* dnd_op = new DragDropOperation(source, origin, icon, drag_start_point,
                                       event_source);
  return dnd_op->weak_ptr_factory_.GetWeakPtr();
}

DragDropOperation::DragDropOperation(
    DataSource* source,
    Surface* origin,
    Surface* icon,
    const gfx::Point& drag_start_point,
    ui::DragDropTypes::DragEventSource event_source)
    : SurfaceTreeHost("ExoDragDropOperation"),
      source_(std::make_unique<ScopedDataSource>(source, this)),
      origin_(std::make_unique<ScopedSurface>(origin, this)),
      drag_start_point_(drag_start_point),
      os_exchange_data_(std::make_unique<ui::OSExchangeData>()),
      event_source_(event_source),
      weak_ptr_factory_(this) {
  aura::Window* root_window = origin_->get()->window()->GetRootWindow();
  DCHECK(root_window);
#if defined(OS_CHROMEOS)
  drag_drop_controller_ = static_cast<ash::DragDropController*>(
      aura::client::GetDragDropClient(root_window));
#else
  drag_drop_controller_ = aura::client::GetDragDropClient(root_window);
#endif
  DCHECK(drag_drop_controller_);

  if (drag_drop_controller_->IsDragDropInProgress())
    drag_drop_controller_->DragCancel();

  drag_drop_controller_->AddObserver(this);

  if (icon)
    icon_ = std::make_unique<ScopedSurface>(icon, this);

  auto start_op_callback =
      base::BindOnce(&DragDropOperation::ScheduleStartDragDropOperation,
                     weak_ptr_factory_.GetWeakPtr());

  // When the icon is present, make the count kMaxClipboardDataTypes + 1 so we
  // can wait for the icon to be captured as well.
  counter_ = base::BarrierClosure(kMaxClipboardDataTypes + (icon ? 1 : 0),
                                  std::move(start_op_callback));

  source->GetDataForPreferredMimeTypes(
      base::BindOnce(&DragDropOperation::OnTextRead,
                     weak_ptr_factory_.GetWeakPtr()),
      DataSource::ReadDataCallback(),
      base::BindOnce(&DragDropOperation::OnHTMLRead,
                     weak_ptr_factory_.GetWeakPtr()),
      DataSource::ReadDataCallback(), counter_);

  if (icon) {
    origin_->get()->window()->AddChild(host_window());
    SetRootSurface(icon);
  }
}

DragDropOperation::~DragDropOperation() {
  drag_drop_controller_->RemoveObserver(this);

  if (source_)
    source_->get()->Cancelled();

  if (drag_drop_controller_->IsDragDropInProgress() && started_by_this_object_)
    drag_drop_controller_->DragCancel();
}

void DragDropOperation::AbortIfPending() {
  if (!started_by_this_object_)
    delete this;
}

void DragDropOperation::OnTextRead(const std::string& mime_type,
                                   base::string16 data) {
  DCHECK(os_exchange_data_);
  os_exchange_data_->SetString(std::move(data));

  // Prefer to use the HTML MIME type if possible
  if (mime_type_.empty())
    mime_type_ = mime_type;
  counter_.Run();
}

void DragDropOperation::OnHTMLRead(const std::string& mime_type,
                                   base::string16 data) {
  DCHECK(os_exchange_data_);
  os_exchange_data_->SetHtml(std::move(data), GURL());
  mime_type_ = mime_type;
  counter_.Run();
}

void DragDropOperation::OnSurfaceCommit() {
  SurfaceTreeHost::OnSurfaceCommit();

  if (icon_)
    CaptureDragIcon();
}

void DragDropOperation::CaptureDragIcon() {
  SubmitCompositorFrame();

  std::unique_ptr<viz::CopyOutputRequest> request =
      std::make_unique<viz::CopyOutputRequest>(
          viz::CopyOutputRequest::ResultFormat::RGBA_BITMAP,
          base::BindOnce(&DragDropOperation::OnDragIconCaptured,
                         weak_ptr_factory_.GetWeakPtr()));
  request->set_result_task_runner(base::SequencedTaskRunnerHandle::Get());

  host_window()->layer()->RequestCopyOfOutput(std::move(request));
}

void DragDropOperation::OnDragIconCaptured(
    std::unique_ptr<viz::CopyOutputResult> icon_result) {
  gfx::ImageSkia icon_skia;

  // An empty response means the request was deleted before it was completed. If
  // this happens, and no operation has yet finished, restart the capture.
  if (icon_result->IsEmpty()) {
    CaptureDragIcon();
    return;
  }

  float scale_factor = origin_->get()->window()->layer()->device_scale_factor();
  icon_skia = gfx::ImageSkia(
      gfx::ImageSkiaRep(icon_result->AsSkBitmap(), scale_factor));

  if (os_exchange_data_) {
    os_exchange_data_->provider().SetDragImage(
        icon_skia, -icon_->get()->GetBufferOffset());
  } else {
#if defined(OS_CHROMEOS)
    drag_drop_controller_->SetDragImage(icon_skia,
                                        -icon_->get()->GetBufferOffset());
#endif
  }

  if (!captured_icon_) {
    captured_icon_ = true;
    counter_.Run();
  }
}

void DragDropOperation::ScheduleStartDragDropOperation() {
  // StartDragAndDrop uses a nested run loop. When restarting, we a) don't want
  // to interrupt the callers task for an arbitrary period of time and b) want
  // to let any nested run loops that are currently running to have a chance to
  // exit to avoid arbitrarily deep nesting. We can accomplish both of those
  // things by posting a new task to actually start the drag and drop operation.
  base::SequencedTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(&DragDropOperation::StartDragDropOperation,
                                weak_ptr_factory_.GetWeakPtr()));
}

void DragDropOperation::StartDragDropOperation() {
  uint32_t dnd_operations =
      DndActionsToDragOperations(source_->get()->GetActions());

  base::WeakPtr<DragDropOperation> weak_ptr = weak_ptr_factory_.GetWeakPtr();

  started_by_this_object_ = true;
  // This triggers a nested run loop that terminates when the drag and drop
  // operation is completed.
  int op = drag_drop_controller_->StartDragAndDrop(
      std::move(os_exchange_data_), origin_->get()->window()->GetRootWindow(),
      origin_->get()->window(), drag_start_point_, dnd_operations,
      event_source_);

  // The instance deleted during StartDragAndDrop's nested RunLoop.
  if (!weak_ptr)
    return;

  if (op) {
    // Success

    // TODO(crbug.com/994065) This is currently not the actual mime type used by
    // the recipient, just an arbitrary one we pick out of the offered types so
    // we can report back whether or not the drop can succeed. This may need to
    // change in the future.
    source_->get()->Target(mime_type_);

    source_->get()->Action(DragOperationToDndAction(op));
    source_->get()->DndDropPerformed();
    source_->get()->DndFinished();

    // Reset |source_| so it the destructor doesn't try to cancel it.
    source_.reset();
  }

  // On failure the destructor will handle canceling the data source.
  delete this;
}

void DragDropOperation::OnDragStarted() {
  if (!started_by_this_object_)
    delete this;
}

void DragDropOperation::OnDragEnded() {}

#if defined(OS_CHROMEOS)
void DragDropOperation::OnDragActionsChanged(int actions) {
  if (!started_by_this_object_)
    return;

  DndAction dnd_action = DragOperationsToPreferredDndAction(actions);
  // We send a mime type along with the action to indicate to the application
  // that dropping is/is not currently possible. We do not currently know of
  // any applications that care about the specific mime type until the drop is
  // actually performed.
  if (dnd_action != DndAction::kNone)
    source_->get()->Target(mime_type_);
  else
    source_->get()->Target(base::nullopt);

  source_->get()->Action(dnd_action);
}
#endif

void DragDropOperation::OnSurfaceDestroying(Surface* surface) {
  if (surface == origin_->get() || surface == icon_->get()) {
    delete this;
  } else {
    NOTREACHED();
  }
}

void DragDropOperation::OnDataSourceDestroying(DataSource* source) {
  if (source == source_->get()) {
    source_.reset();
    delete this;
  } else {
    NOTREACHED();
  }
}
}  // namespace exo