Skip to content

Commit 8541716

Browse files
authored
Safely access browser navigator.deviceMemory (#3268)
1 parent 819c1e7 commit 8541716

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
- Prefix firebase remote config feature flags with `firebase:` ([#3258](https://github.com/getsentry/sentry-dart/pull/3258))
1313

14+
### Fixes
15+
16+
- Safely access browser `navigator.deviceMemory` ([#3268](https://github.com/getsentry/sentry-dart/pull/3268))
17+
1418
## 9.7.0-beta.5
1519

1620
### Dependencies

packages/dart/lib/src/event_processor/enricher/web_enricher_event_processor.dart

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:web/web.dart' as web show window, Window, Navigator;
2+
import 'dart:js_interop';
23

34
import '../../../sentry.dart';
45
import 'enricher_event_processor.dart';
@@ -69,8 +70,7 @@ class WebEnricherEventProcessor implements EnricherEventProcessor {
6970

7071
int? _getMemorySize() {
7172
// https://developer.mozilla.org/en-US/docs/Web/API/Navigator/deviceMemory
72-
// ignore: invalid_null_aware_operator
73-
final size = _window.navigator.deviceMemory?.toDouble();
73+
final size = _window.navigator.safeDeviceMemory?.toDouble();
7474
final memoryByteSize = size != null ? size * 1024 * 1024 * 1024 : null;
7575
return memoryByteSize?.toInt();
7676
}
@@ -116,7 +116,15 @@ class WebEnricherEventProcessor implements EnricherEventProcessor {
116116
}
117117
}
118118

119-
extension on web.Navigator {
120-
// ignore: unused_element
121-
external double? get deviceMemory;
119+
/// Some Navigator properties are not fully supported in all browsers.
120+
/// However, package:web does not provide a safe way to access these properties,
121+
/// and assumes they are always not null.
122+
///
123+
/// This extension provides a safe way to access these properties.
124+
///
125+
/// See: https://github.com/dart-lang/web/issues/326
126+
/// https://github.com/fluttercommunity/plus_plugins/issues/3391
127+
extension SafeNavigationGetterExtensions on web.Navigator {
128+
@JS('deviceMemory')
129+
external double? get safeDeviceMemory;
122130
}

packages/dart/test/event_processor/enricher/web_enricher_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:sentry/sentry.dart';
55
import 'package:sentry/src/event_processor/enricher/web_enricher_event_processor.dart';
66
import 'package:sentry/src/platform/mock_platform.dart';
77
import 'package:test/test.dart';
8+
import 'package:web/web.dart' as web;
89

910
import '../../mocks.dart';
1011
import '../../test_utils.dart';
@@ -195,6 +196,18 @@ void main() {
195196
expect(sentryOptions.eventProcessors.map((e) => e.runtimeType.toString()),
196197
contains('$WebEnricherEventProcessor'));
197198
});
199+
200+
test('SafeNavigationGetterExtensions safely accesses deviceMemory', () {
201+
// Test that the extension can safely access deviceMemory without throwing
202+
final navigator = web.window.navigator;
203+
204+
// This should not throw an exception even if deviceMemory is not supported
205+
final deviceMemory = navigator.safeDeviceMemory;
206+
207+
// deviceMemory can be null if not supported by the browser
208+
// or a double value if supported
209+
expect(deviceMemory == null || deviceMemory > 0, isTrue);
210+
});
198211
});
199212
}
200213

0 commit comments

Comments
 (0)