Fix school browser initialization and redirect detection; improve diagnostics

This commit is contained in:
Codex
2026-10-15 12:23:35 +08:00
parent 46e26ec548
commit a4c2df838e
20 changed files with 530 additions and 17 deletions
+46 -5
View File
@@ -12,6 +12,7 @@ import 'school_page_adapter.dart';
import 'school_exam_adapter.dart';
import 'school_login_flow.dart';
import 'school_page_probe.dart';
import 'school_browser.dart';
enum SchoolConnectionState {
signedOut,
@@ -22,7 +23,28 @@ enum SchoolConnectionState {
}
class SchoolSession extends ChangeNotifier {
SchoolSession();
SchoolSession({this.configureBrowser = configureSchoolBrowser});
final SchoolBrowserConfigurator configureBrowser;
Future<SchoolBrowserInfo>? _browserReady;
String? browserVersion;
Future<void> _loadRequest(Uri uri) async {
final controller = web, generation = _pageGeneration, serial = _openSerial;
try {
final info = await (_browserReady ??= configureBrowser(
controller,
'graduate',
));
if (!hasOpened || generation != _pageGeneration || serial != _openSerial) {
return;
}
browserVersion = info.chromiumVersion;
await controller.loadRequest(uri);
} catch (_) {
_browserReady = null;
rethrow;
}
}
// Creating Android WebView starts its browser engine. Do it on school access,
// not while the student is opening an offline timetable.
late final WebViewController web = _createWeb();
@@ -38,7 +60,7 @@ class SchoolSession extends ChangeNotifier {
if (upgrade != null) {
final generation = _pageGeneration;
try {
await web.loadRequest(upgrade);
await _loadRequest(upgrade);
} catch (_) {
if (generation == _pageGeneration) {
_failure(
@@ -54,6 +76,12 @@ class SchoolSession extends ChangeNotifier {
uri,
isMainFrame: request.isMainFrame,
)) {
DiagnosticLog.instance.resourceFailure(
'graduate',
request.url,
code: 'blocked_navigation',
mainFrame: request.isMainFrame,
);
if (kDebugMode && request.isMainFrame) {
debugPrint(
'Blocked school navigation origin: ${uri?.scheme}://${uri?.host}',
@@ -80,6 +108,13 @@ class SchoolSession extends ChangeNotifier {
},
onPageFinished: _loaded,
onWebResourceError: (error) async {
DiagnosticLog.instance.resourceFailure(
'graduate',
error.url,
code: 'web_resource_error',
status: error.errorCode,
mainFrame: error.isForMainFrame,
);
if (error.isForMainFrame != true || error.url == null) return;
final generation = _pageGeneration;
final current = await web.currentUrl();
@@ -94,6 +129,12 @@ class SchoolSession extends ChangeNotifier {
}
},
onHttpError: (error) async {
DiagnosticLog.instance.resourceFailure(
'graduate',
error.request?.uri.toString(),
code: 'http_error',
status: error.response?.statusCode,
);
final generation = _pageGeneration;
final current = Uri.tryParse(await web.currentUrl() ?? '');
if (hasOpened &&
@@ -231,7 +272,7 @@ class SchoolSession extends ChangeNotifier {
notifyListeners();
if (needsTermSelection) return;
try {
await web.loadRequest(connected ? _target : authUrl);
await _loadRequest(connected ? _target : authUrl);
} catch (_) {
if (serial == _openSerial && generation == _pageGeneration) {
_failure('学校入口无法打开,请检查校园网络后重试。', code: 'entry_load_failed');
@@ -264,7 +305,7 @@ class SchoolSession extends ChangeNotifier {
_armTimeout();
notifyListeners();
try {
await web.loadRequest(_target);
await _loadRequest(_target);
} catch (_) {
if (serial == _openSerial && generation == _pageGeneration) {
_failure('学校学期查询页面暂时无法打开,请重试。', code: 'query_load_failed');
@@ -499,7 +540,7 @@ class SchoolSession extends ChangeNotifier {
_armTimeout();
notifyListeners();
try {
await web.loadRequest(next);
await _loadRequest(next);
} catch (_) {
if (generation == _pageGeneration) {
_failure('课表查询页面暂时无法打开,请重试。', code: 'query_load_failed');