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/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
49public:
50 explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view, int width = 800, int height = 600)
51 : fBrowserView(browser_view), fWidth(width), fHeight(height)
52 {
53 }
54
55 void OnWindowCreated(CefRefPtr<CefWindow> window) override
56 {
57 // Add the browser view and show the window.
58 window->AddChildView(fBrowserView);
59 window->Show();
60
61 // Give keyboard focus to the browser view.
62 fBrowserView->RequestFocus();
63 }
64
65 void OnWindowDestroyed(CefRefPtr<CefWindow> window) override { fBrowserView = nullptr; }
66
67 bool CanClose(CefRefPtr<CefWindow> window) override
68 {
69 // Allow the window to close if the browser says it's OK.
70 CefRefPtr<CefBrowser> browser = fBrowserView->GetBrowser();
71 if (browser)
72 return browser->GetHost()->TryCloseBrowser();
73 return true;
74 }
75
76 CefSize GetPreferredSize(CefRefPtr<CefView> view) override
77 {
78 return CefSize(fWidth, fHeight);
79 }
80
81
82private:
83
84 IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
85 DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
86};
87
88
89class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
90 public:
91 SimpleBrowserViewDelegate() {}
92
93 bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
94 CefRefPtr<CefBrowserView> popup_browser_view,
95 bool is_devtools) override {
96 // Create a new top-level Window for the popup. It will show itself after
97 // creation.
98 CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(popup_browser_view));
99
100 // We created the Window.
101 return true;
102 }
103
104 private:
105 IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
106 DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
107};
108
109
110} // namespace
111
112SimpleApp::SimpleApp(bool use_viewes,
113 THttpServer *serv, const std::string &url, const std::string &cont,
114 int width, int height, bool headless)
115 : CefApp(), CefBrowserProcessHandler(), fUseViewes(use_viewes), fFirstServer(serv), fFirstUrl(url), fFirstContent(cont), fFirstHeadless(headless)
116{
117 fFirstRect.Set(0, 0, width, height);
118
119#if defined(OS_WIN) || defined(OS_LINUX)
120 // Create the browser using the Views framework if "--use-views" is specified
121 // via the command-line. Otherwise, create the browser using the native
122 // platform framework. The Views framework is currently only supported on
123 // Windows and Linux.
124#else
125 if (fUseViewes) {
126 R__LOG_ERROR(CefWebDisplayLog()) << "view framework does not supported by CEF on the platform, switching off";
127 fUseViewes = false;
128 }
129#endif
130
131}
132
133
135{
136 fNextHandle = handle;
137}
138
139
140void SimpleApp::OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar)
141{
142 // registrar->AddCustomScheme("rootscheme", true, true, true, true, true, true);
143 // registrar->AddCustomScheme("rootscheme", true, false, false, true, false, false);
144}
145
146void SimpleApp::OnBeforeCommandLineProcessing(const CefString &process_type, CefRefPtr<CefCommandLine> command_line)
147{
148// command_line->AppendSwitch("allow-file-access-from-files");
149// command_line->AppendSwitch("disable-web-security");
150// if (fBatch) {
151// command_line->AppendSwitch("disable-gpu");
152// command_line->AppendSwitch("disable-gpu-compositing");
153// command_line->AppendSwitch("disable-gpu-sandbox");
154// }
155}
156
157void SimpleApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line)
158{
159// command_line->AppendSwitch("allow-file-access-from-files");
160// command_line->AppendSwitch("disable-web-security");
161// if (fLastBatch) {
162// command_line->AppendSwitch("disable-webgl");
163// command_line->AppendSwitch("disable-gpu");
164// command_line->AppendSwitch("disable-gpu-compositing");
165// }
166}
167
169{
170 CEF_REQUIRE_UI_THREAD();
171
172 if (!fFirstUrl.empty() || !fFirstContent.empty()) {
174 fFirstUrl.clear();
175 fFirstContent.clear();
176 }
177}
178
179
180void SimpleApp::StartWindow(THttpServer *serv, const std::string &addr, const std::string &cont, CefRect &rect)
181{
182 CEF_REQUIRE_UI_THREAD();
183
184 if (!fGuiHandler)
186
187 std::string url;
188
189 //bool is_batch = false;
190
191 if(addr.empty() && !cont.empty()) {
192 url = fGuiHandler->AddBatchPage(cont);
193 // is_batch = true;
194 } else if (serv) {
195 url = fGuiHandler->MakePageUrl(serv, addr);
196 } else {
197 url = addr;
198 }
199
200 // Specify CEF browser settings here.
201 CefBrowserSettings browser_settings;
202 // browser_settings.plugins = STATE_DISABLED;
203 // browser_settings.file_access_from_file_urls = STATE_ENABLED;
204 // browser_settings.universal_access_from_file_urls = STATE_ENABLED;
205 // browser_settings.web_security = STATE_DISABLED;
206
207 if (fUseViewes) {
208 // Create the BrowserView.
209 CefRefPtr<CefBrowserView> browser_view =
210 CefBrowserView::CreateBrowserView(fGuiHandler, url, browser_settings, nullptr, nullptr, new SimpleBrowserViewDelegate());
211
212 // Create the Window. It will show itself after creation.
213 CefWindow::CreateTopLevelWindow(new SimpleWindowDelegate(browser_view, rect.width, rect.height));
214
215 if (fNextHandle) {
216 fNextHandle->SetBrowser(browser_view->GetBrowser());
217 fNextHandle = nullptr; // used only once
218 }
219
220 } else {
221
222 CefWindowInfo window_info;
223
224 // TODO: Seems to be, to configure window_info.SetAsWindowless,
225 // one should implement CefRenderHandler
226
227 #if defined(OS_WIN)
228 RECT wnd_rect = {rect.x, rect.y, rect.x + rect.width, rect.y + rect.height};
229 if (!rect.IsEmpty()) window_info.SetAsChild(0, wnd_rect);
230 // On Windows we need to specify certain flags that will be passed to
231 // CreateWindowEx().
232 window_info.SetAsPopup(0, "cefsimple");
233 //if (is_batch)
234 // window_info.SetAsWindowless(GetDesktopWindow());
235 #elif defined(OS_LINUX)
236 if (!rect.IsEmpty()) window_info.SetAsChild(0, rect);
237 //if (is_batch)
238 // window_info.SetAsWindowless(kNullWindowHandle);
239 #else
240 if (!rect.IsEmpty())
241 window_info.SetAsChild(0, rect.x, rect.y, rect.width, rect.height );
242 //if (is_batch)
243 // window_info.SetAsWindowless(kNullWindowHandle);
244 #endif
245
246 // Create the first browser window.
247 auto browser = CefBrowserHost::CreateBrowserSync(window_info, fGuiHandler, url, browser_settings, nullptr, nullptr);
248
249 if (fNextHandle) {
250 fNextHandle->SetBrowser(browser);
251 fNextHandle = nullptr; // used only once
252 }
253
254 }
255
256}
257
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
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
SimpleApp(bool use_viewes, THttpServer *serv=nullptr, const std::string &url="", const std::string &cont="", int width=0, int height=0, bool headless=false)
std::string fFirstUrl
! first URL to open
Definition simple_app.h:40
bool fUseViewes
! is views framework used
Definition simple_app.h:38
void OnContextInitialized() override
THttpServer * fFirstServer
! first server
Definition simple_app.h:39
void StartWindow(THttpServer *serv, const std::string &url, const std::string &cont, CefRect &rect)
void OnBeforeChildProcessLaunch(CefRefPtr< CefCommandLine > command_line) override
CefRect fFirstRect
! original width
Definition simple_app.h:42
void SetNextHandle(RCefWebDisplayHandle *handle)
void OnRegisterCustomSchemes(CefRawPtr< CefSchemeRegistrar > registrar) override
CefRefPtr< GuiHandler > fGuiHandler
! normal handler
Definition simple_app.h:46
std::string fFirstContent
! first page content open
Definition simple_app.h:41
void OnBeforeCommandLineProcessing(const CefString &process_type, CefRefPtr< CefCommandLine > command_line) override
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
ROOT::Experimental::RLogChannel & CefWebDisplayLog()