File: selection_requestor.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 (275 lines) | stat: -rw-r--r-- 8,252 bytes parent folder | download | duplicates (5)
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
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "ui/base/x/selection_requestor.h"

#include <algorithm>

#include "base/memory/ref_counted_memory.h"
#include "ui/base/x/selection_owner.h"
#include "ui/base/x/selection_utils.h"
#include "ui/base/x/x11_clipboard_helper.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/x/atom_cache.h"
#include "ui/gfx/x/xproto.h"

namespace ui {

namespace {

const char kChromeSelection[] = "CHROME_SELECTION";

// The amount of time to wait for a request to complete before aborting it.
const int kRequestTimeoutMs = 1000;

// Combines |data| into a single std::vector<uint8_t>.
std::vector<uint8_t> CombineData(
    const std::vector<scoped_refptr<base::RefCountedMemory>>& data) {
  size_t bytes = 0;
  for (const auto& datum : data) {
    bytes += datum->size();
  }
  std::vector<uint8_t> combined;
  combined.reserve(bytes);
  for (const auto& datum : data) {
    std::copy(datum->data(), datum->data() + datum->size(),
              std::back_inserter(combined));
  }
  return combined;
}

}  // namespace

SelectionRequestor::SelectionRequestor(x11::Window x_window,
                                       XClipboardHelper* helper)
    : x_window_(x_window),
      helper_(helper),
      x_property_(x11::GetAtom(kChromeSelection)) {}

SelectionRequestor::~SelectionRequestor() = default;

bool SelectionRequestor::PerformBlockingConvertSelection(
    x11::Atom selection,
    x11::Atom target,
    std::vector<uint8_t>* out_data,
    x11::Atom* out_type) {
  base::TimeTicks timeout =
      base::TimeTicks::Now() + base::Milliseconds(kRequestTimeoutMs);
  Request request(selection, target, timeout);
  requests_.push_back(&request);
  if (current_request_index_ == (requests_.size() - 1)) {
    ConvertSelectionForCurrentRequest();
  }
  BlockTillSelectionNotifyForRequest(&request);

  auto request_it = std::ranges::find(requests_, &request);
  CHECK(request_it != requests_.end());
  if (static_cast<int>(current_request_index_) >
      request_it - requests_.begin()) {
    --current_request_index_;
  }
  requests_.erase(request_it);

  if (request.success) {
    if (out_data) {
      *out_data = CombineData(request.out_data);
    }
    if (out_type) {
      *out_type = request.out_type;
    }
  }
  return request.success;
}

void SelectionRequestor::PerformBlockingConvertSelectionWithParameter(
    x11::Atom selection,
    x11::Atom target,
    const std::vector<x11::Atom>& parameter) {
  x11::Connection::Get()->SetArrayProperty(
      x_window_, x11::GetAtom(kChromeSelection), x11::Atom::ATOM, parameter);
  PerformBlockingConvertSelection(selection, target, nullptr, nullptr);
}

SelectionData SelectionRequestor::RequestAndWaitForTypes(
    x11::Atom selection,
    const std::vector<x11::Atom>& types) {
  for (const x11::Atom& item : types) {
    std::vector<uint8_t> data;
    x11::Atom type = x11::Atom::None;
    if (PerformBlockingConvertSelection(selection, item, &data, &type) &&
        type == item) {
      return SelectionData(
          type, base::MakeRefCounted<base::RefCountedBytes>(std::move(data)));
    }
  }

  return SelectionData();
}

void SelectionRequestor::OnSelectionNotify(
    const x11::SelectionNotifyEvent& selection) {
  Request* request = GetCurrentRequest();
  x11::Atom event_property = selection.property;
  if (!request || request->completed ||
      request->selection != selection.selection ||
      request->target != selection.target) {
    // ICCCM requires us to delete the property passed into SelectionNotify.
    if (event_property != x11::Atom::None) {
      x11::Connection::Get()->DeleteProperty(x_window_, event_property);
    }
    return;
  }

  bool success = false;
  if (event_property == x_property_) {
    scoped_refptr<base::RefCountedMemory> out_data;
    success = ui::GetRawBytesOfProperty(x_window_, x_property_, &out_data,
                                        &request->out_type);
    if (success) {
      request->out_data.clear();
      request->out_data.push_back(out_data);
    }
  }
  if (event_property != x11::Atom::None) {
    x11::Connection::Get()->DeleteProperty(x_window_, event_property);
  }

  if (request->out_type == x11::GetAtom(kIncr)) {
    request->data_sent_incrementally = true;
    request->out_data.clear();
    request->out_type = x11::Atom::None;
    request->timeout =
        base::TimeTicks::Now() + base::Milliseconds(kRequestTimeoutMs);
  } else {
    CompleteRequest(current_request_index_, success);
  }
}

bool SelectionRequestor::CanDispatchPropertyEvent(
    const x11::PropertyNotifyEvent& prop) {
  return prop.window == x_window_ && prop.atom == x_property_ &&
         prop.state == x11::Property::NewValue;
}

void SelectionRequestor::OnPropertyEvent(
    const x11::PropertyNotifyEvent& event) {
  Request* request = GetCurrentRequest();
  if (!request || !request->data_sent_incrementally) {
    return;
  }

  scoped_refptr<base::RefCountedMemory> out_data;
  x11::Atom out_type = x11::Atom::None;
  bool success =
      ui::GetRawBytesOfProperty(x_window_, x_property_, &out_data, &out_type);
  if (!success) {
    CompleteRequest(current_request_index_, false);
    return;
  }

  if (request->out_type != x11::Atom::None && request->out_type != out_type) {
    CompleteRequest(current_request_index_, false);
    return;
  }

  request->out_data.push_back(out_data);
  request->out_type = out_type;

  // Delete the property to tell the selection owner to send the next chunk.
  x11::Connection::Get()->DeleteProperty(x_window_, x_property_);

  request->timeout =
      base::TimeTicks::Now() + base::Milliseconds(kRequestTimeoutMs);

  if (!out_data->size()) {
    CompleteRequest(current_request_index_, true);
  }
}

void SelectionRequestor::AbortStaleRequests() {
  base::TimeTicks now = base::TimeTicks::Now();
  for (size_t i = current_request_index_; i < requests_.size(); ++i) {
    if (requests_[i]->timeout <= now) {
      CompleteRequest(i, false);
    }
  }
}

void SelectionRequestor::CompleteRequest(size_t index, bool success) {
  if (index >= requests_.size()) {
    return;
  }

  Request* request = requests_[index];
  if (request->completed) {
    return;
  }
  request->success = success;
  request->completed = true;

  if (index == current_request_index_) {
    while (GetCurrentRequest() && GetCurrentRequest()->completed) {
      ++current_request_index_;
    }
    ConvertSelectionForCurrentRequest();
  }
}

void SelectionRequestor::ConvertSelectionForCurrentRequest() {
  Request* request = GetCurrentRequest();
  if (request) {
    x11::Connection::Get()->ConvertSelection({
        .requestor = x_window_,
        .selection = request->selection,
        .target = request->target,
        .property = x_property_,
        .time = x11::Time::CurrentTime,
    });
  }
}

void SelectionRequestor::BlockTillSelectionNotifyForRequest(Request* request) {
  auto* connection = x11::Connection::Get();
  auto& events = connection->events();
  size_t i = 0;
  while (!request->completed && request->timeout > base::TimeTicks::Now()) {
    connection->Flush();
    connection->ReadResponses();
    size_t events_size = events.size();
    for (; i < events_size; ++i) {
      auto& event = events[i];
      if (helper_->DispatchEvent(event)) {
        event = x11::Event();
      }
    }
    DCHECK_EQ(events_size, events.size());
  }
  AbortStaleRequests();
}

SelectionRequestor::Request* SelectionRequestor::GetCurrentRequest() {
  return current_request_index_ == requests_.size()
             ? nullptr
             : requests_[current_request_index_];
}

SelectionRequestor::Request::Request(x11::Atom selection,
                                     x11::Atom target,
                                     base::TimeTicks timeout)
    : selection(selection),
      target(target),
      data_sent_incrementally(false),
      out_type(x11::Atom::None),
      success(false),
      timeout(timeout),
      completed(false) {}

SelectionRequestor::Request::~Request() = default;

}  // namespace ui