-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathchromiumbrowsercontroller.py
65 lines (57 loc) · 2.16 KB
/
chromiumbrowsercontroller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import fileinput
import logging
import os
import subprocess
from pathlib import Path
class ChromiumBrowserController:
"""Handles Chromium browser instances to show the
blaulichtSMS Einsatzmonitor dashboard.
"""
def __init__(self, session_id):
self.logger = logging.getLogger(__name__)
self._session_id = session_id
self._process = None
def start(self):
self._delete_crash_exit()
self._process = subprocess.Popen(
[
"/usr/bin/chromium-browser",
"--display=:0",
"--noerrdialogs",
"--disable-session-restore",
"--disable-session-crashed-bubble",
"--disable-infobars",
"--start-fullscreen",
"https://dashboard.blaulichtsms.net/#/login?token="
+ self._session_id
],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
self.logger.info("Started browser")
def is_alive(self):
return self._process.poll() is None
def _delete_crash_exit(self):
"""If Chromium starts after an unexpected exit (e.g. the host chrashed),
Chromium displays a notification asking if the user wants to restore the crashed session.
This method removes this notification, as it is not wanted for the alarm monitor.
"""
self.logger.debug("Delete crashed session flag")
file_path = os.path.join(
str(Path.home()), ".config", "chromium", "Default", "Preferences")
try:
with fileinput.input(files=file_path, inplace=True) as file:
for line in file:
replaced_line = line.replace(
"\"exit_type\":\"Crashed\"",
"\"exit_type\":\"Normal\""
)
print(replaced_line, end="")
except FileNotFoundError:
self.logger.debug(
"Preferences file does not exist."
+ " No crashed session flag to delete."
)
def terminate(self):
self._process.terminate()
self.logger.info("Closed browser")