File: wayland_window_drag_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (774 lines) | stat: -rw-r--r-- 29,546 bytes parent folder | download | duplicates (4)
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
// Copyright 2020 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/wayland/host/wayland_window_drag_controller.h"

#include <extended-drag-unstable-v1-client-protocol.h>
#include <wayland-client-protocol.h>
#include <xdg-toplevel-drag-v1-client-protocol.h>

#include <cstdint>
#include <memory>
#include <ostream>
#include <utility>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/task/current_thread.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/platform/platform_event_source.h"
#include "ui/events/platform/scoped_event_dispatcher.h"
#include "ui/events/platform_event.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/ozone/common/features.h"
#include "ui/ozone/platform/wayland/common/wayland_object.h"
#include "ui/ozone/platform/wayland/host/dump_util.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_cursor_position.h"
#include "ui/ozone/platform/wayland/host/wayland_data_device_manager.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_event_source.h"
#include "ui/ozone/platform/wayland/host/wayland_keyboard.h"
#include "ui/ozone/platform/wayland/host/wayland_output_manager.h"
#include "ui/ozone/platform/wayland/host/wayland_screen.h"
#include "ui/ozone/platform/wayland/host/wayland_serial_tracker.h"
#include "ui/ozone/platform/wayland/host/wayland_surface.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"
#include "ui/ozone/platform/wayland/host/wayland_window_manager.h"
#include "ui/ozone/platform/wayland/host/xdg_toplevel.h"
#include "ui/platform_window/platform_window_init_properties.h"

namespace ui {

namespace {

using mojom::DragEventSource;

// Custom mime type used for window dragging DND sessions.
constexpr char kMimeTypeChromiumWindow[] = "chromium/x-window";

// DND action used in window dragging DND sessions.
constexpr uint32_t kDndActionWindowDrag =
    WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE;

// Value minimally higher than `GestureDetector::Config::touch_slop` (see [1])
// to exit the horizontal rail threshold in SnapScrollController, in case of
// an upwards tab dragging detach with touch.
//
// [1] //ui/events/gesture_detection/gesture_detector.h
constexpr int kHorizontalRailExitThreshold = -10;

}  // namespace

class WaylandWindowDragController::ExtendedDragSource {
 public:
  ExtendedDragSource(WaylandConnection& connection, wl_data_source* source)
      : connection_(connection) {
    DCHECK(connection.extended_drag_v1());
    uint32_t options = ZCR_EXTENDED_DRAG_V1_OPTIONS_ALLOW_SWALLOW |
                       ZCR_EXTENDED_DRAG_V1_OPTIONS_ALLOW_DROP_NO_TARGET |
                       ZCR_EXTENDED_DRAG_V1_OPTIONS_LOCK_CURSOR;
    source_.reset(zcr_extended_drag_v1_get_extended_drag_source(
        connection.extended_drag_v1(), source, options));
    DCHECK(source_);
  }

  void SetDraggedWindow(WaylandToplevelWindow* window,
                        const gfx::Vector2d& offset) {
    auto* surface = window ? window->root_surface()->surface() : nullptr;
    zcr_extended_drag_source_v1_drag(source_.get(), surface, offset.x(),
                                     offset.y());
    connection_->Flush();
  }

 private:
  wl::Object<zcr_extended_drag_source_v1> source_;
  const raw_ref<WaylandConnection> connection_;
};

class WaylandWindowDragController::XdgToplevelDrag {
 public:
  XdgToplevelDrag(WaylandConnection& connection, wl_data_source* source)
      : connection_(connection) {
    DCHECK(connection.toplevel_drag_manager_v1());
    drag_.reset(xdg_toplevel_drag_manager_v1_get_xdg_toplevel_drag(
        connection.toplevel_drag_manager_v1(), source));
    DCHECK(drag_);
  }

