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
4/*************************************************************************
5 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
13
14#include <ROOT/RLogger.hxx>
15
16#include "RConfigure.h"
17#include "TSystem.h"
18#include "TRandom3.h"
19#include "TString.h"
20#include "TObjArray.h"
21#include "THttpServer.h"
22#include "TEnv.h"
23#include "TError.h"
24#include "TROOT.h"
25#include "TBase64.h"
26#include "TBufferJSON.h"
28
29#include <fstream>
30#include <iostream>
31#include <filesystem>
32#include <memory>
33#include <regex>
34
35#ifdef _MSC_VER
36#include <process.h>
37#else
38#include <unistd.h>
39#include <cstdlib>
40#include <csignal>
41#include <spawn.h>
42#ifdef R__MACOSX
43#include <sys/wait.h>
44#include <crt_externs.h>
45#elif defined(__FreeBSD__)
46#include <sys/wait.h>
47#include <dlfcn.h>
48#else
49#include <wait.h>
50#endif
51#endif
52
53using namespace ROOT;
54using namespace std::string_literals;
55
56/** \class ROOT::RWebDisplayHandle
57\ingroup webdisplay
58
59Handle of created web-based display
60Depending from type of web display, holds handle of started browser process or other display-specific information
61to correctly stop and cleanup display.
62*/
63
64
65//////////////////////////////////////////////////////////////////////////////////////////////////
66/// Static holder of registered creators of web displays
67
68std::map<std::string, std::unique_ptr<RWebDisplayHandle::Creator>> &RWebDisplayHandle::GetMap()
69{
70 static std::map<std::string, std::unique_ptr<RWebDisplayHandle::Creator>> sMap;
71 return sMap;
72}
73
74//////////////////////////////////////////////////////////////////////////////////////////////////
75/// Search for specific browser creator
76/// If not found, try to add one
77/// \param name - creator name like ChromeCreator
78/// \param libname - shared library name where creator could be provided
79
80std::unique_ptr<RWebDisplayHandle::Creator> &RWebDisplayHandle::FindCreator(const std::string &name, const std::string &libname)
81{
82 auto &m = GetMap();
83 auto search = m.find(name);
84 if (search == m.end()) {
85
86 if (libname == "ChromeCreator") {
87 m.emplace(name, std::make_unique<ChromeCreator>(name == "edge"));
88 } else if (libname == "FirefoxCreator") {
89 m.emplace(name, std::make_unique<FirefoxCreator>());
90 } else if (libname == "SafariCreator") {
91 m.emplace(name, std::make_unique<SafariCreator>());
92 } else if (libname == "BrowserCreator") {
93 m.emplace(name, std::make_unique<BrowserCreator>(false));
94 } else if (!libname.empty()) {
95 gSystem->Load(libname.c_str());
96 }
97
98 search = m.find(name); // try again
99 }
100
101 if (search != m.end())
102 return search->second;
103
104 static std::unique_ptr<RWebDisplayHandle::Creator> dummy;
105 return dummy;
106}
107
108namespace ROOT {
109
110//////////////////////////////////////////////////////////////////////////////////////////////////
111/// Specialized handle to hold information about running browser process
112/// Used to correctly cleanup all processes and temporary directories
113
115
116#ifdef _MSC_VER
117 typedef int browser_process_id;
118#else
119 typedef pid_t browser_process_id;
120#endif
121 std::string fTmpDir; ///< temporary directory to delete at the end
122 std::string fTmpFile; ///< temporary file to remove
123 bool fHasPid{false};
125
126public:
127 RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile,
128 const std::string &dump)
130 {
131 SetContent(dump);
132 }
133
134 RWebBrowserHandle(const std::string &url, const std::string &tmpdir, const std::string &tmpfile,
137 {
138 }
139
141 {
142#ifdef _MSC_VER
143 if (fHasPid)
144 gSystem->Exec(("taskkill /F /PID " + std::to_string(fPid) + " >NUL 2>NUL").c_str());
145 std::string rmdir = "rmdir /S /Q ";
146#else
147 if (fHasPid)
148 kill(fPid, SIGKILL);
149 std::string rmdir = "rm -rf ";
150#endif
151 if (!fTmpDir.empty())
152 gSystem->Exec((rmdir + fTmpDir).c_str());
154 }
155
156 void RemoveStartupFiles() override
157 {
158#ifdef _MSC_VER
159 std::string rmfile = "del /F ";
160#else
161 std::string rmfile = "rm -f ";
162#endif
163 if (!fTmpFile.empty()) {
164 gSystem->Exec((rmfile + fTmpFile).c_str());
165 fTmpFile.clear();
166 }
167 }
168};
169
170} // namespace ROOT
171
172//////////////////////////////////////////////////////////////////////////////////////////////////
173/// Class to handle starting of web-browsers like Chrome or Firefox
174
176{
177 if (custom) return;
178
179 if (!exec.empty()) {
180 if (exec.find("$url") == std::string::npos) {
181 fProg = exec;
182#ifdef _MSC_VER
183 fExec = exec + " $url";
184#else
185 fExec = exec + " $url &";
186#endif
187 } else {
188 fExec = exec;
189 auto pos = exec.find(" ");
190 if (pos != std::string::npos)
191 fProg = exec.substr(0, pos);
192 }
193 } else if (gSystem->InheritsFrom("TMacOSXSystem")) {
194 fExec = "open \'$url\'";
195 } else if (gSystem->InheritsFrom("TWinNTSystem")) {
196 fExec = "start $url";
197 } else {
198 fExec = "xdg-open \'$url\' &";
199 }
200}
201
202//////////////////////////////////////////////////////////////////////////////////////////////////
203/// Check if browser executable exists and can be used
204
206{
207 if (nexttry.empty() || !fProg.empty())
208 return;
209
211#ifdef R__MACOSX
212 fProg = std::regex_replace(nexttry, std::regex("%20"), " ");
213#else
214 fProg = nexttry;
215#endif
216 return;
217 }
218
219 if (!check_std_paths)
220 return;
221
222#ifdef _MSC_VER
223 std::string ProgramFiles = gSystem->Getenv("ProgramFiles");
224 auto pos = ProgramFiles.find(" (x86)");
225 if (pos != std::string::npos)
226 ProgramFiles.erase(pos, 6);
227 std::string ProgramFilesx86 = gSystem->Getenv("ProgramFiles(x86)");
228
229 if (!ProgramFiles.empty())
230 TestProg(ProgramFiles + nexttry, false);
231 if (!ProgramFilesx86.empty())
232 TestProg(ProgramFilesx86 + nexttry, false);
233#endif
234}
235
236//////////////////////////////////////////////////////////////////////////////////////////////////
237/// Create temporary file for web display
238/// Normally gSystem->TempFileName() method used to create file in default temporary directory
239/// For snap chromium use of default temp directory is not always possible therefore one switches to home directory
240/// But one checks if default temp directory modified and already points to /home folder
241
243{
244 std::string dirname;
245 if (use_home_dir > 0) {
246 if (use_home_dir == 1) {
247 const char *tmp_dir = gSystem->TempDirectory();
248 if (tmp_dir && (strncmp(tmp_dir, "/home", 5) == 0))
249 use_home_dir = 0;
250 else if (!tmp_dir || (strncmp(tmp_dir, "/tmp", 4) == 0))
251 use_home_dir = 2;
252 }
253
254 if (use_home_dir > 1)
256 }
257 return gSystem->TempFileName(name, use_home_dir > 1 ? dirname.c_str() : nullptr, suffix);
258}
259
260static void DummyTimeOutHandler(int /* Sig */) {}
261
262
263//////////////////////////////////////////////////////////////////////////////////////////////////
264/// Display given URL in web browser
265/// \note See more details related to webdisplay on RWebWindowsManager::ShowWindow
266
267std::unique_ptr<RWebDisplayHandle>
269{
270 std::string url = args.GetFullUrl();
271 if (url.empty())
272 return nullptr;
273
275 std::cout << "New web window: " << url << std::endl;
276 return std::make_unique<RWebBrowserHandle>(url, "", "", "");
277 }
278
279 std::string exec;
280 if (args.IsBatchMode())
281 exec = fBatchExec;
282 else if (args.IsHeadless())
283 exec = fHeadlessExec;
284 else if (args.IsStandalone())
285 exec = fExec;
286 else
287 exec = "$prog $url &";
288
289 if (exec.empty())
290 return nullptr;
291
292 std::string swidth = std::to_string(args.GetWidth() > 0 ? args.GetWidth() : 800),
293 sheight = std::to_string(args.GetHeight() > 0 ? args.GetHeight() : 600),
294 sposx = std::to_string(args.GetX() >= 0 ? args.GetX() : 0),
295 sposy = std::to_string(args.GetY() >= 0 ? args.GetY() : 0);
296
297 ProcessGeometry(exec, args);
298
299 std::string extra = args.GetExtraArgs();
300 if (!extra.empty()) {
301 auto p = exec.find("$url");
302 if (p != std::string::npos)
303 exec.insert(p, extra + " ");
304 }
305
306 std::string rmdir = MakeProfile(exec, args.IsBatchMode() || args.IsHeadless());
307
308 std::string tmpfile;
309
310 // these are secret parameters, hide them in temp file
311 if (((url.find("token=") != std::string::npos) || (url.find("key=") != std::string::npos)) && !args.IsBatchMode() && !args.IsHeadless()) {
312 TString filebase = "root_start_";
313
314 auto f = TemporaryFile(filebase, IsSnapBrowser() ? 1 : 0, ".html");
315
316 bool ferr = false;
317
318 if (!f) {
319 ferr = true;
320 } else {
321 std::string content = std::regex_replace(
322 "<!DOCTYPE html>\n"
323 "<html lang=\"en\">\n"
324 "<head>\n"
325 " <meta charset=\"utf-8\">\n"
326 " <meta http-equiv=\"refresh\" content=\"0;url=$url\"/>\n"
327 " <title>Opening ROOT widget</title>\n"
328 "</head>\n"
329 "<body>\n"
330 "<p>\n"
331 " This page should redirect you to a ROOT widget. If it doesn't,\n"
332 " <a href=\"$url\">click here to go to ROOT</a>.\n"
333 "</p>\n"
334 "</body>\n"
335 "</html>\n", std::regex("\\$url"), url);
336
337 if (fwrite(content.c_str(), 1, content.length(), f) != content.length())
338 ferr = true;
339
340 if (fclose(f) != 0)
341 ferr = true;
342
343 tmpfile = filebase.Data();
344
345 url = "file://"s + tmpfile;
346 }
347
348 if (ferr) {
349 if (!tmpfile.empty())
350 gSystem->Unlink(tmpfile.c_str());
351 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary HTML file to startup widget";
352 return nullptr;
353 }
354 }
355
356 exec = std::regex_replace(exec, std::regex("\\$rootetcdir"), TROOT::GetEtcDir().Data());
357 exec = std::regex_replace(exec, std::regex("\\$url"), url);
358 exec = std::regex_replace(exec, std::regex("\\$width"), swidth);
359 exec = std::regex_replace(exec, std::regex("\\$height"), sheight);
360 exec = std::regex_replace(exec, std::regex("\\$posx"), sposx);
361 exec = std::regex_replace(exec, std::regex("\\$posy"), sposy);
362
363 if (exec.compare(0,5,"fork:") == 0) {
364 if (fProg.empty()) {
365 if (!tmpfile.empty())
366 gSystem->Unlink(tmpfile.c_str());
367 R__LOG_ERROR(WebGUILog()) << "Fork instruction without executable";
368 return nullptr;
369 }
370
371 exec.erase(0, 5);
372
373 // in case of redirection process will wait until output is produced
374 std::string redirect = args.GetRedirectOutput();
375
376#ifndef _MSC_VER
377
378 std::unique_ptr<TObjArray> fargs(TString(exec.c_str()).Tokenize(" "));
379 if (!fargs || (fargs->GetLast()<=0)) {
380 if (!tmpfile.empty())
381 gSystem->Unlink(tmpfile.c_str());
382 R__LOG_ERROR(WebGUILog()) << "Fork instruction is empty";
383 return nullptr;
384 }
385
386 std::vector<char *> argv;
387 argv.push_back((char *) fProg.c_str());
388 for (Int_t n = 0; n <= fargs->GetLast(); ++n)
389 argv.push_back((char *)fargs->At(n)->GetName());
390 argv.push_back(nullptr);
391
392 R__LOG_DEBUG(0, WebGUILog()) << "Show web window in browser with posix_spawn:\n" << fProg << " " << exec;
393
396 if (redirect.empty())
398 else
401
402#ifdef R__MACOSX
403 char **envp = *_NSGetEnviron();
404#elif defined (__FreeBSD__)
405 //this is needed because the FreeBSD linker does not like to resolve these special symbols
406 //in shared libs with -Wl,--no-undefined
407 char** envp = (char**)dlsym(RTLD_DEFAULT, "environ");
408#else
409 char **envp = environ;
410#endif
411
412 pid_t pid;
413 int status = posix_spawn(&pid, argv[0], &action, nullptr, argv.data(), envp);
414
416
417 if (status != 0) {
418 if (!tmpfile.empty())
419 gSystem->Unlink(tmpfile.c_str());
420 R__LOG_ERROR(WebGUILog()) << "Fail to launch " << argv[0];
421 return nullptr;
422 }
423
424 if (!redirect.empty()) {
425 Int_t batch_timeout = gEnv->GetValue("WebGui.BatchTimeout", 30);
426 struct sigaction Act, Old;
427 int elapsed_time = 0;
428
429 if (batch_timeout) {
430 memset(&Act, 0, sizeof(Act));
431 Act.sa_handler = DummyTimeOutHandler;
432 sigemptyset(&Act.sa_mask);
437 }
438
439 int job_done = 0;
440 std::string dump_content;
441
442 while (!job_done) {
443
444 // wait until output is produced
445 int wait_status = 0;
446
448
449 // try read dump anyway
451
452 if (dump_content.find("<div>###batch###job###done###</div>") != std::string::npos)
453 job_done = 1;
454
455 if (wait_res == -1) {
456 // failure when finish process
458 if ((errno == EINTR) && (alarm_timeout > 0) && !job_done) {
459 if (alarm_timeout > 2) alarm_timeout = 2;
462 } else {
463 // end of timeout - do not try to wait any longer
464 job_done = 1;
465 }
466 } else if (!WIFEXITED(wait_status) && !WIFSIGNALED(wait_status)) {
467 // abnormal end of browser process
468 job_done = 1;
469 } else {
470 // this is normal finish, no need for process kill
471 job_done = 2;
472 }
473 }
474
475 if (job_done != 2) {
476 // kill browser process when no normal end was detected
477 kill(pid, SIGKILL);
478 }
479
480 if (batch_timeout) {
481 alarm(0); // disable alarm
482 sigaction(SIGALRM, &Old, nullptr);
483 }
484
485 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
486 ::Info("RWebDisplayHandle::Display", "Preserve dump file %s", redirect.c_str());
487 else
488 gSystem->Unlink(redirect.c_str());
489
490 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, dump_content);
491 }
492
493 // add processid and rm dir
494
495 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, pid);
496
497#else
498
499 if (fProg.empty()) {
500 if (!tmpfile.empty())
501 gSystem->Unlink(tmpfile.c_str());
502 R__LOG_ERROR(WebGUILog()) << "No Web browser found";
503 return nullptr;
504 }
505
506 // use UnixPathName to simplify handling of backslashes
507 exec = "wmic process call create '"s + gSystem->UnixPathName(fProg.c_str()) + " " + exec + "' | find \"ProcessId\" "s;
508 std::string process_id = gSystem->GetFromPipe(exec.c_str()).Data();
509 std::stringstream ss(process_id);
510 std::string tmp;
511 char c;
512 int pid = 0;
513 ss >> tmp >> c >> pid;
514
515 if (pid <= 0) {
516 if (!tmpfile.empty())
517 gSystem->Unlink(tmpfile.c_str());
518 R__LOG_ERROR(WebGUILog()) << "Fail to launch " << fProg;
519 return nullptr;
520 }
521
522 // add processid and rm dir
523 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, pid);
524#endif
525 }
526
527#ifdef _MSC_VER
528
529 if (exec.rfind("&") == exec.length() - 1) {
530
531 // if last symbol is &, use _spawn to detach execution
532 exec.resize(exec.length() - 1);
533
534 std::vector<char *> argv;
535 std::string firstarg = fProg;
536 auto slashpos = firstarg.find_last_of("/\\");
537 if (slashpos != std::string::npos)
538 firstarg.erase(0, slashpos + 1);
539 argv.push_back((char *)firstarg.c_str());
540
541 std::unique_ptr<TObjArray> fargs(TString(exec.c_str()).Tokenize(" "));
542 for (Int_t n = 1; n <= fargs->GetLast(); ++n)
543 argv.push_back((char *)fargs->At(n)->GetName());
544 argv.push_back(nullptr);
545
546 R__LOG_DEBUG(0, WebGUILog()) << "Showing web window in " << fProg << " with:\n" << exec;
547
548 _spawnv(_P_NOWAIT, gSystem->UnixPathName(fProg.c_str()), argv.data());
549
550 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, ""s);
551 }
552
553 std::string prog = "\""s + gSystem->UnixPathName(fProg.c_str()) + "\""s;
554
555#else
556
557#ifdef R__MACOSX
558 std::string prog = std::regex_replace(fProg, std::regex(" "), "\\ ");
559#else
560 std::string prog = fProg;
561#endif
562
563#endif
564
565 exec = std::regex_replace(exec, std::regex("\\$prog"), prog);
566
567 std::string redirect = args.GetRedirectOutput(), dump_content;
568
569 if (!redirect.empty()) {
570 if (exec.find("$dumpfile") != std::string::npos) {
571 exec = std::regex_replace(exec, std::regex("\\$dumpfile"), redirect);
572 } else {
573 auto p = exec.length();
574 if (exec.rfind("&") == p-1) --p;
575 exec.insert(p, " >"s + redirect + " "s);
576 }
577 }
578
579 R__LOG_DEBUG(0, WebGUILog()) << "Showing web window in browser with:\n" << exec;
580
581 gSystem->Exec(exec.c_str());
582
583 // read content of redirected output
584 if (!redirect.empty()) {
586
587 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
588 ::Info("RWebDisplayHandle::Display", "Preserve dump file %s", redirect.c_str());
589 else
590 gSystem->Unlink(redirect.c_str());
591 }
592
593 return std::make_unique<RWebBrowserHandle>(url, rmdir, tmpfile, dump_content);
594}
595
596//////////////////////////////////////////////////////////////////////////////////////////////////
597/// Constructor
598
600{
601 fExec = gEnv->GetValue("WebGui.SafariInteractive", "open -a Safari $url");
602}
603
604//////////////////////////////////////////////////////////////////////////////////////////////////
605/// Returns true if it can be used
606
608{
609#ifdef R__MACOSX
610 return true;
611#else
612 return false;
613#endif
614}
615
616//////////////////////////////////////////////////////////////////////////////////////////////////
617/// Constructor
618
620{
621 fEdge = _edge;
622
623 fEnvPrefix = fEdge ? "WebGui.Edge" : "WebGui.Chrome";
624
625 TestProg(gEnv->GetValue(fEnvPrefix.c_str(), ""));
626
627 if (!fProg.empty() && !fEdge)
628 fChromeVersion = gEnv->GetValue("WebGui.ChromeVersion", -1);
629
630#ifdef _MSC_VER
631 if (fEdge)
632 TestProg("\\Microsoft\\Edge\\Application\\msedge.exe", true);
633 else
634 TestProg("\\Google\\Chrome\\Application\\chrome.exe", true);
635#endif
636#ifdef R__MACOSX
637 TestProg("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
638#endif
639#ifdef R__LINUX
640 TestProg("/snap/bin/chromium"); // test snap before to detect it properly
641 TestProg("/usr/bin/chromium");
642 TestProg("/usr/bin/chromium-browser");
643 TestProg("/usr/bin/chrome-browser");
644 TestProg("/usr/bin/google-chrome-stable");
645 TestProg("/usr/bin/google-chrome");
646#endif
647
648// --no-sandbox is required to run chrome with super-user, but only in headless mode
649// --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
650
651#ifdef _MSC_VER
652 // here --headless=old was used to let normally end of Edge process when --dump-dom is used
653 // while on Windows chrome and edge version not tested, just suppose that newest chrome is used
654 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "$prog --headless --no-sandbox $geometry --dump-dom $url");
655 // in interactive headless mode fork used to let stop browser via process id
656 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless --no-sandbox --disable-gpu $geometry \"$url\"");
657 fExec = gEnv->GetValue((fEnvPrefix + "Interactive").c_str(), "$prog $geometry --new-window --app=$url &"); // & in windows mean usage of spawn
658#else
659#ifdef R__MACOSX
660 bool use_normal = true; // mac does not like new flag
661#else
662 bool use_normal = (fChromeVersion < 119) || (fChromeVersion > 131);
663#endif
664 if (use_normal) {
665 // old or newest browser with standard headless mode
666 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "fork:--headless --no-sandbox --disable-extensions --disable-audio-output $geometry --dump-dom $url");
667 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless --no-sandbox --disable-extensions --disable-audio-output $geometry $url");
668 } else {
669 // newer version with headless=new mode
670 fBatchExec = gEnv->GetValue((fEnvPrefix + "Batch").c_str(), "fork:--headless=new --no-sandbox --disable-extensions --disable-audio-output $geometry --dump-dom $url");
671 fHeadlessExec = gEnv->GetValue((fEnvPrefix + "Headless").c_str(), "fork:--headless=new --no-sandbox --disable-extensions --disable-audio-output $geometry $url");
672 }
673 fExec = gEnv->GetValue((fEnvPrefix + "Interactive").c_str(), "$prog $geometry --new-window --app=\'$url\' >/dev/null 2>/dev/null &");
674#endif
675}
676
677
678//////////////////////////////////////////////////////////////////////////////////////////////////
679/// Replace $geometry placeholder with geometry settings
680/// Also RWebDisplayArgs::GetExtraArgs() are appended
681
683{
684 std::string geometry;
685 if ((args.GetWidth() > 0) && (args.GetHeight() > 0))
686 geometry = "--window-size="s + std::to_string(args.GetWidth())
687 + (args.IsHeadless() ? "x"s : ","s)
688 + std::to_string(args.GetHeight());
689
690 if (((args.GetX() >= 0) || (args.GetY() >= 0)) && !args.IsHeadless()) {
691 if (!geometry.empty()) geometry.append(" ");
692 geometry.append("--window-position="s + std::to_string(args.GetX() >= 0 ? args.GetX() : 0) + ","s +
693 std::to_string(args.GetY() >= 0 ? args.GetY() : 0));
694 }
695
696 exec = std::regex_replace(exec, std::regex("\\$geometry"), geometry);
697}
698
699
700//////////////////////////////////////////////////////////////////////////////////////////////////
701/// Handle profile argument
702
703std::string RWebDisplayHandle::ChromeCreator::MakeProfile(std::string &exec, bool)
704{
705 std::string rmdir, profile_arg;
706
707 if (exec.find("$profile") == std::string::npos)
708 return rmdir;
709
710 const char *chrome_profile = gEnv->GetValue((fEnvPrefix + "Profile").c_str(), "");
713 } else {
715 rnd.SetSeed(0);
717 if ((profile_arg.compare(0, 4, "/tmp") == 0) && IsSnapBrowser())
719
720#ifdef _MSC_VER
721 char slash = '\\';
722#else
723 char slash = '/';
724#endif
725 if (!profile_arg.empty() && (profile_arg[profile_arg.length()-1] != slash))
727 profile_arg += "root_chrome_profile_"s + std::to_string(rnd.Integer(0x100000));
728
729 rmdir = profile_arg;
730 }
731
732 exec = std::regex_replace(exec, std::regex("\\$profile"), profile_arg);
733
734 return rmdir;
735}
736
737
738//////////////////////////////////////////////////////////////////////////////////////////////////
739/// Constructor
740
742{
743 TestProg(gEnv->GetValue("WebGui.Firefox", ""));
744
745#ifdef _MSC_VER
746 TestProg("\\Mozilla Firefox\\firefox.exe", true);
747#endif
748#ifdef R__MACOSX
749 TestProg("/Applications/Firefox.app/Contents/MacOS/firefox");
750#endif
751#ifdef R__LINUX
752 TestProg("/snap/bin/firefox");
753 TestProg("/usr/bin/firefox");
754 TestProg("/usr/bin/firefox-bin");
755#endif
756
757#ifdef _MSC_VER
758 // there is a problem when specifying the window size with wmic on windows:
759 // It gives: Invalid format. Hint: <paramlist> = <param> [, <paramlist>].
760 fBatchExec = gEnv->GetValue("WebGui.FirefoxBatch", "$prog -headless -no-remote $profile $url");
761 fHeadlessExec = gEnv->GetValue("WebGui.FirefoxHeadless", "fork:-headless -no-remote $profile \"$url\"");
762 fExec = gEnv->GetValue("WebGui.FirefoxInteractive", "$prog -no-remote $profile $geometry $url &");
763#else
764 fBatchExec = gEnv->GetValue("WebGui.FirefoxBatch", "fork:--headless -no-remote -new-instance $profile $url");
765 fHeadlessExec = gEnv->GetValue("WebGui.FirefoxHeadless", "fork:--headless -no-remote $profile --private-window $url");
766 fExec = gEnv->GetValue("WebGui.FirefoxInteractive", "$rootetcdir/runfirefox.sh __nodump__ $cleanup_profile $prog -no-remote $profile $geometry -url \'$url\' &");
767#endif
768}
769
770//////////////////////////////////////////////////////////////////////////////////////////////////
771/// Process window geometry for Firefox
772
774{
775 std::string geometry;
776 if ((args.GetWidth() > 0) && (args.GetHeight() > 0) && !args.IsHeadless())
777 geometry = "-width="s + std::to_string(args.GetWidth()) + " -height=" + std::to_string(args.GetHeight());
778
779 exec = std::regex_replace(exec, std::regex("\\$geometry"), geometry);
780}
781
782//////////////////////////////////////////////////////////////////////////////////////////////////
783/// Create Firefox profile to run independent browser window
784
786{
787 std::string rmdir, profile_arg;
788
789 if (exec.find("$profile") == std::string::npos)
790 return rmdir;
791
792 const char *ff_profile = gEnv->GetValue("WebGui.FirefoxProfile", "");
793 const char *ff_profilepath = gEnv->GetValue("WebGui.FirefoxProfilePath", "");
794 Int_t ff_randomprofile = RWebWindowWSHandler::GetBoolEnv("WebGui.FirefoxRandomProfile", 1);
795 if (ff_profile && *ff_profile) {
796 profile_arg = "-P "s + ff_profile;
797 } else if (ff_profilepath && *ff_profilepath) {
798 profile_arg = "-profile "s + ff_profilepath;
799 } else if (ff_randomprofile > 0) {
801 rnd.SetSeed(0);
802 std::string profile_dir = gSystem->TempDirectory();
803 if ((profile_dir.compare(0, 4, "/tmp") == 0) && IsSnapBrowser())
805
806#ifdef _MSC_VER
807 char slash = '\\';
808#else
809 char slash = '/';
810#endif
811 if (!profile_dir.empty() && (profile_dir[profile_dir.length()-1] != slash))
813 profile_dir += "root_ff_profile_"s + std::to_string(rnd.Integer(0x100000));
814
815 profile_arg = "-profile "s + profile_dir;
816
817 if (gSystem->mkdir(profile_dir.c_str()) == 0) {
818 rmdir = profile_dir;
819
820 std::ofstream user_js(profile_dir + "/user.js", std::ios::trunc);
821 // workaround for current Firefox, without such settings it fail to close window and terminate it from batch
822 // also disable question about upload of data
823 user_js << "user_pref(\"datareporting.policy.dataSubmissionPolicyBypassNotification\", true);" << std::endl;
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 avoid any kind of dialogs on the start
828 user_js << "user_pref(\"app.update.auto\", false);" << std::endl;
829 user_js << "user_pref(\"browser.shell.checkDefaultBrowser\", false);" << std::endl;
830 user_js << "user_pref(\"browser.aboutwelcome.enabled\", false);" << std::endl;
831 user_js << "user_pref(\"browser.tabs.disableBackgroundLinkLoading\", true);" << std::endl;
832
833 // try to ensure that window closes with last tab
834 user_js << "user_pref(\"browser.tabs.closeWindowWithLastTab\", true);" << std::endl;
835 user_js << "user_pref(\"dom.allow_scripts_to_close_windows\", true);" << std::endl;
836 user_js << "user_pref(\"browser.sessionstore.resume_from_crash\", false);" << std::endl;
837
838 if (batch_mode) {
839 // allow to dump messages to std output
840 user_js << "user_pref(\"browser.dom.window.dump.enabled\", true);" << std::endl;
841 } else {
842 // to suppress annoying privacy tab
843 user_js << "user_pref(\"datareporting.policy.firstRunURL\", \"\");" << std::endl;
844 // to use custom userChrome.css files
845 user_js << "user_pref(\"toolkit.legacyUserProfileCustomizations.stylesheets\", true);" << std::endl;
846 // do not put tabs in title
847 user_js << "user_pref(\"browser.tabs.inTitlebar\", 0);" << std::endl;
848
849#ifdef R__LINUX
850 // fix WebGL creation problem on some Linux platforms
851 user_js << "user_pref(\"webgl.out-of-process\", false);" << std::endl;
852#endif
853
854 std::ofstream times_json(profile_dir + "/times.json", std::ios::trunc);
855 times_json << "{" << std::endl;
856 times_json << " \"created\": 1699968480952," << std::endl;
857 times_json << " \"firstUse\": null" << std::endl;
858 times_json << "}" << std::endl;
859 if (gSystem->mkdir((profile_dir + "/chrome").c_str()) == 0) {
860 std::ofstream style(profile_dir + "/chrome/userChrome.css", std::ios::trunc);
861 // do not show tabs
862 style << "#TabsToolbar { visibility: collapse; }" << std::endl;
863 // do not show URL
864 style << "#nav-bar, #urlbar-container, #searchbar { visibility: collapse !important; }" << std::endl;
865 }
866 }
867
868 } else {
869 R__LOG_ERROR(WebGUILog()) << "Cannot create Firefox profile directory " << profile_dir;
870 }
871 }
872
873 exec = std::regex_replace(exec, std::regex("\\$profile"), profile_arg);
874
875 if (exec.find("$cleanup_profile") != std::string::npos) {
876 if (rmdir.empty()) rmdir = "__dummy__";
877 exec = std::regex_replace(exec, std::regex("\\$cleanup_profile"), rmdir);
878 rmdir.clear(); // no need to delete directory - it will be removed by script
879 }
880
881 return rmdir;
882}
883
884///////////////////////////////////////////////////////////////////////////////////////////////////
885/// Check if http server required for display
886/// \param args - defines where and how to display web window
887
889{
892 return false;
893
894 if (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)) {
895
896#ifdef WITH_QT6WEB
897 auto &qt6 = FindCreator("qt6", "libROOTQt6WebDisplay");
898 if (qt6 && qt6->IsActive())
899 return false;
900#endif
901#ifdef WITH_CEFWEB
902 auto &cef = FindCreator("cef", "libROOTCefDisplay");
903 if (cef && cef->IsActive())
904 return false;
905#endif
906 }
907
908 return true;
909}
910
911
912///////////////////////////////////////////////////////////////////////////////////////////////////
913/// Create web display
914/// \param args - defines where and how to display web window
915/// Returns RWebDisplayHandle, which holds information of running browser application
916/// Can be used fully independent from RWebWindow classes just to show any web page
917
918std::unique_ptr<RWebDisplayHandle> RWebDisplayHandle::Display(const RWebDisplayArgs &args)
919{
920 std::unique_ptr<RWebDisplayHandle> handle;
921
923 return handle;
924
925 auto try_creator = [&](std::unique_ptr<Creator> &creator) {
926 if (!creator || !creator->IsActive())
927 return false;
928 handle = creator->Display(args);
929 return handle ? true : false;
930 };
931
933 (!args.IsHeadless() && (args.GetBrowserKind() == RWebDisplayArgs::kOn)),
934 has_qt6web = false, has_cefweb = false;
935
936#ifdef WITH_QT6WEB
937 has_qt6web = true;
938#endif
939
940#ifdef WITH_CEFWEB
941 has_cefweb = true;
942#endif
943
945 if (try_creator(FindCreator("qt6", "libROOTQt6WebDisplay")))
946 return handle;
947 }
948
950 if (try_creator(FindCreator("cef", "libROOTCefDisplay")))
951 return handle;
952 }
953
954 if (args.IsLocalDisplay()) {
955 R__LOG_ERROR(WebGUILog()) << "Neither Qt5/6 nor CEF libraries were found to provide local display";
956 return handle;
957 }
958
959 bool handleAsNative =
961
963 if (try_creator(FindCreator("chrome", "ChromeCreator")))
964 return handle;
965 }
966
968 if (try_creator(FindCreator("firefox", "FirefoxCreator")))
969 return handle;
970 }
971
972#ifdef _MSC_VER
973 // Edge browser cannot be run headless without registry change, therefore do not try it by default
974 if ((handleAsNative && !args.IsHeadless() && !args.IsBatchMode()) || (args.GetBrowserKind() == RWebDisplayArgs::kEdge)) {
975 if (try_creator(FindCreator("edge", "ChromeCreator")))
976 return handle;
977 }
978#endif
979
982 // R__LOG_ERROR(WebGUILog()) << "Neither Chrome nor Firefox browser cannot be started to provide display";
983 return handle;
984 }
985
987 if (try_creator(FindCreator("safari", "SafariCreator")))
988 return handle;
989 }
990
992 std::unique_ptr<Creator> creator = std::make_unique<BrowserCreator>(false, args.GetCustomExec());
993 try_creator(creator);
994 } else {
995 try_creator(FindCreator("browser", "BrowserCreator"));
996 }
997
998 return handle;
999}
1000
1001///////////////////////////////////////////////////////////////////////////////////////////////////
1002/// Display provided url in configured web browser
1003/// \param url - specified URL address like https://root.cern
1004/// Browser can specified when starting `root --web=firefox`
1005/// Returns true when browser started
1006/// It is convenience method, equivalent to:
1007/// ~~~
1008/// RWebDisplayArgs args;
1009/// args.SetUrl(url);
1010/// args.SetStandalone(false);
1011/// auto handle = RWebDisplayHandle::Display(args);
1012/// ~~~
1013
1014bool RWebDisplayHandle::DisplayUrl(const std::string &url)
1015{
1016 RWebDisplayArgs args;
1017 args.SetUrl(url);
1018 args.SetStandalone(false);
1019
1020 auto handle = Display(args);
1021
1022 return !!handle;
1023}
1024
1025///////////////////////////////////////////////////////////////////////////////////////////////////
1026/// Checks if configured browser can be used for image production
1027
1029{
1033 bool detected = false;
1034
1035 auto &h1 = FindCreator("chrome", "ChromeCreator");
1036 if (h1 && h1->IsActive()) {
1038 detected = true;
1039 }
1040
1041 if (!detected) {
1042 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1043 if (h2 && h2->IsActive()) {
1045 detected = true;
1046 }
1047 }
1048
1049 return detected;
1050 }
1051
1053 auto &h1 = FindCreator("chrome", "ChromeCreator");
1054 return h1 && h1->IsActive();
1055 }
1056
1058 auto &h2 = FindCreator("firefox", "FirefoxCreator");
1059 return h2 && h2->IsActive();
1060 }
1061
1062#ifdef _MSC_VER
1063 if (args.GetBrowserKind() == RWebDisplayArgs::kEdge) {
1064 auto &h3 = FindCreator("edge", "ChromeCreator");
1065 return h3 && h3->IsActive();
1066 }
1067#endif
1068
1069 return true;
1070}
1071
1072///////////////////////////////////////////////////////////////////////////////////////////////////
1073/// Returns true if image production for specified browser kind is supported
1074/// If browser not specified - use currently configured browser or try to test existing web browsers
1075
1077{
1079
1080 return CheckIfCanProduceImages(args);
1081}
1082
1083///////////////////////////////////////////////////////////////////////////////////////////////////
1084/// Detect image format
1085/// There is special handling of ".screenshot.pdf" and ".screenshot.png" extensions
1086/// Creation of such files relies on headless browser functionality and fully supported only by Chrome browser
1087
1088std::string RWebDisplayHandle::GetImageFormat(const std::string &fname)
1089{
1090 std::string _fname = fname;
1091 std::transform(_fname.begin(), _fname.end(), _fname.begin(), ::tolower);
1092 auto EndsWith = [&_fname](const std::string &suffix) {
1093 return (_fname.length() > suffix.length()) ? (0 == _fname.compare(_fname.length() - suffix.length(), suffix.length(), suffix)) : false;
1094 };
1095
1096 if (EndsWith(".screenshot.pdf"))
1097 return "s.pdf"s;
1098 if (EndsWith(".pdf"))
1099 return "pdf"s;
1100 if (EndsWith(".json"))
1101 return "json"s;
1102 if (EndsWith(".svg"))
1103 return "svg"s;
1104 if (EndsWith(".screenshot.png"))
1105 return "s.png"s;
1106 if (EndsWith(".png"))
1107 return "png"s;
1108 if (EndsWith(".html") || EndsWith(".htm"))
1109 return "html"s;
1110 if (EndsWith(".jpg") || EndsWith(".jpeg"))
1111 return "jpeg"s;
1112 if (EndsWith(".webp"))
1113 return "webp"s;
1114
1115 return ""s;
1116}
1117
1118
1119///////////////////////////////////////////////////////////////////////////////////////////////////
1120/// Produce image file using JSON data as source
1121/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1122
1123bool RWebDisplayHandle::ProduceImage(const std::string &fname, const std::string &json, int width, int height, const char *batch_file)
1124{
1125 return ProduceImages(fname, {json}, {width}, {height}, batch_file);
1126}
1127
1128
1129///////////////////////////////////////////////////////////////////////////////////////////////////
1130/// Produce vector of file names for specified file pattern
1131/// Depending from supported file formats
1132
1133std::vector<std::string> RWebDisplayHandle::ProduceImagesNames(const std::string &fname, unsigned nfiles)
1134{
1135 auto fmt = GetImageFormat(fname);
1136
1137 std::vector<std::string> fnames;
1138
1139 if ((fmt == "s.pdf") || (fmt == "s.png")) {
1140 fnames.emplace_back(fname);
1141 } else {
1142 std::string farg = fname;
1143
1144 bool has_quialifier = farg.find("%") != std::string::npos;
1145
1146 if (!has_quialifier && (nfiles > 1) && (fmt != "pdf") && (fmt != "html")) {
1147 farg.insert(farg.rfind("."), "%d");
1148 has_quialifier = true;
1149 }
1150
1151 for (unsigned n = 0; n < nfiles; n++) {
1152 if(has_quialifier) {
1153 auto expand_name = TString::Format(farg.c_str(), (int) n);
1154 fnames.emplace_back(expand_name.Data());
1155 } else if (n > 0)
1156 fnames.emplace_back(""); // empty name is multiPdf or multiHtml
1157 else
1158 fnames.emplace_back(fname);
1159 }
1160 }
1161
1162 return fnames;
1163}
1164
1165
1166///////////////////////////////////////////////////////////////////////////////////////////////////
1167/// Produce image file(s) using JSON data as source
1168/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1169
1170bool 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)
1171{
1173}
1174
1175///////////////////////////////////////////////////////////////////////////////////////////////////
1176/// Produce image file(s) using JSON data as source
1177/// Invokes JSROOT drawing functionality in headless browser - Google Chrome or Mozilla Firefox
1178
1179bool 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)
1180{
1181 if (fnames.empty() || jsons.empty())
1182 return false;
1183
1184 std::vector<std::string> fmts;
1185 unsigned num_non_empty_fmts = 0, num_non_empty_files = 0;
1186 for (auto& fname : fnames) {
1187 if (!fname.empty())
1189 std::string fmt = GetImageFormat(fname);
1190 if (!fmt.empty())
1192 fmts.emplace_back(fmt);
1193 }
1194
1195 bool is_any_image = false;
1196
1197 const char *jsrootsys = gSystem->Getenv("JSROOTSYS");
1198
1199 for (unsigned n = 0; (n < fmts.size()) && (n < jsons.size()); n++) {
1200 if (fmts[n] == "json") {
1201 std::ofstream ofs(fnames[n]);
1202 ofs << jsons[n];
1203 fmts[n].clear();
1204 ::Info("ProduceImages", "JSON file %s size %d bytes has been created", fnames[n].c_str(), (int) jsons[n].length());
1205
1206 } else if (fmts[n] == "html") {
1207 bool is_multi_html = (num_non_empty_fmts == 1) && (num_non_empty_files == 1) && (jsons.size() > 1) && (n == 0);
1208
1209 std::string filejsrootsys;
1210 if (jsrootsys)
1212 if (filejsrootsys.empty() || ((filejsrootsys.find("http://") != 0) && (filejsrootsys.find("https://") != 0)))
1213 filejsrootsys = "https://root.cern/js/latest";
1214
1215 std::ofstream ofs(fnames[n]);
1216
1217 ofs << "<!DOCTYPE html>\n"
1218 "<html lang=\"en\">\n"
1219 "<head>\n"
1220 " <meta charset=\"utf-8\">\n"
1221 " <title>Dsiplay of ROOT " << (is_multi_html ? "objects" : "object") << "</title>\n"
1222 " <link rel=\"shortcut icon\" href=\"" << filejsrootsys << "/img/RootIcon.ico\"/>\n"
1223 " <script type=\"importmap\">\n"
1224 " { \"imports\": { \"jsroot\": \"" << filejsrootsys << "/modules/main.mjs\" } }\n"
1225 " </script>\n"
1226 " <style>\n";
1227 if (is_multi_html) {
1228 ofs << " .root-container {\n"
1229 " display: flex;\n"
1230 " flex-direction: column;\n"
1231 " align-items: center;\n"
1232 " gap: 20px;\n"
1233 " width: 100%;\n"
1234 " }\n";
1235 } else {
1236 ofs << " body {\n"
1237 " margin: 0;\n"
1238 " padding: 0;\n"
1239 " display: flex;\n"
1240 " justify-content: center;\n"
1241 " align-items: center;\n"
1242 " min-height: 100vh;\n"
1243 " background-color: #f0f0f0;\n"
1244 " }\n";
1245 }
1246 ofs << " .root-drawing {\n"
1247 " background-color: white;\n"
1248 " box-shadow: 0 4px 10px rgba(0,0,0,0.1);\n"
1249 " }\n"
1250 " </style>\n"
1251 "</head>\n"
1252 "<body>\n";
1253 if (is_multi_html) {
1254 ofs << " <div class=\"root-container\">\n";
1255 for (unsigned k = 0; k < jsons.size(); ++k)
1256 ofs << " <div id=\"drawing" << k << "\" class=\"root-drawing\""
1257 " style=\"width: " << widths[k] << "px;"
1258 " height: " << heights[k] << "px;\"></div>\n";
1259 ofs << " </div>\n";
1260 } else {
1261 ofs << " <div id=\"drawing\" class=\"root-drawing\""
1262 " style=\"width: " << widths[n] << "px;"
1263 " min-height: " << heights[n] << "px;\"></div>\n";
1264 }
1265 ofs << " <script type=\"module\">\n"
1266 " import { parse, draw } from \"jsroot\";\n";
1267 if (is_multi_html) {
1268 for (unsigned k = 0; k < jsons.size(); ++k) {
1269 ofs << " const obj" << k << " = parse(" << jsons[k] << ");\n"
1270 " draw(\"drawing" << k << "\", obj" << k << ");\n";
1271 }
1272 } else {
1273 ofs << " const obj = parse(" << jsons[n] << ");\n"
1274 " draw(\"drawing\", obj);\n";
1275 }
1276 ofs << " </script>\n"
1277 "</body>\n"
1278 "</html>\n";
1279
1280 ::Info("ProduceImages", "HTML file %s size %d bytes has been created", fnames[n].c_str(), (int) ofs.tellp());
1281
1282 fmts[n].clear();
1283 if (is_multi_html)
1284 break;
1285 } else if (!fmts[n].empty())
1286 is_any_image = true;
1287 }
1288
1289 if (!is_any_image)
1290 return true;
1291
1292 std::string fdebug;
1293 if (fnames.size() == 1)
1294 fdebug = fnames[0];
1295 else
1297
1299 if (!jsrootsys) {
1300 jsrootsysdflt = TROOT::GetDataDir() + "/js";
1302 R__LOG_ERROR(WebGUILog()) << "Fail to locate JSROOT " << jsrootsysdflt;
1303 return false;
1304 }
1305 jsrootsys = jsrootsysdflt.Data();
1306 }
1307
1308 RWebDisplayArgs args; // set default browser kind, only Chrome/Firefox/Edge or CEF/Qt6 can be used here
1309 if (!CheckIfCanProduceImages(args)) {
1310 R__LOG_ERROR(WebGUILog()) << "Fail to detect supported browsers for image production";
1311 return false;
1312 }
1313
1317
1318 std::vector<std::string> draw_kinds;
1319 bool use_browser_draw = false, can_optimize_json = false;
1320 int use_home_dir = 0;
1322
1323 // Some Chrome installation do not allow run html code from files, created in /tmp directory
1324 // When during session such failures happened, force usage of home directory from the beginning
1325 static int chrome_tmp_workaround = 0;
1326
1327 if (isChrome) {
1329 auto &h1 = FindCreator("chrome", "ChromeCreator");
1330 if (h1 && h1->IsActive() && h1->IsSnapBrowser() && (use_home_dir == 0))
1331 use_home_dir = 1;
1332 }
1333
1334 if (fmts[0] == "s.png") {
1335 if (!isChromeBased && !isFirefox) {
1336 R__LOG_ERROR(WebGUILog()) << "Direct png image creation supported only by Chrome and Firefox browsers";
1337 return false;
1338 }
1339 use_browser_draw = true;
1340 jsonkind = "1111"; // special mark in canv_batch.htm
1341 } else if (fmts[0] == "s.pdf") {
1342 if (!isChromeBased) {
1343 R__LOG_ERROR(WebGUILog()) << "Direct creation of PDF files supported only by Chrome-based browser";
1344 return false;
1345 }
1346 use_browser_draw = true;
1347 jsonkind = "2222"; // special mark in canv_batch.htm
1348 } else {
1349 draw_kinds = fmts;
1351 can_optimize_json = true;
1352 }
1353
1354 if (!batch_file || !*batch_file)
1355 batch_file = "/js/files/canv_batch.htm";
1356
1359 R__LOG_ERROR(WebGUILog()) << "Fail to find " << origin;
1360 return false;
1361 }
1362
1364 if (filecont.empty()) {
1365 R__LOG_ERROR(WebGUILog()) << "Fail to read content of " << origin;
1366 return false;
1367 }
1368
1369 int max_width = 0, max_height = 0, page_margin = 10;
1370 for (auto &w : widths)
1371 if (w > max_width)
1372 max_width = w;
1373 for (auto &h : heights)
1374 if (h > max_height)
1375 max_height = h;
1376
1379
1380 std::string mains, prev;
1381 for (auto &json : jsons) {
1382 mains.append(mains.empty() ? "[" : ", ");
1383 if (can_optimize_json && (json == prev)) {
1384 mains.append("'same'");
1385 } else {
1386 mains.append(json);
1387 prev = json;
1388 }
1389 }
1390 mains.append("]");
1391
1392 if (strstr(jsrootsys, "http://") || strstr(jsrootsys, "https://") || strstr(jsrootsys, "file://"))
1393 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), jsrootsys);
1394 else {
1395 static std::string jsroot_include = "<script id=\"jsroot\" src=\"$jsrootsys/build/jsroot.js\"></script>";
1396 auto p = filecont.find(jsroot_include);
1397 if (p != std::string::npos) {
1398 auto jsroot_build = THttpServer::ReadFileContent(std::string(jsrootsys) + "/build/jsroot.js");
1399 if (!jsroot_build.empty()) {
1400 // insert actual jsroot file location
1401 jsroot_build = std::regex_replace(jsroot_build, std::regex("'\\$jsrootsys'"), std::string("'file://") + jsrootsys + "/'");
1402 filecont.erase(p, jsroot_include.length());
1403 filecont.insert(p, "<script id=\"jsroot\">" + jsroot_build + "</script>");
1404 }
1405 }
1406
1407 filecont = std::regex_replace(filecont, std::regex("\\$jsrootsys"), "file://"s + jsrootsys);
1408 }
1409
1410 filecont = std::regex_replace(filecont, std::regex("\\$page_margin"), std::to_string(page_margin) + "px");
1411 filecont = std::regex_replace(filecont, std::regex("\\$page_width"), std::to_string(max_width + 2*page_margin) + "px");
1412 filecont = std::regex_replace(filecont, std::regex("\\$page_height"), std::to_string(max_height + 2*page_margin) + "px");
1413
1414 filecont = std::regex_replace(filecont, std::regex("\\$draw_kind"), jsonkind.Data());
1415 filecont = std::regex_replace(filecont, std::regex("\\$draw_widths"), jsonw.Data());
1416 filecont = std::regex_replace(filecont, std::regex("\\$draw_heights"), jsonh.Data());
1417 filecont = std::regex_replace(filecont, std::regex("\\$draw_objects"), mains);
1418
1420
1422 dump_name = "canvasdump";
1424 if (!df) {
1425 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for dump-dom";
1426 return false;
1427 }
1428 fputs("placeholder", df);
1429 fclose(df);
1430 }
1431
1432try_again:
1433
1435 args.SetUrl(""s);
1437
1438 html_name.Clear();
1439
1440 R__LOG_DEBUG(0, WebGUILog()) << "Using file content_len " << filecont.length() << " to produce batch images ";
1441
1442 } else {
1443 html_name = "canvasbody";
1445 if (!hf) {
1446 R__LOG_ERROR(WebGUILog()) << "Fail to create temporary file for batch job";
1447 return false;
1448 }
1449 fputs(filecont.c_str(), hf);
1450 fclose(hf);
1451
1452 args.SetUrl("file://"s + gSystem->UnixPathName(html_name.Data()));
1453 args.SetPageContent(""s);
1454
1455 R__LOG_DEBUG(0, WebGUILog()) << "Using " << html_name << " content_len " << filecont.length() << " to produce batch images " << fdebug;
1456 }
1457
1459
1460 args.SetStandalone(true);
1461 args.SetHeadless(true);
1462 args.SetBatchMode(true);
1463 args.SetSize(widths[0], heights[0]);
1464
1465 if (use_browser_draw) {
1466
1467 tgtfilename = fnames[0].c_str();
1470
1472
1473 if (fmts[0] == "s.pdf")
1474 args.SetExtraArgs("--print-to-pdf-no-header --print-to-pdf="s + gSystem->UnixPathName(tgtfilename.Data()));
1475 else if (isFirefox) {
1476 args.SetExtraArgs("--screenshot"); // firefox does not let specify output image file
1477 wait_file_name = "screenshot.png";
1478 } else
1479 args.SetExtraArgs("--screenshot="s + gSystem->UnixPathName(tgtfilename.Data()));
1480
1481 // remove target image file - we use it as detection when chrome is ready
1482 gSystem->Unlink(tgtfilename.Data());
1483
1484 } else if (isFirefox) {
1485 // firefox will use window.dump to output produced result
1486 args.SetRedirectOutput(dump_name.Data());
1487 gSystem->Unlink(dump_name.Data());
1488 } else if (isChromeBased) {
1489 // chrome should have --dump-dom args configures
1490 args.SetRedirectOutput(dump_name.Data());
1491 gSystem->Unlink(dump_name.Data());
1492 }
1493
1494 auto handle = RWebDisplayHandle::Display(args);
1495
1496 // ensure file is created by browser draw
1497 if (use_browser_draw && handle) {
1498 Int_t batch_timeout = gEnv->GetValue("WebGui.BatchTimeout", 30) * 10;
1499 while (gSystem->AccessPathName(wait_file_name.Data()) && (--batch_timeout > 0)) {
1501 gSystem->Sleep(100);
1502 }
1503 }
1504
1505 // delete temporary HTML file
1506 if (html_name.Length() > 0) {
1507 if (gEnv->GetValue("WebGui.PreserveBatchFiles", -1) > 0)
1508 ::Info("ProduceImages", "Preserve batch file %s", html_name.Data());
1509 else
1510 gSystem->Unlink(html_name.Data());
1511 }
1512
1513 if (!handle) {
1514 R__LOG_DEBUG(0, WebGUILog()) << "Cannot start " << args.GetBrowserName() << " to produce image " << fdebug;
1515 return false;
1516 }
1517
1518 if (use_browser_draw) {
1519
1520 if (gSystem->AccessPathName(wait_file_name.Data())) {
1521 R__LOG_ERROR(WebGUILog()) << "Fail to produce image " << fdebug;
1522 return false;
1523 }
1524
1525 if (fmts[0] == "s.pdf")
1526 ::Info("ProduceImages", "PDF file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1527 else {
1528 if (isFirefox)
1529 gSystem->Rename("screenshot.png", fnames[0].c_str());
1530 ::Info("ProduceImages", "PNG file %s with %d pages has been created", fnames[0].c_str(), (int) jsons.size());
1531 }
1532 } else {
1533 auto dumpcont = handle->GetContent();
1534
1535 if ((dumpcont.length() > 20) && (dumpcont.length() < 60) && (use_home_dir < 2) && isChrome) {
1536 // chrome creates dummy html file with mostly no content
1537 // problem running chrome from /tmp directory, lets try work from home directory
1538 R__LOG_INFO(WebGUILog()) << "Use home directory for running chrome in batch, set TMPDIR for preferable temp directory";
1540 goto try_again;
1541 }
1542
1543 if (dumpcont.length() < 100) {
1544 R__LOG_ERROR(WebGUILog()) << "Fail to dump HTML code into " << (dump_name.IsNull() ? "CEF" : dump_name.Data());
1545 return false;
1546 }
1547
1548 std::string::size_type p = 0;
1549
1550 for (unsigned n = 0; n < fmts.size(); n++) {
1551 if (fmts[n].empty())
1552 continue;
1553 if (fmts[n] == "svg") {
1554 auto p1 = dumpcont.find("<div><svg", p);
1555 auto p2 = dumpcont.find("</svg></div>", p1 + 8);
1556 p = p2 + 12;
1557 std::ofstream ofs(fnames[n]);
1558 if ((p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1559 if (p2 - p1 > 10) {
1560 ofs << dumpcont.substr(p1 + 5, p2 - p1 + 1);
1561 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) (p2 - p1 + 1));
1562 } else {
1563 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1564 }
1565 }
1566 } else {
1567 auto p0 = dumpcont.find("<img src=\"", p);
1568 auto p1 = dumpcont.find(";base64,", p0 + 8);
1569 auto p2 = dumpcont.find("\">", p1 + 8);
1570 p = p2 + 2;
1571
1572 if ((p0 != std::string::npos) && (p1 != std::string::npos) && (p2 != std::string::npos) && (p1 < p2)) {
1573 auto base64 = dumpcont.substr(p1+8, p2-p1-8);
1574 if ((base64 == "failure") || (base64.length() < 10)) {
1575 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1576 } else {
1577 auto binary = TBase64::Decode(base64.c_str());
1578 std::ofstream ofs(fnames[n], std::ios::binary);
1579 ofs.write(binary.Data(), binary.Length());
1580 ::Info("ProduceImages", "Image file %s size %d bytes has been created", fnames[n].c_str(), (int) binary.Length());
1581 }
1582 } else {
1583 ::Error("ProduceImages", "Failure producing %s", fnames[n].c_str());
1584 return false;
1585 }
1586 }
1587 }
1588 }
1589
1590 R__LOG_DEBUG(0, WebGUILog()) << "Create " << (fnames.size() > 1 ? "files " : "file ") << fdebug;
1591
1592 return true;
1593}
1594
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 TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
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 formats.
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