summaryrefslogtreecommitdiff
path: root/chromium/services/ui/ws/drag_controller.cc
blob: cc3b99925ec38d48ed385b7afb8bf6de913821d1 (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
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
// Copyright 2016 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 "services/ui/ws/drag_controller.h"

#include <utility>

#include "base/logging.h"
#include "services/ui/public/interfaces/cursor/cursor.mojom.h"
#include "services/ui/ws/drag_cursor_updater.h"
#include "services/ui/ws/drag_source.h"
#include "services/ui/ws/drag_target_connection.h"
#include "services/ui/ws/event_dispatcher.h"
#include "services/ui/ws/server_window.h"
#include "ui/base/cursor/cursor.h"

namespace ui {
namespace ws {

struct DragController::Operation {
  OperationType type;
  uint32_t event_flags;
  gfx::Point screen_position;
};

struct DragController::WindowState {
  // Set to true once we've observed the ServerWindow* that is the key to this
  // instance in |window_state_|.
  bool observed = false;

  // If we're waiting for a response, this is the type of message. NONE means
  // there's no outstanding
  OperationType waiting_on_reply = OperationType::NONE;

  // The operation that we'll send off if |waiting_on_reply| isn't NONE.
  Operation queued_operation = {OperationType::NONE, 0, gfx::Point()};

  // The current set of operations that this window accepts. This gets updated
  // on each return message.
  DropEffectBitmask bitmask = 0u;
};

DragController::DragController(
    DragCursorUpdater* cursor_updater,
    DragSource* source,
    ServerWindow* source_window,
    DragTargetConnection* source_connection,
    int32_t drag_pointer,
    const std::unordered_map<std::string, std::vector<uint8_t>>& mime_data,
    DropEffectBitmask drag_operations)
    : source_(source),
      cursor_updater_(cursor_updater),
      drag_operations_(drag_operations),
      drag_pointer_id_(drag_pointer),
      current_cursor_(ui::CursorType::kNoDrop),
      source_window_(source_window),
      source_connection_(source_connection),
      mime_data_(mime_data),
      weak_factory_(this) {
  SetCurrentTargetWindow(nullptr);
  EnsureWindowObserved(source_window_);
}

DragController::~DragController() {
  for (auto& pair : window_state_) {
    if (pair.second.observed)
      pair.first->RemoveObserver(this);
  }
}

void DragController::Cancel() {
  MessageDragCompleted(false, ui::mojom::kDropEffectNone);
  // |this| may be deleted now.
}

bool DragController::DispatchPointerEvent(const ui::PointerEvent& event,
                                          ServerWindow* current_target) {
  DVLOG(2) << "DragController dispatching pointer event at "
           << event.location().ToString();
  uint32_t event_flags =
      event.flags() &
      (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | ui::EF_ALT_DOWN);
  gfx::Point screen_position = event.location();

  if (waiting_for_final_drop_response_) {
    // If we're waiting on a target window to respond to the final drag drop
    // call, don't process any more pointer events.
    DVLOG(1) << "Ignoring event because we're waiting for final drop response";
    return false;
  }

  if (event.pointer_details().id != drag_pointer_id_) {
    DVLOG(1) << "Ignoring event from different pointer "
             << event.pointer_details().id;
    return false;
  }

  // If |current_target| doesn't accept drags, walk its hierarchy up until we
  // find one that does (or set to nullptr at the top of the tree).
  while (current_target && !current_target->can_accept_drops())
    current_target = current_target->parent();

  if (current_target) {
    // If we're non-null, we're about to use |current_target| in some
    // way. Ensure that we receive notifications that this window has gone
    // away.
    EnsureWindowObserved(current_target);
  }

  source_->OnDragMoved(screen_position);

  if (current_target && current_target == current_target_window_ &&
      event.type() != ET_POINTER_UP) {
    QueueOperation(current_target, OperationType::OVER, event_flags,
                   screen_position);
  } else if (current_target != current_target_window_) {
    if (current_target_window_) {
      QueueOperation(current_target_window_, OperationType::LEAVE, event_flags,
                     screen_position);
    }

    if (current_target) {
      // TODO(erg): If we have a queued LEAVE operation, does this turn into a
      // noop?
      QueueOperation(current_target, OperationType::ENTER, event_flags,
                     screen_position);
    }

    SetCurrentTargetWindow(current_target);
  } else if (event.type() != ET_POINTER_UP) {
    DVLOG(1) << "Performing no action for pointer event at "
             << screen_position.ToString()
             << "! current_target=" << current_target;
  }

  if (event.type() == ET_POINTER_UP) {
    if (current_target) {
      QueueOperation(current_target, OperationType::DROP, event_flags,
                     screen_position);
      waiting_for_final_drop_response_ = true;
    } else {
      // The pointer was released over no window or a window that doesn't
      // accept drags.
      MessageDragCompleted(false, ui::mojom::kDropEffectNone);
    }
  }

  return true;
}

void DragController::OnWillDestroyDragTargetConnection(
    DragTargetConnection* connection) {
  called_on_drag_mime_types_.erase(connection);
}

void DragController::MessageDragCompleted(bool success,
                                          DropEffect action_taken) {
  DVLOG(1) << "Drag Completed: success=" << success
           << ", action_taken=" << action_taken;
  for (DragTargetConnection* connection : called_on_drag_mime_types_)
    connection->PerformOnDragDropDone();
  called_on_drag_mime_types_.clear();

  source_->OnDragCompleted(success, action_taken);
  // |this| may be deleted now.
}

size_t DragController::GetSizeOfQueueForWindow(ServerWindow* window) {
  auto it = window_state_.find(window);
  if (it == window_state_.end())
    return 0u;
  if (it->second.waiting_on_reply == OperationType::NONE)
    return 0u;
  if (it->second.queued_operation.type == OperationType::NONE)
    return 1u;
  return 2u;
}

void DragController::SetWindowDropOperations(ServerWindow* window,
                                             DropEffectBitmask bitmask) {
  WindowState& state = window_state_[window];
  state.bitmask = bitmask;

  if (current_target_window_ == window) {
    current_cursor_ = CursorForEffectBitmask(bitmask);
    cursor_updater_->OnDragCursorUpdated();
  }
}

ui::CursorData DragController::CursorForEffectBitmask(
    DropEffectBitmask bitmask) {
  DropEffectBitmask combined = bitmask & drag_operations_;
  return combined == ui::mojom::kDropEffectNone
             ? ui::CursorData(ui::CursorType::kNoDrop)
             : ui::CursorData(ui::CursorType::kCopy);
}

void DragController::SetCurrentTargetWindow(ServerWindow* current_target) {
  current_target_window_ = current_target;

  if (current_target_window_) {
    // Immediately set the cursor to the last known set of operations (which
    // could be none).
    WindowState& state = window_state_[current_target_window_];
    current_cursor_ = CursorForEffectBitmask(state.bitmask);
  } else {
    // Can't drop in empty areas.
    current_cursor_ = ui::CursorData(ui::CursorType::kNoDrop);
  }

  cursor_updater_->OnDragCursorUpdated();
}

void DragController::EnsureWindowObserved(ServerWindow* window) {
  if (!window)
    return;

  WindowState& state = window_state_[window];
  if (!state.observed) {
    state.observed = true;
    window->AddObserver(this);
  }
}

void DragController::QueueOperation(ServerWindow* window,
                                    OperationType type,
                                    uint32_t event_flags,
                                    const gfx::Point& screen_position) {
  DVLOG(2) << "Queueing operation " << ToString(type) << " to " << window;

  // If this window doesn't have the mime data, send it.
  DragTargetConnection* connection = source_->GetDragTargetForWindow(window);
  if (connection != source_connection_ &&
      !base::ContainsKey(called_on_drag_mime_types_, connection)) {
    connection->PerformOnDragDropStart(mime_data_);
    called_on_drag_mime_types_.insert(connection);
  }

  WindowState& state = window_state_[window];
  // Set the queued operation to the incoming.
  state.queued_operation = {type, event_flags, screen_position};

  if (state.waiting_on_reply == OperationType::NONE) {
    // Send the operation immediately.
    DispatchOperation(window, &state);
  }
}

void DragController::DispatchOperation(ServerWindow* target,
                                       WindowState* state) {
  DragTargetConnection* connection = source_->GetDragTargetForWindow(target);

  DCHECK_EQ(OperationType::NONE, state->waiting_on_reply);
  Operation& op = state->queued_operation;
  switch (op.type) {
    case OperationType::NONE: {
      // NONE case to silence the compiler.
      NOTREACHED();
      break;
    }
    case OperationType::ENTER: {
      connection->PerformOnDragEnter(
          target, op.event_flags, op.screen_position, drag_operations_,
          base::Bind(&DragController::OnDragStatusCompleted,
                     weak_factory_.GetWeakPtr(), target->id()));
      state->waiting_on_reply = OperationType::ENTER;
      break;
    }
    case OperationType::OVER: {
      connection->PerformOnDragOver(
          target, op.event_flags, op.screen_position, drag_operations_,
          base::Bind(&DragController::OnDragStatusCompleted,
                     weak_factory_.GetWeakPtr(), target->id()));
      state->waiting_on_reply = OperationType::OVER;
      break;
    }
    case OperationType::LEAVE: {
      connection->PerformOnDragLeave(target);
      state->waiting_on_reply = OperationType::NONE;
      break;
    }
    case OperationType::DROP: {
      connection->PerformOnCompleteDrop(
          target, op.event_flags, op.screen_position, drag_operations_,
          base::Bind(&DragController::OnDragDropCompleted,
                     weak_factory_.GetWeakPtr(), target->id()));
      state->waiting_on_reply = OperationType::DROP;
      break;
    }
  }

  state->queued_operation = {OperationType::NONE, 0, gfx::Point()};
}

void DragController::OnRespondToOperation(ServerWindow* window) {
  WindowState& state = window_state_[window];
  DCHECK_NE(OperationType::NONE, state.waiting_on_reply);
  state.waiting_on_reply = OperationType::NONE;
  if (state.queued_operation.type != OperationType::NONE)
    DispatchOperation(window, &state);
}

void DragController::OnDragStatusCompleted(const WindowId& id,
                                           DropEffectBitmask bitmask) {
  ServerWindow* window = source_->GetWindowById(id);
  if (!window) {
    // The window has been deleted and its queue is empty.
    return;
  }

  // We must remove the completed item.
  OnRespondToOperation(window);
  SetWindowDropOperations(window, bitmask);
}

void DragController::OnDragDropCompleted(const WindowId& id,
                                         DropEffect action) {
  ServerWindow* window = source_->GetWindowById(id);
  if (!window) {
    // The window has been deleted after we sent the drop message. It's really
    // hard to recover from this so just signal to the source that our drag
    // failed.
    MessageDragCompleted(false, ui::mojom::kDropEffectNone);
    return;
  }

  OnRespondToOperation(window);
  MessageDragCompleted(action != 0u, action);
}

void DragController::OnWindowDestroying(ServerWindow* window) {
  auto it = window_state_.find(window);
  if (it != window_state_.end()) {
    window->RemoveObserver(this);
    window_state_.erase(it);
  }

  if (current_target_window_ == window)
    SetCurrentTargetWindow(nullptr);

  if (source_window_ == window) {
    source_window_ = nullptr;
    // Our source window is being deleted, fail the drag.
    MessageDragCompleted(false, ui::mojom::kDropEffectNone);
  }
}

// static
std::string DragController::ToString(OperationType type) {
  switch (type) {
    case OperationType::NONE:
      return "NONE";
    case OperationType::ENTER:
      return "ENTER";
    case OperationType::OVER:
      return "OVER";
    case OperationType::LEAVE:
      return "LEAVE";
    case OperationType::DROP:
      return "DROP";
  }
  NOTREACHED();
  return std::string();
}

}  // namespace ws
}  // namespace ui