File: xdg_foreign_wrapper.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 (237 lines) | stat: -rw-r--r-- 8,661 bytes parent folder | download | duplicates (2)
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
// 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/xdg_foreign_wrapper.h"

#include <xdg-foreign-unstable-v1-client-protocol.h>
#include <xdg-foreign-unstable-v2-client-protocol.h>

#include <algorithm>

#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "ui/ozone/platform/wayland/host/wayland_connection.h"
#include "ui/ozone/platform/wayland/host/wayland_window.h"
#include "ui/platform_window/platform_window_init_properties.h"

namespace ui {

constexpr uint32_t kMinVersion = 1;

using OnHandleExported = XdgForeignWrapper::OnHandleExported;

namespace {

template <typename ExporterType>
std::unique_ptr<XdgForeignWrapper> CreateWrapper(WaylandConnection* connection,
                                                 wl_registry* registry,
                                                 uint32_t name,
                                                 uint32_t version) {
  auto zxdg_exporter = wl::Bind<ExporterType>(registry, name, version);
  if (!zxdg_exporter) {
    LOG(ERROR) << "Failed to bind zxdg_exporter";
    return {};
  }
  return std::make_unique<XdgForeignWrapper>(connection,
                                             std::move(zxdg_exporter));
}

}  // namespace

template <typename ExportedType>
struct ExportedSurface {
  ExportedSurface(wl_surface* surface, OnHandleExported cb)
      : surface_for_export(surface) {
    callbacks.emplace_back((std::move(cb)));
  }
  ExportedSurface(ExportedSurface&& buffer) = default;
  ExportedSurface& operator=(ExportedSurface&& buffer) = default;
  ~ExportedSurface() = default;

  // Surface that is exported.
  raw_ptr<wl_surface> surface_for_export = nullptr;

  // Exported |surface|.
  wl::Object<ExportedType> exported;

  // Handle of the exported |surface|.
  std::string exported_handle;

  // The cb that will be executed when |handle| is exported.
  std::vector<OnHandleExported> callbacks;
};

class XdgForeignWrapper::XdgForeignWrapperInternal {
 public:
  virtual ~XdgForeignWrapperInternal() = default;

  virtual void ExportSurfaceToForeign(wl_surface* surface,
                                      OnHandleExported cb) = 0;

