Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
simple_app.cxx
Go to the documentation of this file.
1// Author: Sergey Linev <S.Linev@gsi.de>
2// Date: 2017-06-29
3
4// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
5// reserved. Use of this source code is governed by a BSD-style license that
6// can be found in the LICENSE file.
7
8/*************************************************************************
9 * Copyright (C) 1995-2023, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16
17#if !defined(_MSC_VER)
18#pragma GCC diagnostic ignored "-Wunused-parameter"
19#pragma GCC diagnostic ignored "-Wshadow"
20#endif
21
22#include "simple_app.h"
23
24#include <string>
25#include <cstdio>
26#include <memory>
27
28#include "include/base/cef_build.h"
29#include "include/cef_browser.h"
30#include "include/cef_version.h"
31#include "include/views/cef_browser_view.h"
32#include "include/views/cef_window.h"
33#include "include/wrapper/cef_helpers.h"
34
35#include "THttpServer.h"
36#include "TTimer.h"
37#include <ROOT/RLogger.hxx>
38
40
41namespace {
42
43// When using the Views framework this object provides the delegate
44// implementation for the CefWindow that hosts the Views-based browser.
45class SimpleWindowDelegate : public CefWindowDelegate {
46 CefRefPtr<CefBrowserView> fBrowserView;
47 int fWidth = 800; ///< preferred window width
48 int fHeight = 600; ///< preferred window height
49 bool fBatch = false;
50public:
51 explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view, int width = 800, int height = 600, bool batch = false)
52 : fBrowserView(browser_view), fWidth(width), fHeight(height), fBatch(batch)
53 {
54 }
55
56 void OnWindowCreated(CefRefPtr<CefWindow> window) override
57 {
58 window->AddChildView(fBrowserView);
59
60 if (fBatch) {
61 window->Hide();
62 } else {
63 window->Show();
64 // Give keyboard focus to the browser view.
65 fBrowserView->RequestFocus();
66 }
67 }
68
69 void OnWindowDestroyed(CefRefPtr<CefWindow> window) override { fBrowserView = nullptr; }
70
71 bool CanClose(CefRefPtr<CefWindow> window) override
72 {
73 // Allow the window to close if the browser says it's OK.
74 CefRefPtr<CefBrowser> browser = fBrowserView->GetBrowser();
75 if (browser)
76 return browser->GetHost()->TryCloseBrowser();
77 return true;
78 }
79
81 {
82 return CefSize(fWidth, fHeight);
83 }
84
85
86private:
87
88 IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
89 DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
90};
91
92
93class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
94 public:
95 SimpleBrowserViewDelegate() {}
96
99 bool is_devtools) override {
100 // Create a new top-level Window for the popup. It will show itself after
101 // creation.
102 CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(popup_browser_view));
103
104 // We created the Window.
105 return true;
106 }
107
108 private:
109 IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
110 DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
111};
112
113
114} // namespace
115
117 THttpServer *serv, const std::string &url, const std::string &cont,
118 int width, int height, bool headless)
119 : CefApp(), CefBrowserProcessHandler(), fUseViewes(use_viewes), fSupressLog(supress_log),
120 fFirstServer(serv), fFirstUrl(url), fFirstContent(cont), fFirstHeadless(headless)
121{
122 fFirstRect.Set(0, 0, width, height);
123
124#if defined(OS_WIN) || defined(OS_LINUX)
125 // Create the browser using the Views framework if "--use-views" is specified
126 // via the command-line. Otherwise, create the browser using the native
127 // platform framework. The Views framework is currently only supported on
128 // Windows and Linux.
129#else
130// if (fUseViewes) {
131// R__LOG_ERROR(CefWebDisplayLog()) << "view framework does not supported by CEF on the platform, switching off";
132// fUseViewes = false;
133// }
134#endif
135
136}
137
138
144
145
147{
148 // registrar->AddCustomScheme("rootscheme", true, true, true, true, true, true);
149 // registrar->AddCustomScheme("rootscheme", true, false, false, true, false, false);
150}
151
153{
154 if (fSupressLog) {
155 command_line->AppendSwitchWithValue("v", "-1");
156 command_line->AppendSwitch("disable-logging");
157 command_line->AppendSwitchWithValue("enable-logging", "none");
158 }
159
160 if (fNextHeadless) {
161 command_line->AppendSwitchWithValue("user-data-dir", ".");
162 command_line->AppendSwitch("allow-file-access-from-files");
163 command_line->AppendSwitch("disable-web-security");
164 command_line->AppendSwitch("off-screen-rendering-enabled");
165 }
166
167#ifdef OS_MACOSX
168 if (fNextHeadless) {
169 command_line->AppendSwitch("headless");
170 command_line->AppendSwitchWithValue("use-angle", "swiftshader");
171 command_line->AppendSwitch("enable-unsafe-swiftshader");
172 command_line->AppendSwitch("disable-gpu");
173 // command_line->AppendSwitch("enable-gpu-rasterization");
174 // if (fUseViewes)
175 // command_line->AppendSwitchWithValue("ozone-platform", "headless");
176 } else {
177 command_line->AppendSwitchWithValue("use-angle", "metal");
178 command_line->AppendSwitch("ignore-gpu-blocklist");
179 command_line->AppendSwitch("enable-webgl");
180 }
181#endif
182
183#ifdef OS_LINUX
184 if (fNextHeadless) {
185 command_line->AppendSwitch("headless");
186 command_line->AppendSwitchWithValue("use-gl", "swiftshader");
187 command_line->AppendSwitch("disable-gpu");
188 command_line->AppendSwitch("ignore-gpu-blocklist");
189 command_line->AppendSwitch("enable-unsafe-swiftshader");
190
191 if (fUseViewes)
192 command_line->AppendSwitchWithValue("ozone-platform", "headless");
193 }
194#endif
195}
196
200
202{
204
205 if (!fFirstUrl.empty() || !fFirstContent.empty()) {
207 fFirstUrl.clear();
208 fFirstContent.clear();
209 }
210}
211
212
213void SimpleApp::StartWindow(THttpServer *serv, const std::string &addr, const std::string &cont, CefRect &rect, bool is_headless)
214{
216
217 bool is_batch = (addr.empty() && !cont.empty()) || is_headless;
218
219 if (!fGuiHandler)
221
222 std::string url;
223
224 if(is_batch)
225 url = fGuiHandler->AddBatchPage(cont);
226 else if (serv)
227 url = fGuiHandler->MakePageUrl(serv, addr);
228 else
229 url = addr;
230
231 // Specify CEF browser settings here.
233 if (is_batch)
234 browser_settings.windowless_frame_rate = 30;
235 // browser_settings.plugins = STATE_DISABLED;
236 // browser_settings.file_access_from_file_urls = STATE_ENABLED;
237 // browser_settings.universal_access_from_file_urls = STATE_ENABLED;
238 // browser_settings.web_security = STATE_DISABLED;
239
240 if (fUseViewes) {
241 // Create the BrowserView.
243 CefBrowserView::CreateBrowserView(fGuiHandler, url, browser_settings, nullptr, nullptr, new SimpleBrowserViewDelegate());
244
245 // Create the Window. It will show itself after creation.
246 CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view, rect.width, rect.height, is_batch));
247
248 if (fNextHandle) {
249 fNextHandle->SetBrowser(browser_view->GetBrowser());
250 fNextHandle = nullptr; // used only once
251 }
252
253 } else {
254
256
257 // TODO: Seems to be, to configure window_info.SetAsWindowless,
258 // one should implement CefRenderHandler
259
260 #if defined(OS_WIN)
261 if (!rect.IsEmpty())
262 window_info.SetAsChild(0, rect);
263 // On Windows we need to specify certain flags that will be passed to
264 // CreateWindowEx().
265 window_info.SetAsPopup(0, "cefsimple");
266 if (is_batch)
267 // window_info.SetAsWindowless(GetDesktopWindow());
268 window_info.SetAsWindowless(kNullWindowHandle);
269 #else
270 if (!rect.IsEmpty())
271 window_info.SetAsChild(0, rect);
272 if (is_batch)
273 window_info.SetAsWindowless(kNullWindowHandle);
274 #endif
275
276 // Create the first browser window.
277 auto browser = CefBrowserHost::CreateBrowserSync(window_info, fGuiHandler, url, browser_settings, nullptr, nullptr);
278
279 if (fNextHandle) {
281 fNextHandle = nullptr; // used only once
282 }
283
284 }
285
286}
287
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t rect
Option_t Option_t width
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
void SetBrowser(CefRefPtr< CefBrowser > br)
RCefWebDisplayHandle * fNextHandle
next handle where browser will be created
Definition simple_app.h:44
bool fNextHeadless
if next handle display is headless
Definition simple_app.h:45
std::string fFirstUrl
! first URL to open
Definition simple_app.h:40
void SetNextHandle(RCefWebDisplayHandle *handle, bool headless)
bool fUseViewes
! is views framework used
Definition simple_app.h:37
void OnContextInitialized() override
THttpServer * fFirstServer
! first server
Definition simple_app.h:39
bool fFirstHeadless
! is first window is headless
Definition simple_app.h:43
bool fSupressLog
! supress log output when possible
Definition simple_app.h:38
void OnBeforeChildProcessLaunch(CefRefPtr< CefCommandLine > command_line) override
CefRect fFirstRect
! original width
Definition simple_app.h:42
void OnRegisterCustomSchemes(CefRawPtr< CefSchemeRegistrar > registrar) override
CefRefPtr< GuiHandler > fGuiHandler
! normal handler
Definition simple_app.h:47
std::string fFirstContent
! first page content open
Definition simple_app.h:41
SimpleApp(bool use_viewes, bool supress_log, THttpServer *serv=nullptr, const std::string &url="", const std::string &cont="", int width=0, int height=0, bool headless=false)
void StartWindow(THttpServer *serv, const std::string &url, const std::string &cont, CefRect &rect, bool is_headless=false)
void OnBeforeCommandLineProcessing(const CefString &process_type, CefRefPtr< CefCommandLine > command_line) override
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31