chore(webview): log sleep, wake and what keeps the web views awake
- diagnostics lines "webview: asleep", "webview: awake", "webview: stays awake, <reason>" - the reason is logged once per change, only after the quiet period Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -132,6 +132,7 @@ fn transport(
132
132
let transport = webview::start(webview::HostConfig {
133
133
user_data_folder,
134
134
window_title: format!("{APP_NAME} - Sign in"),
135
log: |line| app_base::diagnostics::record(line),
135
136
});
136
137
task_dialog::install_host(transport.clone());
137
138
return transport;
@@ -132,6 +132,7 @@ fn transport(132
let transport = webview::start(webview::HostConfig {132
let transport = webview::start(webview::HostConfig {133
user_data_folder,133
user_data_folder,134
window_title: format!("{APP_NAME} - Sign in"),134
window_title: format!("{APP_NAME} - Sign in"),135
log: |line| app_base::diagnostics::record(line),135
});136
});136
task_dialog::install_host(transport.clone());137
task_dialog::install_host(transport.clone());137
return transport;138
return transport;@@ -25,7 +25,7 @@ use crate::cdp_failure::{CallContext, failure_reason, tab_state};
25
25
use crate::fanout::Subscribers;
26
26
use crate::host_dialog::{self, Dialog};
27
27
use crate::park::{ParkDecision, ParkGate};
28
use crate::sleep::{Activity, Sleep};
28
use crate::sleep::{Activity, Sleep, blocker};
29
29
use crate::transport::{HostState, UiRequest, WebViewTransport};
30
30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};
31
31
@@ -46,6 +46,7 @@ const WINDOW_HEIGHT: i32 = 800;
46
46
pub struct HostConfig {
47
47
pub user_data_folder: PathBuf,
48
48
pub window_title: String,
49
pub log: fn(&str),
49
50
}
50
51
51
52
struct View {
@@ -66,6 +67,7 @@ pub(crate) struct Shared {
66
67
restarting: Cell<bool>,
67
68
last_login_prompt: Cell<Option<Instant>>,
68
69
sleep: RefCell<Sleep<TabCommand>>,
70
sleep_blocker: Cell<Option<&'static str>>,
69
71
}
70
72
71
73
thread_local! {
@@ -128,6 +130,7 @@ fn run(
128
130
restarting: Cell::new(false),
129
131
last_login_prompt: Cell::new(None),
130
132
sleep: RefCell::new(Sleep::default()),
133
sleep_blocker: Cell::new(None),
131
134
});
132
135
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));
133
136
load(&shared);
@@ -623,10 +626,20 @@ fn maybe_sleep(shared: &Rc<Shared>) {
623
626
dialogs_open: !shared.dialogs.borrow().is_empty(),
624
627
subscribed: shared.subscribers.borrow_mut().has_live(),
625
628
};
626
if !shared.sleep.borrow().should_sleep(activity, Instant::now()) {
629
if !shared.sleep.borrow().quiet(Instant::now()) {
630
return;
631
}
632
let blocked = blocker(activity);
633
if blocked != shared.sleep_blocker.replace(blocked)
634
&& let Some(reason) = blocked
635
{
636
(shared.config.log)(&format!("webview: stays awake, {reason}"));
637
}
638
if blocked.is_some() {
627
639
return;
628
640
}
629
641
shared.sleep.borrow_mut().fall_asleep();
642
(shared.config.log)("webview: asleep");
630
643
let views: Vec<View> = shared.views.borrow_mut().drain().map(|(_, view)| view).collect();
631
644
for view in views {
632
645
unsafe {
@@ -641,6 +654,10 @@ fn wake_views(shared: &Rc<Shared>, full: Option<App>) {
641
654
if !shared.sleep.borrow_mut().wake() {
642
655
return;
643
656
}
657
(shared.config.log)(&match full {
658
Some(app) => format!("webview: awake, {app} reloads"),
659
None => "webview: awake".to_owned(),
660
});
644
661
let outcome = create_views(shared, |app| if Some(app) == full { app.start_url() } else { app.park_url() });
645
662
if let Err(reason) = outcome {
646
663
let stranded = shared.sleep.borrow_mut().reset();
@@ -25,7 +25,7 @@ use crate::cdp_failure::{CallContext, failure_reason, tab_state};25
use crate::fanout::Subscribers;25
use crate::fanout::Subscribers;26
use crate::host_dialog::{self, Dialog};26
use crate::host_dialog::{self, Dialog};27
use crate::park::{ParkDecision, ParkGate};27
use crate::park::{ParkDecision, ParkGate};28
use crate::sleep::{Activity, Sleep};28
use crate::sleep::{Activity, Sleep, blocker};29
use crate::transport::{HostState, UiRequest, WebViewTransport};29
use crate::transport::{HostState, UiRequest, WebViewTransport};30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};30
use crate::{BROWSER_ARGUMENTS, FORWARDED_EVENTS};31
31
@@ -46,6 +46,7 @@ const WINDOW_HEIGHT: i32 = 800;46
pub struct HostConfig {46
pub struct HostConfig {47
pub user_data_folder: PathBuf,47
pub user_data_folder: PathBuf,48
pub window_title: String,48
pub window_title: String,49
pub log: fn(&str),49
}50
}50
51
51
struct View {52
struct View {@@ -66,6 +67,7 @@ pub(crate) struct Shared {66
restarting: Cell<bool>,67
restarting: Cell<bool>,67
last_login_prompt: Cell<Option<Instant>>,68
last_login_prompt: Cell<Option<Instant>>,68
sleep: RefCell<Sleep<TabCommand>>,69
sleep: RefCell<Sleep<TabCommand>>,70
sleep_blocker: Cell<Option<&'static str>>,69
}71
}70
72
71
thread_local! {73
thread_local! {@@ -128,6 +130,7 @@ fn run(128
restarting: Cell::new(false),130
restarting: Cell::new(false),129
last_login_prompt: Cell::new(None),131
last_login_prompt: Cell::new(None),130
sleep: RefCell::new(Sleep::default()),132
sleep: RefCell::new(Sleep::default()),133
sleep_blocker: Cell::new(None),131
});134
});132
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));135
SHARED.with(|slot| *slot.borrow_mut() = Some(shared.clone()));133
load(&shared);136
load(&shared);@@ -623,10 +626,20 @@ fn maybe_sleep(shared: &Rc<Shared>) {623
dialogs_open: !shared.dialogs.borrow().is_empty(),626
dialogs_open: !shared.dialogs.borrow().is_empty(),624
subscribed: shared.subscribers.borrow_mut().has_live(),627
subscribed: shared.subscribers.borrow_mut().has_live(),625
};628
};626
if !shared.sleep.borrow().should_sleep(activity, Instant::now()) {629
if !shared.sleep.borrow().quiet(Instant::now()) {630
return;631
}632
let blocked = blocker(activity);633
if blocked != shared.sleep_blocker.replace(blocked)634
&& let Some(reason) = blocked635
{636
(shared.config.log)(&format!("webview: stays awake, {reason}"));637
}638
if blocked.is_some() {627
return;639
return;628
}640
}629
shared.sleep.borrow_mut().fall_asleep();641
shared.sleep.borrow_mut().fall_asleep();642
(shared.config.log)("webview: asleep");630
let views: Vec<View> = shared.views.borrow_mut().drain().map(|(_, view)| view).collect();643
let views: Vec<View> = shared.views.borrow_mut().drain().map(|(_, view)| view).collect();631
for view in views {644
for view in views {632
unsafe {645
unsafe {@@ -641,6 +654,10 @@ fn wake_views(shared: &Rc<Shared>, full: Option<App>) {641
if !shared.sleep.borrow_mut().wake() {654
if !shared.sleep.borrow_mut().wake() {642
return;655
return;643
}656
}657
(shared.config.log)(&match full {658
Some(app) => format!("webview: awake, {app} reloads"),659
None => "webview: awake".to_owned(),660
});644
let outcome = create_views(shared, |app| if Some(app) == full { app.start_url() } else { app.park_url() });661
let outcome = create_views(shared, |app| if Some(app) == full { app.start_url() } else { app.park_url() });645
if let Err(reason) = outcome {662
if let Err(reason) = outcome {646
let stranded = shared.sleep.borrow_mut().reset();663
let stranded = shared.sleep.borrow_mut().reset();@@ -17,6 +17,20 @@ pub(crate) struct Activity {
17
17
pub subscribed: bool,
18
18
}
19
19
20
pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {
21
[
22
(activity.restarting, "restarting"),
23
(!activity.all_parked, "an app is not parked"),
24
(activity.login_pending, "sign-in pending"),
25
(activity.held, "an app is held"),
26
(activity.in_flight, "a protocol call is running"),
27
(activity.dialogs_open, "a dialog is open"),
28
(activity.subscribed, "a tab subscriber is attached"),
29
]
30
.into_iter()
31
.find_map(|(blocked, reason)| blocked.then_some(reason))
32
}
33
20
34
/// The web views close once both apps are parked and idle, and come back on the next protocol call, at the park page.
21
35
#[derive(Debug)]
22
36
pub(crate) struct Sleep<C> {
@@ -42,16 +56,12 @@ impl<C> Sleep<C> {
42
56
}
43
57
44
58
pub(crate) fn should_sleep(&self, activity: Activity, now: Instant) -> bool {
45
!self.asleep
46
&& self.loading.is_empty()
47
&& now.duration_since(self.last_use) >= SLEEP_AFTER
48
&& activity.all_parked
49
&& !(activity.restarting
50
|| activity.login_pending
51
|| activity.held
52
|| activity.in_flight
53
|| activity.dialogs_open
54
|| activity.subscribed)
59
self.quiet(now) && blocker(activity).is_none()
60
}
61
62
/// Answers whether the views have been idle long enough to close.
63
pub(crate) fn quiet(&self, now: Instant) -> bool {
64
!self.asleep && self.loading.is_empty() && now.duration_since(self.last_use) >= SLEEP_AFTER
55
65
}
56
66
57
67
pub(crate) fn fall_asleep(&mut self) {
@@ -123,6 +133,12 @@ mod tests {
123
133
}
124
134
}
125
135
136
#[test]
137
fn the_first_blocker_is_named() {
138
assert_eq!(blocker(idle()), None);
139
assert_eq!(blocker(Activity { held: true, dialogs_open: true, ..idle() }), Some("an app is held"));
140
}
141
126
142
#[test]
127
143
fn calls_wait_for_the_page_after_waking_and_run_in_order() {
128
144
let mut sleep = Sleep::default();
@@ -17,6 +17,20 @@ pub(crate) struct Activity {17
pub subscribed: bool,17
pub subscribed: bool,18
}18
}19
19
20
pub(crate) fn blocker(activity: Activity) -> Option<&'static str> {21
[22
(activity.restarting, "restarting"),23
(!activity.all_parked, "an app is not parked"),24
(activity.login_pending, "sign-in pending"),25
(activity.held, "an app is held"),26
(activity.in_flight, "a protocol call is running"),27
(activity.dialogs_open, "a dialog is open"),28
(activity.subscribed, "a tab subscriber is attached"),29
]30
.into_iter()31
.find_map(|(blocked, reason)| blocked.then_some(reason))32
}33
20
/// The web views close once both apps are parked and idle, and come back on the next protocol call, at the park page.34
/// The web views close once both apps are parked and idle, and come back on the next protocol call, at the park page.21
#[derive(Debug)]35
#[derive(Debug)]22
pub(crate) struct Sleep<C> {36
pub(crate) struct Sleep<C> {@@ -42,16 +56,12 @@ impl<C> Sleep<C> {42
}56
}43
57
44
pub(crate) fn should_sleep(&self, activity: Activity, now: Instant) -> bool {58
pub(crate) fn should_sleep(&self, activity: Activity, now: Instant) -> bool {45
!self.asleep59
self.quiet(now) && blocker(activity).is_none()46
&& self.loading.is_empty()60
}47
&& now.duration_since(self.last_use) >= SLEEP_AFTER61
48
&& activity.all_parked62
/// Answers whether the views have been idle long enough to close.49
&& !(activity.restarting63
pub(crate) fn quiet(&self, now: Instant) -> bool {50
|| activity.login_pending64
!self.asleep && self.loading.is_empty() && now.duration_since(self.last_use) >= SLEEP_AFTER51
|| activity.held52
|| activity.in_flight53
|| activity.dialogs_open54
|| activity.subscribed)55
}65
}56
66
57
pub(crate) fn fall_asleep(&mut self) {67
pub(crate) fn fall_asleep(&mut self) {@@ -123,6 +133,12 @@ mod tests {123
}133
}124
}134
}125
135
136
#[test]137
fn the_first_blocker_is_named() {138
assert_eq!(blocker(idle()), None);139
assert_eq!(blocker(Activity { held: true, dialogs_open: true, ..idle() }), Some("an app is held"));140
}141
126
#[test]142
#[test]127
fn calls_wait_for_the_page_after_waking_and_run_in_order() {143
fn calls_wait_for_the_page_after_waking_and_run_in_order() {128
let mut sleep = Sleep::default();144
let mut sleep = Sleep::default();