  // WaylandWindowObserver:
  virtual void OnWindowRemoved(WaylandWindow* window) = 0;
};

template <typename ExporterType, typename ExportedType>
class XdgForeignWrapperImpl
    : public XdgForeignWrapper::XdgForeignWrapperInternal {
 public:
  XdgForeignWrapperImpl(WaylandConnection* connection,
                        wl::Object<ExporterType> exporter)
      : connection_(connection), exporter_(std::move(exporter)) {}
  XdgForeignWrapperImpl(const XdgForeignWrapperImpl&) = delete;
  XdgForeignWrapperImpl& operator=(const XdgForeignWrapperImpl&) = delete;
  ~XdgForeignWrapperImpl() override = default;

  void ExportSurfaceToForeign(wl_surface* surface,
                              OnHandleExported cb) override {
    auto* exported_surface = GetExportedSurface(surface);
    if (!exported_surface) {
      // The |surface| has never been exported. Export it and return the handle
      // via the |cb|.
      ExportSurfaceInternal(surface, std::move(cb));
    } else if (exported_surface->exported_handle.empty()) {
      // The |surface| has already been exported, but its handle hasn't been
      // received yet. Store the |cb| and execute when the handle is obtained.
      exported_surface->callbacks.emplace_back(std::move(cb));
    } else {
      // The |surface| has already been exported and its handle has been
      // received. Execute the |cb| and send the handle.
      DCHECK(!exported_surface->exported_handle.empty());
      std::move(cb).Run(exported_surface->exported_handle);
    }
  }

  void ExportSurfaceInternal(wl_surface* surface, OnHandleExported cb);

  void OnWindowRemoved(WaylandWindow* window) override {
    auto it =
        std::ranges::find(exported_surfaces_, window->root_surface()->surface(),
                          &ExportedSurface<ExportedType>::surface_for_export);
    if (it != exported_surfaces_.end())
      exported_surfaces_.erase(it);
  }

  ExportedSurface<ExportedType>* GetExportedSurface(wl_surface* surface) {
    for (auto& item : exported_surfaces_) {
      if (item.surface_for_export == surface)
        return &item;
    }
    return nullptr;
  }

 private:
  // zxdg_exported_{v1,v2}_listener callbacks:
  static void OnHandle(void* data, ExportedType* exported, const char* handle) {
    auto* self =
        static_cast<XdgForeignWrapperImpl<ExporterType, ExportedType>*>(data);
    DCHECK(self);

    auto exported_surface_it =
        std::ranges::find(self->exported_surfaces_, exported,
                          [](const auto& item) { return item.exported.get(); });
    CHECK(exported_surface_it != self->exported_surfaces_.end());
    exported_surface_it->exported_handle = handle;

    for (auto& cb : exported_surface_it->callbacks) {
      std::move(cb).Run(exported_surface_it->exported_handle);
    }

    exported_surface_it->callbacks.clear();
  }

  const raw_ptr<WaylandConnection> connection_;
  wl::Object<ExporterType> exporter_;
  std::vector<ExportedSurface<ExportedType>> exported_surfaces_;
};

template <>
void XdgForeignWrapperImpl<zxdg_exporter_v1, zxdg_exported_v1>::
    ExportSurfaceInternal(wl_surface* surface, OnHandleExported cb) {
  ExportedSurface<zxdg_exported_v1> exported_surface(surface, std::move(cb));
  exported_surface.exported.reset(
      zxdg_exporter_v1_export(exporter_.get(), surface));

  static constexpr zxdg_exported_v1_listener kXdgExportedListener = {
      .handle = &OnHandle};
  zxdg_exported_v1_add_listener(exported_surface.exported.get(),
                                &kXdgExportedListener, this);

  exported_surfaces_.emplace_back(std::move(exported_surface));
  connection_->Flush();
}

template <>
void XdgForeignWrapperImpl<zxdg_exporter_v2, zxdg_exported_v2>::
    ExportSurfaceInternal(wl_surface* surface, OnHandleExported cb) {
  ExportedSurface<zxdg_exported_v2> exported_surface(surface, std::move(cb));
  exported_surface.exported.reset(
      zxdg_exporter_v2_export_toplevel(exporter_.get(), surface));

  static constexpr zxdg_exported_v2_listener kXdgExportedListener = {
      .handle = &OnHandle};
  zxdg_exported_v2_add_listener(exported_surface.exported.get(),
                                &kXdgExportedListener, this);

  exported_surfaces_.emplace_back(std::move(exported_surface));
  connection_->Flush();
}

// static
void XdgForeignWrapper::Instantiate(WaylandConnection* connection,
                                    wl_registry* registry,
                                    uint32_t name,
                                    const std::string& interface,
                                    uint32_t version) {
  if (connection->xdg_foreign_ ||
      !wl::CanBind(interface, version, kMinVersion, kMinVersion)) {
    return;
  }

  if (interface == kInterfaceNameV1) {
    connection->xdg_foreign_ = CreateWrapper<zxdg_exporter_v1>(
        connection, registry, name, kMinVersion);
  } else if (interface == kInterfaceNameV2) {
    connection->xdg_foreign_ = CreateWrapper<zxdg_exporter_v2>(
        connection, registry, name, kMinVersion);
  } else {
    NOTREACHED() << " unexpected interface name: " << interface;
  }
}

XdgForeignWrapper::XdgForeignWrapper(WaylandConnection* connection,
                                     wl::Object<zxdg_exporter_v1> exporter_v1)
    : XdgForeignWrapper(connection) {
  impl_ = std::make_unique<
      XdgForeignWrapperImpl<zxdg_exporter_v1, zxdg_exported_v1>>(
      connection, std::move(exporter_v1));
}

XdgForeignWrapper::XdgForeignWrapper(WaylandConnection* connection,
                                     wl::Object<zxdg_exporter_v2> exporter_v2)
    : XdgForeignWrapper(connection) {
  impl_ = std::make_unique<
      XdgForeignWrapperImpl<zxdg_exporter_v2, zxdg_exported_v2>>(
      connection, std::move(exporter_v2));
}

XdgForeignWrapper::XdgForeignWrapper(WaylandConnection* connection) {
  CHECK(connection);
  window_removal_observer_.Observe(connection->window_manager());
}

XdgForeignWrapper::~XdgForeignWrapper() = default;

void XdgForeignWrapper::ExportSurfaceToForeign(WaylandWindow* window,
                                               OnHandleExported cb) {
  DCHECK_EQ(window->type(), PlatformWindowType::kWindow);
  auto* surface = window->root_surface()->surface();
  impl_->ExportSurfaceToForeign(surface, std::move(cb));
}

void XdgForeignWrapper::OnWindowRemoved(WaylandWindow* window) {
  impl_->OnWindowRemoved(window);
}

}  // namespace ui