[M120][Tizen][Onscreen] Fix build errors for TV profile
[platform/framework/web/chromium-efl.git] / chrome / browser / chrome_browser_main_extra_parts_linux.cc
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chrome_browser_main_extra_parts_linux.h"
6
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/logging.h"
10 #include "build/build_config.h"
11 #include "ui/ozone/buildflags.h"
12 #include "ui/ozone/public/ozone_switches.h"
13
14 #if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
15 #include "base/files/file_path.h"
16 #include "base/files/file_util.h"
17 #include "base/nix/xdg_util.h"
18 #include "base/threading/thread_restrictions.h"
19 #endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
20
21 #if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
22
23 constexpr char kPlatformWayland[] = "wayland";
24
25 bool HasWaylandDisplay(base::Environment* env) {
26   std::string wayland_display;
27   const bool has_wayland_display =
28       env->GetVar("WAYLAND_DISPLAY", &wayland_display) &&
29       !wayland_display.empty();
30   if (has_wayland_display)
31     return true;
32
33   std::string xdg_runtime_dir;
34   const bool has_xdg_runtime_dir =
35       env->GetVar("XDG_RUNTIME_DIR", &xdg_runtime_dir) &&
36       !xdg_runtime_dir.empty();
37   if (has_xdg_runtime_dir) {
38     auto wayland_server_pipe =
39         base::FilePath(xdg_runtime_dir).Append("wayland-0");
40     // Normally, this should happen exactly once, at the startup of the main
41     // process.
42     base::ScopedAllowBlocking allow_blocking;
43     return base::PathExists(wayland_server_pipe);
44   }
45
46   return false;
47 }
48
49 #endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
50
51 #if BUILDFLAG(OZONE_PLATFORM_X11)
52 constexpr char kPlatformX11[] = "x11";
53 #endif
54
55 namespace {
56
57 // Evaluates the environment and returns the effective platform name for the
58 // given |ozone_platform_hint|.
59 // For the "auto" value, returns "wayland" if the XDG session type is "wayland",
60 // "x11" otherwise.
61 // For the "wayland" value, checks if the Wayland server is available, and
62 // returns "x11" if it is not.
63 // See https://crbug.com/1246928.
64 std::string MaybeFixPlatformName(const std::string& ozone_platform_hint) {
65 #if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
66   // Wayland is selected if both conditions below are true:
67   // 1. The user selected either 'wayland' or 'auto'.
68   // 2. The XDG session type is 'wayland', OR the user has selected 'wayland'
69   //    explicitly and a Wayland server is running.
70   // Otherwise, fall back to X11.
71   if (ozone_platform_hint == kPlatformWayland ||
72       ozone_platform_hint == "auto") {
73     auto env(base::Environment::Create());
74
75     std::string xdg_session_type;
76     const bool has_xdg_session_type =
77         env->GetVar(base::nix::kXdgSessionTypeEnvVar, &xdg_session_type) &&
78         !xdg_session_type.empty();
79
80     if ((has_xdg_session_type && xdg_session_type == "wayland") ||
81         (ozone_platform_hint == kPlatformWayland &&
82          HasWaylandDisplay(env.get()))) {
83       return kPlatformWayland;
84     }
85   }
86 #endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
87
88 #if BUILDFLAG(OZONE_PLATFORM_X11)
89   if (ozone_platform_hint == kPlatformX11) {
90     return kPlatformX11;
91   }
92 #if BUILDFLAG(OZONE_PLATFORM_WAYLAND)
93   if (ozone_platform_hint == kPlatformWayland ||
94       ozone_platform_hint == "auto") {
95     // We are here if:
96     // - The binary has both X11 and Wayland backends.
97     // - The user wanted Wayland but that did not work, otherwise it would have
98     //   been returned above.
99     if (ozone_platform_hint == kPlatformWayland) {
100       LOG(WARNING) << "No Wayland server is available. Falling back to X11.";
101     } else {
102       LOG(WARNING) << "This is not a Wayland session.  Falling back to X11. "
103                       "If you need to run Chrome on Wayland using some "
104                       "embedded compositor, e. g., Weston, please specify "
105                       "Wayland as your preferred Ozone platform, or use "
106                       "--ozone-platform=wayland.";
107     }
108     return kPlatformX11;
109   }
110 #endif  // BUILDFLAG(OZONE_PLATFORM_WAYLAND)
111 #endif  // BUILDFLAG(OZONE_PLATFORM_X11)
112
113   return ozone_platform_hint;
114 }
115
116 }  // namespace
117
118 ChromeBrowserMainExtraPartsLinux::ChromeBrowserMainExtraPartsLinux() = default;
119
120 ChromeBrowserMainExtraPartsLinux::~ChromeBrowserMainExtraPartsLinux() = default;
121
122 void ChromeBrowserMainExtraPartsLinux::PreEarlyInitialization() {
123 #if BUILDFLAG(IS_LINUX)
124   // On the desktop, we fix the platform name if necessary.
125   // See https://crbug.com/1246928.
126   auto* const command_line = base::CommandLine::ForCurrentProcess();
127   if (!command_line->HasSwitch(switches::kOzonePlatform)) {
128     const auto ozone_platform_hint =
129         command_line->GetSwitchValueASCII(switches::kOzonePlatformHint);
130     if (!ozone_platform_hint.empty()) {
131       command_line->AppendSwitchASCII(
132           switches::kOzonePlatform, MaybeFixPlatformName(ozone_platform_hint));
133     }
134   }
135
136   auto env = base::Environment::Create();
137   std::string desktop_startup_id;
138   if (env->GetVar("DESKTOP_STARTUP_ID", &desktop_startup_id))
139     command_line->AppendSwitchASCII("desktop-startup-id", desktop_startup_id);
140 #endif  // BUILDFLAG(IS_LINUX)
141
142   ChromeBrowserMainExtraPartsOzone::PreEarlyInitialization();
143 }