Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RWebDisplayHandle.cxx
Go to the documentation of this file.
1// Author: Sergey Linev <s.linev@gsi.de>
2// Date: 2018-10-17
3// Warning: This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
14
15#include <ROOT/RLogger.hxx>
16
17#include "RConfigure.h"
18#include "TSystem.h"
19#include "TRandom3.h"
20#include "TString.h"
21#include "TObjArray.h"
22#include "THttpServer.h"
23#include "TEnv.h"
24#include "TError.h"
25#include "TROOT.h"
26#include "TBase64.h"
27#include "TBufferJSON.h"
29
30#include <fstream>
31#include <iostream>
32#include <filesystem>
33#include <memory>
34#include <regex>
35
36#ifdef _MSC_VER
37#include <process.h>
38#else
39#include <unistd.h>
40#include <cstdlib>
41#include <csignal>
42#include <spawn.h>
43#ifdef R__MACOSX
44#include <sys/wait.h>
45#include <crt_externs.h>
46#elif defined(__FreeBSD__)
47#include <sys/wait.h>
48#include <dlfcn.h>
49#else
50#include <wait.h>
51#endif
52#endif
53
54using namespace ROOT;
55using namespace std::string_literals;
56
57/** \class ROOT::RWebDisplayHandle
58\ingroup webdisplay
59
60Handle of created web-based display
61Depending from type of web display, holds handle of started browser process or other display-specific information
62to correctly stop and cleanup display.
63*/
64
65
66//////////////////////////////////////////////////////////////////////////////////////////////////
67/// Static holder of registered creators of web displays
68
69std::map<std::string, std::unique_ptr<RWebDisplayHandle::Creator>> &RWebDisplayHandle::GetMap()
70{
71 static std::map<std::string, std::unique_ptr<RWebDisplayHandle::Creator>> sMap;
72 return sMap;
73}
74
75//////////////////////////////////////////////////////////////////////////////////////////////////
76/// Search for specific browser creator
77/// If not found, try to add one
78/// \param name - creator name like ChromeCreator
79/// \param libname - shared library name where creator could be provided
80
81std::unique_ptr<RWebDisplayHandle::Creator> &RWebDisplayHandle::FindCreator(const std::string &name, const std::string &libname)
82{
83 auto &m = GetMap();
84 auto search = m.find(name);
85 if (search == m.end()) {
86
87 if (libname == "ChromeCreator") {
88 m.emplace(name, std::make_unique<ChromeCreator>(name == "edge"));
89 } else if (libname == "FirefoxCreator") {
90 m.emplace(name, std::make_unique<FirefoxCreator>());
91 } else if (libname == "SafariCreator") {
92 m.emplace(name, std::make_unique<SafariCreator>());
93 } else if (libname == "BrowserCreator") {
94 m.emplace(name, std::make_unique<BrowserCreator>(false));
95 } else if (!libname.empty()) {
96 gSystem->Load(libname.c_str());
97 }
98
99 search = m.find(name); // try again
100 }
101
102 if (search != m.end())
103 return search->second;
104
105 static std::unique_ptr<RWebDisplayHandle::Creator> dummy;
106 return dummy;
107}
108
109namespace ROOT {
110
111//////////////////////////////////////////////////////////////////////////////////////////////////
112/// Specialized handle to hold information about running browser process
113/// Used to correctly cleanup all processes and temporary directories
114
116
117#ifdef _MSC_VER
118 typedef int browser_process_id;
119#else
120 typedef pid_t browser_process_id;
121#endif
122 std::string fTmpDir; ///< temporary directory to delete at the end
123 std::string fTmpFile; ///< temporary file to remove
124 bool fHasPid{false};
126
127public:
128 RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile,
129 const std::string &dump)
131 {
132 SetContent(dump);
133 }
134
135 RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile,
138 {
139 }
140
142 {
143#ifdef _MSC_VER
144 if (fHasPid)
145 gSystem->Exec(("taskkill /F /PID " + std::to_string(fPid) + " >NUL 2>NUL").c_str());
146 std::string rmdir = "rmdir /S /Q ";
147#else
148 if (fHasPid)
149 kill(fPid, SIGKILL);
150 std::string rmdir = "rm -rf ";
151#endif
152 if (!fTmpDir.empty())
153 gSystem->Exec((rmdir + fTmpDir).c_str());
155 }
156
157 void RemoveStartupFiles() override
158 {
159#ifdef _MSC_VER
160 std::string rmfile = "del /F ";
161#else
162 std::string rmfile = "rm -f ";
163#endif
164 if (!fTmpFile.empty()) {
165 gSystem->Exec((rmfile + fTmpFile).c_str());
166 fTmpFile.clear();
167 }
168 }
169};
170
171} // namespace ROOT
172
173//////////////////////////////////////////////////////////////////////////////////////////////////
174/// Class to handle starting of web-browsers like Chrome or Firefox
175
177{
178 if (custom) return;
179
180 if (!exec.empty()) {
181 if (exec.find("$url") == std::string::npos) {
182 fProg = exec;
183#ifdef _MSC_VER
184 fExec = exec + " $url";
185#else
186 fExec = exec + " $url &";
187#endif
188 } else {
189 fExec = exec;
190 auto pos = exec.find(" ");
191 if (pos != std::string::npos)
192 fProg = exec.substr(0, pos);
193 }
194 } else if (gSystem->InheritsFrom("TMacOSXSystem")) {
195 fExec = "open \'$url\'";
196 } else if (gSystem->InheritsFrom("TWinNTSystem")) {
197 fExec = "start $url";
198 } else {
199 fExec = "xdg-open \'$url\' &";
200 }
201}
202
203//////////////////////////////////////////////////////////////////////////////////////////////////
204/// Check if browser executable exists and can be used
205
207{
208 if (nexttry.empty() || !fProg.empty())
209 return;
210
212#ifdef R__MACOSX
213 fProg = std::regex_replace(nexttry, std::regex("%20"), " ");
214#else
215 fProg = nexttry;
216#endif
217 return;
218 }
219
220 if (!check_std_paths)
221 return;
222
223#ifdef _MSC_VER
224 std::string ProgramFiles = gSystem->Getenv("ProgramFiles");
225 auto pos = ProgramFiles.find(" (x86)");
226 if (pos != std::string::npos)
227 ProgramFiles.erase(pos, 6);
228 std::string ProgramFilesx86 = gSystem->Getenv("ProgramFiles(x86)");
229
230 if (!ProgramFiles.empty())
231 TestProg(ProgramFiles + nexttry, false);
232 if (!ProgramFilesx86.empty())
233 TestProg(ProgramFilesx86 + nexttry, false);
234#endif
235}
236
237//////////////////////////////////////////////////////////////////////////////////////////////////
238/// Create temporary file for web display
239/// Normally gSystem->TempFileName() method used to create file in default temporary directory
240/// For snap chromium use of default temp directory is not always possible therefore one switches to home directory
241/// But one checks if default temp directory modified and already points to /home folder
242
244{
245 std::string dirname;
246 if (use_home_dir > 0) {
247 if (use_home_dir == 1) {
248 const char *tmp_dir = gSystem->TempDirectory();
249 if (tmp_dir && (strncmp(tmp_dir, "/home", 5) == 0))
250 use_home_dir = 0;
251 else if (!tmp_dir || (strncmp(tmp_dir, "/tmp", 4) == 0))
252 use_home_dir = 2;
253 }
254
255 if (use_home_dir > 1)
257 }
258 return gSystem->TempFileName(name, use_home_dir > 1 ? dirname.c_str() : nullptr, suffix);
259}
260
261static void DummyTimeOutHandler(int /* Sig */) {}
262
263
264//////////////////////////////////////////////////////////////////////////////////////////////////
265/// Display given URL in web browser
266/// \note See more details related to webdisplay on RWebWindowsManager::ShowWindow
267
268std::unique_ptr<RWebDisplayHandle>
270{
271 std::string url = args.GetFullUrl();
272 if (url.empty())
273 return nullptr;
274
276 std::cout << "New web window: " << url << std::endl;
277 return std::make_unique<RWebBrowserHandle>(url, "", "", "");
278 }
279
280 std::string exec;
281 if (args.IsBatchMode())
282 exec = fBatchExec;
283 else if (args.IsHeadless())
284 exec = fHeadlessExec;
285 else if (args.IsStandalone())
286 exec = fExec;
287 else
288 exec = "$prog $url &";
289
290 if (exec.empty())
291 return nullptr;
292
293 std::string swidth = std::to_string(args.GetWidth() > 0 ? args.GetWidth() : 800),
294 sheight = std::to_string(args.GetHeight() > 0 ? args.GetHeight() : 600),
295 sposx = std::to_string(args.GetX() >= 0 ? args.GetX() : 0),
296 sposy = std::to_string(args.GetY() >= 0 ? args.GetY() : 0);
297
298 ProcessGeometry(exec, args);
299
300 std::string extra = args.GetExtraArgs();
301 if (!extra.empty()) {
302 auto p = exec.find("$url");
303 if (p != std::string::npos)
304 exec.insert(p, extra + " ");
305 }
306
307 std::string rmdir = MakeProfile(exec, args.IsBatchMode() || args.IsHeadless());
308
309 std::string tmpfile;
310
311 // these are secret parameters, hide them in temp file
312 if (((url.find("token=") != std::string::npos) || (url.find("key=") != std::string::npos)) && !args.IsBatchMode() && !args.IsHeadless()) {
313 TString filebase = "root_start_";
314
315 auto f = TemporaryFile(filebase, IsSnapBrowser() ? 1 : 0, ".html");
316
317 bool ferr = false;
318
319 if (!f) {
320 ferr = true;
321 } else {
322 std::string content = std::regex_replace(
323 "<!DOCTYPE html>\n"
324 "<html lang=\"en\">\n"
325 "<head>\n"
326 " <meta charset=\"utf-8\">\n"
327 " <meta http-equiv=\"refresh\" content=\"0;url=$url\"/>\n"
328 " <title>Opening ROOT widget</title>\n"
329 "</head>\n"
330 "<body>\n"
331 "<p>\n"
332 " This page should redirect you to a ROOT widget. If it doesn't,\n"
333 " <a href=\"$url\">click here to go to ROOT</a>.\n"
334 "</p>\n"
335 "</body>\n"
336 "</html>\n", std::regex("\\$url"), url);
337
338 if (fwrite(content.c_str(), 1, content.length(), f) != content.length())
339 ferr = true;
340
341 if (fclose(f) != 0)
342 ferr = true;
343
344 tmpfile = filebase.Data();
345
346 url = "file://"s + tmpfile;
347 }
348
349 if (ferr) {
350 if (!tmpfile.empty())
351 gSystem->Unlink(tmpfile.c_str());
352 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary HTML file to startup widget";
353 return nullptr;
354 }
355 }
356
357 exec = std::regex_replace(exec, std::regex("\\$rootetcdir"), TROOT::GetEtcDir().Data());
358 exec = std::regex_replace(exec, std::regex("\\$url"), url);
359 exec = std::regex_replace(exec, std::regex("\\$width"), swidth);
360 exec = std::regex_replace(exec, std::regex("\\$height"), sheight);
361 exec = std::regex_replace(exec, std::regex("\\$posx"), sposx);
362 exec = std::regex_replace(exec, std::regex("\\$posy"), sposy);
363
364 if (exec.compare(0,5,"fork:") == 0) {
365 if (fProg.empty()) {
366 if (!tmpfile.empty())
367 gSystem->Unlink(tmpfile.c_str());
368 R__LOG_ERROR(WebGUILog()) << "Fork instruction without executable";
369 return nullptr;
370 }
371
372 exec.erase(0, 5);
373
374 // in case of redirection process will wait until output is produced
375 std::string redirect = args.GetRedirectOutput();
376
377#ifndef _MSC_VER
378
379 std::unique_ptr<TObjArray> fargs(TString(exec.c_str()).Tokenize(" "));
380 if (!fargs || (fargs->GetLast()<=0)) {
381 if (!tmpfile.empty())
382 gSystem->Unlink(tmpfile.c_str());
383 R__LOG_ERROR(WebGUILog()) << "Fork instruction is empty";
384 return nullptr;
385 }
386
387 std::vector<char *> argv;
388 argv.push_back((char *) fProg.c_str());
389 for (Int_t n = 0; n <= fargs->GetLast(); ++n)
390 argv.push_back((char *)fargs->At(n)->GetName());
391 argv.push_back(nullptr);
392
393 R__LOG_DEBUG(0, WebGUILog()) << "Show web window in browser with posix_spawn:\n" << fProg << " " << exec;
394
397 if (redirect.empty())
399 else
402
403#ifdef R__MACOSX
404 char **envp = *_NSGetEnviron();
405#elif defined (__FreeBSD__)
406 //this is needed because the FreeBSD linker does not like to resolve these special symbols
407 //in shared libs with -Wl,--no-undefined
408 char** envp = (char**)dlsym(RTLD_DEFAULT, "environ");
409#else
410 char **envp = environ;
411#endif
412
413 pid_t pid;
414 int status = posix_spawn(&pid, argv[0], &action, nullptr, argv.data(), envp);
415
417
418 if (status != 0) {
419 if (!tmpfile.empty())
420 gSystem->Unlink(tmpfile.c_str());
421 R__LOG_ERROR(WebGUILog()) << "Fail to launch " << argv[0];
422 return nullptr;
423 }
424
425 if (!redirect.empty()) {
426 Int_t batch_timeout = gEnv->GetValue("WebGui.BatchTimeout", 30);
427 struct sigaction Act, Old;
428 int elapsed_time = 0;
429
430 if (batch_timeout) {
431 memset(&Act, 0, sizeof(Act));
432 Act.sa_handler = DummyTimeOutHandler;
433 sigemptyset(&Act.sa_mask);
438 }
439
440 int job_done = 0;
441 std::string dump_content;
442
443 while (!job_done) {
444
445 // wait until output is produced
446 int wait_status = 0;
447
449
450 // try read dump anyway
452
453 if (dump_content.find("<div>###batch###job###done###</div>") != std::string::npos)
454 job_done = 1;
455
456 if (wait_res == -1) {
457 // failure when finish process
459 if ((errno == EINTR) && (alarm_timeout > 0) && !job_done) {
460 if (alarm_timeout > 2) alarm_timeout = 2;
463 } else {
464 // end of timeout - do not try to wait any longer
465 job_done = 1;
466 }
467 } else if (!WIFEXITED(wait_status) && !WIFSIGNALED(wait_status)) {
468 // abnormal end of browser process
469 job_done = 1;
470 } else {
471 // this is normal finish, no need for process kill
472 job_done = 2;
473 }
474 }
475
476 if (job_done != 2) {
477 // kill browser process when no normal end was detected
478 kill(pid, SIGKILL);
479 }
480
481 if (batch_timeout) {
482 alarm(0); // disable alarm
483 sigaction(SIGALRM, &Old, nullptr);
484 }
485
486 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
487 ::Info("RWebDisplayHandle::Display", "Preserve dump file %s", redirect.c_str());
488 else
489 gSystem->Unlink(redirect.c_str());
490
491 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, dump_content);
492 }
493
494 // add processid and rm dir
495
496 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, pid);
497
498#else
499
500 if (fProg.empty()) {
501 if (!tmpfile.empty())
502 gSystem->Unlink(tmpfile.c_str());
503 R__LOG_ERROR(WebGUILog()) << "No Web browser found";
504 return nullptr;
505 }
506
507 // use UnixPathName to simplify handling of backslashes
508 exec = "wmic process call create '"s + gSystem->UnixPathName(fProg.c_str()) + " " + exec + "' | find \"ProcessId\" "s;
509 std::string process_id = gSystem->GetFromPipe(exec.c_str()).Data();
510 std::stringstream ss(process_id);
511 std::string tmp;
512 char c;
513 int pid = 0;
514 ss >> tmp >> c >> pid;
515
516 if (pid <= 0) {
517 if (!tmpfile.empty())
518 gSystem->Unlink(tmpfile.c_str());
519 R__LOG_ERROR(WebGUILog()) << "Fail to launch " << fProg;
520 return nullptr;
521 }
522
523 // add processid and rm dir
524 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, pid);
525#endif
526 }
527
528#ifdef _MSC_VER
529
530 if (exec.rfind("&") == exec.length() - 1) {
531
532 // if last symbol is &, use _spawn to detach execution
533 exec.resize(exec.length() - 1);
534
535 std::vector<char *> argv;
536 std::string firstarg = fProg;
537 auto slashpos = firstarg.find_last_of("/\\");
538 if (slashpos != std::string::npos)
539 firstarg.erase(0, slashpos + 1);
540 argv.push_back((char *)firstarg.c_str());
541
542 std::unique_ptr<TObjArray> fargs(TString(exec.c_str()).Tokenize(" "));
543 for (Int_t n = 1; n <= fargs->GetLast(); ++n)
544 argv.push_back((char *)fargs->At(n)->GetName());
545 argv.push_back(nullptr);
546
547 R__LOG_DEBUG(0, WebGUILog()) << "Showing web window in " << fProg << " with:\n" << exec;
548
549 _spawnv(_P_NOWAIT, gSystem->UnixPathName(fProg.c_str()), argv.data());
550
551 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, ""s);
552 }
553
554 std::string prog = "\""s + gSystem->UnixPathName(fProg.c_str()) + "\""s;
555
556#else
557
558#ifdef R__MACOSX
559 std::string prog = std::regex_replace(fProg, std::regex(" "), "\\ ");
560#else
561 std::string prog = fProg;
562#endif
563
564#endif
565
566 exec = std::regex_replace(exec, std::regex("\\$prog"), prog);
567
568 std::string redirect = args.GetRedirectOutput(), dump_content;
569
570 if (!redirect.empty()) {
571 if (exec.find("$dumpfile") != std::string::npos) {
572 exec = std::regex_replace(exec, std::regex("\\$dumpfile"), redirect);
573 } else {
574 auto p = exec.length();
575 if (exec.rfind("&") == p-1) --p;
576 exec.insert(p, " >"s + redirect + " "s);
577 }
578 }
579
580 R__LOG_DEBUG(0, WebGUILog()) << "Showing web window in browser with:\n" << exec;
581
582 gSystem->Exec(exec.c_str());
583
584 // read content of redirected output
585 if (!redirect.empty()) {
587
588 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
589 ::Info("RWebDisplayHandle::Display", "Preserve dump file %s", redirect.c_str());
590 else
591 gSystem->Unlink(redirect.c_str());
592 }
593
594 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, dump_content);
595}
596
597//////////////////////////////////////////////////////////////////////////////////////////////////
598/// Constructor
599
601{
602 fExec = gEnv->GetValue("WebGui.SafariInteractive", "open -a Safari $url");
603}
604
605//////////////////////////////////////////////////////////////////////////////////////////////////
606/// Returns true if it can be used
607
609{
610#ifdef R__MACOSX
611 return true;
612#else
613 return false;
614#endif
615}
616
617//////////////////////////////////////////////////////////////////////////////////////////////////
618/// Constructor
619
621{
622 fEdge = _edge;
623
624 fEnvPrefix = fEdge ? "WebGui.Edge" : "WebGui.Chrome";
625
626 TestProg(gEnv->GetValue(fEnvPrefix.c_str(), ""));
627
628 if (!fProg.empty() && !fEdge)
629 fChromeVersion = gEnv->GetValue("WebGui.ChromeVersion", -1);
630
631#ifdef _MSC_VER
632 if (fEdge)
633 TestProg("\\Microsoft\\Edge\\Application\\msedge.exe", true);
634 else
635 TestProg("\\Google\\Chrome\\Application\\chrome.exe", true);
636#endif
637#ifdef R__MACOSX
638 TestProg("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
639#endif
640#ifdef R__LINUX
641 TestProg("/snap/bin/chromium"); // test snap before to detect it properly
642 TestProg("/usr/bin/chromium");
643 TestProg("/usr/bin/chromium-browser");
644 TestProg("/usr/bin/chrome-browser");
645 TestProg("/usr/bin/google-chrome-stable");
646 TestProg("/usr/bin/google-chrome");
647#endif
648
649// --no-sandbox is required to run chrome with super-user, but only in headless mode
650// --headless=new was used when both old and new were available, but old was removed from chrome 132, see https://developer.chrome.com/blog/removing-headless-old-from-chrome
651
652#ifdef _MSC_VER
653 // here --headless=old was used to let normally end of Edge process when --dump-dom is used
654 // while on Windows chrome and edge version not tested, just suppose that newest chrome is used
655 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "$prog --headless --no-sandbox $geometry --dump-dom $url");
656 // in interactive headless mode fork used to let stop browser via process id
657 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless --no-sandbox --disable-gpu $geometry \"$url\"");
658 fExec = gEnv->GetValue((fEnvPrefix + "Interactive").c_str(), "$prog $geometry --new-window --app=$url &"); // & in windows mean usage of spawn
659#else
660#ifdef R__MACOSX
661 bool use_normal = true; // mac does not like new flag
662#else
663 bool use_normal = (fChromeVersion < 119) || (fChromeVersion > 131);
664#endif
665 if (use_normal) {
666 // old or newest browser with standard headless mode
667 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "fork:--headless --no-sandbox --disable-extensions --disable-audio-output $geometry --dump-dom $url");
668 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless --no-sandbox --disable-extensions --disable-audio-output $geometry $url");
669 } else {
670 // newer version with headless=new mode
671 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "fork:--headless=new --no-sandbox --disable-extensions --disable-audio-output $geometry --dump-dom $url");
672 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless=new --no-sandbox --disable-extensions --disable-audio-output $geometry $url");
673 }
674 fExec = gEnv->GetValue((fEnvPrefix + "Interactive").c_str(), "$prog $geometry --new-window --app=\'$url\' >/dev/null 2>/dev/null &");
675#endif
676}
677
678
679//////////////////////////////////////////////////////////////////////////////////////////////////
680/// Replace $geometry placeholder with geometry settings
681/// Also RWebDisplayArgs::GetExtraArgs() are appended
682
684{
685 std::string geometry;
686 if ((args.GetWidth() > 0) && (args.GetHeight() > 0))
687 geometry = "--window-size="s + std::to_string(args.GetWidth())
688 + (args.IsHeadless() ? "x"s : ","s)
689 + std::to_string(args.GetHeight());
690
691 if (((args.GetX() >= 0) || (args.GetY() >= 0)) && !args.IsHeadless()) {
692 if (!geometry.empty()) geometry.append(" ");
693 geometry.append("--window-position="s + std::to_string(args.GetX() >= 0 ? args.GetX() : 0) + ","s +
694 std::to_string(args.GetY() >= 0 ? args.GetY() : 0));
695 }
696
697 exec = std::regex_replace(exec, std::regex("\\$geometry"), geometry);
698}
699
700
701//////////////////////////////////////////////////////////////////////////////////////////////////
702/// Handle profile argument
703
704std::string RWebDisplayHandle::ChromeCreator::MakeProfile(std::string &exec, bool)
705{
706 std::string rmdir, profile_arg;
707
708 if (exec.find("$profile") == std::string::npos)
709 return rmdir;
710
711 const char *chrome_profile = gEnv->GetValue((fEnvPrefix + "Profile").c_str(), "");
714 } else {
716 rnd.SetSeed(0);
718 if ((profile_arg.compare(0, 4, "/tmp") == 0) && IsSnapBrowser())
720
721#ifdef _MSC_VER
722 char slash = '\\';
723#else
724 char slash = '/';
725#endif
726 if (!profile_arg.empty() && (profile_arg[profile_arg.length()-1] != slash))
728 profile_arg += "root_chrome_profile_"s + std::to_string(rnd.Integer(0x100000));
729
730 rmdir = profile_arg;
731 }
732
733 exec = std::regex_replace(exec, std::regex("\\$profile"), profile_arg);
734
735 return rmdir;
736}
737
738
739//////////////////////////////////////////////////////////////////////////////////////////////////
740/// Constructor
741
743{
744 TestProg(gEnv->GetValue("WebGui.Firefox", ""));
745
746#ifdef _MSC_VER
747 TestProg("\\Mozilla Firefox\\firefox.exe", true);
748#endif
749#ifdef R__MACOSX
750 TestProg("/Applications/Firefox.app/Contents/MacOS/firefox");
751#endif
752#ifdef R__LINUX
753 TestProg("/snap/bin/firefox");
754 TestProg("/usr/bin/firefox");
755 TestProg("/usr/bin/firefox-bin");
756#endif
757
758#ifdef _MSC_VER
759 // there is a problem when specifying the window size with wmic on windows:
760 // It gives: Invalid format. Hint: <paramlist> = <param> [, <paramlist>].
761 fBatchExec = gEnv->GetValue("WebGui.FirefoxBatch", "$prog -headless -no-remote $profile $url");
762 fHeadlessExec = gEnv->GetValue("WebGui.FirefoxHeadless", "fork:-headless -no-remote $profile \"$url\"");
763 fExec = gEnv->GetValue("WebGui.FirefoxInteractive", "$prog -no-remote $profile $geometry $url &");
764#else
765 fBatchExec = gEnv->GetValue("WebGui.FirefoxBatch", "fork:--headless -no-remote -new-instance $profile $url");
766 fHeadlessExec = gEnv->GetValue("WebGui.FirefoxHeadless", "fork:--headless -no-remote $profile --private-window $url");
767 fExec = gEnv->GetValue("WebGui.FirefoxInteractive", "$rootetcdir/runfirefox.sh __nodump__ $cleanup_profile $prog -no-remote $profile $geometry -url \'$url\' &");
768#endif
769}
770
771//////////////////////////////////////////////////////////////////////////////////////////////////
772/// Process window geometry for Firefox
773
775{
776 std::string geometry;
777 if ((args.GetWidth() > 0) && (args.GetHeight() > 0) && !args.IsHeadless())
778 geometry = "-width="s + std::to_string(args.GetWidth()) + " -height=" + std::to_string(args.GetHeight());
779
780 exec = std::regex_replace(exec, std::regex("\\$geometry"), geometry);
781}
782
783//////////////////////////////////////////////////////////////////////////////////////////////////
784/// Create Firefox profile to run independent browser window
785
787{
788 std::string rmdir, profile_arg;
789
790 if (exec.find("$profile") == std::string::npos)
791 return rmdir;
792
793 const char *ff_profile = gEnv->GetValue("WebGui.FirefoxProfile", "");
794 const char *ff_profilepath = gEnv->GetValue("WebGui.FirefoxProfilePath", "");
795 Int_t ff_randomprofile = RWebWindowWSHandler::GetBoolEnv("WebGui.FirefoxRandomProfile", 1);
796 if (ff_profile && *ff_profile) {
797 profile_arg = "-P "s + ff_profile;
798 } else if (ff_profilepath && *ff_profilepath) {
799 profile_arg = "-profile "s + ff_profilepath;
800 } else if (ff_randomprofile > 0) {
802 rnd.SetSeed(0);
803 std::string profile_dir = gSystem->TempDirectory();
804 if ((profile_dir.compare(0, 4, "/tmp") == 0) && IsSnapBrowser())
806
807#ifdef _MSC_VER
808 char slash = '\\';
809#else
810 char slash = '/';
811#endif
812 if (!profile_dir.empty() && (profile_dir[profile_dir.length()-1] != slash))
814 profile_dir += "root_ff_profile_"s + std::to_string(rnd.Integer(0x100000));
815
816 profile_arg = "-profile "s + profile_dir;
817
818 if (gSystem->mkdir(profile_dir.c_str()) == 0) {
819 rmdir = profile_dir;
820
821 std::ofstream user_js(profile_dir + "/user.js", std::ios::trunc);
822 // workaround for current Firefox, without such settings it fail to close window and terminate it from batch
823 // also disable question about upload of data
824 user_js << "user_pref(\"datareporting.policy.dataSubmissionPolicyAcceptedVersion\", 2);" << std::endl;
825 user_js << "user_pref(\"datareporting.policy.dataSubmissionPolicyNotifiedTime\", \"1635760572813\");" << std::endl;
826
827 // try to ensure that window closes with last tab
828 user_js << "user_pref(\"browser.tabs.closeWindowWithLastTab\", true);" << std::endl;
829 user_js << "user_pref(\"dom.allow_scripts_to_close_windows\", true);" << std::endl;
830 user_js << "user_pref(\"browser.sessionstore.resume_from_crash\", false);" << std::endl;
831
832 if (batch_mode) {
833 // allow to dump messages to std output
834 user_js << "user_pref(\"browser.dom.window.dump.enabled\", true);" << std::endl;
835 } else {
836 // to suppress annoying privacy tab
837 user_js << "user_pref(\"datareporting.policy.firstRunURL\", \"\");" << std::endl;
838 // to use custom userChrome.css files
839 user_js << "user_pref(\"toolkit.legacyUserProfileCustomizations.stylesheets\", true);" << std::endl;
840 // do not put tabs in title
841 user_js << "user_pref(\"browser.tabs.inTitlebar\", 0);" << std::endl;
842
843#ifdef R__LINUX
844 // fix WebGL creation problem on some Linux platforms
845 user_js << "user_pref(\"webgl.out-of-process\", false);" << std::endl;
846#endif
847
848 std::ofstream times_json(profile_dir + "/times.json", std::ios::trunc);
849 times_json << "{" << std::endl;
850 times_json << " \"created\": 1699968480952," << std::endl;
851 times_json << " \"firstUse\": null" << std::endl;
852 times_json << "}" << std::endl;
853 if (gSystem->mkdir((profile_dir + "/chrome").c_str()) == 0) {
854 std::ofstream style(profile_dir + "/chrome/userChrome.css", std::ios::trunc);
855 // do not show tabs
856 style << "#TabsToolbar { visibility: collapse; }" << std::endl;
857 // do not show URL
858 style << "#nav-bar, #urlbar-container, #searchbar { visibility: collapse !important; }" << std::endl;
859 }
860 }
861
862 } else {
863 R__LOG_ERROR(WebGUILog()) << "Cannot create Firefox profile directory " << profile_dir;
864 }
865 }
866
867 exec = std::regex_replace(exec, std::regex("\\$profile"), profile_arg);
868
869 if (exec.find("$cleanup_profile") != std::string::npos) {
870 if (rmdir.empty()) rmdir = "__dummy__";
871 exec = std::regex_replace(exec, std::regex("\\$cleanup_profile"), rmdir);
872 rmdir.clear(); // no need to delete directory - it will be removed by script
873 }
874
875 return rmdir;
876}
877
878///////////////////////////////////////////////////////////////////////////////////////////////////
879/// Check if http server required for display
880/// \param args - defines where and how to display web window
881
883{
886 return false;
887
888 if (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)) {
889
890#ifdef WITH_QT6WEB
891 auto &qt6 = FindCreator("qt6", "libROOTQt6WebDisplay");
892 if (qt6 && qt6->IsActive())
893 return false;
894#endif
895#ifdef WITH_CEFWEB
896 auto &cef = FindCreator("cef", "libROOTCefDisplay");
897 if (cef && cef->IsActive())
898 return false;
899#endif
900 }
901
902 return true;
903}
904
905
906///////////////////////////////////////////////////////////////////////////////////////////////////
907/// Create web display
908/// \param args - defines where and how to display web window
909/// Returns RWebDisplayHandle, which holds information of running browser application
910/// Can be used fully independent from RWebWindow classes just to show any web page
911
912std::unique_ptr<RWebDisplayHandle> RWebDisplayHandle::Display(const RWebDisplayArgs &args)
913{
914 std::unique_ptr<RWebDisplayHandle> handle;
915
917 return handle;
918
919 auto try_creator = [&](std::unique_ptr<Creator> &creator) {
920 if (!creator || !creator->IsActive())
921 return false;
922 handle = creator->Display(args);
923 return handle ? true : false;
924 };
925
927 (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)),
928 has_qt6web = false, has_cefweb = false;
929
930#ifdef WITH_QT6WEB
931 has_qt6web = true;
932#endif
933
934#ifdef WITH_CEFWEB
935 has_cefweb = true;
936#endif
937
939 if (try_creator(FindCreator("qt6", "libROOTQt6WebDisplay")))
940 return handle;
941 }
942
944 if (try_creator(FindCreator("cef", "libROOTCefDisplay")))
945 return handle;
946 }
947
948 if (args.IsLocalDisplay()) {
949 R__LOG_ERROR(WebGUILog()) << "Neither Qt5/6 nor CEF libraries were found to provide local display";
950 return handle;
951 }
952
953 bool handleAsNative =
955
957 if (try_creator(FindCreator("chrome", "ChromeCreator")))
958 return handle;
959 }
960
962 if (try_creator(FindCreator("firefox", "FirefoxCreator")))
963 return handle;
964 }
965
966#ifdef _MSC_VER
967 // Edge browser cannot be run headless without registry change, therefore do not try it by default
968 if ((handleAsNative && !args.IsHeadless() && !args.IsBatchMode()) || (args.GetBrowserKind() == RWebDisplayArgs::kEdge)) {
969 if (try_creator(FindCreator("edge", "ChromeCreator")))
970 return handle;
971 }
972#endif
973
976 // R__LOG_ERROR(WebGUILog()) << "Neither Chrome nor Firefox browser cannot be started to provide display";
977 return handle;
978 }
979
981 if (try_creator(FindCreator("safari", "SafariCreator")))
982 return handle;
983 }
984
986 std::unique_ptr<Creator> creator = std::make_unique<BrowserCreator>(false, args.GetCustomExec());
987 try_creator(creator);
988 } else {
989 try_creator(FindCreator("browser", "BrowserCreator"));
990 }
991
992 return handle;
993}
994
995///////////////////////////////////////////////////////////////////////////////////////////////////
996/// Display provided url in configured web browser
997/// \param url - specified URL address like https://root.cern
998/// Browser can specified when starting `root --web=firefox`
999/// Returns true when browser started
1000/// It is convenience method, equivalent to:
1001/// ~~~
1002/// RWebDisplayArgs args;
1003/// args.SetUrl(url);
1004/// args.SetStandalone(false);
1005/// auto handle = RWebDisplayHandle::Display(args);
1006/// ~~~
1007
1008bool RWebDisplayHandle::DisplayUrl(const std::string &url)
1009{
1010 RWebDisplayArgs args;
1011 args.SetUrl(url);
1012 args.SetStandalone(false);
1013
1014 auto handle = Display(args);
1015
1016 return !!handle;
1017}
1018
1019///////////////////////////////////////////////////////////////////////////////////////////////////
1020/// Checks if configured browser can be used for image production
1021
1023{
1027 bool detected = false;
1028
1029 auto &h1 = FindCreator("chrome", "ChromeCreator");
1030 if (h1 && h1->IsActive()) {
1032 detected = true;
1033 }
1034
1035 if (!detected) {
1036 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1037 if (h2 && h2->IsActive()) {
1039 detected = true;
1040 }
1041 }
1042
1043 return detected;
1044 }
1045
1047 auto &h1 = FindCreator("chrome", "ChromeCreator");
1048 return h1 && h1->IsActive();
1049 }
1050
1052 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1053 return h2 && h2->IsActive();
1054 }
1055
1056#ifdef _MSC_VER
1057 if (args.GetBrowserKind() == RWebDisplayArgs::kEdge) {
1058 auto &h3 = FindCreator("edge", "ChromeCreator");
1059 return h3 && h3->IsActive();
1060 }
1061#endif
1062
1063 return true;
1064}
1065
1066///////////////////////////////////////////////////////////////////////////////////////////////////
1067/// Returns true if image production for specified browser kind is supported
1068/// If browser not specified - use currently configured browser or try to test existing web browsers
1069
1071{
1073
1074 return CheckIfCanProduceImages(args);
1075}
1076
1077///////////////////////////////////////////////////////////////////////////////////////////////////
1078/// Detect image format
1079/// There is special handling of ".screenshot.pdf" and ".screenshot.png" extensions
1080/// Creation of such files relies on headless browser functionality and fully supported only by Chrome browser
1081
1082std::string RWebDisplayHandle::GetImageFormat(const std::string &fname)
1083{
1084 std::string _fname = fname;
1085 std::transform(_fname.begin(), _fname.end(), _fname.begin(), ::tolower);
1086 auto EndsWith = [&_fname](const std::string &suffix) {
1087 return (_fname.length() > suffix.length()) ? (0 == _fname.compare(_fname.length() - suffix.length(), suffix.length(), suffix)) : false;
1088 };
1089
1090 if (EndsWith(".screenshot.pdf"))
1091 return "s.pdf"s;
1092 if (EndsWith(".pdf"))
1093 return "pdf"s;
1094 if (EndsWith(".json"))
1095 return "json"s;
1096 if (EndsWith(".svg"))
1097 return "svg"s;
1098 if (EndsWith(".screenshot.png"))
1099 return "s.png"s;
1100 if (EndsWith(".png"))
1101 return "png"s;
1102 if (EndsWith(".jpg") || EndsWith(".jpeg"))
1103 return "jpeg"s;
1104 if (EndsWith(".webp"))
1105 return "webp"s;
1106
1107 return ""s;
1108}
1109
1110
1111///////////////////////////////////////////////////////////////////////////////////////////////////
1112/// Produce image file using JSON data as source
1113/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1114
1115bool RWebDisplayHandle::ProduceImage(const std::string &fname, const std::string &json, int width, int height, const char *batch_file)
1116{
1117 return ProduceImages(fname, {json}, {width}, {height}, batch_file);
1118}
1119
1120
1121///////////////////////////////////////////////////////////////////////////////////////////////////
1122/// Produce vector of file names for specified file pattern
1123/// Depending from supported file forma
1124
1125std::vector<std::string> RWebDisplayHandle::ProduceImagesNames(const std::string &fname, unsigned nfiles)
1126{
1127 auto fmt = GetImageFormat(fname);
1128
1129 std::vector<std::string> fnames;
1130
1131 if ((fmt == "s.pdf") || (fmt == "s.png")) {
1132 fnames.emplace_back(fname);
1133 } else {
1134 std::string farg = fname;
1135
1136 bool has_quialifier = farg.find("%") != std::string::npos;
1137
1138 if (!has_quialifier && (nfiles > 1) && (fmt != "pdf")) {
1139 farg.insert(farg.rfind("."), "%d");
1140 has_quialifier = true;
1141 }
1142
1143 for (unsigned n = 0; n < nfiles; n++) {
1144 if(has_quialifier) {
1145 auto expand_name = TString::Format(farg.c_str(), (int) n);
1146 fnames.emplace_back(expand_name.Data());
1147 } else if (n > 0)
1148 fnames.emplace_back(""); // empty name is multiPdf
1149 else
1150 fnames.emplace_back(fname);
1151 }
1152 }
1153
1154 return fnames;
1155}
1156
1157
1158///////////////////////////////////////////////////////////////////////////////////////////////////
1159/// Produce image file(s) using JSON data as source
1160/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1161
1162bool RWebDisplayHandle::ProduceImages(const std::string &fname, const std::vector<std::string> &jsons, const std::vector<int> &widths, const std::vector<int> &heights, const char *batch_file)
1163{
1165}
1166
1167///////////////////////////////////////////////////////////////////////////////////////////////////
1168/// Produce image file(s) using JSON data as source
1169/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1170
1171bool RWebDisplayHandle::ProduceImages(const std::vector<std::string> &fnames, const std::vector<std::string> &jsons, const std::vector<int> &widths, const std::vector<int> &heights, const char *batch_file)
1172{
1173 if (fnames.empty() || jsons.empty())
1174 return false;
1175
1176 std::vector<std::string> fmts;
1177 for (auto& fname : fnames)
1178 fmts.emplace_back(GetImageFormat(fname));
1179
1180 bool is_any_image = false;
1181
1182 for (unsigned n = 0; (n < fmts.size()) && (n < jsons.size()); n++) {
1183 if (fmts[n] == "json") {
1184 std::ofstream ofs(fnames[n]);
1185 ofs << jsons[n];
1186 fmts[n].clear();
1187 } else if (!fmts[n].empty())
1188 is_any_image = true;
1189 }
1190
1191 if (!is_any_image)
1192 return true;
1193
1194 std::string fdebug;
1195 if (fnames.size() == 1)
1196 fdebug = fnames[0];
1197 else
1199
1200 const char *jsrootsys = gSystem->Getenv("JSROOTSYS");
1202 if (!jsrootsys) {
1203 jsrootsysdflt = TROOT::GetDataDir() + "/js";
1205 R__LOG_ERROR(WebGUILog()) << "Fail to locate JSROOT " << jsrootsysdflt;
1206 return false;
1207 }
1208 jsrootsys = jsrootsysdflt.Data();
1209 }
1210
1211 RWebDisplayArgs args; // set default browser kind, only Chrome/Firefox/Edge or CEF/Qt5/Qt6 can be used here
1212 if (!CheckIfCanProduceImages(args)) {
1213 R__LOG_ERROR(WebGUILog()) << "Fail to detect supported browsers for image production";
1214 return false;
1215 }
1216
1220
1221 std::vector<std::string> draw_kinds;
1222 bool use_browser_draw = false, can_optimize_json = false;
1223 int use_home_dir = 0;
1225
1226 // Some Chrome installation do not allow run html code from files, created in /tmp directory
1227 // When during session such failures happened, force usage of home directory from the beginning
1228 static int chrome_tmp_workaround = 0;
1229
1230 if (isChrome) {
1232 auto &h1 = FindCreator("chrome", "ChromeCreator");
1233 if (h1 && h1->IsActive() && h1->IsSnapBrowser() && (use_home_dir == 0))
1234 use_home_dir = 1;
1235 }
1236
1237 if (fmts[0] == "s.png") {
1238 if (!isChromeBased && !isFirefox) {
1239 R__LOG_ERROR(WebGUILog()) << "Direct png image creation supported only by Chrome and Firefox browsers";
1240 return false;
1241 }
1242 use_browser_draw = true;
1243 jsonkind = "1111"; // special mark in canv_batch.htm
1244 } else if (fmts[0] == "s.pdf") {
1245 if (!isChromeBased) {
1246 R__LOG_ERROR(WebGUILog()) << "Direct creation of PDF files supported only by Chrome-based browser";
1247 return false;
1248 }
1249 use_browser_draw = true;
1250 jsonkind = "2222"; // special mark in canv_batch.htm
1251 } else {
1252 draw_kinds = fmts;
1254 can_optimize_json = true;
1255 }
1256
1257 if (!batch_file || !*batch_file)
1258 batch_file = "/js/files/canv_batch.htm";
1259
1262 R__LOG_ERROR(WebGUILog()) << "Fail to find " << origin;
1263 return false;
1264 }
1265
1267 if (filecont.empty()) {
1268 R__LOG_ERROR(WebGUILog()) << "Fail to read content of " << origin;
1269 return false;
1270 }
1271
1272 int max_width = 0, max_height = 0, page_margin = 10;
1273 for (auto &w : widths)
1274 if (w > max_width)
1275 max_width = w;
1276 for (auto &h : heights)
1277 if (h > max_height)
1278 max_height = h;
1279
1282
1283 std::string mains, prev;
1284 for (auto &json : jsons) {
1285 mains.append(mains.empty() ? "[" : ", ");
1286 if (can_optimize_json && (json == prev)) {
1287 mains.append("'same'");
1288 } else {
1289 mains.append(json);
1290 prev = json;
1291 }
1292 }
1293 mains.append("]");
1294
1295 if (strstr(jsrootsys, "http://") || strstr(jsrootsys, "https://") || strstr(jsrootsys, "file://"))
1296 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), jsrootsys);
1297 else {
1298 static std::string jsroot_include = "<script id=\"jsroot\" src=\"$jsrootsys/build/jsroot.js\"></script>";
1299 auto p = filecont.find(jsroot_include);
1300 if (p != std::string::npos) {
1301 auto jsroot_build = THttpServer::ReadFileContent(std::string(jsrootsys) + "/build/jsroot.js");
1302 if (!jsroot_build.empty()) {
1303 // insert actual jsroot file location
1304 jsroot_build = std::regex_replace(jsroot_build, std::regex("'\\$jsrootsys'"), std::string("'file://") + jsrootsys + "/'");
1305 filecont.erase(p, jsroot_include.length());
1306 filecont.insert(p, "<script id=\"jsroot\">" + jsroot_build + "</script>");
1307 }
1308 }
1309
1310 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), "file://"s + jsrootsys);
1311 }
1312
1313 filecont = std::regex_replace(filecont, std::regex("\\$page_margin"), std::to_string(page_margin) + "px");
1314 filecont = std::regex_replace(filecont, std::regex("\\$page_width"), std::to_string(max_width + 2*page_margin) + "px");
1315 filecont = std::regex_replace(filecont, std::regex("\\$page_height"), std::to_string(max_height + 2*page_margin) + "px");
1316
1317 filecont = std::regex_replace(filecont, std::regex("\\$draw_kind"), jsonkind.Data());
1318 filecont = std::regex_replace(filecont, std::regex("\\$draw_widths"), jsonw.Data());
1319 filecont = std::regex_replace(filecont, std::regex("\\$draw_heights"), jsonh.Data());
1320 filecont = std::regex_replace(filecont, std::regex("\\$draw_objects"), mains);
1321
1323
1325 dump_name = "canvasdump";
1327 if (!df) {
1328 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for dump-dom";
1329 return false;
1330 }
1331 fputs("placeholder", df);
1332 fclose(df);
1333 }
1334
1335try_again:
1336
1338 args.SetUrl(""s);
1340
1341 html_name.Clear();
1342
1343 R__LOG_DEBUG(0, WebGUILog()) << "Using file content_len " << filecont.length() << " to produce batch images ";
1344
1345 } else {
1346 html_name = "canvasbody";
1348 if (!hf) {
1349 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for batch job";
1350 return false;
1351 }
1352 fputs(filecont.c_str(), hf);
1353 fclose(hf);
1354
1355 args.SetUrl("file://"s + gSystem->UnixPathName(html_name.Data()));
1356 args.SetPageContent(""s);
1357
1358 R__LOG_DEBUG(0, WebGUILog()) << "Using " << html_name << " content_len " << filecont.length() << " to produce batch images " << fdebug;
1359 }
1360
1362
1363 args.SetStandalone(true);
1364 args.SetHeadless(true);
1365 args.SetBatchMode(true);
1366 args.SetSize(widths[0], heights[0]);
1367
1368 if (use_browser_draw) {
1369
1370 tgtfilename = fnames[0].c_str();
1373
1375
1376 if (fmts[0] == "s.pdf")
1377 args.SetExtraArgs("--print-to-pdf-no-header --print-to-pdf="s + gSystem->UnixPathName(tgtfilename.Data()));
1378 else if (isFirefox) {
1379 args.SetExtraArgs("--screenshot"); // firefox does not let specify output image file
1380 wait_file_name = "screenshot.png";
1381 } else
1382 args.SetExtraArgs("--screenshot="s + gSystem->UnixPathName(tgtfilename.Data()));
1383
1384 // remove target image file - we use it as detection when chrome is ready
1385 gSystem->Unlink(tgtfilename.Data());
1386
1387 } else if (isFirefox) {
1388 // firefox will use window.dump to output produced result
1389 args.SetRedirectOutput(dump_name.Data());
1390 gSystem->Unlink(dump_name.Data());
1391 } else if (isChromeBased) {
1392 // chrome should have --dump-dom args configures
1393 args.SetRedirectOutput(dump_name.Data());
1394 gSystem->Unlink(dump_name.Data());
1395 }
1396
1397 auto handle = RWebDisplayHandle::Display(args);
1398
1399 // ensure file is created by browser draw
1400 if (use_browser_draw && handle) {
1401 Int_t batch_timeout = gEnv->GetValue("WebGui.BatchTimeout", 30) * 10;
1402 while (gSystem->AccessPathName(wait_file_name.Data()) && (--batch_timeout > 0)) {
1404 gSystem->Sleep(100);
1405 }
1406 }
1407
1408 // delete temporary HTML file
1409 if (html_name.Length() > 0) {
1410 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
1411 ::Info("ProduceImages", "Preserve batch file %s", html_name.Data());
1412 else
1413 gSystem->Unlink(html_name.Data());
1414 }
1415
1416 if (!handle) {
1417 R__LOG_DEBUG(0, WebGUILog()) << "Cannot start " << args.GetBrowserName() << " to produce image " << fdebug;
1418 return false;
1419 }
1420
1421 if (use_browser_draw) {
1422
1423 if (gSystem->AccessPathName(wait_file_name.Data())) {
1424 R__LOG_ERROR(WebGUILog()) << "Fail to produce image " << fdebug;
1425 return false;
1426 }
1427
1428 if (fmts[0] == "s.pdf")
1429 ::Info("ProduceImages", "PDF file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1430 else {
1431 if (isFirefox)
1432 gSystem->Rename("screenshot.png", fnames[0].c_str());
1433 ::Info("ProduceImages", "PNG file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1434 }
1435 } else {
1436 auto dumpcont = handle->GetContent();
1437
1438 if ((dumpcont.length() > 20) && (dumpcont.length() < 60) && (use_home_dir < 2) && isChrome) {
1439 // chrome creates dummy html file with mostly no content
1440 // problem running chrome from /tmp directory, lets try work from home directory
1441 R__LOG_INFO(WebGUILog()) << "Use home directory for running chrome in batch, set TMPDIR for preferable temp directory";
1443 goto try_again;
1444 }
1445
1446 if (dumpcont.length() < 100) {
1447 R__LOG_ERROR(WebGUILog()) << "Fail to dump HTML code into " << (dump_name.IsNull() ? "CEF" : dump_name.Data());
1448 return false;
1449 }
1450
1451 std::string::size_type p = 0;
1452
1453 for (unsigned n = 0; n < fmts.size(); n++) {
1454 if (fmts[n].empty())
1455 continue;
1456 if (fmts[n] == "svg") {
1457 auto p1 = dumpcont.find("<div><svg", p);
1458 auto p2 = dumpcont.find("</svg></div>", p1 + 8);
1459 p = p2 + 12;
1460 std::ofstream ofs(fnames[n]);
1461 if ((p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1462 if (p2 - p1 > 10) {
1463 ofs << dumpcont.substr(p1 + 5, p2 - p1 + 1);
1464 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) (p2 - p1 + 1));
1465 } else {
1466 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1467 }
1468 }
1469 } else {
1470 auto p0 = dumpcont.find("<img src=\"", p);
1471 auto p1 = dumpcont.find(";base64,", p0 + 8);
1472 auto p2 = dumpcont.find("\">", p1 + 8);
1473 p = p2 + 2;
1474
1475 if ((p0 != std::string::npos) && (p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1476 auto base64 = dumpcont.substr(p1+8, p2-p1-8);
1477 if ((base64 == "failure") || (base64.length() < 10)) {
1478 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1479 } else {
1480 auto binary = TBase64::Decode(base64.c_str());
1481 std::ofstream ofs(fnames[n], std::ios::binary);
1482 ofs.write(binary.Data(), binary.Length());
1483 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) binary.Length());
1484 }
1485 } else {
1486 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1487 return false;
1488 }
1489 }
1490 }
1491 }
1492
1493 R__LOG_DEBUG(0, WebGUILog()) << "Create " << (fnames.size() > 1 ? "files " : "file ") << fdebug;
1494
1495 return true;
1496}
1497
nlohmann::json json
#define R__LOG_ERROR(...)
Definition RLogger.hxx:357
#define R__LOG_DEBUG(DEBUGLEVEL,...)
Definition RLogger.hxx:360
#define R__LOG_INFO(...)
Definition RLogger.hxx:359
#define f(i)
Definition RSha256.hxx:104
#define c(i)
Definition RSha256.hxx:101
#define h(i)
Definition RSha256.hxx:106
static void DummyTimeOutHandler(int)
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
R__EXTERN TEnv * gEnv
Definition TEnv.h:170
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:241
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:208
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t width
Option_t Option_t style
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
char name[80]
Definition TGX11.cxx:110
@ kExecutePermission
Definition TSystem.h:53
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
const_iterator begin() const
const_iterator end() const
Specialized handle to hold information about running browser process Used to correctly cleanup all pr...
RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile, browser_process_id pid)
std::string fTmpDir
temporary directory to delete at the end
void RemoveStartupFiles() override
remove file which was used to startup widget - if possible
RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile, const std::string &dump)
std::string fTmpFile
temporary file to remove
Holds different arguments for starting browser with RWebDisplayHandle::Display() method.
std::string GetBrowserName() const
Returns configured browser name.
EBrowserKind GetBrowserKind() const
returns configured browser kind, see EBrowserKind for supported values
const std::string & GetRedirectOutput() const
get file name to which web browser output should be redirected
void SetStandalone(bool on=true)
Set standalone mode for running browser, default on When disabled, normal browser window (or just tab...
void SetBatchMode(bool on=true)
set batch mode
RWebDisplayArgs & SetSize(int w, int h)
set preferable web window width and height
RWebDisplayArgs & SetUrl(const std::string &url)
set window url
int GetWidth() const
returns preferable web window width
RWebDisplayArgs & SetPageContent(const std::string &cont)
set window url
int GetY() const
set preferable web window y position
std::string GetFullUrl() const
returns window url with append options
bool IsStandalone() const
Return true if browser should runs in standalone mode.
int GetHeight() const
returns preferable web window height
RWebDisplayArgs & SetBrowserKind(const std::string &kind)
Set browser kind as string argument.
std::string GetCustomExec() const
returns custom executable to start web browser
void SetExtraArgs(const std::string &args)
set extra command line arguments for starting web browser command
bool IsBatchMode() const
returns batch mode
bool IsHeadless() const
returns headless mode
@ kOn
web display enable, first try use embed displays like Qt or CEF, then native browsers and at the end ...
@ kFirefox
Mozilla Firefox browser.
@ kNative
either Chrome or Firefox - both support major functionality
@ kLocal
either CEF or Qt5 - both runs on local display without real http server
@ kServer
indicates that ROOT runs as server and just printouts window URL, browser should be started by the us...
@ kOff
disable web display, do not start any browser
@ kCEF
Chromium Embedded Framework - local display with CEF libs.
@ kSafari
Safari browser.
@ kQt6
Qt6 QWebEngine libraries - Chromium code packed in qt6.
@ kCustom
custom web browser, execution string should be provided
@ kChrome
Google Chrome browser.
@ kEdge
Microsoft Edge browser (Windows only)
void SetRedirectOutput(const std::string &fname="")
specify file name to which web browser output should be redirected
void SetHeadless(bool on=true)
set headless mode
const std::string & GetExtraArgs() const
get extra command line arguments for starting web browser command
int GetX() const
set preferable web window x position
bool IsLocalDisplay() const
returns true if local display like CEF or Qt5 QWebEngine should be used
std::string fBatchExec
batch execute line
std::string fHeadlessExec
headless execute line
static FILE * TemporaryFile(TString &name, int use_home_dir=0, const char *suffix=nullptr)
Create temporary file for web display Normally gSystem->TempFileName() method used to create file in ...
std::unique_ptr< RWebDisplayHandle > Display(const RWebDisplayArgs &args) override
Display given URL in web browser.
std::string fExec
standard execute line
void TestProg(const std::string &nexttry, bool check_std_paths=false)
Check if browser executable exists and can be used.
BrowserCreator(bool custom=true, const std::string &exec="")
Class to handle starting of web-browsers like Chrome or Firefox.
ChromeCreator(bool is_edge=false)
Constructor.
void ProcessGeometry(std::string &, const RWebDisplayArgs &) override
Replace $geometry placeholder with geometry settings Also RWebDisplayArgs::GetExtraArgs() are appende...
std::string MakeProfile(std::string &exec, bool) override
Handle profile argument.
std::string MakeProfile(std::string &exec, bool batch) override
Create Firefox profile to run independent browser window.
void ProcessGeometry(std::string &, const RWebDisplayArgs &) override
Process window geometry for Firefox.
bool IsActive() const override
Returns true if it can be used.
Handle of created web-based display Depending from type of web display, holds handle of started brows...
static std::map< std::string, std::unique_ptr< Creator > > & GetMap()
Static holder of registered creators of web displays.
static bool CheckIfCanProduceImages(RWebDisplayArgs &args)
Checks if configured browser can be used for image production.
static bool ProduceImages(const std::string &fname, const std::vector< std::string > &jsons, const std::vector< int > &widths, const std::vector< int > &heights, const char *batch_file=nullptr)
Produce image file(s) using JSON data as source Invokes JSROOT drawing functionality in headless brow...
static std::vector< std::string > ProduceImagesNames(const std::string &fname, unsigned nfiles=1)
Produce vector of file names for specified file pattern Depending from supported file forma.
static std::string GetImageFormat(const std::string &fname)
Detect image format There is special handling of ".screenshot.pdf" and ".screenshot....
void SetContent(const std::string &cont)
set content
static bool ProduceImage(const std::string &fname, const std::string &json, int width=800, int height=600, const char *batch_file=nullptr)
Produce image file using JSON data as source Invokes JSROOT drawing functionality in headless browser...
static bool CanProduceImages(const std::string &browser="")
Returns true if image production for specified browser kind is supported If browser not specified - u...
static bool NeedHttpServer(const RWebDisplayArgs &args)
Check if http server required for display.
static bool DisplayUrl(const std::string &url)
Display provided url in configured web browser.
static std::unique_ptr< RWebDisplayHandle > Display(const RWebDisplayArgs &args)
Create web display.
static std::unique_ptr< Creator > & FindCreator(const std::string &name, const std::string &libname="")
Search for specific browser creator If not found, try to add one.
static int GetBoolEnv(const std::string &name, int dfl=-1)
Parse boolean gEnv variable which should be "yes" or "no".
static TString Decode(const char *data)
Decode a base64 string date into a generic TString.
Definition TBase64.cxx:130
static TString ToJSON(const T *obj, Int_t compact=0, const char *member_name=nullptr)
Definition TBufferJSON.h:75
@ kNoSpaces
no new lines plus remove all spaces around "," and ":" symbols
Definition TBufferJSON.h:39
virtual Int_t GetValue(const char *name, Int_t dflt) const
Returns the integer value for a resource.
Definition TEnv.cxx:490
static char * ReadFileContent(const char *filename, Int_t &len)
Reads content of file from the disk.
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:543
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
Definition TROOT.cxx:3107
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3117
Random number generator class based on M.
Definition TRandom3.h:27
Basic string class.
Definition TString.h:138
TObjArray * Tokenize(const TString &delim) const
This function is used to isolate sequential tokens in a TString.
Definition TString.cxx:2270
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2384
virtual FILE * TempFileName(TString &base, const char *dir=nullptr, const char *suffix=nullptr)
Create a secure temporary file by appending a unique 6 letter string to base.
Definition TSystem.cxx:1510
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1285
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1676
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:916
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:651
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1868
virtual const char * PrependPathName(const char *dir, TString &name)
Concatenate a directory and a file name.
Definition TSystem.cxx:1092
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:1307
virtual std::string GetHomeDirectory(const char *userName=nullptr) const
Return the user's home directory.
Definition TSystem.cxx:905
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1073
virtual int Rename(const char *from, const char *to)
Rename a file.
Definition TSystem.cxx:1361
virtual TString GetFromPipe(const char *command, Int_t *ret=nullptr, Bool_t redirectStderr=kFALSE)
Execute command and return output in TString.
Definition TSystem.cxx:684
virtual Bool_t IsAbsoluteFileName(const char *dir)
Return true if dir is an absolute pathname.
Definition TSystem.cxx:961
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:435
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:881
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:414
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1392
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition TSystem.cxx:1493
const Int_t n
Definition legend1.C:16
TH1F * h1
Definition legend1.C:5
bool EndsWith(std::string_view string, std::string_view suffix)
ROOT::RLogChannel & WebGUILog()
Log channel for WebGUI diagnostics.
TCanvas * slash()
Definition slash.C:1
TMarker m
Definition textangle.C:8