Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
gui_handler.cxx
Go to the documentation of this file.
1/// \file gui_handler.cxx
2// Author: Sergey Linev <S.Linev@gsi.de>
3// Date: 2017-06-29
4// Warning: This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
5
6/*************************************************************************
7 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#if !defined(_MSC_VER)
15#pragma GCC diagnostic ignored "-Wunused-parameter"
16#pragma GCC diagnostic ignored "-Wshadow"
17#endif
18
19#include "gui_handler.h"
20#include "simple_app.h"
21
22#include <sstream>
23#include <string>
24
25#include "include/base/cef_bind.h"
26#include "include/cef_app.h"
27#include "include/cef_version.h"
28#include "include/views/cef_browser_view.h"
29#include "include/views/cef_window.h"
30#include "include/wrapper/cef_closure_task.h"
31#include "include/wrapper/cef_helpers.h"
32#include "include/cef_parser.h"
33#include "include/wrapper/cef_stream_resource_handler.h"
34
35#include "TEnv.h"
36#include "TUrl.h"
37#include "THttpServer.h"
38#include "THttpCallArg.h"
39#include "TSystem.h"
40#include "TBase64.h"
41#include <ROOT/RLogger.hxx>
42
43
45{
46 static ROOT::Experimental::RLogChannel sChannel("ROOT.CefWebDisplay");
47 return sChannel;
48}
49
50
51GuiHandler::GuiHandler(bool use_views) : fUseViews(use_views), is_closing_(false)
52{
53 fConsole = gEnv->GetValue("WebGui.Console", (int)0);
54
55 // see https://bitbucket.org/chromiumembedded/cef-project/src/master/examples/resource_manager/?at=master for demo
56 // one probably can avoid to use scheme handler and just redirect requests
57 fResourceManager = new CefResourceManager();
58}
59
60void GuiHandler::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString &title)
61{
62 CEF_REQUIRE_UI_THREAD();
63
64 if (fUseViews) {
65 // Set the title of the window using the Views framework.
66 CefRefPtr<CefBrowserView> browser_view = CefBrowserView::GetForBrowser(browser);
67 if (browser_view) {
68 CefRefPtr<CefWindow> window = browser_view->GetWindow();
69 if (window) window->SetTitle(title);
70 }
71 } else {
72 // Set the title of the window using platform APIs.
73 PlatformTitleChange(browser, title);
74 }
75}
76
77void GuiHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
78{
79 CEF_REQUIRE_UI_THREAD();
80
81 // Add to the list of existing browsers.
82 fBrowserList.emplace_back(browser);
83}
84
85bool GuiHandler::DoClose(CefRefPtr<CefBrowser> browser)
86{
87 CEF_REQUIRE_UI_THREAD();
88
89 // Closing the main window requires special handling. See the DoClose()
90 // documentation in the CEF header for a detailed description of this
91 // process.
92 if (fBrowserList.size() == 1) {
93 // Set a flag to indicate that the window close should be allowed.
94 is_closing_ = true;
95 }
96
97 // Allow the close. For windowed browsers this will result in the OS close
98 // event being sent.
99 return false;
100}
101
102void GuiHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser)
103{
104 CEF_REQUIRE_UI_THREAD();
105
106 // Remove from the list of existing browsers.
107 auto bit = fBrowserList.begin();
108 for (; bit != fBrowserList.end(); ++bit) {
109 if ((*bit)->IsSame(browser)) {
110 fBrowserList.erase(bit);
111 break;
112 }
113 }
114
115 if (fBrowserList.empty()) {
116
117 // All browser windows have closed. Quit the application message loop.
118
119 CefQuitMessageLoop();
120 }
121}
122
123// Returns a data: URI with the specified contents.
124std::string GuiHandler::GetDataURI(const std::string& data, const std::string& mime_type)
125{
126 return std::string("data:") + mime_type + ";base64," +
127 CefURIEncode(CefBase64Encode(data.data(), data.size()), false).ToString();
128}
129
130
131void GuiHandler::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode,
132 const CefString &errorText, const CefString &failedUrl)
133{
134 CEF_REQUIRE_UI_THREAD();
135
136 // Don't display an error for downloaded files.
137 if (errorCode == ERR_ABORTED)
138 return;
139
140 // Display a load error message.
141 /*
142 std::stringstream ss;
143 ss << "<html><body bgcolor=\"white\">"
144 "<h2>Failed to load URL "
145 << failedUrl.ToString().substr(0,100) << " with error " << errorText.ToString() << " (" << errorCode
146 << ").</h2></body></html>";
147 frame->LoadURL(GetDataURI(ss.str(), "text/html"));
148 */
149
150 printf("Fail to load URL %s\n", failedUrl.ToString().substr(0,100).c_str());
151}
152
153void GuiHandler::CloseAllBrowsers(bool force_close)
154{
155 if (!CefCurrentlyOn(TID_UI)) {
156 // Execute on the UI thread.
157 CefPostTask(TID_UI, base::BindOnce(&GuiHandler::CloseAllBrowsers, this, force_close));
158 return;
159 }
160
161 if (fBrowserList.empty())
162 return;
163
164 for (auto &br : fBrowserList)
165 br->GetHost()->CloseBrowser(force_close);
166}
167
168bool GuiHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
169 cef_log_severity_t level,
170 const CefString &message, const CefString &source,
171 int line)
172{
173 std::string src = source.ToString().substr(0,100);
174
175 switch (level) {
176 case LOGSEVERITY_WARNING:
177 if (fConsole > -1)
178 R__LOG_WARNING(CefWebDisplayLog()) << Form("CEF: %s:%d: %s", src.c_str(), line, message.ToString().c_str());
179 break;
180 case LOGSEVERITY_ERROR:
181 if (fConsole > -2)
182 R__LOG_ERROR(CefWebDisplayLog()) << Form("CEF: %s:%d: %s", src.c_str(), line, message.ToString().c_str());
183 break;
184 default:
185 if (fConsole > 0)
186 R__LOG_DEBUG(0, CefWebDisplayLog()) << Form("CEF: %s:%d: %s", src.c_str(), line, message.ToString().c_str());
187 break;
188 }
189
190 return true;
191}
192
194 CefRefPtr<CefBrowser> browser,
195 CefRefPtr<CefFrame> frame,
196 CefRefPtr<CefRequest> request,
197 CefRefPtr<CefCallback> callback) {
198 CEF_REQUIRE_IO_THREAD();
199
200 return fResourceManager->OnBeforeResourceLoad(browser, frame, request,
201 callback);
202}
203
204
206protected:
207
208 CefRefPtr<CefCallback> fCallBack{nullptr};
209
210public:
211 explicit TCefHttpCallArg() = default;
212
213 /** provide WS kind */
214 const char *GetWSKind() const override { return "longpoll"; }
215
216 /** provide WS platform */
217 const char *GetWSPlatform() const override { return "cef3"; }
218
219 void AssignCallback(CefRefPtr<CefCallback> cb) { fCallBack = cb; }
220
221 // this is callback from HTTP server
222 void HttpReplied() override
223 {
224 if (IsFile()) {
225 // send file
226 std::string file_content = THttpServer::ReadFileContent((const char *)GetContent());
227 SetContent(std::move(file_content));
228 }
229
230 fCallBack->Continue(); // we have result and can continue with processing
231 }
232};
233
234
236public:
237 // QWebEngineUrlRequestJob *fRequest;
238
240 std::shared_ptr<TCefHttpCallArg> fArg;
241
243
244 explicit TGuiResourceHandler(THttpServer *serv, bool dummy = false)
245 {
246 fServer = serv;
247
248 if (!dummy)
249 fArg = std::make_shared<TCefHttpCallArg>();
250 }
251
253
254 void Cancel() override { CEF_REQUIRE_IO_THREAD(); }
255
256 bool ProcessRequest(CefRefPtr<CefRequest> request, CefRefPtr<CefCallback> callback) override
257 {
258 CEF_REQUIRE_IO_THREAD();
259
260 if (fArg) {
261 fArg->AssignCallback(callback);
263 } else {
264 callback->Continue();
265 }
266
267 return true;
268 }
269#if CEF_VERSION_MAJOR > 114
270 void GetResponseHeaders(CefRefPtr<CefResponse> response, int64_t &response_length, CefString &redirectUrl) override
271#else
272 void GetResponseHeaders(CefRefPtr<CefResponse> response, int64 &response_length, CefString &redirectUrl) override
273#endif
274 {
275 CEF_REQUIRE_IO_THREAD();
276
277 if (!fArg || fArg->Is404()) {
278 response->SetMimeType("text/html");
279 response->SetStatus(404);
280 response_length = 0;
281 } else {
282 response->SetMimeType(fArg->GetContentType());
283 response->SetStatus(200);
284 response_length = fArg->GetContentLength();
285
286 if (fArg->NumHeader() > 0) {
287 CefResponse::HeaderMap headers;
288 for (Int_t n = 0; n < fArg->NumHeader(); ++n) {
289 TString name = fArg->GetHeaderName(n);
290 TString value = fArg->GetHeader(name.Data());
291 headers.emplace(CefString(name.Data()), CefString(value.Data()));
292 }
293 response->SetHeaderMap(headers);
294 }
295 }
296 // DCHECK(!fArg->Is404());
297 }
298
299 bool ReadResponse(void *data_out, int bytes_to_read, int &bytes_read, CefRefPtr<CefCallback> callback) override
300 {
301 CEF_REQUIRE_IO_THREAD();
302
303 if (!fArg) return false;
304
305 bytes_read = 0;
306
307 if (fTransferOffset < fArg->GetContentLength()) {
308 char *data_ = (char *)fArg->GetContent();
309 // Copy the next block of data into the buffer.
310 int transfer_size = fArg->GetContentLength() - fTransferOffset;
311 if (transfer_size > bytes_to_read)
312 transfer_size = bytes_to_read;
313 memcpy(data_out, data_ + fTransferOffset, transfer_size);
314 fTransferOffset += transfer_size;
315
316 bytes_read = transfer_size;
317 }
318
319 // if content fully copied - can release reference, object will be cleaned up
320 if (fTransferOffset >= fArg->GetContentLength())
321 fArg.reset();
322
323 return bytes_read > 0;
324 }
325
328};
329
330
331CefRefPtr<CefResourceHandler> GuiHandler::GetResourceHandler(
332 CefRefPtr<CefBrowser> browser,
333 CefRefPtr<CefFrame> frame,
334 CefRefPtr<CefRequest> request) {
335 CEF_REQUIRE_IO_THREAD();
336
337 std::string addr = request->GetURL().ToString();
338 std::string prefix = "http://rootserver.local";
339
340 if (addr.compare(0, prefix.length(), prefix) != 0)
341 return fResourceManager->GetResourceHandler(browser, frame, request);
342
343 int indx = std::stoi(addr.substr(prefix.length(), addr.find("/", prefix.length()) - prefix.length()));
344
345 if ((indx < 0) || (indx >= (int) fServers.size()) || !fServers[indx]) {
346 R__LOG_ERROR(CefWebDisplayLog()) << "No THttpServer with index " << indx;
347 return nullptr;
348 }
349
350 THttpServer *serv = fServers[indx];
351 if (serv->IsZombie()) {
352 fServers[indx] = nullptr;
353 R__LOG_ERROR(CefWebDisplayLog()) << "THttpServer with index " << indx << " is zombie now";
354 return nullptr;
355 }
356
357 TUrl url(addr.c_str());
358
359 const char *inp_path = url.GetFile();
360
361 TString inp_query = url.GetOptions();
362
363 TString fname;
364
365 if (serv->IsFileRequested(inp_path, fname)) {
366 // process file - no need for special requests handling
367
368 // when file not exists - return nullptr
369 if (gSystem->AccessPathName(fname.Data()))
370 return new TGuiResourceHandler(serv, true);
371
372 const char *mime = THttpServer::GetMimeType(fname.Data());
373
374 CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(fname.Data());
375
376 // Constructor for HTTP status code 200 and no custom response headers.
377 // There’s also a version of the constructor for custom status code and response headers.
378 return new CefStreamResourceHandler(mime, stream);
379 }
380
381 std::string inp_method = request->GetMethod().ToString();
382
383 TGuiResourceHandler *handler = new TGuiResourceHandler(serv);
384 handler->fArg->SetMethod(inp_method.c_str());
385 handler->fArg->SetPathAndFileName(inp_path);
386 handler->fArg->SetTopName("webgui");
387
388 if (inp_method == "POST") {
389
390 CefRefPtr< CefPostData > post_data = request->GetPostData();
391
392 if (!post_data) {
393 R__LOG_ERROR(CefWebDisplayLog()) << "FATAL - NO POST DATA in CEF HANDLER!!!";
394 exit(1);
395 } else {
396 CefPostData::ElementVector elements;
397 post_data->GetElements(elements);
398 size_t sz = 0, off = 0;
399 for (unsigned n = 0; n < elements.size(); ++n)
400 sz += elements[n]->GetBytesCount();
401 std::string data;
402 data.resize(sz);
403
404 for (unsigned n = 0; n < elements.size(); ++n) {
405 sz = elements[n]->GetBytes(elements[n]->GetBytesCount(), (char *)data.data() + off);
406 off += sz;
407 }
408 handler->fArg->SetPostData(std::move(data));
409 }
410 } else if (inp_query.Index("&post=") != kNPOS) {
411 Int_t pos = inp_query.Index("&post=");
412 TString buf = TBase64::Decode(inp_query.Data() + pos + 6);
413 handler->fArg->SetPostData(std::string(buf.Data()));
414 inp_query.Resize(pos);
415 }
416
417 handler->fArg->SetQuery(inp_query.Data());
418
419 // just return handler
420 return handler;
421}
422
423///////////////////////////////////////////////////////////////////////////////////////////////////
424/// Generate URL for batch page
425/// Uses file:/// prefix to let access JSROOT scripts placed on file system
426/// Register provider for that page in resource manager
427
428std::string GuiHandler::AddBatchPage(const std::string &cont)
429{
430 std::string url = "file:///batch_page";
431 url.append(std::to_string(fBatchPageCount++));
432 url.append(".html");
433
434 fResourceManager->AddContentProvider(url, cont, "text/html", 0, std::string());
435 return url;
436}
437
438///////////////////////////////////////////////////////////////////////////////////////////////////
439/// Generate URL for RWebWindow page
440/// Register server instance and assign it with the index
441/// Produced URL only works inside CEF and does not represent real HTTP address
442
443std::string GuiHandler::MakePageUrl(THttpServer *serv, const std::string &addr)
444{
445 unsigned indx = 0;
446 while ((indx < fServers.size()) && (fServers[indx] != serv)) indx++;
447 if (indx >= fServers.size()) {
448 fServers.emplace_back(serv);
449 indx = fServers.size() - 1;
450 }
451
452 return std::string("http://rootserver.local") + std::to_string(indx) + addr;
453}
454
#define R__LOG_WARNING(...)
Definition RLogger.hxx:363
#define R__LOG_ERROR(...)
Definition RLogger.hxx:362
#define R__LOG_DEBUG(DEBUGLEVEL,...)
Definition RLogger.hxx:365
constexpr Ssiz_t kNPOS
Definition RtypesCore.h:124
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
const char * mime_type
Definition civetweb.c:8028
CefRefPtr< CefResourceManager > fResourceManager
bool is_closing_
Definition gui_handler.h:52
int fConsole
! console parameter, assigned via WebGui.Console rootrc parameter
Definition gui_handler.h:46
GuiHandler(bool use_views=false)
std::vector< THttpServer * > fServers
Definition gui_handler.h:50
void OnBeforeClose(CefRefPtr< CefBrowser > browser) override
CefRefPtr< CefResourceHandler > GetResourceHandler(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, CefRefPtr< CefRequest > request) override
bool DoClose(CefRefPtr< CefBrowser > browser) override
BrowserList fBrowserList
Definition gui_handler.h:48
void CloseAllBrowsers(bool force_close)
int fBatchPageCount
void PlatformTitleChange(CefRefPtr< CefBrowser > browser, const CefString &title)
cef_return_value_t OnBeforeResourceLoad(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, CefRefPtr< CefRequest > request, CefRefPtr< CefCallback > callback) override
void OnLoadError(CefRefPtr< CefBrowser > browser, CefRefPtr< CefFrame > frame, ErrorCode errorCode, const CefString &errorText, const CefString &failedUrl) override
static std::string GetDataURI(const std::string &data, const std::string &mime_type)
bool fUseViews
! if view framework is used, required for true headless mode
Definition gui_handler.h:45
std::string AddBatchPage(const std::string &cont)
Generate URL for batch page Uses file:/// prefix to let access JSROOT scripts placed on file system R...
std::string MakePageUrl(THttpServer *serv, const std::string &addr)
Generate URL for RWebWindow page Register server instance and assign it with the index Produced URL o...
bool OnConsoleMessage(CefRefPtr< CefBrowser > browser, cef_log_severity_t level, const CefString &message, const CefString &source, int line) override
void OnTitleChange(CefRefPtr< CefBrowser > browser, const CefString &title) override
void OnAfterCreated(CefRefPtr< CefBrowser > browser) override
A log configuration for a channel, e.g.
Definition RLogger.hxx:101
static TString Decode(const char *data)
Decode a base64 string date into a generic TString.
Definition TBase64.cxx:131
void AssignCallback(CefRefPtr< CefCallback > cb)
void HttpReplied() override
virtual method to inform object that http request is processed
const char * GetWSPlatform() const override
provide WS platform
CefRefPtr< CefCallback > fCallBack
const char * GetWSKind() const override
provide WS kind
TCefHttpCallArg()=default
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:491
~TGuiResourceHandler() override
bool ProcessRequest(CefRefPtr< CefRequest > request, CefRefPtr< CefCallback > callback) override
DISALLOW_COPY_AND_ASSIGN(TGuiResourceHandler)
std::shared_ptr< TCefHttpCallArg > fArg
IMPLEMENT_REFCOUNTING(TGuiResourceHandler)
bool ReadResponse(void *data_out, int bytes_to_read, int &bytes_read, CefRefPtr< CefCallback > callback) override
TGuiResourceHandler(THttpServer *serv, bool dummy=false)
THttpServer * fServer
void GetResponseHeaders(CefRefPtr< CefResponse > response, int64 &response_length, CefString &redirectUrl) override
void Cancel() override
Contains arguments for single HTTP call.
const void * GetContent() const
void SetContent(const char *cont)
Set content as text.
Bool_t IsFile() const
Online http server for arbitrary ROOT application.
Definition THttpServer.h:31
Bool_t IsFileRequested(const char *uri, TString &res) const
Check if file is requested, thread safe.
Bool_t SubmitHttp(std::shared_ptr< THttpCallArg > arg, Bool_t can_run_immediately=kFALSE)
Submit HTTP request.
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
static const char * GetMimeType(const char *path)
Guess mime type base on file extension.
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
Basic string class.
Definition TString.h:139
const char * Data() const
Definition TString.h:380
void Resize(Ssiz_t n)
Resize the string. Truncate or add blanks as necessary.
Definition TString.cxx:1140
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1283
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetFile() const
Definition TUrl.h:69
const char * GetOptions() const
Definition TUrl.h:71
TLine * line
ROOT::Experimental::RLogChannel & CefWebDisplayLog()
ROOT::Experimental::RLogChannel & CefWebDisplayLog()
const Int_t n
Definition legend1.C:16