• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include <gtk/gtk.h>
6 
7 #include <X11/Xlib.h>
8 #undef Success     // Definition conflicts with cef_message_router.h
9 #undef RootWindow  // Definition conflicts with root_window.h
10 
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <memory>
14 #include <string>
15 
16 #include "include/base/cef_logging.h"
17 #include "include/cef_app.h"
18 #include "include/cef_command_line.h"
19 #include "include/wrapper/cef_helpers.h"
20 #include "tests/cefclient/browser/main_context_impl.h"
21 #include "tests/cefclient/browser/main_message_loop_multithreaded_gtk.h"
22 #include "tests/cefclient/browser/test_runner.h"
23 #include "tests/shared/browser/client_app_browser.h"
24 #include "tests/shared/browser/main_message_loop_external_pump.h"
25 #include "tests/shared/browser/main_message_loop_std.h"
26 #include "tests/shared/common/client_app_other.h"
27 #include "tests/shared/common/client_switches.h"
28 #include "tests/shared/renderer/client_app_renderer.h"
29 
30 namespace client {
31 namespace {
32 
XErrorHandlerImpl(Display * display,XErrorEvent * event)33 int XErrorHandlerImpl(Display* display, XErrorEvent* event) {
34   LOG(WARNING) << "X error received: "
35                << "type " << event->type << ", "
36                << "serial " << event->serial << ", "
37                << "error_code " << static_cast<int>(event->error_code) << ", "
38                << "request_code " << static_cast<int>(event->request_code)
39                << ", "
40                << "minor_code " << static_cast<int>(event->minor_code);
41   return 0;
42 }
43 
XIOErrorHandlerImpl(Display * display)44 int XIOErrorHandlerImpl(Display* display) {
45   return 0;
46 }
47 
TerminationSignalHandler(int signatl)48 void TerminationSignalHandler(int signatl) {
49   LOG(ERROR) << "Received termination signal: " << signatl;
50   MainContext::Get()->GetRootWindowManager()->CloseAllWindows(true);
51 }
52 
RunMain(int argc,char * argv[])53 int RunMain(int argc, char* argv[]) {
54   // Create a copy of |argv| on Linux because Chromium mangles the value
55   // internally (see issue #620).
56   CefScopedArgArray scoped_arg_array(argc, argv);
57   char** argv_copy = scoped_arg_array.array();
58 
59   CefMainArgs main_args(argc, argv);
60 
61   // Parse command-line arguments.
62   CefRefPtr<CefCommandLine> command_line = CefCommandLine::CreateCommandLine();
63   command_line->InitFromArgv(argc, argv);
64 
65   // Create a ClientApp of the correct type.
66   CefRefPtr<CefApp> app;
67   ClientApp::ProcessType process_type = ClientApp::GetProcessType(command_line);
68   if (process_type == ClientApp::BrowserProcess) {
69     app = new ClientAppBrowser();
70   } else if (process_type == ClientApp::RendererProcess ||
71              process_type == ClientApp::ZygoteProcess) {
72     // On Linux the zygote process is used to spawn other process types. Since
73     // we don't know what type of process it will be give it the renderer
74     // client.
75     app = new ClientAppRenderer();
76   } else if (process_type == ClientApp::OtherProcess) {
77     app = new ClientAppOther();
78   }
79 
80   // Execute the secondary process, if any.
81   int exit_code = CefExecuteProcess(main_args, app, nullptr);
82   if (exit_code >= 0)
83     return exit_code;
84 
85   // Create the main context object.
86   auto context = std::make_unique<MainContextImpl>(command_line, true);
87 
88   CefSettings settings;
89 
90 // When generating projects with CMake the CEF_USE_SANDBOX value will be defined
91 // automatically. Pass -DUSE_SANDBOX=OFF to the CMake command-line to disable
92 // use of the sandbox.
93 #if !defined(CEF_USE_SANDBOX)
94   settings.no_sandbox = true;
95 #endif
96 
97   // Populate the settings based on command line arguments.
98   context->PopulateSettings(&settings);
99 
100   if (settings.windowless_rendering_enabled) {
101     // Force the app to use OpenGL <= 3.1 when off-screen rendering is enabled.
102     // TODO(cefclient): Rewrite OSRRenderer to use shaders instead of the
103     // fixed-function pipeline which was removed in OpenGL 3.2 (back in 2009).
104     setenv("MESA_GL_VERSION_override", "3.1", /*overwrite=*/0);
105   }
106 
107   // Create the main message loop object.
108   std::unique_ptr<MainMessageLoop> message_loop;
109   if (settings.multi_threaded_message_loop)
110     message_loop.reset(new MainMessageLoopMultithreadedGtk);
111   else if (settings.external_message_pump)
112     message_loop = MainMessageLoopExternalPump::Create();
113   else
114     message_loop.reset(new MainMessageLoopStd);
115 
116   // Initialize CEF.
117   context->Initialize(main_args, settings, app, nullptr);
118 
119   // Force Gtk to use Xwayland (in case a Wayland compositor is being used).
120   gdk_set_allowed_backends("x11");
121 
122   // The Chromium sandbox requires that there only be a single thread during
123   // initialization. Therefore initialize GTK after CEF.
124   gtk_init(&argc, &argv_copy);
125 
126   // Install xlib error handlers so that the application won't be terminated
127   // on non-fatal errors. Must be done after initializing GTK.
128   XSetErrorHandler(XErrorHandlerImpl);
129   XSetIOErrorHandler(XIOErrorHandlerImpl);
130 
131   // Install a signal handler so we clean up after ourselves.
132   signal(SIGINT, TerminationSignalHandler);
133   signal(SIGTERM, TerminationSignalHandler);
134 
135   // Register scheme handlers.
136   test_runner::RegisterSchemeHandlers();
137 
138   auto window_config = std::make_unique<RootWindowConfig>();
139   window_config->always_on_top =
140       command_line->HasSwitch(switches::kAlwaysOnTop);
141   window_config->with_controls =
142       !command_line->HasSwitch(switches::kHideControls);
143   window_config->with_osr =
144       settings.windowless_rendering_enabled ? true : false;
145 
146   // Create the first window.
147   context->GetRootWindowManager()->CreateRootWindow(std::move(window_config));
148 
149   // Run the message loop. This will block until Quit() is called.
150   int result = message_loop->Run();
151 
152   // Shut down CEF.
153   context->Shutdown();
154 
155   // Release objects in reverse order of creation.
156   message_loop.reset();
157   context.reset();
158 
159   return result;
160 }
161 
162 }  // namespace
163 }  // namespace client
164 
165 // Program entry point function.
main(int argc,char * argv[])166 int main(int argc, char* argv[]) {
167   return client::RunMain(argc, argv);
168 }
169