-
Notifications
You must be signed in to change notification settings - Fork 189
feat(mobile): optional crash reporting #2142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
brow
wants to merge
6
commits into
main
Choose a base branch
from
tomb/mobile-sentry-crash-reporting
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fb375dd
feat(mobile): add consent-gated crash reporting
bdfc94b
fix(mobile): keep startup independent of crash reporting
63a9348
fix(mobile): preserve diagnostics revocation
a072957
feat(mobile): default crash reporting on
e73f3c2
fix(mobile): retry diagnostics teardown
60877dd
fix(mobile): restore diagnostics heading
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export 'diagnostics_controller.dart'; | ||
| export 'diagnostics_provider.dart'; | ||
| export 'sentry_config.dart'; |
151 changes: 151 additions & 0 deletions
151
mobile/lib/shared/diagnostics/diagnostics_controller.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import 'dart:async'; | ||
|
|
||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:sentry_flutter/sentry_flutter.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
|
|
||
| import 'sentry_config.dart'; | ||
|
|
||
| const diagnosticsConsentPreferenceKey = 'buzz_crash_reporting_consent'; | ||
| const _diagnosticsEnabledByDefault = true; | ||
|
|
||
| typedef DiagnosticsLog = void Function(String message); | ||
|
|
||
| abstract interface class CrashReporter { | ||
| Future<void> initialize(SentryConfig config); | ||
| Future<void> close(); | ||
| } | ||
|
|
||
| class SentryCrashReporter implements CrashReporter { | ||
| const SentryCrashReporter(); | ||
|
|
||
| @override | ||
| Future<void> initialize(SentryConfig config) { | ||
| return SentryFlutter.init(config.applyTo); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> close() => Sentry.close(); | ||
| } | ||
|
|
||
| class DiagnosticsController extends ChangeNotifier { | ||
| DiagnosticsController({ | ||
| required SharedPreferences preferences, | ||
| required SentryConfig config, | ||
| required CrashReporter crashReporter, | ||
| DiagnosticsLog? log, | ||
| }) : _preferences = preferences, | ||
| _config = config, | ||
| _crashReporter = crashReporter, | ||
| _log = log ?? debugPrint, | ||
| _consentGranted = | ||
| preferences.getBool(diagnosticsConsentPreferenceKey) ?? | ||
| _diagnosticsEnabledByDefault; | ||
|
|
||
| final SharedPreferences _preferences; | ||
| final SentryConfig _config; | ||
| final CrashReporter _crashReporter; | ||
| final DiagnosticsLog _log; | ||
|
|
||
| bool _consentGranted; | ||
| bool _initialized = false; | ||
| Future<void> _pendingOperation = Future.value(); | ||
|
|
||
| bool get consentGranted => _consentGranted; | ||
| bool get isConfigured => _config.isConfigured; | ||
|
|
||
| /// Applies persisted consent before the app starts rendering. | ||
| Future<void> applyStartupConsent() => _serialize(_applyCurrentConsent); | ||
|
|
||
| /// Persists consent and applies it immediately. | ||
| Future<void> setConsent(bool granted) { | ||
| return _serialize(() async { | ||
| if (_consentGranted == granted) { | ||
| // A failed close keeps the runtime initialized even though the | ||
| // persisted preference and visible control are already off. Allow the | ||
| // same revocation to retry teardown until it succeeds. | ||
| if (!granted && _initialized) { | ||
| await _applyCurrentConsent(); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (granted && !_config.isConfigured) { | ||
| _log( | ||
| 'Diagnostics unchanged: SENTRY_DSN is empty; consent not enabled.', | ||
| ); | ||
| throw StateError('Crash reporting is unavailable in this build'); | ||
| } | ||
|
|
||
| final previousConsent = _consentGranted; | ||
| var consentPersisted = false; | ||
| _consentGranted = granted; | ||
| notifyListeners(); | ||
| try { | ||
| final persisted = await _preferences.setBool( | ||
| diagnosticsConsentPreferenceKey, | ||
| granted, | ||
| ); | ||
| if (!persisted) { | ||
| throw StateError('Failed to persist diagnostics consent'); | ||
| } | ||
| consentPersisted = true; | ||
| await _applyCurrentConsent(); | ||
| } on Object { | ||
| // A persisted revocation must survive teardown failure. Otherwise the | ||
| // next launch could initialize crash reporting against the user's | ||
| // explicit choice. | ||
| if (!granted && consentPersisted) { | ||
| rethrow; | ||
| } | ||
| _consentGranted = previousConsent; | ||
| final rolledBack = await _preferences.setBool( | ||
| diagnosticsConsentPreferenceKey, | ||
| previousConsent, | ||
| ); | ||
| notifyListeners(); | ||
| if (!rolledBack) { | ||
| throw StateError('Failed to roll back diagnostics consent'); | ||
| } | ||
| rethrow; | ||
|
brow marked this conversation as resolved.
|
||
| } | ||
| }); | ||
| } | ||
|
|
||
| Future<void> _applyCurrentConsent() async { | ||
| if (!_consentGranted) { | ||
| if (_initialized) { | ||
| await _crashReporter.close(); | ||
| _initialized = false; | ||
| _log('Diagnostics disabled: user consent is off; Sentry closed.'); | ||
| } else { | ||
| _log( | ||
| 'Diagnostics disabled: user consent is off; Sentry not initialized.', | ||
| ); | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (!_config.isConfigured) { | ||
| _log( | ||
| 'Diagnostics disabled: SENTRY_DSN is empty; Sentry not initialized.', | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| if (_initialized) { | ||
| _log('Diagnostics already enabled: Sentry initialization skipped.'); | ||
| return; | ||
| } | ||
|
|
||
| await _crashReporter.initialize(_config); | ||
| _initialized = true; | ||
| _log('Diagnostics enabled: Sentry initialized after user consent.'); | ||
| } | ||
|
|
||
| Future<void> _serialize(Future<void> Function() operation) { | ||
| final result = _pendingOperation.then((_) => operation()); | ||
| _pendingOperation = result.catchError((Object _) {}); | ||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import 'package:hooks_riverpod/legacy.dart'; | ||
|
|
||
| import 'diagnostics_controller.dart'; | ||
|
|
||
| /// Created during startup and overridden in main.dart. | ||
| final diagnosticsControllerProvider = | ||
| ChangeNotifierProvider<DiagnosticsController>( | ||
| (_) => throw UnimplementedError('Must be overridden'), | ||
| ); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.