Bug 1270832 - Activate standard c++ library hardening r=glandium
[gecko.git] / third_party / wayland-proxy / wayland-proxy.h
blob2854926839ce1ce25bd8005ffa4b5b6184f4e151
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef _wayland_proxy_h_
7 #define _wayland_proxy_h_
9 #include <poll.h>
10 #include <vector>
11 #include <fcntl.h>
12 #include <atomic>
13 #include <memory>
15 class ProxiedConnection;
17 typedef void (*CompositorCrashHandler)();
19 #define WAYLAND_PROXY_ENABLED (1 << 0)
20 #define WAYLAND_PROXY_DISABLED (1 << 1)
21 #define WAYLAND_PROXY_RUN_FAILED (1 << 2)
22 #define WAYLAND_PROXY_TERMINATED (1 << 3)
23 #define WAYLAND_PROXY_CONNECTION_ADDED (1 << 4)
24 #define WAYLAND_PROXY_CONNECTION_REMOVED (1 << 5)
25 #define WAYLAND_PROXY_APP_TERMINATED (1 << 6)
26 #define WAYLAND_PROXY_APP_CONNECTION_FAILED (1 << 7)
27 #define WAYLAND_PROXY_COMPOSITOR_ATTACHED (1 << 8)
28 #define WAYLAND_PROXY_COMPOSITOR_CONNECTION_FAILED (1 << 9)
29 #define WAYLAND_PROXY_COMPOSITOR_SOCKET_FAILED (1 << 10)
31 class WaylandProxy {
32 public:
33 static std::unique_ptr<WaylandProxy> Create();
35 // Launch an application with Wayland proxy set
36 bool RunChildApplication(char* argv[]);
38 // Run proxy as part of already running application
39 // and set Wayland proxy display for it.
40 bool RunThread();
42 // Set original Wayland display env variable and clear
43 // proxy display file.
44 void RestoreWaylandDisplay();
46 static void SetVerbose(bool aVerbose);
47 static void SetCompositorCrashHandler(CompositorCrashHandler aCrashHandler);
48 static void CompositorCrashed();
49 static void AddState(unsigned aState);
50 static const char* GetState();
52 ~WaylandProxy();
54 private:
55 bool Init();
56 void Run();
58 void SetWaylandProxyDisplay();
59 static void* RunProxyThread(WaylandProxy* aProxy);
60 bool CheckWaylandDisplay(const char* aWaylandDisplay);
62 bool SetupWaylandDisplays();
63 bool StartProxyServer();
64 bool IsChildAppTerminated();
66 bool PollConnections();
67 bool ProcessConnections();
69 void Info(const char* aFormat, ...);
70 void Warning(const char* aOperation);
71 void Error(const char* aOperation);
72 void ErrorPlain(const char* aFormat, ...);
74 void CheckCompositor();
76 private:
77 // List of all Compositor <-> Application connections
78 std::vector<std::unique_ptr<ProxiedConnection>> mConnections;
79 int mProxyServerSocket = -1;
80 pid_t mApplicationPID = 0;
81 std::atomic<bool> mThreadRunning = false;
82 pthread_t mThread;
84 // sockaddr_un has hardcoded max len of sun_path
85 static constexpr int sMaxDisplayNameLen = 108;
86 // Name of Wayland display provided by compositor
87 char mWaylandDisplay[sMaxDisplayNameLen];
88 // Name of Wayland display provided by us
89 char mWaylandProxy[sMaxDisplayNameLen];
91 static CompositorCrashHandler sCompositorCrashHandler;
92 static std::atomic<unsigned> sProxyStateFlags;
95 #endif // _wayland_proxy_h_