Line data Source code
1 : import 'dart:convert';
2 : import 'package:flutter/foundation.dart';
3 : import '../worker.dart';
4 :
5 : /// Custom native worker configuration.
6 : ///
7 : /// Allows users to register and use their own native worker implementations
8 : /// without modifying the plugin source code.
9 : @immutable
10 : final class CustomNativeWorker extends Worker {
11 2 : CustomNativeWorker({
12 : required this.className,
13 : this.input,
14 : }) {
15 4 : if (className.isEmpty) {
16 1 : throw ArgumentError.value(
17 1 : className,
18 : 'className',
19 : 'className cannot be empty. Provide the name of your custom worker class.',
20 : );
21 : }
22 : // Validate className format: only letters, digits, dots, underscores, and dollar signs.
23 : // Prevents injection of shell metacharacters or class-loading tricks.
24 : // Android: "com.example.MyWorker" — iOS: "MyWorker" or "MyModule.MyWorker"
25 2 : final validPattern = RegExp(r'^[a-zA-Z][a-zA-Z0-9._$]*$');
26 4 : if (!validPattern.hasMatch(className)) {
27 0 : throw ArgumentError.value(
28 0 : className,
29 : 'className',
30 : 'className contains invalid characters. Use letters, digits, dots, underscores, '
31 : 'or dollar signs only (e.g. "com.example.MyWorker" or "MyWorker").',
32 : );
33 : }
34 : // Guard against excessively long class names (> 256 chars is unrealistic and may indicate abuse).
35 6 : if (className.length > 256) {
36 0 : throw ArgumentError.value(
37 0 : className,
38 : 'className',
39 : 'className exceeds maximum allowed length of 256 characters.',
40 : );
41 : }
42 : }
43 :
44 : /// The native worker class name (must be registered on native side).
45 : final String className;
46 :
47 : /// Optional input data (will be JSON encoded).
48 : final Map<String, dynamic>? input;
49 :
50 2 : @override
51 2 : String get workerClassName => className;
52 :
53 2 : @override
54 2 : Map<String, dynamic> toMap() => {
55 : 'workerType': 'custom',
56 2 : 'className': className,
57 6 : 'input': input != null ? jsonEncode(input) : null,
58 : };
59 : }
|