  void SetDraggedWindow(WaylandToplevelWindow* window,
                        const gfx::Vector2d& offset) {
    if (!window) {
      // Detaching happens implicitly via wl_data_source.dnd_drop_performed (see
      // OnDataSourceDropPerformed()) or when the toplevel gets unmapped.
      return;
    }
    DCHECK(window->xdg_toplevel());
    DCHECK(window->xdg_toplevel()->wl_object());
    auto* toplevel = window->xdg_toplevel()->wl_object();

    // xdg-toplevel-drag protocol expects the passed in offset to be relative to
    // the surface's geometry, i.e: no client-side decoration insets included.
    auto toplevel_offset = offset - window->GetWindowGeometryOffsetInDIP();
    xdg_toplevel_drag_v1_attach(drag_.get(), toplevel, toplevel_offset.x(),
                                toplevel_offset.y());
    connection_->Flush();
  }

 private:
  wl::Object<xdg_toplevel_drag_v1> drag_;
  const raw_ref<WaylandConnection> connection_;
};

WaylandWindowDragController::WaylandWindowDragController(
    WaylandConnection* connection,
    WaylandDataDeviceManager* device_manager,
    WaylandPointer::Delegate* pointer_delegate,
    WaylandTouch::Delegate* touch_delegate,
    WaylandKeyboard::Delegate* keyboard_delegate)
    : connection_(connection),
      data_device_manager_(device_manager),
      data_device_(device_manager->GetDevice()),
      window_manager_(connection_->window_manager()),
      pointer_delegate_(pointer_delegate),
      touch_delegate_(touch_delegate),
      keyboard_delegate_(keyboard_delegate) {
  DCHECK(data_device_);
  DCHECK(pointer_delegate_);
  DCHECK(touch_delegate_);
  DCHECK(keyboard_delegate_);
}

WaylandWindowDragController::~WaylandWindowDragController() = default;

bool WaylandWindowDragController::StartDragSession(
    WaylandToplevelWindow* origin,
    DragEventSource drag_source) {
  if (state_ != State::kIdle)
    return true;

  // TODO(crbug.com/340398746): This should be a CHECK instead. However
  // currently buggy compositors, eg: KWin 6, which do not send
  // data_source.dnd_finish|cancelled in some cases. In such a case for a data
  // drag, the data drag controller is left in an inconsistent state and shared
  // dragging state is not reset. This may cause chrome to crash if a window
  // drag session is requested before the shared dragging state is reset. Reset
  // the shared drag state in this case, revert this handling once it stabilizes
  // at compositors side (see also comment in
  // WaylandDataDragController::StartSession).
  if (connection_->IsDragInProgress()) {
    LOG(ERROR) << "Received window drag request for active data drag, "
                  "resetting shared data device state.";
    data_device_->ResetDragDelegate();
    CHECK(!connection_->IsDragInProgress());
  }

  auto serial = GetSerial(drag_source, origin);
  if (!serial) {
    LOG(ERROR) << "Failed to retrieve dnd serial. origin=" << origin
               << " drag_source=" << drag_source;
    return false;
  }

  VLOG(1) << "Starting Window Drag session. tracker_id=" << serial->value
          << ", data_source="
          << (drag_source == DragEventSource::kMouse ? "mouse" : "touch")
          << ", serial tracker=" << connection_->serial_tracker().ToString();

  state_ = State::kAttached;
  origin_window_ = origin;
  drag_source_ = drag_source;

  CHECK(!data_source_);
  data_source_ = data_device_manager_->CreateSource(this);
  data_source_->Offer({kMimeTypeChromiumWindow});
  data_source_->SetDndActions(kDndActionWindowDrag);

  if (connection_->toplevel_drag_manager_v1()) {
    xdg_toplevel_drag_ = std::make_unique<XdgToplevelDrag>(
        *connection_, data_source_->data_source());
  } else if (connection_->extended_drag_v1()) {
    extended_drag_source_ = std::make_unique<ExtendedDragSource>(
        *connection_, data_source_->data_source());
  } else {
    LOG(ERROR)
        << "zcr_extended_drag_v1 and xdg_toplevel_drag_v1 extensions "
           "not available! Window/Tab dragging won't be fully functional.";
  }

  data_device_->StartDrag(*data_source_, *origin_window_, serial->value,
                          /*icon_surface=*/nullptr, this);
  events_grabber_ = origin_window_;
  should_process_drag_motion_events_ = false;
  has_received_enter_ = false;
  nested_dispatcher_ =
      PlatformEventSource::GetInstance()->OverrideDispatcher(this);

  // Observe window so we can take ownership of the origin surface in case it
  // is destroyed during the DND session.
  window_manager_->AddObserver(this);
  return true;
}

bool WaylandWindowDragController::Drag(WaylandToplevelWindow* window,
                                       const gfx::Vector2d& offset) {
  DCHECK_GE(state_, State::kAttached);
  DCHECK(window);

  SetDraggedWindow(window, offset);
  state_ = State::kDetached;
  RunLoop();
  SetDraggedWindow(nullptr, {});

  DCHECK(state_ == State::kAttaching || state_ == State::kDropped ||
         state_ == State::kCancelled) << "Drag state: " << int(state_);
  if (state_ == State::kAttaching) {
    state_ = State::kAttached;
    return false;
  }

  auto state = state_;
  HandleDropAndResetState(EventTimeForNow());

  return state != State::kCancelled;
}

void WaylandWindowDragController::StopDragging() {
  if (state_ != State::kDetached)
    return;

  VLOG(1) << "End drag loop requested. state=" << state_;

  // This function is supposed to be called to indicate that the window was just
  // snapped into a tab strip. So switch to |kAttached| state, store the focused
  // window as the pointer grabber and ask to quit the nested loop.
  state_ = State::kAttaching;
  events_grabber_ = window_manager_->GetCurrentPointerOrTouchFocusedWindow();
  VLOG(1) << "Quiting Loop : StopDragging";
  QuitLoop();
}

bool WaylandWindowDragController::IsDragInProgress() const {
  return state_ != State::kIdle;
}

bool WaylandWindowDragController::IsDraggingWindow(
    WaylandToplevelWindow* window) const {
  CHECK(window);
  return IsDragInProgress() && dragged_window_ == window;
}

void WaylandWindowDragController::CancelDragSession() {
  if (!IsActiveDragAndDropSession()) {
    return;
  }

  VLOG(1) << "Cancelling the drag session. state=" << state_;

  // Per the spec, destroying the data source triggers session cancellation. See
  // https://wayland.app/protocols/wayland#wl_data_device:request:start_drag
  data_source_.reset();
  HandleDragEnd(/*completed=*/true, EventTimeForNow());
}

bool WaylandWindowDragController::IsDragSource() const {
  CHECK(!IsDragInProgress() || !!data_source_) << " state=" << state_;
  return IsDragInProgress();
}

// Icon drawing and update for window/tab dragging is handled by buffer manager.
void WaylandWindowDragController::DrawIcon() {}

void WaylandWindowDragController::OnDragOffer(
    std::unique_ptr<WaylandDataOffer> offer) {
  DCHECK_GE(state_, State::kAttached);
  DCHECK(offer);
  DCHECK(!data_offer_);

  DVLOG(1) << "OnOffer. mime_types=" << offer->mime_types().size();
  data_offer_ = std::move(offer);
}

void WaylandWindowDragController::OnDragEnter(WaylandWindow* window,
                                              const gfx::PointF& location,
                                              base::TimeTicks timestamp,
                                              uint32_t serial) {
  // Drag-and-drop sessions may be terminated by the client while drag-and-drop
  // server events are still in-flight. No-op if this is the case.
  if (!IsActiveDragAndDropSession()) {
    return;
  }

  CHECK_GE(state_, State::kAttached);
  CHECK(window);
  CHECK(data_source_);
  CHECK(data_offer_);

  has_received_enter_ = true;
  drag_target_window_ = window;

  // Forward focus change event to the input delegate, so other components, such
  // as WaylandScreen, are able to properly retrieve focus related info during
  // window dragging sesstions.
  pointer_location_ = location;

  DCHECK(drag_source_.has_value());
  // Check if this is necessary.
  if (*drag_source_ == DragEventSource::kMouse) {
    pointer_delegate_->OnPointerFocusChanged(
        window, location, timestamp, wl::EventDispatchPolicy::kImmediate);
  } else {
    touch_delegate_->OnTouchFocusChanged(window);
  }

  DVLOG(1) << "OnEnter. widget=" << window->GetWidget();

  // TODO(crbug.com/40704369): Exo does not support custom mime types. In this
  // case, |data_offer_| will hold an empty mime_types list and, at this point,
  // it's safe just to skip the offer checks and requests here.
  if (!base::Contains(data_offer_->mime_types(), kMimeTypeChromiumWindow)) {
    DVLOG(1) << "OnEnter. No valid mime type found.";
    return;
  }

  // Accept the offer and set the dnd action.
  data_offer_->SetDndActions(kDndActionWindowDrag);
  data_offer_->Accept(serial, kMimeTypeChromiumWindow);
}

void WaylandWindowDragController::OnDragMotion(const gfx::PointF& location,
                                               base::TimeTicks timestamp) {
  // Drag-and-drop sessions may be terminated by the client while drag-and-drop
  // server events are still in-flight. No-op if this is the case.
  if (!IsActiveDragAndDropSession()) {
    return;
  }

  DCHECK_GE(state_, State::kAttached);
  DVLOG(2) << "OnMotion. location=" << location.ToString();

  // Motion events are not expected to be dispatched while waiting for the drag
  // loop to exit, ie: kAttaching transitional state. See crbug.com/1169446.
  if (state_ == State::kAttaching)
    return;

  // Always process drag events.
  should_process_drag_motion_events_ = true;

  // Forward cursor location update info to the input handling delegate.
  pointer_location_ = location;

  if (*drag_source_ == DragEventSource::kMouse) {
    pointer_delegate_->OnPointerMotionEvent(location, timestamp,
                                            wl::EventDispatchPolicy::kImmediate,
                                            /*is_synthesized=*/true);
  } else {
    const auto touch_pointer_ids = touch_delegate_->GetActiveTouchPointIds();
    LOG_IF(WARNING, touch_pointer_ids.size() < 1u)
        << "Unexpected touch drag motion. Active touch_points: "
        << touch_pointer_ids.size();
    for (auto id : touch_pointer_ids) {
      touch_delegate_->OnTouchMotionEvent(location, timestamp, id,
                                          wl::EventDispatchPolicy::kImmediate,
                                          /*is_synthesized=*/true);
    }
  }
}

void WaylandWindowDragController::OnDragLeave(base::TimeTicks timestamp) {
  // Drag and drop sessions may be terminated by the client while drag-and-drop
  // server events may still be in-flight. No-op if this is the case.
  if (!IsActiveDragAndDropSession()) {
    return;
  }

  drag_target_window_ = nullptr;

  // In order to guarantee EventType::kMouseReleased event is delivered once the
  // DND session finishes, the focused window is not reset here. This is similar
  // to the "implicit grab" behavior implemented by Wayland compositors for
  // wl_pointer events. Additionally, this makes it possible for the drag
  // controller to overcome deviations in the order that wl_data_source and
  // wl_pointer events arrive when the drop happens. For example, unlike Weston
  // and Sway, Gnome Shell <= 2.26 sends them in the following order:
  //
  // wl_data_device.leave >  wl_pointer.enter > wl_data_source.cancel/finish
  //
  // which would require hacky workarounds in HandleDropAndResetState function
  // to properly detect and handle such cases.

  if (!data_offer_)
    return;

  DVLOG(1) << "OnLeave";
  data_offer_.reset();

  // As wl-shell/xdg-shell clients are only aware of surface-local coordinates
  // and there is no implicit grab during DND sessions, a fake motion event with
  // negative y coordinate is used here to allow higher level UI components to
  // detect when a window should be detached. E.g: On Chrome, dragging a tab all
  // the way up to the top edge of the window won't work without this fake
  // motion event upon wl_data_device::leave events. This is a workaround and
  // should ideally be reworked in the future, at higher level layers such that
  // they properly handle platforms that do not support global screen
  // coordinates, like Wayland.
  //
  // TODO(crbug.com/40209502): Find a better solution for upwards tab
  // detaching.
  if (state_ != State::kAttached)
    return;

  if (*drag_source_ == DragEventSource::kMouse) {
    pointer_delegate_->OnPointerMotionEvent(
        {pointer_location_.x(), -1}, timestamp,
        wl::EventDispatchPolicy::kImmediate, /*is_synthesized=*/true);
  } else {
    const auto touch_pointer_ids = touch_delegate_->GetActiveTouchPointIds();
    for (auto id : touch_pointer_ids) {
      // If an user starts dragging a tab horizontally with touch, Chrome enters
      // in "horizontal snapping" mode (see SnapScrollController for details).
      // Hence, in case of touch driven dragging, use a higher negative dy
      // to work around the threshold in ScrollSnapController otherwise,
      // the drag event is discarded.
      touch_delegate_->OnTouchMotionEvent(
          {pointer_location_.x(), kHorizontalRailExitThreshold}, timestamp, id,
          wl::EventDispatchPolicy::kImmediate, /*is_synthesized=*/true);
    }
  }
}

void WaylandWindowDragController::OnDragDrop(base::TimeTicks timestamp) {
  // Drag-and-drop sessions may be terminated by the client while drag-and-drop
  // server events are still in-flight. No-op if this is the case.
  if (!IsActiveDragAndDropSession()) {
    return;
  }

  DCHECK_GE(state_, State::kAttached);
  VLOG(1) << "Dropped. state=" << state_;

  DCHECK(data_offer_);
  data_offer_->FinishOffer();
  data_offer_.reset();

  drag_target_window_ = nullptr;
}

void WaylandWindowDragController::OnDataSourceDropPerformed(
    WaylandDataSource* source,
    base::TimeTicks timestamp) {
  CHECK_GE(state_, State::kAttached);
  VLOG(1) << __func__ << " state=" << state_;
  HandleDragEnd(/*completed=*/true, timestamp);
}

// This function is called when either 'cancelled' or 'finished' data source
// events is received during a window dragging session. It is used to detect
// when drop happens, since it is the only event sent by the server regardless
// where it happens, inside or outside toplevel surfaces.
void WaylandWindowDragController::OnDataSourceFinish(WaylandDataSource* source,
                                                     base::TimeTicks timestamp,
                                                     bool completed) {
  VLOG(1) << __func__ << " completed=" << completed << " state=" << state_;
  HandleDragEnd(completed, timestamp);
  data_source_.reset();
}

void WaylandWindowDragController::HandleDragEnd(bool completed,
                                                base::TimeTicks timestamp) {
  // No-op if drag end has already been handled.
  if (state_ != State::kAttached && state_ != State::kDetached) {
    return;
  }

  // Release DND objects.
  nested_dispatcher_.reset();
  data_offer_.reset();
  extended_drag_source_.reset();
  xdg_toplevel_drag_.reset();
  origin_surface_.reset();
  origin_window_ = nullptr;
  has_received_enter_ = false;

  // Transition to |kDropped| state and determine the next action to take. If
  // drop happened while the move loop was running (i.e: kDetached), ask to quit
  // the loop, otherwise notify session end and reset state right away.
  State state_when_dropped =
      std::exchange(state_, completed || !IsWindowDragProtocolAvailable()
                                ? State::kDropped
                                : State::kCancelled);
  if (state_when_dropped == State::kDetached) {
    VLOG(1) << "Quiting Loop : Detached";
    QuitLoop();
  } else {
    HandleDropAndResetState(timestamp);
  }

  data_device_->ResetDragDelegate();
  window_manager_->RemoveObserver(this);
}

void WaylandWindowDragController::OnDataSourceSend(WaylandDataSource* source,
                                                   const std::string& mime_type,
                                                   std::string* contents) {
  // There is no actual data exchange in DnD window dragging sessions. Window
  // snapping, for example, is supposed to be handled at higher level UI layers.
}

bool WaylandWindowDragController::CanDispatchEvent(const PlatformEvent& event) {
  return state_ != State::kIdle;
}

uint32_t WaylandWindowDragController::DispatchEvent(
    const PlatformEvent& event) {
  CHECK_NE(state_, State::kIdle);
  CHECK(base::CurrentUIThread::IsSet());

  // Currently, there's no reliable way in the protocol to determine when the
  // drag session has effectively started, so as a best-effort heuristic we
  // consider it started once wl_data_device.enter has been received at least
  // once.
  if (wl::EventShouldCancelDrag(event)) {
    if (!has_received_enter_) {
      CancelDragSession();
      return POST_DISPATCH_PERFORM_DEFAULT;
    }
    return POST_DISPATCH_STOP_PROPAGATION;
  }

  if (state_ == State::kDetached &&
      (event->type() == EventType::kMouseMoved ||
       event->type() == EventType::kMouseDragged ||
       event->type() == EventType::kTouchMoved)) {
    HandleMotionEvent(event->AsLocatedEvent());
    return POST_DISPATCH_STOP_PROPAGATION;
  }
  return POST_DISPATCH_PERFORM_DEFAULT;
}

void WaylandWindowDragController::OnWindowRemoved(WaylandWindow* window) {
  DCHECK_NE(state_, State::kIdle);
  DVLOG(1) << "Window being destroyed. widget=" << window->GetWidget();

  // The drag should only be cancelled after all window pointers have had a
  // chance invalidate. This is necessary as a single aura::Window can serve
  // multiple roles (e.g target window can also be the origin window). Cancel
  // The drag if either `drag_target_window_` or `dragged_window_` have been
  // removed.
  bool should_cancel_drag = false;

  if (window == drag_target_window_) {
    drag_target_window_ = nullptr;
    should_cancel_drag = true;
  }

  if (window == events_grabber_) {
    events_grabber_ = nullptr;
  }

  if (window == origin_window_) {
    // See the declaration of TakeWaylandSurface() for why this is needed.
    origin_surface_ = origin_window_->TakeWaylandSurface();
    origin_window_ = nullptr;
  }

  if (window == dragged_window_) {
    should_cancel_drag = true;
  }

  if (should_cancel_drag) {
    LOG(ERROR) << "OnDataSourceFinish";
    SetDraggedWindow(nullptr, {});
    OnDataSourceFinish(data_source_.get(), EventTimeForNow(),
                       /*completed=*/false);
  }
}

void WaylandWindowDragController::HandleMotionEvent(LocatedEvent* event) {
  DCHECK_EQ(state_, State::kDetached);
  DCHECK(event);

  if (!should_process_drag_motion_events_) {
    return;
  }

  // Notify listeners about window bounds change (i.e: re-positioning) event.
  // To do so, set the new bounds as per the motion event location and the drag
  // offset. Note that setting a new location (i.e: bounds.origin()) for a
  // surface has no visual effect in ozone/wayland backend. Actual window
  // re-positioning during dragging session is done through the drag icon.
  if (dragged_window_) {
    dragged_window_->SetOrigin(event->location() - drag_offset_);
  }

  should_process_drag_motion_events_ = false;
}

// Dispatch mouse release events (to tell clients that the drop just happened)
// clear focus and reset internal state. Must be called when the session is
// about to finish.
void WaylandWindowDragController::HandleDropAndResetState(
    base::TimeTicks timestamp) {
  DCHECK(state_ == State::kDropped || state_ == State::kCancelled);
  VLOG(1) << "Notifying drop. window=" << events_grabber_;

  // StopDragging() may get called in response to bogus input events, eg:
  // wl_pointer.button release, which would imply in multiple calls to this
  // function for a single drop event. That results in ILL_ILLOPN crashes in
  // below code, because |drag_source_| is null after the first call to this
  // function. So, early out here in that case.
  // TODO(crbug.com/40209138): Revert this once Exo-side issue gets solved.
  if (!drag_source_.has_value())
    return;

  // At this point, kCancelled + extended-drag protocol extension available
  // means that the drag session was indeed cancelled, eg: when the user press
  // ESC key. Currently, the only way of notifing platform delegate code (upper
  // layers) about the window drag cancellation is to dispatch an ESC key press
  // event, so do it now.
  //
  // Note: On environments with no ext-drag available, kCancelled is ignored,
  // because without the extension, detaching a tab into a window would be
  // mostly impossible, as a regular dnd session would end up with
  // wl_data_source.dnd_finished event.
  if (state_ == State::kCancelled && IsWindowDragProtocolAvailable()) {
    VLOG(1) << "Dispatching cancellation event.";
    keyboard_delegate_->OnSynthesizedKeyPressEvent(events_grabber_,
                                                   DomCode::ESCAPE, timestamp);
  } else {
    if (*drag_source_ == DragEventSource::kMouse) {
      if (events_grabber_) {
        pointer_delegate_->ReleasePressedPointerButtons(events_grabber_,
                                                        timestamp);
      }
    } else {
      const auto touch_pointer_ids = touch_delegate_->GetActiveTouchPointIds();
      for (auto id : touch_pointer_ids) {
        touch_delegate_->OnTouchReleaseEvent(
            timestamp, id, wl::EventDispatchPolicy::kImmediate,
            /*is_synthesized=*/true);
      }
    }
  }

  events_grabber_ = nullptr;
  state_ = State::kIdle;
  drag_source_.reset();
  data_source_.reset();
}

void WaylandWindowDragController::RunLoop() {
  DCHECK_EQ(state_, State::kDetached);
  DCHECK(dragged_window_);
  CHECK(nested_dispatcher_);

  VLOG(1) << "Starting drag loop. widget=" << dragged_window_->GetWidget()
          << " offset=" << drag_offset_.ToString();

  base::WeakPtr<WaylandWindowDragController> alive(weak_factory_.GetWeakPtr());

  base::RunLoop loop(base::RunLoop::Type::kNestableTasksAllowed);
  quit_loop_closure_ = loop.QuitClosure();
  loop.Run();

  if (!alive) {
    VLOG(1) << "Exited drag loop: destroyed";
    return;
  }

  VLOG(1) << "Exited drag loop: state=" << state_;
}

void WaylandWindowDragController::QuitLoop() {
  DCHECK(!quit_loop_closure_.is_null());
  VLOG(1) << "Quit Loop";
  std::move(quit_loop_closure_).Run();
}

void WaylandWindowDragController::SetDraggedWindow(
    WaylandToplevelWindow* window,
    const gfx::Vector2d& offset) {
  if (dragged_window_ == window && offset == drag_offset_)
    return;

  dragged_window_ = window;
  drag_offset_ = offset;

  // TODO(crbug.com/40598679): Fallback when no window drag protocol available.
  if (extended_drag_source_) {
    extended_drag_source_->SetDraggedWindow(dragged_window_, drag_offset_);
  } else if (xdg_toplevel_drag_) {
    xdg_toplevel_drag_->SetDraggedWindow(dragged_window_, drag_offset_);
  }
}

bool WaylandWindowDragController::IsWindowDragProtocolAvailable() const {
  return !!connection_->extended_drag_v1() ||
         !!connection_->toplevel_drag_manager_v1() ||
         window_drag_protocol_available_for_testing_;
}

bool WaylandWindowDragController::IsActiveDragAndDropSession() const {
  return !!data_source_;
}

void WaylandWindowDragController::DumpState(std::ostream& out) const {
  constexpr auto kStateToString = base::MakeFixedFlatMap<State, const char*>(
      {{State::kIdle, "idle"},
       {State::kAttached, "attached"},
       {State::kDetached, "detached"},
       {State::kDropped, "dropped"},
       {State::kCancelled, "canceled"},
       {State::kAttaching, "attaching"}});

  out << "WaylandWindowDragController:" << " state="
      << GetMapValueOrDefault(kStateToString, state_)
      << ", drag_offset=" << drag_offset_.ToString()
      << ", pointer_position=" << pointer_location_.ToString()
      << ", data_source=" << !!data_source_
      << ", dragged_window=" << GetWindowName(dragged_window_.get())
      << ", events_grabber=" << GetWindowName(events_grabber_.get())
      << ", origin_window=" << GetWindowName(origin_window_.get())
      << ", drag_target_window=" << GetWindowName(drag_target_window_.get())
      << ", nested_dispatcher=" << !!nested_dispatcher_
      << ", has_received_enter=" << has_received_enter_;
}

std::ostream& operator<<(std::ostream& out,
                         WaylandWindowDragController::State state) {
  return out << static_cast<int>(state);
}

std::optional<wl::Serial> WaylandWindowDragController::GetSerial(
    DragEventSource drag_source,
    WaylandToplevelWindow* origin) {
  auto* focused = drag_source == DragEventSource::kMouse
                      ? window_manager_->GetCurrentPointerFocusedWindow()
                      : window_manager_->GetCurrentTouchFocusedWindow();
  if (!origin || focused != origin) {
    return std::nullopt;
  }
  return connection_->serial_tracker().GetSerial(
      drag_source == DragEventSource::kMouse ? wl::SerialType::kMousePress
                                             : wl::SerialType::kTouchPress);
}

}  // namespace ui