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.dataSubmissionPolicyBypassNotification\", true);" << std::endl;
825 user_js << "user_pref(\"datareporting.policy.dataSubmissionPolicyAcceptedVersion\", 2);" << std::endl;
826 user_js << "user_pref(\"datareporting.policy.dataSubmissionPolicyNotifiedTime\", \"1635760572813\");" << std::endl;
827
828 // try to avoid any kind of dialogs on the start
829 user_js << "user_pref(\"app.update.auto\", false);" << std::endl;
830 user_js << "user_pref(\"browser.shell.checkDefaultBrowser\", false);" << std::endl;
831 user_js << "user_pref(\"browser.aboutwelcome.enabled\", false);" << std::endl;
832 user_js << "user_pref(\"browser.tabs.disableBackgroundLinkLoading\", true);" << std::endl;
833
834 // try to ensure that window closes with last tab
835 user_js << "user_pref(\"browser.tabs.closeWindowWithLastTab\", true);" << std::endl;
836 user_js << "user_pref(\"dom.allow_scripts_to_close_windows\", true);" << std::endl;
837 user_js << "user_pref(\"browser.sessionstore.resume_from_crash\", false);" << std::endl;
838
839 if (batch_mode) {
840 // allow to dump messages to std output
841 user_js << "user_pref(\"browser.dom.window.dump.enabled\", true);" << std::endl;
842 } else {
843 // to suppress annoying privacy tab
844 user_js << "user_pref(\"datareporting.policy.firstRunURL\", \"\");" << std::endl;
845 // to use custom userChrome.css files
846 user_js << "user_pref(\"toolkit.legacyUserProfileCustomizations.stylesheets\", true);" << std::endl;
847 // do not put tabs in title
848 user_js << "user_pref(\"browser.tabs.inTitlebar\", 0);" << std::endl;
849
850#ifdef R__LINUX
851 // fix WebGL creation problem on some Linux platforms
852 user_js << "user_pref(\"webgl.out-of-process\", false);" << std::endl;
853#endif
854
855 std::ofstream times_json(profile_dir + "/times.json", std::ios::trunc);
856 times_json << "{" << std::endl;
857 times_json << " \"created\": 1699968480952," << std::endl;
858 times_json << " \"firstUse\": null" << std::endl;
859 times_json << "}" << std::endl;
860 if (gSystem->mkdir((profile_dir + "/chrome").c_str()) == 0) {
861 std::ofstream style(profile_dir + "/chrome/userChrome.css", std::ios::trunc);
862 // do not show tabs
863 style << "#TabsToolbar { visibility: collapse; }" << std::endl;
864 // do not show URL
865 style << "#nav-bar, #urlbar-container, #searchbar { visibility: collapse !important; }" << std::endl;
866 }
867 }
868
869 } else {
870 R__LOG_ERROR(WebGUILog()) << "Cannot create Firefox profile directory " << profile_dir;
871 }
872 }
873
874 exec = std::regex_replace(exec, std::regex("\\$profile"), profile_arg);
875
876 if (exec.find("$cleanup_profile") != std::string::npos) {
877 if (rmdir.empty()) rmdir = "__dummy__";
878 exec = std::regex_replace(exec, std::regex("\\$cleanup_profile"), rmdir);
879 rmdir.clear(); // no need to delete directory - it will be removed by script
880 }
881
882 return rmdir;
883}
884
885///////////////////////////////////////////////////////////////////////////////////////////////////
886/// Check if http server required for display
887/// \param args - defines where and how to display web window
888
890{
893 return false;
894
895 if (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)) {
896
897#ifdef WITH_QT6WEB
898 auto &qt6 = FindCreator("qt6", "libROOTQt6WebDisplay");
899 if (qt6 && qt6->IsActive())
900 return false;
901#endif
902#ifdef WITH_CEFWEB
903 auto &cef = FindCreator("cef", "libROOTCefDisplay");
904 if (cef && cef->IsActive())
905 return false;
906#endif
907 }
908
909 return true;
910}
911
912
913///////////////////////////////////////////////////////////////////////////////////////////////////
914/// Create web display
915/// \param args - defines where and how to display web window
916/// Returns RWebDisplayHandle, which holds information of running browser application
917/// Can be used fully independent from RWebWindow classes just to show any web page
918
919std::unique_ptr<RWebDisplayHandle> RWebDisplayHandle::Display(const RWebDisplayArgs &args)
920{
921 std::unique_ptr<RWebDisplayHandle> handle;
922
924 return handle;
925
926 auto try_creator = [&](std::unique_ptr<Creator> &creator) {
927 if (!creator || !creator->IsActive())
928 return false;
929 handle = creator->Display(args);
930 return handle ? true : false;
931 };
932
934 (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)),
935 has_qt6web = false, has_cefweb = false;
936
937#ifdef WITH_QT6WEB
938 has_qt6web = true;
939#endif
940
941#ifdef WITH_CEFWEB
942 has_cefweb = true;
943#endif
944
946 if (try_creator(FindCreator("qt6", "libROOTQt6WebDisplay")))
947 return handle;
948 }
949
951 if (try_creator(FindCreator("cef", "libROOTCefDisplay")))
952 return handle;
953 }
954
955 if (args.IsLocalDisplay()) {
956 R__LOG_ERROR(WebGUILog()) << "Neither Qt5/6 nor CEF libraries were found to provide local display";
957 return handle;
958 }
959
960 bool handleAsNative =
962
964 if (try_creator(FindCreator("chrome", "ChromeCreator")))
965 return handle;
966 }
967
969 if (try_creator(FindCreator("firefox", "FirefoxCreator")))
970 return handle;
971 }
972
973#ifdef _MSC_VER
974 // Edge browser cannot be run headless without registry change, therefore do not try it by default
975 if ((handleAsNative && !args.IsHeadless() && !args.IsBatchMode()) || (args.GetBrowserKind() == RWebDisplayArgs::kEdge)) {
976 if (try_creator(FindCreator("edge", "ChromeCreator")))
977 return handle;
978 }
979#endif
980
983 // R__LOG_ERROR(WebGUILog()) << "Neither Chrome nor Firefox browser cannot be started to provide display";
984 return handle;
985 }
986
988 if (try_creator(FindCreator("safari", "SafariCreator")))
989 return handle;
990 }
991
993 std::unique_ptr<Creator> creator = std::make_unique<BrowserCreator>(false, args.GetCustomExec());
994 try_creator(creator);
995 } else {
996 try_creator(FindCreator("browser", "BrowserCreator"));
997 }
998
999 return handle;
1000}
1001
1002///////////////////////////////////////////////////////////////////////////////////////////////////
1003/// Display provided url in configured web browser
1004/// \param url - specified URL address like https://root.cern
1005/// Browser can specified when starting `root --web=firefox`
1006/// Returns true when browser started
1007/// It is convenience method, equivalent to:
1008/// ~~~
1009/// RWebDisplayArgs args;
1010/// args.SetUrl(url);
1011/// args.SetStandalone(false);
1012/// auto handle = RWebDisplayHandle::Display(args);
1013/// ~~~
1014
1015bool RWebDisplayHandle::DisplayUrl(const std::string &url)
1016{
1017 RWebDisplayArgs args;
1018 args.SetUrl(url);
1019 args.SetStandalone(false);
1020
1021 auto handle = Display(args);
1022
1023 return !!handle;
1024}
1025
1026///////////////////////////////////////////////////////////////////////////////////////////////////
1027/// Checks if configured browser can be used for image production
1028
1030{
1034 bool detected = false;
1035
1036 auto &h1 = FindCreator("chrome", "ChromeCreator");
1037 if (h1 && h1->IsActive()) {
1039 detected = true;
1040 }
1041
1042 if (!detected) {
1043 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1044 if (h2 && h2->IsActive()) {
1046 detected = true;
1047 }
1048 }
1049
1050 return detected;
1051 }
1052
1054 auto &h1 = FindCreator("chrome", "ChromeCreator");
1055 return h1 && h1->IsActive();
1056 }
1057
1059 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1060 return h2 && h2->IsActive();
1061 }
1062
1063#ifdef _MSC_VER
1064 if (args.GetBrowserKind() == RWebDisplayArgs::kEdge) {
1065 auto &h3 = FindCreator("edge", "ChromeCreator");
1066 return h3 && h3->IsActive();
1067 }
1068#endif
1069
1070 return true;
1071}
1072
1073///////////////////////////////////////////////////////////////////////////////////////////////////
1074/// Returns true if image production for specified browser kind is supported
1075/// If browser not specified - use currently configured browser or try to test existing web browsers
1076
1078{
1080
1081 return CheckIfCanProduceImages(args);
1082}
1083
1084///////////////////////////////////////////////////////////////////////////////////////////////////
1085/// Detect image format
1086/// There is special handling of ".screenshot.pdf" and ".screenshot.png" extensions
1087/// Creation of such files relies on headless browser functionality and fully supported only by Chrome browser
1088
1089std::string RWebDisplayHandle::GetImageFormat(const std::string &fname)
1090{
1091 std::string _fname = fname;
1092 std::transform(_fname.begin(), _fname.end(), _fname.begin(), ::tolower);
1093 auto EndsWith = [&_fname](const std::string &suffix) {
1094 return (_fname.length() > suffix.length()) ? (0 == _fname.compare(_fname.length() - suffix.length(), suffix.length(), suffix)) : false;
1095 };
1096
1097 if (EndsWith(".screenshot.pdf"))
1098 return "s.pdf"s;
1099 if (EndsWith(".pdf"))
1100 return "pdf"s;
1101 if (EndsWith(".json"))
1102 return "json"s;
1103 if (EndsWith(".svg"))
1104 return "svg"s;
1105 if (EndsWith(".screenshot.png"))
1106 return "s.png"s;
1107 if (EndsWith(".png"))
1108 return "png"s;
1109 if (EndsWith(".jpg") || EndsWith(".jpeg"))
1110 return "jpeg"s;
1111 if (EndsWith(".webp"))
1112 return "webp"s;
1113
1114 return ""s;
1115}
1116
1117
1118///////////////////////////////////////////////////////////////////////////////////////////////////
1119/// Produce image file using JSON data as source
1120/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1121
1122bool RWebDisplayHandle::ProduceImage(const std::string &fname, const std::string &json, int width, int height, const char *batch_file)
1123{
1124 return ProduceImages(fname, {json}, {width}, {height}, batch_file);
1125}
1126
1127
1128///////////////////////////////////////////////////////////////////////////////////////////////////
1129/// Produce vector of file names for specified file pattern
1130/// Depending from supported file forma
1131
1132std::vector<std::string> RWebDisplayHandle::ProduceImagesNames(const std::string &fname, unsigned nfiles)
1133{
1134 auto fmt = GetImageFormat(fname);
1135
1136 std::vector<std::string> fnames;
1137
1138 if ((fmt == "s.pdf") || (fmt == "s.png")) {
1139 fnames.emplace_back(fname);
1140 } else {
1141 std::string farg = fname;
1142
1143 bool has_quialifier = farg.find("%") != std::string::npos;
1144
1145 if (!has_quialifier && (nfiles > 1) && (fmt != "pdf")) {
1146 farg.insert(farg.rfind("."), "%d");
1147 has_quialifier = true;
1148 }
1149
1150 for (unsigned n = 0; n < nfiles; n++) {
1151 if(has_quialifier) {
1152 auto expand_name = TString::Format(farg.c_str(), (int) n);
1153 fnames.emplace_back(expand_name.Data());
1154 } else if (n > 0)
1155 fnames.emplace_back(""); // empty name is multiPdf
1156 else
1157 fnames.emplace_back(fname);
1158 }
1159 }
1160
1161 return fnames;
1162}
1163
1164
1165///////////////////////////////////////////////////////////////////////////////////////////////////
1166/// Produce image file(s) using JSON data as source
1167/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1168
1169bool 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)
1170{
1172}
1173
1174///////////////////////////////////////////////////////////////////////////////////////////////////
1175/// Produce image file(s) using JSON data as source
1176/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1177
1178bool 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)
1179{
1180 if (fnames.empty() || jsons.empty())
1181 return false;
1182
1183 std::vector<std::string> fmts;
1184 for (auto& fname : fnames)
1185 fmts.emplace_back(GetImageFormat(fname));
1186
1187 bool is_any_image = false;
1188
1189 for (unsigned n = 0; (n < fmts.size()) && (n < jsons.size()); n++) {
1190 if (fmts[n] == "json") {
1191 std::ofstream ofs(fnames[n]);
1192 ofs << jsons[n];
1193 fmts[n].clear();
1194 } else if (!fmts[n].empty())
1195 is_any_image = true;
1196 }
1197
1198 if (!is_any_image)
1199 return true;
1200
1201 std::string fdebug;
1202 if (fnames.size() == 1)
1203 fdebug = fnames[0];
1204 else
1206
1207 const char *jsrootsys = gSystem->Getenv("JSROOTSYS");
1209 if (!jsrootsys) {
1210 jsrootsysdflt = TROOT::GetDataDir() + "/js";
1212 R__LOG_ERROR(WebGUILog()) << "Fail to locate JSROOT " << jsrootsysdflt;
1213 return false;
1214 }
1215 jsrootsys = jsrootsysdflt.Data();
1216 }
1217
1218 RWebDisplayArgs args; // set default browser kind, only Chrome/Firefox/Edge or CEF/Qt5/Qt6 can be used here
1219 if (!CheckIfCanProduceImages(args)) {
1220 R__LOG_ERROR(WebGUILog()) << "Fail to detect supported browsers for image production";
1221 return false;
1222 }
1223
1227
1228 std::vector<std::string> draw_kinds;
1229 bool use_browser_draw = false, can_optimize_json = false;
1230 int use_home_dir = 0;
1232
1233 // Some Chrome installation do not allow run html code from files, created in /tmp directory
1234 // When during session such failures happened, force usage of home directory from the beginning
1235 static int chrome_tmp_workaround = 0;
1236
1237 if (isChrome) {
1239 auto &h1 = FindCreator("chrome", "ChromeCreator");
1240 if (h1 && h1->IsActive() && h1->IsSnapBrowser() && (use_home_dir == 0))
1241 use_home_dir = 1;
1242 }
1243
1244 if (fmts[0] == "s.png") {
1245 if (!isChromeBased && !isFirefox) {
1246 R__LOG_ERROR(WebGUILog()) << "Direct png image creation supported only by Chrome and Firefox browsers";
1247 return false;
1248 }
1249 use_browser_draw = true;
1250 jsonkind = "1111"; // special mark in canv_batch.htm
1251 } else if (fmts[0] == "s.pdf") {
1252 if (!isChromeBased) {
1253 R__LOG_ERROR(WebGUILog()) << "Direct creation of PDF files supported only by Chrome-based browser";
1254 return false;
1255 }
1256 use_browser_draw = true;
1257 jsonkind = "2222"; // special mark in canv_batch.htm
1258 } else {
1259 draw_kinds = fmts;
1261 can_optimize_json = true;
1262 }
1263
1264 if (!batch_file || !*batch_file)
1265 batch_file = "/js/files/canv_batch.htm";
1266
1269 R__LOG_ERROR(WebGUILog()) << "Fail to find " << origin;
1270 return false;
1271 }
1272
1274 if (filecont.empty()) {
1275 R__LOG_ERROR(WebGUILog()) << "Fail to read content of " << origin;
1276 return false;
1277 }
1278
1279 int max_width = 0, max_height = 0, page_margin = 10;
1280 for (auto &w : widths)
1281 if (w > max_width)
1282 max_width = w;
1283 for (auto &h : heights)
1284 if (h > max_height)
1285 max_height = h;
1286
1289
1290 std::string mains, prev;
1291 for (auto &json : jsons) {
1292 mains.append(mains.empty() ? "[" : ", ");
1293 if (can_optimize_json && (json == prev)) {
1294 mains.append("'same'");
1295 } else {
1296 mains.append(json);
1297 prev = json;
1298 }
1299 }
1300 mains.append("]");
1301
1302 if (strstr(jsrootsys, "http://") || strstr(jsrootsys, "https://") || strstr(jsrootsys, "file://"))
1303 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), jsrootsys);
1304 else {
1305 static std::string jsroot_include = "<script id=\"jsroot\" src=\"$jsrootsys/build/jsroot.js\"></script>";
1306 auto p = filecont.find(jsroot_include);
1307 if (p != std::string::npos) {
1308 auto jsroot_build = THttpServer::ReadFileContent(std::string(jsrootsys) + "/build/jsroot.js");
1309 if (!jsroot_build.empty()) {
1310 // insert actual jsroot file location
1311 jsroot_build = std::regex_replace(jsroot_build, std::regex("'\\$jsrootsys'"), std::string("'file://") + jsrootsys + "/'");
1312 filecont.erase(p, jsroot_include.length());
1313 filecont.insert(p, "<script id=\"jsroot\">" + jsroot_build + "</script>");
1314 }
1315 }
1316
1317 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), "file://"s + jsrootsys);
1318 }
1319
1320 filecont = std::regex_replace(filecont, std::regex("\\$page_margin"), std::to_string(page_margin) + "px");
1321 filecont = std::regex_replace(filecont, std::regex("\\$page_width"), std::to_string(max_width + 2*page_margin) + "px");
1322 filecont = std::regex_replace(filecont, std::regex("\\$page_height"), std::to_string(max_height + 2*page_margin) + "px");
1323
1324 filecont = std::regex_replace(filecont, std::regex("\\$draw_kind"), jsonkind.Data());
1325 filecont = std::regex_replace(filecont, std::regex("\\$draw_widths"), jsonw.Data());
1326 filecont = std::regex_replace(filecont, std::regex("\\$draw_heights"), jsonh.Data());
1327 filecont = std::regex_replace(filecont, std::regex("\\$draw_objects"), mains);
1328
1330
1332 dump_name = "canvasdump";
1334 if (!df) {
1335 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for dump-dom";
1336 return false;
1337 }
1338 fputs("placeholder", df);
1339 fclose(df);
1340 }
1341
1342try_again:
1343
1345 args.SetUrl(""s);
1347
1348 html_name.Clear();
1349
1350 R__LOG_DEBUG(0, WebGUILog()) << "Using file content_len " << filecont.length() << " to produce batch images ";
1351
1352 } else {
1353 html_name = "canvasbody";
1355 if (!hf) {
1356 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for batch job";
1357 return false;
1358 }
1359 fputs(filecont.c_str(), hf);
1360 fclose(hf);
1361
1362 args.SetUrl("file://"s + gSystem->UnixPathName(html_name.Data()));
1363 args.SetPageContent(""s);
1364
1365 R__LOG_DEBUG(0, WebGUILog()) << "Using " << html_name << " content_len " << filecont.length() << " to produce batch images " << fdebug;
1366 }
1367
1369
1370 args.SetStandalone(true);
1371 args.SetHeadless(true);
1372 args.SetBatchMode(true);
1373 args.SetSize(widths[0], heights[0]);
1374
1375 if (use_browser_draw) {
1376
1377 tgtfilename = fnames[0].c_str();
1380
1382
1383 if (fmts[0] == "s.pdf")
1384 args.SetExtraArgs("--print-to-pdf-no-header --print-to-pdf="s + gSystem->UnixPathName(tgtfilename.Data()));
1385 else if (isFirefox) {
1386 args.SetExtraArgs("--screenshot"); // firefox does not let specify output image file
1387 wait_file_name = "screenshot.png";
1388 } else
1389 args.SetExtraArgs("--screenshot="s + gSystem->UnixPathName(tgtfilename.Data()));
1390
1391 // remove target image file - we use it as detection when chrome is ready
1392 gSystem->Unlink(tgtfilename.Data());
1393
1394 } else if (isFirefox) {
1395 // firefox will use window.dump to output produced result
1396 args.SetRedirectOutput(dump_name.Data());
1397 gSystem->Unlink(dump_name.Data());
1398 } else if (isChromeBased) {
1399 // chrome should have --dump-dom args configures
1400 args.SetRedirectOutput(dump_name.Data());
1401 gSystem->Unlink(dump_name.Data());
1402 }
1403
1404 auto handle = RWebDisplayHandle::Display(args);
1405
1406 // ensure file is created by browser draw
1407 if (use_browser_draw && handle) {
1408 Int_t batch_timeout = gEnv->GetValue("WebGui.BatchTimeout", 30) * 10;
1409 while (gSystem->AccessPathName(wait_file_name.Data()) && (--batch_timeout > 0)) {
1411 gSystem->Sleep(100);
1412 }
1413 }
1414
1415 // delete temporary HTML file
1416 if (html_name.Length() > 0) {
1417 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
1418 ::Info("ProduceImages", "Preserve batch file %s", html_name.Data());
1419 else
1420 gSystem->Unlink(html_name.Data());
1421 }
1422
1423 if (!handle) {
1424 R__LOG_DEBUG(0, WebGUILog()) << "Cannot start " << args.GetBrowserName() << " to produce image " << fdebug;
1425 return false;
1426 }
1427
1428 if (use_browser_draw) {
1429
1430 if (gSystem->AccessPathName(wait_file_name.Data())) {
1431 R__LOG_ERROR(WebGUILog()) << "Fail to produce image " << fdebug;
1432 return false;
1433 }
1434
1435 if (fmts[0] == "s.pdf")
1436 ::Info("ProduceImages", "PDF file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1437 else {
1438 if (isFirefox)
1439 gSystem->Rename("screenshot.png", fnames[0].c_str());
1440 ::Info("ProduceImages", "PNG file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1441 }
1442 } else {
1443 auto dumpcont = handle->GetContent();
1444
1445 if ((dumpcont.length() > 20) && (dumpcont.length() < 60) && (use_home_dir < 2) && isChrome) {
1446 // chrome creates dummy html file with mostly no content
1447 // problem running chrome from /tmp directory, lets try work from home directory
1448 R__LOG_INFO(WebGUILog()) << "Use home directory for running chrome in batch, set TMPDIR for preferable temp directory";
1450 goto try_again;
1451 }
1452
1453 if (dumpcont.length() < 100) {
1454 R__LOG_ERROR(WebGUILog()) << "Fail to dump HTML code into " << (dump_name.IsNull() ? "CEF" : dump_name.Data());
1455 return false;
1456 }
1457
1458 std::string::size_type p = 0;
1459
1460 for (unsigned n = 0; n < fmts.size(); n++) {
1461 if (fmts[n].empty())
1462 continue;
1463 if (fmts[n] == "svg") {
1464 auto p1 = dumpcont.find("<div><svg", p);
1465 auto p2 = dumpcont.find("</svg></div>", p1 + 8);
1466 p = p2 + 12;
1467 std::ofstream ofs(fnames[n]);
1468 if ((p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1469 if (p2 - p1 > 10) {
1470 ofs << dumpcont.substr(p1 + 5, p2 - p1 + 1);
1471 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) (p2 - p1 + 1));
1472 } else {
1473 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1474 }
1475 }
1476 } else {
1477 auto p0 = dumpcont.find("<img src=\"", p);
1478 auto p1 = dumpcont.find(";base64,", p0 + 8);
1479 auto p2 = dumpcont.find("\">", p1 + 8);
1480 p = p2 + 2;
1481
1482 if ((p0 != std::string::npos) && (p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1483 auto base64 = dumpcont.substr(p1+8, p2-p1-8);
1484 if ((base64 == "failure") || (base64.length() < 10)) {
1485 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1486 } else {
1487 auto binary = TBase64::Decode(base64.c_str());
1488 std::ofstream ofs(fnames[n], std::ios::binary);
1489 ofs.write(binary.Data(), binary.Length());
1490 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) binary.Length());
1491 }
1492 } else {
1493 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1494 return false;
1495 }
1496 }
1497 }
1498 }
1499
1500 R__LOG_DEBUG(0, WebGUILog()) << "Create " << (fnames.size() > 1 ? "files " : "file ") << fdebug;
1501
1502 return true;
1503}
1504
nlohmann::json json
#define R__LOG_ERROR(...)
Definition RLogger.hxx:356
#define R__LOG_DEBUG(DEBUGLEVEL,...)
Definition RLogger.hxx:359
#define R__LOG_INFO(...)
Definition RLogger.hxx:358
#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:126
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:148
@ kExecutePermission
Definition TSystem.h:53
R__EXTERN TSystem * gSystem
Definition TSystem.h:582
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:77
@ 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:511
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:549
static const TString & GetEtcDir()
Get the sysconfig directory in the installation. Static utility function.
Definition TROOT.cxx:3381
static const TString & GetDataDir()
Get the data directory in the installation. Static utility function.
Definition TROOT.cxx:3391
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:2344
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:2459
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:1514
virtual Bool_t ExpandPathName(TString &path)
Expand a pathname getting rid of special shell characters like ~.
Definition TSystem.cxx:1289
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1680
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition TSystem.cxx:920
virtual Int_t Exec(const char *shellcmd)
Execute a command.
Definition TSystem.cxx:655
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1872
virtual const char * PrependPathName(const char *dir, TString &name)
Concatenate a directory and a file name.
Definition TSystem.cxx:1096
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:1311
virtual std::string GetHomeDirectory(const char *userName=nullptr) const
Return the user's home directory.
Definition TSystem.cxx:909
virtual const char * UnixPathName(const char *unixpathname)
Convert from a local pathname to a Unix pathname.
Definition TSystem.cxx:1077
virtual int Rename(const char *from, const char *to)
Rename a file.
Definition TSystem.cxx:1365
virtual TString GetFromPipe(const char *command, Int_t *ret=nullptr, Bool_t redirectStderr=kFALSE)
Execute command and return output in TString.
Definition TSystem.cxx:688
virtual Bool_t IsAbsoluteFileName(const char *dir)
Return true if dir is an absolute pathname.
Definition TSystem.cxx:965
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:439
virtual const char * WorkingDirectory()
Return working directory.
Definition TSystem.cxx:885
virtual Bool_t ProcessEvents()
Process pending events (GUI, timers, sockets).
Definition TSystem.cxx:418
virtual int Unlink(const char *name)
Unlink, i.e.
Definition TSystem.cxx:1396
virtual const char * TempDirectory() const
Return a user configured or systemwide directory to create temporary files in.
Definition TSystem.cxx:1497
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