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