File: wayland_server_controller.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download | duplicates (8)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/exo/server/wayland_server_controller.h"

#include <memory>

#include "base/atomic_sequence_num.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/task/current_thread.h"
#include "components/exo/data_exchange_delegate.h"
#include "components/exo/display.h"
#include "components/exo/input_method_surface_manager.h"
#include "components/exo/notification_surface_manager.h"
#include "components/exo/security_delegate.h"
#include "components/exo/server/wayland_server_handle.h"
#include "components/exo/toast_surface_manager.h"
#include "components/exo/wayland/server.h"
#include "components/exo/wm_helper.h"

namespace exo {

namespace {
WaylandServerController* g_instance = nullptr;
}  // namespace

// static
std::unique_ptr<WaylandServerController>
WaylandServerController::CreateIfNecessary(
    std::unique_ptr<DataExchangeDelegate> data_exchange_delegate,
    std::unique_ptr<SecurityDelegate> security_delegate,
    std::unique_ptr<NotificationSurfaceManager> notification_surface_manager,
    std::unique_ptr<InputMethodSurfaceManager> input_method_surface_manager,
    std::unique_ptr<ToastSurfaceManager> toast_surface_manager) {
  return std::make_unique<WaylandServerController>(
      std::move(data_exchange_delegate), std::move(security_delegate),
      std::move(notification_surface_manager),
      std::move(input_method_surface_manager),
      std::move(toast_surface_manager));
}

// static
WaylandServerController* WaylandServerController::Get() {
  DCHECK(g_instance);
  return g_instance;
}

WaylandServerController::~WaylandServerController() {
  // TODO(crbug.com/40717074): Investigate if we can eliminate Shutdown
  // methods.
  display_->Shutdown();
  wayland::Server::SetServerGetter(base::NullCallback());
  DCHECK_EQ(g_instance, this);
  g_instance = nullptr;
}

wayland::Server* WaylandServerController::GetServerForDisplay(
    wl_display* display) {
  if (default_server_ && default_server_->GetWaylandDisplay() == display) {
    return default_server_.get();
  }

  for (const auto& pair : on_demand_servers_) {
    if (pair.second->GetWaylandDisplay() == display) {
      return pair.second.get();
    }
  }

  return nullptr;
}

WaylandServerController::WaylandServerController(
    std::unique_ptr<DataExchangeDelegate> data_exchange_delegate,
    std::unique_ptr<SecurityDelegate> security_delegate,
    std::unique_ptr<NotificationSurfaceManager> notification_surface_manager,
    std::unique_ptr<InputMethodSurfaceManager> input_method_surface_manager,
    std::unique_ptr<ToastSurfaceManager> toast_surface_manager)
    : wm_helper_(std::make_unique<WMHelper>()),
      display_(
          std::make_unique<Display>(std::move(notification_surface_manager),
                                    std::move(input_method_surface_manager),
                                    std::move(toast_surface_manager),
                                    std::move(data_exchange_delegate))) {
  DCHECK(!g_instance);
  g_instance = this;
  default_server_ =
      wayland::Server::Create(display_.get(), std::move(security_delegate));
  default_server_->StartWithDefaultPath(base::BindOnce([](bool success) {
    DCHECK(success) << "Failed to start the default wayland server.";
  }));
  wayland::Server::SetServerGetter(base::BindRepeating(
      &WaylandServerController::GetServerForDisplay, base::Unretained(this)));
}

void WaylandServerController::ListenOnSocket(
    std::unique_ptr<SecurityDelegate> security_delegate,
    base::ScopedFD socket,
    base::OnceCallback<void(std::unique_ptr<WaylandServerHandle>)> callback) {
  std::unique_ptr<wayland::Server> server =
      wayland::Server::Create(display_.get(), std::move(security_delegate));
  auto* server_ptr = server.get();
  auto start_callback = base::BindOnce(&WaylandServerController::OnSocketAdded,
                                       weak_factory_.GetWeakPtr(),
                                       std::move(server), std::move(callback));
  server_ptr->StartWithFdAsync(std::move(socket), std::move(start_callback));
}

void WaylandServerController::OnSocketAdded(
    std::unique_ptr<wayland::Server> server,
    base::OnceCallback<void(std::unique_ptr<WaylandServerHandle>)> callback,
    bool success) {
  if (!success) {
    std::move(callback).Run(nullptr);
    return;
  }

  // WrapUnique() is needed since the constructor is private.
  auto handle = base::WrapUnique(new WaylandServerHandle());
  on_demand_servers_.emplace(handle.get(), std::move(server));
  std::move(callback).Run(std::move(handle));
}

void WaylandServerController::CloseSocket(WaylandServerHandle* server) {
  on_demand_servers_.erase(server);
}

}  // namespace exo