From cb122c7dc06d14d9ca59efce8b24c471cc776e07 Mon Sep 17 00:00:00 2001 From: codeking Date: Sat, 28 Sep 2024 04:22:25 +0200 Subject: [PATCH] Implement support for synchronous profile sessions --- core/controllers/ApplicationController.py | 34 +++++++++++++++-------- core/controllers/ProfileController.py | 4 +-- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/core/controllers/ApplicationController.py b/core/controllers/ApplicationController.py index 42e9560..1dc06d6 100644 --- a/core/controllers/ApplicationController.py +++ b/core/controllers/ApplicationController.py @@ -28,7 +28,9 @@ class ApplicationController: return Application.all() @staticmethod - def launch(version: ApplicationVersion, profile: SessionProfile, port_number: int = None, profile_observer: Optional[ProfileObserver] = None): + def launch(version: ApplicationVersion, profile: SessionProfile, port_number: int = None, asynchronous: bool = True, profile_observer: Optional[ProfileObserver] = None): + + from core.controllers.ProfileController import ProfileController persistent_state_path = f'{profile.get_data_path()}/persistent-state' @@ -56,23 +58,23 @@ class ApplicationController: initialization_file.write(initialization_file_contents) initialization_file.close() - fork_process_id = os.fork() + if asynchronous: - if not fork_process_id: + fork_process_id = os.fork() - process = subprocess.Popen(('xinit', initialization_file_path, '--', '/usr/bin/Xephyr', '-ac', '-title', f'Simplified Privacy - {profile.name or "Unnamed Profile"}', '-screen', profile.resolution, '-no-host-grab', display), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) + if not fork_process_id: - session_state = SessionState(session_state.id, session_state.network_port_numbers, [process.pid]) - SessionStateController.update_or_create(session_state) + ApplicationController.__run_process(initialization_file_path, profile, display, session_state) + ProfileController.disable(profile, False, profile_observer=profile_observer) - os.waitpid(process.pid, 0) + time.sleep(1.0) + sys.exit() - from core.controllers.ProfileController import ProfileController + else: + + ApplicationController.__run_process(initialization_file_path, profile, display, session_state) ProfileController.disable(profile, False, profile_observer=profile_observer) - time.sleep(1.0) - sys.exit() - @staticmethod def _sync(proxies: Optional[dict] = None): @@ -105,3 +107,13 @@ class ApplicationController: else: return ':170' + + @staticmethod + def __run_process(initialization_file_path, profile, display, session_state): + + process = subprocess.Popen(('xinit', initialization_file_path, '--', '/usr/bin/Xephyr', '-ac', '-title', f'Simplified Privacy - {profile.name or "Unnamed Profile"}', '-screen', profile.resolution, '-no-host-grab', display), stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) + + session_state = SessionState(session_state.id, session_state.network_port_numbers, [process.pid]) + SessionStateController.update_or_create(session_state) + + process.wait() diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index 7537a08..d5d2b36 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -42,7 +42,7 @@ class ProfileController: profile_observer.notify('updated', profile) @staticmethod - def enable(profile: Union[SessionProfile, SystemProfile], force: bool = False, pristine: bool = False, profile_observer: ProfileObserver = None, application_version_observer: ApplicationVersionObserver = None, connection_observer: ConnectionObserver = None): + def enable(profile: Union[SessionProfile, SystemProfile], force: bool = False, pristine: bool = False, asynchronous: bool = True, profile_observer: ProfileObserver = None, application_version_observer: ApplicationVersionObserver = None, connection_observer: ConnectionObserver = None): from core.controllers.ConnectionController import ConnectionController @@ -71,7 +71,7 @@ class ProfileController: if profile_observer is not None: profile_observer.notify('enabled', profile) - ApplicationController.launch(application_version, profile, port_number, profile_observer=profile_observer) + ApplicationController.launch(application_version, profile, port_number, asynchronous=asynchronous, profile_observer=profile_observer) if profile.is_system_profile():