""" Chrome browser launcher for traceability application Launches Chrome in fullscreen with the web-based traceability app """ import subprocess import os import time import logging from logger_module import log_with_server def get_chrome_path(): """Find Chrome/Chromium executable""" possible_paths = [ '/usr/bin/chromium-browser', '/usr/bin/chromium', '/usr/bin/google-chrome', '/snap/bin/chromium', '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome' # macOS ] for path in possible_paths: if os.path.exists(path): return path return None def launch_chrome_app(hostname, device_ip, app_url="http://localhost"): """ Launch Chrome in fullscreen with the traceability application Args: hostname: Device hostname device_ip: Device IP app_url: URL of the traceability web app """ chrome_path = get_chrome_path() if not chrome_path: logging.error("Chrome/Chromium not found on system") log_with_server("ERROR: Chrome browser not installed", hostname, device_ip) return False try: logging.info(f"Launching Chrome with app: {app_url}") log_with_server(f"Launching Chrome app at {app_url}", hostname, device_ip) # Chrome launch arguments for fullscreen kiosk mode chrome_args = [ chrome_path, '--start-maximized', # Start maximized '--fullscreen', # Fullscreen mode '--no-default-browser-check', '--no-first-run', '--disable-popup-blocking', '--disable-infobars', '--disable-extensions', '--disable-plugins', '--disable-sync', '--disable-background-timer-throttling', '--disable-backgrounding-occluded-windows', '--disable-breakpad', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-default-apps', '--disable-device-discovery-notifications', '--disable-image-animation-resync', '--disable-media-session-api', '--disable-permissions-api', '--disable-push-messaging', '--disable-sync', '--disable-web-resources', '--metrics-recording-only', '--no-component-extensions-with-background-pages', '--user-data-dir=/tmp/chrome_kiosk_data', f'--app={app_url}' ] # Launch Chrome as subprocess process = subprocess.Popen( chrome_args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL ) logging.info(f"Chrome launched with PID: {process.pid}") log_with_server(f"Chrome launched (PID: {process.pid})", hostname, device_ip) return True except Exception as e: logging.error(f"Failed to launch Chrome: {e}") log_with_server(f"ERROR: Chrome launch failed: {str(e)}", hostname, device_ip) return False def install_chrome(hostname, device_ip): """Install Chrome on system if not present""" try: logging.info("Installing Chrome browser...") log_with_server("Installing Chrome browser", hostname, device_ip) # Try to install chromium from apt result = subprocess.run( ['sudo', 'apt-get', 'install', '-y', 'chromium-browser'], capture_output=True, text=True, timeout=300 ) if result.returncode == 0: logging.info("Chrome installed successfully") log_with_server("Chrome installed successfully", hostname, device_ip) return True else: logging.error(f"Chrome installation failed: {result.stderr}") log_with_server(f"Chrome installation failed: {result.stderr}", hostname, device_ip) return False except Exception as e: logging.error(f"Error installing Chrome: {e}") log_with_server(f"Chrome installation error: {str(e)}", hostname, device_ip) return False def launch_app_on_startup(hostname, device_ip, app_url="http://localhost"): """ Setup Chrome to launch automatically on system startup Creates a systemd service file """ service_content = f"""[Unit] Description=Prezenta Work Chrome Application After=network-online.target Wants=network-online.target [Service] Type=simple User={os.environ.get('USER', 'pi')} Environment="DISPLAY=:0" Environment="XAUTHORITY=/home/{os.environ.get('USER', 'pi')}/.Xauthority" ExecStart={get_chrome_path()} --start-maximized --fullscreen --app={app_url} Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target """ try: service_file = "/etc/systemd/system/prezenta-chrome.service" # Write service file with open(service_file, 'w') as f: f.write(service_content) # Enable and start service subprocess.run(['sudo', 'systemctl', 'daemon-reload'], check=True) subprocess.run(['sudo', 'systemctl', 'enable', 'prezenta-chrome.service'], check=True) logging.info("Chrome app service enabled for startup") log_with_server("Chrome app configured for automatic startup", hostname, device_ip) return True except Exception as e: logging.error(f"Failed to setup startup service: {e}") log_with_server(f"Startup service setup failed: {str(e)}", hostname, device_ip) return False