Skip to content

Commit 25fe345

Browse files
committed
make chromium controller code more resilient
1 parent 98fef04 commit 25fe345

1 file changed

Lines changed: 49 additions & 17 deletions

File tree

src/modules/fullpageos/filesystem/home/pi/browser_scripts/chromium_controller.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -144,42 +144,74 @@ def run_forever(self):
144144
time.sleep(1)
145145

146146
def _response_received(self, **kwargs):
147-
doc_root = self.tab.DOM.getDocument()['root']
147+
try:
148+
doc_root = self.tab.DOM.getDocument()['root']
149+
150+
if not doc_root.get('children'):
151+
return
148152

149-
if not doc_root['children']:
150-
return
153+
# Safely check frameId - extension frames might not have this
154+
last_child = doc_root['children'][-1]
155+
frame_id = last_child.get('frameId')
156+
kwargs_frame_id = kwargs.get('frame', {}).get('id')
151157

152-
if not doc_root['children'][-1]['frameId'] == kwargs['frame']['id']:
153-
return
158+
if not frame_id or not kwargs_frame_id or frame_id != kwargs_frame_id:
159+
return
154160

155-
if self.mute_time > 0:
156-
subprocess.run(['amixer', 'set', 'PCM', 'mute'], check=True)
161+
if self.mute_time > 0:
162+
subprocess.run(['amixer', 'set', 'PCM', 'mute'], check=True)
157163

158-
self.mute_time_left = self.mute_time
164+
self.mute_time_left = self.mute_time
159165

160-
if self.initial_load:
161-
self.initial_load = False
166+
if self.initial_load:
167+
self.initial_load = False
168+
except Exception as e:
169+
print(f"Error in _response_received: {e}")
170+
traceback.print_exc()
162171

163172
def _loading_failed(self, **kwargs):
164-
# We only care about the main page loading, not of any subelement
165-
if (kwargs['type'] != 'Document' or self.tab.DOM.getDocument()['root']['children'][-1]['frameId'] != kwargs['frameId']):
166-
return
173+
try:
174+
# We only care about the main page loading, not of any subelement
175+
if kwargs.get('type') != 'Document':
176+
return
167177

168-
time.sleep(5)
169-
self._load_page()
178+
doc_root = self.tab.DOM.getDocument()['root']
179+
if not doc_root.get('children'):
180+
return
181+
182+
last_child = doc_root['children'][-1]
183+
frame_id = last_child.get('frameId')
184+
kwargs_frame_id = kwargs.get('frameId')
185+
186+
if not frame_id or not kwargs_frame_id or frame_id != kwargs_frame_id:
187+
return
188+
189+
print(f"Main page loading failed, retrying in 5 seconds...")
190+
time.sleep(5)
191+
self._load_page()
192+
except Exception as e:
193+
print(f"Error in _loading_failed: {e}")
194+
traceback.print_exc()
170195

171196
def _load_page(self):
172197
# Use dynamic jace URL instead of cycling through kiosk_urls
173198
self.initial_load = True
174199

200+
print(f"[Controller] Navigating to: {self.current_jace_url}")
201+
175202
# Use location.replace() to navigate without adding to history
176203
js_code = f"window.location.replace('{self.current_jace_url}');"
177204
try:
178205
self.tab.Runtime.evaluate(expression=js_code)
206+
print(f"[Controller] Navigation via JavaScript succeeded")
179207
except Exception as e:
180-
print(f"Failed to use location.replace, falling back to navigate: {e}")
208+
print(f"[Controller] Failed to use location.replace, falling back to navigate: {e}")
181209
# Fallback to normal navigation if Runtime.evaluate fails
182-
self.tab.Page.navigate(url=self.current_jace_url)
210+
try:
211+
self.tab.Page.navigate(url=self.current_jace_url)
212+
print(f"[Controller] Navigation via Page.navigate succeeded")
213+
except Exception as e2:
214+
print(f"[Controller] Page.navigate also failed: {e2}")
183215

184216

185217
while True:

0 commit comments

Comments
 (0)