mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-16 21:33:41 +00:00
commit
b88144c864
564 files changed
+71880
-39
No files matched your search
@@ -0,0 +1,66 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
|
||||
// Private host/plugin bridge. These settings never originate from extension JS.
|
||||
class WebViewProfileSettings extends InAppWebViewSettings {
|
||||
WebViewProfileSettings({required this.profileId, required bool debug, super.userAgent})
|
||||
: super(javaScriptEnabled: true, transparentBackground: true, isInspectable: debug);
|
||||
|
||||
final String profileId;
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toMap() => {...super.toMap(), 'gopeedProfileId': profileId};
|
||||
}
|
||||
|
||||
class WebViewProfile {
|
||||
WebViewProfile(this.id);
|
||||
|
||||
final String id;
|
||||
static const _channel = MethodChannel('com.pichillilorenzo/flutter_inappwebview_cookiemanager');
|
||||
|
||||
Future<void> remove() async {
|
||||
await _call('gopeed.removeProfile', {});
|
||||
}
|
||||
|
||||
Future<void> prepare(String proxyUrl) async {
|
||||
await _call('gopeed.prepareProfile', {'proxyUrl': proxyUrl});
|
||||
}
|
||||
|
||||
Future<dynamic> _call(String method, Map<String, dynamic> arguments) =>
|
||||
_channel.invokeMethod(method, {...arguments, 'gopeedProfileId': id});
|
||||
|
||||
Future<List<Cookie>> getCookies({required WebUri url}) async {
|
||||
final values = await _call('getCookies', {'url': url.toString()}) as List? ?? [];
|
||||
return values.map((value) => Cookie.fromMap(Map<String, dynamic>.from(value as Map))!).toList();
|
||||
}
|
||||
|
||||
Future<void> setCookie({
|
||||
required WebUri url,
|
||||
required String name,
|
||||
required String value,
|
||||
String? domain,
|
||||
String path = '/',
|
||||
int? expiresDate,
|
||||
bool isSecure = false,
|
||||
bool isHttpOnly = false,
|
||||
}) async {
|
||||
await _call('setCookie', {
|
||||
'url': url.toString(),
|
||||
'name': name,
|
||||
'value': value,
|
||||
'domain': domain,
|
||||
'path': path,
|
||||
'expiresDate': expiresDate?.toString(),
|
||||
'isSecure': isSecure,
|
||||
'isHttpOnly': isHttpOnly,
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteCookie({required WebUri url, required String name, String? domain, String path = '/'}) async {
|
||||
await _call('deleteCookie', {'url': url.toString(), 'name': name, 'domain': domain, 'path': path});
|
||||
}
|
||||
|
||||
Future<void> deleteAllCookies() async {
|
||||
await _call('deleteAllCookies', {});
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@ import 'dart:collection';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
||||
|
||||
import '../../core/common/start_config.dart';
|
||||
import '../../util/log_util.dart';
|
||||
import '../../util/util.dart';
|
||||
import 'server.dart';
|
||||
import 'webview_profile.dart';
|
||||
|
||||
String buildWebViewExecuteScript({
|
||||
required String channelName,
|
||||
@@ -73,7 +75,6 @@ class WebViewRpcService {
|
||||
|
||||
final Map<String, WebViewRpcPageSession> _pagesById = {};
|
||||
final Completer<void> _overlayReady = Completer<void>();
|
||||
late final CookieManager _cookieManager = CookieManager.instance();
|
||||
|
||||
RpcServerHandle? _server;
|
||||
int _pageSeq = 0;
|
||||
@@ -129,6 +130,9 @@ class WebViewRpcService {
|
||||
switch (method) {
|
||||
case 'webview.isAvailable':
|
||||
return {'available': true};
|
||||
case 'profile.remove':
|
||||
await _removeProfile(_string(params, 'profileId'));
|
||||
return {};
|
||||
case 'page.open':
|
||||
return _openPage(params);
|
||||
case 'page.addInitScript':
|
||||
@@ -162,10 +166,12 @@ class WebViewRpcService {
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> _openPage(Map<String, dynamic> params) async {
|
||||
final profile = WebViewProfile(_string(params, 'profileId'));
|
||||
await profile.prepare(_string(params, 'proxyUrl'));
|
||||
final pageId = 'page-${++_pageSeq}';
|
||||
final session = WebViewRpcPageSession(
|
||||
pageId: pageId,
|
||||
cookieManager: _cookieManager,
|
||||
cookieManager: profile,
|
||||
headless: params['headless'] as bool? ?? false,
|
||||
debug: params['debug'] as bool? ?? false,
|
||||
title: params['title'] as String? ?? '',
|
||||
@@ -185,6 +191,15 @@ class WebViewRpcService {
|
||||
return {'pageId': pageId};
|
||||
}
|
||||
|
||||
Future<void> _removeProfile(String id) async {
|
||||
for (final page in _pagesById.values.where((page) => page.cookieManager.id == id).toList()) {
|
||||
await _closePage(page.pageId);
|
||||
}
|
||||
// Let visible platform views unmount before releasing their native store.
|
||||
await WidgetsBinding.instance.endOfFrame;
|
||||
await WebViewProfile(id).remove();
|
||||
}
|
||||
|
||||
WebViewRpcPageSession _page(Map<String, dynamic> params) {
|
||||
final page = _pagesById[_pageId(params)];
|
||||
if (page == null) {
|
||||
@@ -271,6 +286,9 @@ class WebViewRpcService {
|
||||
if (error is WebViewRpcException) {
|
||||
return {'code': error.code, 'message': error.message};
|
||||
}
|
||||
if (error is PlatformException && error.code == 'UNAVAILABLE') {
|
||||
return {'code': error.code, 'message': error.message ?? 'WebView is unavailable'};
|
||||
}
|
||||
final code = switch (method) {
|
||||
'page.goto' => 'NAVIGATION_FAILED',
|
||||
'page.execute' => 'EVALUATION_FAILED',
|
||||
@@ -293,7 +311,7 @@ class WebViewRpcPageSession {
|
||||
});
|
||||
|
||||
final String pageId;
|
||||
final CookieManager cookieManager;
|
||||
final WebViewProfile cookieManager;
|
||||
final bool headless;
|
||||
final bool debug;
|
||||
final String title;
|
||||
@@ -449,6 +467,8 @@ class WebViewRpcPageSession {
|
||||
'path': cookie.path ?? '/',
|
||||
'secure': cookie.isSecure,
|
||||
'httpOnly': cookie.isHttpOnly,
|
||||
if (cookie.expiresDate != null)
|
||||
'expires': DateTime.fromMillisecondsSinceEpoch(cookie.expiresDate!, isUtc: true).toIso8601String(),
|
||||
};
|
||||
})
|
||||
.toList(growable: false);
|
||||
@@ -523,10 +543,9 @@ class WebViewRpcPageSession {
|
||||
);
|
||||
}
|
||||
|
||||
InAppWebViewSettings get _settings => InAppWebViewSettings(
|
||||
javaScriptEnabled: true,
|
||||
transparentBackground: true,
|
||||
isInspectable: debug,
|
||||
InAppWebViewSettings get _settings => WebViewProfileSettings(
|
||||
profileId: cookieManager.id,
|
||||
debug: debug,
|
||||
userAgent: userAgent.isNotEmpty ? userAgent : null,
|
||||
);
|
||||
|
||||
|
||||
Reference in new issue
Block a user