226 lines
7.2 KiB
Dart
226 lines
7.2 KiB
Dart
/*
|
|
* Copyright (c) 2024-2026 Fabian Freund.
|
|
*
|
|
* This file is part of WebLibre
|
|
* (see https://weblibre.eu).
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
import 'package:fast_equatable/fast_equatable.dart';
|
|
import 'package:riverpod/riverpod.dart';
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
import 'package:weblibre/data/models/web_page_info.dart';
|
|
import 'package:weblibre/domain/services/generic_website.dart';
|
|
import 'package:weblibre/extensions/ref_cache.dart';
|
|
import 'package:weblibre/extensions/uri.dart';
|
|
import 'package:weblibre/features/geckoview/domain/entities/states/tab.dart';
|
|
import 'package:weblibre/features/geckoview/domain/providers/tab_state.dart';
|
|
import 'package:weblibre/features/geckoview/features/tabs/data/entities/tab_mode.dart';
|
|
import 'package:weblibre/features/geckoview/features/tabs/data/providers.dart';
|
|
import 'package:weblibre/features/geckoview/features/tabs/domain/repositories/container.dart';
|
|
import 'package:weblibre/features/geckoview/features/tabs/domain/repositories/tab.dart';
|
|
import 'package:weblibre/features/proxy/data/proxy_connection.dart';
|
|
import 'package:weblibre/features/tor/domain/services/tor_proxy.dart';
|
|
import 'package:weblibre/features/user/data/models/proxy_routing_settings.dart';
|
|
import 'package:weblibre/features/user/domain/repositories/proxy_routing_settings.dart';
|
|
|
|
part 'website_title.g.dart';
|
|
|
|
sealed class _PageInfoProxyAssignment {
|
|
const _PageInfoProxyAssignment();
|
|
}
|
|
|
|
final class _PageInfoDirectAssignment extends _PageInfoProxyAssignment {
|
|
const _PageInfoDirectAssignment();
|
|
}
|
|
|
|
final class _PageInfoExplicitProxyAssignment extends _PageInfoProxyAssignment {
|
|
final ProxyConnectionId proxyConnectionId;
|
|
|
|
const _PageInfoExplicitProxyAssignment(this.proxyConnectionId);
|
|
}
|
|
|
|
@Riverpod()
|
|
class CompletePageInfo extends _$CompletePageInfo {
|
|
@override
|
|
AsyncValue<WebPageInfo> build(TabState cached) {
|
|
ref.cacheFor(const Duration(minutes: 2));
|
|
|
|
if (cached.isPageInfoComplete || !cached.url.isHttpOrHttps) {
|
|
return AsyncData(cached);
|
|
}
|
|
|
|
ref.listen(
|
|
fireImmediately: true,
|
|
tabStateProvider(cached.id).select((value) => value?.title),
|
|
(previous, next) {
|
|
if (next != null) {
|
|
final current = stateOrNull?.value ?? cached;
|
|
state = AsyncData(current.copyWith.title(next));
|
|
}
|
|
},
|
|
);
|
|
|
|
ref.listen(
|
|
fireImmediately: true,
|
|
tabStateProvider(cached.id).select((value) => value?.favicon),
|
|
(previous, next) {
|
|
if (next != null) {
|
|
final current = stateOrNull?.value ?? cached;
|
|
state = AsyncData(current.copyWith.favicon(next));
|
|
}
|
|
},
|
|
);
|
|
|
|
return AsyncData(cached);
|
|
}
|
|
}
|
|
|
|
@Riverpod()
|
|
Future<WebPageInfo> pageInfo(
|
|
Ref ref,
|
|
Uri url, {
|
|
required bool isImageRequest,
|
|
}) async {
|
|
final link = ref.cacheFor(const Duration(minutes: 2));
|
|
|
|
final tabState = ref.read(selectedTabStateProvider);
|
|
|
|
int? proxyPort;
|
|
if (tabState?.id != null) {
|
|
final containerData = await ref
|
|
.read(tabDataRepositoryProvider.notifier)
|
|
.getTabContainerData(tabState!.id);
|
|
|
|
final proxyRoutingSettings = ref.read(
|
|
proxyRoutingSettingsWithDefaultsProvider,
|
|
);
|
|
|
|
final isolatedAssignment = tabState.isolationContextId != null
|
|
? await _pageInfoAssignmentForIsolationContext(
|
|
ref,
|
|
tabState.isolationContextId!,
|
|
)
|
|
: null;
|
|
|
|
final effectiveContainerProxyConnectionId = switch (isolatedAssignment) {
|
|
_PageInfoExplicitProxyAssignment(:final proxyConnectionId) =>
|
|
proxyConnectionId,
|
|
_PageInfoDirectAssignment() => null,
|
|
null => containerData?.metadata.proxyConnectionId,
|
|
};
|
|
final bypassesGlobalProxy =
|
|
isolatedAssignment is _PageInfoDirectAssignment ||
|
|
(isolatedAssignment == null &&
|
|
containerData?.metadata.bypassGlobalProxy == true);
|
|
final usesGlobalRegularTor =
|
|
tabState.tabMode is! PrivateTabMode &&
|
|
effectiveContainerProxyConnectionId == null &&
|
|
!bypassesGlobalProxy &&
|
|
proxyRoutingSettings.regularTabsMode ==
|
|
ProxyRegularTabRoutingMode.all &&
|
|
proxyRoutingSettings.regularTabsProxyConnectionId
|
|
is TorProxyConnectionId;
|
|
|
|
if (effectiveContainerProxyConnectionId is TorProxyConnectionId ||
|
|
usesGlobalRegularTor ||
|
|
(tabState.tabMode is PrivateTabMode &&
|
|
proxyRoutingSettings.privateTabsProxyConnectionId
|
|
is TorProxyConnectionId)) {
|
|
proxyPort = await ref.read(
|
|
torProxyServiceProvider.selectAsync((value) => value.socksPort),
|
|
);
|
|
|
|
if (proxyPort == null) {
|
|
throw Exception('Could not proxy request');
|
|
}
|
|
}
|
|
}
|
|
|
|
final result = await ref
|
|
.watch(genericWebsiteServiceProvider.notifier)
|
|
.fetchPageInfo(
|
|
url: url,
|
|
isImageRequest: isImageRequest,
|
|
proxyPort: proxyPort,
|
|
);
|
|
|
|
if (!result.isSuccess) {
|
|
link.close();
|
|
}
|
|
|
|
return result.value;
|
|
}
|
|
|
|
Future<_PageInfoProxyAssignment?> _pageInfoAssignmentForIsolationContext(
|
|
Ref ref,
|
|
String contextId,
|
|
) async {
|
|
final db = ref.read(tabDatabaseProvider);
|
|
final pairs = await db.tabDao.isolatedContextContainerPairs().get();
|
|
final containerIds = pairs
|
|
.where((pair) => pair.isolationContextId == contextId)
|
|
.map((pair) => pair.containerId)
|
|
.nonNulls
|
|
.toSet();
|
|
|
|
if (containerIds.isEmpty) return null;
|
|
|
|
final containers = await ref
|
|
.read(containerRepositoryProvider.notifier)
|
|
.getAllContainersWithCount();
|
|
final matchedContainers = containers.where(
|
|
(container) => containerIds.contains(container.id),
|
|
);
|
|
final proxyIds =
|
|
matchedContainers
|
|
.map((container) => container.metadata.proxyConnectionId?.encode())
|
|
.nonNulls
|
|
.toSet()
|
|
.toList()
|
|
..sort();
|
|
|
|
if (proxyIds.isNotEmpty) {
|
|
final proxyConnectionId = ProxyConnectionId.decode(proxyIds.first);
|
|
return proxyConnectionId != null
|
|
? _PageInfoExplicitProxyAssignment(proxyConnectionId)
|
|
: null;
|
|
}
|
|
final matchedContainerList = matchedContainers.toList();
|
|
if (matchedContainerList.isNotEmpty &&
|
|
matchedContainerList.every(
|
|
(container) => container.metadata.bypassGlobalProxy,
|
|
)) {
|
|
return const _PageInfoDirectAssignment();
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@Riverpod()
|
|
AsyncValue<EquatableValue<Set<Uri>?>> websiteFeedProvider(
|
|
Ref ref,
|
|
String tabId,
|
|
) {
|
|
final tabState = ref.watch(tabStateProvider(tabId))!;
|
|
final feeds = ref.watch(
|
|
pageInfoProvider(
|
|
tabState.url,
|
|
isImageRequest: false,
|
|
).select((value) => value.whenData((data) => EquatableValue(data.feeds))),
|
|
);
|
|
|
|
return feeds;
|
|
}
|