Skip to content

Commit

Permalink
fix: EWM-362 (#608)
Browse files Browse the repository at this point in the history
* EWM-352. user agent

* EWM-352. Add check url with http and without www

* EWM-352. after analyze
  • Loading branch information
knightsforce authored Oct 31, 2024
1 parent 85ec4a1 commit 57654c1
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 31 deletions.
76 changes: 50 additions & 26 deletions lib/feature/browser/browser_tab_view/browser_tab_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:app/di/di.dart';
import 'package:app/feature/browser/browser.dart';
import 'package:app/feature/browser/browser_tab_view/browser_error_view.dart';
import 'package:app/feature/browser/browser_tab_view/browser_view_events_listener/browser_view_events_listener_cubit.dart';
import 'package:app/feature/browser/browser_user_agent_utils.dart';
import 'package:elementary_helper/elementary_helper.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -63,6 +64,10 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {
'about',
];

static const Duration _scrollTimerDelay = Duration(milliseconds: 100);

static final _log = Logger('BrowserTabView');

// Last SANE Y position (i.e. not overscrolled)
int? _lastScrollY;

Expand All @@ -76,10 +81,10 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {
final List<DelayedScrollEvent> _delayedScrollEvents = [];

// How long to wait before considering scroll event as not overscroll
static const Duration _scrollTimerDelay = Duration(milliseconds: 100);

InAppWebViewController? _webViewController;
PullToRefreshController? _pullToRefreshController;

late final _inpageProvider = InpageProvider(
tabId: widget.tab.id,
approvalsService: inject(),
Expand All @@ -93,13 +98,14 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {

Timer? _screenshotTimer;

static final _log = Logger('BrowserTabView');
final _userAgentState = StateNotifier<String?>();

@override
void initState() {
super.initState();

_setBrowserTabCallbacks();
_setUserAgent();
}

@override
Expand Down Expand Up @@ -177,29 +183,37 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {
loadingBuilder: (_, __) => const SizedBox.shrink(),
errorBuilder: (_, __, ___) => const SizedBox.shrink(),
builder: (_, String? jsStr) {
return InAppWebView(
key: ValueKey(widget.tab.id),
pullToRefreshController: _pullToRefreshController,
initialSettings: initialSettings,
initialUserScripts: UnmodifiableListView<UserScript>([
if (jsStr != null)
UserScript(
source: jsStr,
injectionTime:
UserScriptInjectionTime.AT_DOCUMENT_START,
),
]),
onOverScrolled: _onOverScrolled,
onScrollChanged: _onScrollChanged,
onWebViewCreated: (c) => _onWebViewCreated(c, context),
onLoadStart: _onLoadStart,
onLoadStop: _onLoadStop,
onLoadResource: _onLoadResource,
onReceivedError: _onReceivedError,
onReceivedHttpError: _onReceivedHttpError,
onTitleChanged: _onTitleChanged,
onReceivedHttpAuthRequest: _onReceivedHttpAuthRequest,
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
return StateNotifierBuilder<String?>(
listenableState: _userAgentState,
builder: (_, String? userAgent) {
if (userAgent == null) {
return const SizedBox.shrink();
}
return InAppWebView(
key: ValueKey(widget.tab.id),
pullToRefreshController: _pullToRefreshController,
initialSettings: initialSettings..userAgent = userAgent,
initialUserScripts: UnmodifiableListView<UserScript>([
if (jsStr != null)
UserScript(
source: jsStr,
injectionTime:
UserScriptInjectionTime.AT_DOCUMENT_START,
),
]),
onOverScrolled: _onOverScrolled,
onScrollChanged: _onScrollChanged,
onWebViewCreated: (c) => _onWebViewCreated(c, context),
onLoadStart: _onLoadStart,
onLoadStop: _onLoadStop,
onLoadResource: _onLoadResource,
onReceivedError: _onReceivedError,
onReceivedHttpError: _onReceivedHttpError,
onTitleChanged: _onTitleChanged,
onReceivedHttpAuthRequest: _onReceivedHttpAuthRequest,
shouldOverrideUrlLoading: _shouldOverrideUrlLoading,
);
},
);
},
);
Expand All @@ -223,6 +237,7 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {

_screenshotTimer?.cancel();

_userAgentState.dispose();
super.dispose();
}

Expand Down Expand Up @@ -522,6 +537,10 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {
);
}

Future<void> _setUserAgent() async {
_userAgentState.accept(await platformUserAgent);
}

Future<void> _saveScreenshot({bool force = false}) async {
if ((_screenshotTimer?.isActive ?? false) && !force) {
return;
Expand Down Expand Up @@ -612,14 +631,19 @@ class _BrowserTabViewState extends State<BrowserTabView> with ContextMixin {

final scheme = navigationAction.request.url?.scheme;

if (!_allowSchemes.contains(scheme) && await canLaunchUrl(url)) {
if ((!_allowSchemes.contains(scheme) || _checkIsCustomAppLink(url)) &&
await canLaunchUrl(url)) {
await launchUrl(url);

return NavigationActionPolicy.CANCEL;
}

return NavigationActionPolicy.ALLOW;
}

bool _checkIsCustomAppLink(Uri url) {
return url.origin.startsWith('http') && !url.origin.contains('www');
}
}

// Delayed scroll event dataclass
Expand Down
48 changes: 48 additions & 0 deletions lib/feature/browser/browser_user_agent_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'dart:io';

import 'package:device_info_plus/device_info_plus.dart';

Future<String> platformUserAgent = (() async =>
Platform.isIOS ? await _iOSUserAgent : await _androidUserAgent)();

Future<String> get _androidUserAgent async {
String? androidVersion;
String? deviceModel;
String? buildId;

try {
final deviceInfo = DeviceInfoPlugin();
final androidInfo = await deviceInfo.androidInfo;
androidVersion = androidInfo.version.release;
deviceModel = '; ${androidInfo.model}';
buildId = ' Build/${androidInfo.id}';
} catch (_) {
androidVersion = '12';
}

return 'Mozilla/5.0 '
'(Linux; '
'Android $androidVersion$deviceModel$buildId) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/117.0.0.0 Mobile Safari/537.36';
}

Future<String> get _iOSUserAgent async {
String? iosVersion;
String? deviceModel;

try {
final deviceInfo = DeviceInfoPlugin();
final iosInfo = await deviceInfo.iosInfo;
iosVersion = iosInfo.systemVersion;
deviceModel = iosInfo.utsname.machine;
} catch (_) {
deviceModel = 'iPhone';
iosVersion = '15_0';
}

return 'Mozilla/5.0 ($deviceModel; '
'CPU iPhone OS $iosVersion like Mac OS X) '
'AppleWebKit/605.1.15 (KHTML, like Gecko) '
'Version/$iosVersion Mobile/15E148 Safari/604.1';
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ class AccountAssetsTab extends StatelessWidget {
return BlocProvider<AccountAssetTabCubit>(
create: (_) => AccountAssetTabCubit(
account,
isFirstEntering,
inject<TokenWalletsService>(),
inject<AssetsService>(),
isFirstEntering: isFirstEntering,
),
child: BlocBuilder<AccountAssetTabCubit, AccountAssetTabState>(
builder: (context, state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ part 'account_asset_tab_state.dart';
class AccountAssetTabCubit extends Cubit<AccountAssetTabState> {
AccountAssetTabCubit(
KeyAccount account,
// ignore: avoid_positional_boolean_parameters
this.isFirstEntering,
this.tokenWalletsService,
this.assetsService,
) : tonWallet = account.account.tonWallet,
this.assetsService, {
required this.isFirstEntering,
}) : tonWallet = account.account.tonWallet,
super(
AccountAssetTabState.accounts(account.account.tonWallet, null, 0),
) {
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.3"
device_info_plus:
dependency: "direct main"
description:
name: device_info_plus
sha256: c4af09051b4f0508f6c1dc0a5c085bf014d5c9a4a0678ce1799c2b4d716387a0
url: "https://pub.dev"
source: hosted
version: "11.1.0"
device_info_plus_platform_interface:
dependency: transitive
description:
name: device_info_plus_platform_interface
sha256: "282d3cf731045a2feb66abfe61bbc40870ae50a3ed10a4d3d217556c35c8c2ba"
url: "https://pub.dev"
source: hosted
version: "7.0.1"
diff_match_patch:
dependency: transitive
description:
Expand Down Expand Up @@ -2367,6 +2383,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "5.5.4"
win32_registry:
dependency: transitive
description:
name: win32_registry
sha256: "21ec76dfc731550fd3e2ce7a33a9ea90b828fdf19a5c3bcf556fa992cfa99852"
url: "https://pub.dev"
source: hosted
version: "1.1.5"
xdg_directories:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
clock: 1.1.1
collection: 1.18.0
crypto: 3.0.5
device_info_plus: ^11.1.0
dio: ^5.7.0
dotted_border: 2.1.0
easy_localization: 3.0.6
Expand Down

0 comments on commit 57654c1

Please sign in to comment.