@onekeyfe/react-native-capture-protection 0.0.0-stage → 3.0.166

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/ACKNOWLEDGEMENTS.md +18 -0
  2. package/LICENSE +22 -0
  3. package/LICENSE.upstream +20 -0
  4. package/README.md +25 -2
  5. package/ReactNativeCaptureProtection.podspec +37 -0
  6. package/android/CMakeLists.txt +24 -0
  7. package/android/build.gradle +130 -0
  8. package/android/gradle.properties +4 -0
  9. package/android/src/main/AndroidManifest.xml +3 -0
  10. package/android/src/main/cpp/cpp-adapter.cpp +6 -0
  11. package/android/src/main/java/com/margelo/nitro/reactnativecaptureprotection/ReactNativeCaptureProtection.kt +285 -0
  12. package/android/src/main/java/com/margelo/nitro/reactnativecaptureprotection/ReactNativeCaptureProtectionPackage.kt +22 -0
  13. package/docs/SPEC.md +117 -0
  14. package/ios/ReactNativeCaptureProtection.swift +251 -0
  15. package/ios/tests/CaptureProtectionTests.swift +50 -0
  16. package/lib/module/ReactNativeCaptureProtection.nitro.js +4 -0
  17. package/lib/module/ReactNativeCaptureProtection.nitro.js.map +1 -0
  18. package/lib/module/index.js +61 -0
  19. package/lib/module/index.js.map +1 -0
  20. package/lib/module/package.json +1 -0
  21. package/lib/typescript/package.json +1 -0
  22. package/lib/typescript/src/ReactNativeCaptureProtection.nitro.d.ts +10 -0
  23. package/lib/typescript/src/ReactNativeCaptureProtection.nitro.d.ts.map +1 -0
  24. package/lib/typescript/src/index.d.ts +22 -0
  25. package/lib/typescript/src/index.d.ts.map +1 -0
  26. package/nitro.json +17 -0
  27. package/nitrogen/generated/android/c++/JFunc_void_double.hpp +75 -0
  28. package/nitrogen/generated/android/c++/JHybridReactNativeCaptureProtectionSpec.cpp +88 -0
  29. package/nitrogen/generated/android/c++/JHybridReactNativeCaptureProtectionSpec.hpp +65 -0
  30. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativecaptureprotection/Func_void_double.kt +78 -0
  31. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativecaptureprotection/HybridReactNativeCaptureProtectionSpec.kt +70 -0
  32. package/nitrogen/generated/android/kotlin/com/margelo/nitro/reactnativecaptureprotection/reactnativecaptureprotectionOnLoad.kt +35 -0
  33. package/nitrogen/generated/android/reactnativecaptureprotection+autolinking.cmake +81 -0
  34. package/nitrogen/generated/android/reactnativecaptureprotection+autolinking.gradle +27 -0
  35. package/nitrogen/generated/android/reactnativecaptureprotectionOnLoad.cpp +56 -0
  36. package/nitrogen/generated/android/reactnativecaptureprotectionOnLoad.hpp +34 -0
  37. package/nitrogen/generated/ios/ReactNativeCaptureProtection+autolinking.rb +62 -0
  38. package/nitrogen/generated/ios/ReactNativeCaptureProtection-Swift-Cxx-Bridge.cpp +57 -0
  39. package/nitrogen/generated/ios/ReactNativeCaptureProtection-Swift-Cxx-Bridge.hpp +157 -0
  40. package/nitrogen/generated/ios/ReactNativeCaptureProtection-Swift-Cxx-Umbrella.hpp +46 -0
  41. package/nitrogen/generated/ios/ReactNativeCaptureProtectionAutolinking.mm +33 -0
  42. package/nitrogen/generated/ios/ReactNativeCaptureProtectionAutolinking.swift +26 -0
  43. package/nitrogen/generated/ios/c++/HybridReactNativeCaptureProtectionSpecSwift.cpp +11 -0
  44. package/nitrogen/generated/ios/c++/HybridReactNativeCaptureProtectionSpecSwift.hpp +98 -0
  45. package/nitrogen/generated/ios/swift/Func_void.swift +46 -0
  46. package/nitrogen/generated/ios/swift/Func_void_double.swift +46 -0
  47. package/nitrogen/generated/ios/swift/Func_void_std__exception_ptr.swift +46 -0
  48. package/nitrogen/generated/ios/swift/HybridReactNativeCaptureProtectionSpec.swift +57 -0
  49. package/nitrogen/generated/ios/swift/HybridReactNativeCaptureProtectionSpec_cxx.swift +187 -0
  50. package/nitrogen/generated/shared/c++/HybridReactNativeCaptureProtectionSpec.cpp +23 -0
  51. package/nitrogen/generated/shared/c++/HybridReactNativeCaptureProtectionSpec.hpp +66 -0
  52. package/package.json +173 -4
  53. package/src/ReactNativeCaptureProtection.nitro.ts +8 -0
  54. package/src/index.tsx +65 -0
@@ -0,0 +1,187 @@
1
+ ///
2
+ /// HybridReactNativeCaptureProtectionSpec_cxx.swift
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ import NitroModules
9
+
10
+ /**
11
+ * A class implementation that bridges HybridReactNativeCaptureProtectionSpec over to C++.
12
+ * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined.
13
+ *
14
+ * Also, some Swift types need to be bridged with special handling:
15
+ * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330)
16
+ * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper
17
+ * - Throwing methods need to be wrapped with a Result<T, Error> type, as exceptions cannot be propagated to C++
18
+ */
19
+ open class HybridReactNativeCaptureProtectionSpec_cxx {
20
+ /**
21
+ * The Swift <> C++ bridge's namespace (`margelo::nitro::reactnativecaptureprotection::bridge::swift`)
22
+ * from `ReactNativeCaptureProtection-Swift-Cxx-Bridge.hpp`.
23
+ * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift.
24
+ */
25
+ public typealias bridge = margelo.nitro.reactnativecaptureprotection.bridge.swift
26
+
27
+ /**
28
+ * Holds an instance of the `HybridReactNativeCaptureProtectionSpec` Swift protocol.
29
+ */
30
+ private var __implementation: any HybridReactNativeCaptureProtectionSpec
31
+
32
+ /**
33
+ * Holds a weak pointer to the C++ class that wraps the Swift class.
34
+ */
35
+ private var __cxxPart: bridge.std__weak_ptr_HybridReactNativeCaptureProtectionSpec_
36
+
37
+ /**
38
+ * Create a new `HybridReactNativeCaptureProtectionSpec_cxx` that wraps the given `HybridReactNativeCaptureProtectionSpec`.
39
+ * All properties and methods bridge to C++ types.
40
+ */
41
+ public init(_ implementation: any HybridReactNativeCaptureProtectionSpec) {
42
+ self.__implementation = implementation
43
+ self.__cxxPart = .init()
44
+ /* no base class */
45
+ }
46
+
47
+ /**
48
+ * Get the actual `HybridReactNativeCaptureProtectionSpec` instance this class wraps.
49
+ */
50
+ @inline(__always)
51
+ public func getHybridReactNativeCaptureProtectionSpec() -> any HybridReactNativeCaptureProtectionSpec {
52
+ return __implementation
53
+ }
54
+
55
+ /**
56
+ * Casts this instance to a retained unsafe raw pointer.
57
+ * This acquires one additional strong reference on the object!
58
+ */
59
+ public func toUnsafe() -> UnsafeMutableRawPointer {
60
+ return Unmanaged.passRetained(self).toOpaque()
61
+ }
62
+
63
+ /**
64
+ * Casts an unsafe pointer to a `HybridReactNativeCaptureProtectionSpec_cxx`.
65
+ * The pointer has to be a retained opaque `Unmanaged<HybridReactNativeCaptureProtectionSpec_cxx>`.
66
+ * This removes one strong reference from the object!
67
+ */
68
+ public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridReactNativeCaptureProtectionSpec_cxx {
69
+ return Unmanaged<HybridReactNativeCaptureProtectionSpec_cxx>.fromOpaque(pointer).takeRetainedValue()
70
+ }
71
+
72
+ /**
73
+ * Gets (or creates) the C++ part of this Hybrid Object.
74
+ * The C++ part is a `std::shared_ptr<HybridReactNativeCaptureProtectionSpec>`.
75
+ */
76
+ public func getCxxPart() -> bridge.std__shared_ptr_HybridReactNativeCaptureProtectionSpec_ {
77
+ let cachedCxxPart = self.__cxxPart.lock()
78
+ if Bool(fromCxx: cachedCxxPart) {
79
+ return cachedCxxPart
80
+ } else {
81
+ let newCxxPart = bridge.create_std__shared_ptr_HybridReactNativeCaptureProtectionSpec_(self.toUnsafe())
82
+ __cxxPart = bridge.weakify_std__shared_ptr_HybridReactNativeCaptureProtectionSpec_(newCxxPart)
83
+ return newCxxPart
84
+ }
85
+ }
86
+
87
+
88
+
89
+ /**
90
+ * Get the memory size of the Swift class (plus size of any other allocations)
91
+ * so the JS VM can properly track it and garbage-collect the JS object if needed.
92
+ */
93
+ @inline(__always)
94
+ public var memorySize: Int {
95
+ return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize
96
+ }
97
+
98
+ /**
99
+ * Compares this object with the given [other] object for reference equality.
100
+ */
101
+ @inline(__always)
102
+ public func equals(other: HybridReactNativeCaptureProtectionSpec_cxx) -> Bool {
103
+ return self.__implementation === other.__implementation
104
+ }
105
+
106
+ /**
107
+ * Call dispose() on the Swift class.
108
+ * This _may_ be called manually from JS.
109
+ */
110
+ @inline(__always)
111
+ public func dispose() {
112
+ self.__implementation.dispose()
113
+ }
114
+
115
+ /**
116
+ * Call toString() on the Swift class.
117
+ */
118
+ @inline(__always)
119
+ public func toString() -> String {
120
+ return self.__implementation.toString()
121
+ }
122
+
123
+ // Properties
124
+
125
+
126
+ // Methods
127
+ @inline(__always)
128
+ public final func prevent() -> bridge.Result_std__shared_ptr_Promise_void___ {
129
+ do {
130
+ let __result = try self.__implementation.prevent()
131
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
132
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
133
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
134
+ __result
135
+ .then({ __result in __promiseHolder.resolve() })
136
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
137
+ return __promise
138
+ }()
139
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
140
+ } catch (let __error) {
141
+ let __exceptionPtr = __error.toCpp()
142
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
143
+ }
144
+ }
145
+
146
+ @inline(__always)
147
+ public final func allow() -> bridge.Result_std__shared_ptr_Promise_void___ {
148
+ do {
149
+ let __result = try self.__implementation.allow()
150
+ let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in
151
+ let __promise = bridge.create_std__shared_ptr_Promise_void__()
152
+ let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise)
153
+ __result
154
+ .then({ __result in __promiseHolder.resolve() })
155
+ .catch({ __error in __promiseHolder.reject(__error.toCpp()) })
156
+ return __promise
157
+ }()
158
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp)
159
+ } catch (let __error) {
160
+ let __exceptionPtr = __error.toCpp()
161
+ return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr)
162
+ }
163
+ }
164
+
165
+ @inline(__always)
166
+ public final func setListener(callback: bridge.std__optional_std__function_void_double____event______) -> bridge.Result_void_ {
167
+ do {
168
+ try self.__implementation.setListener(callback: { () -> ((_ event: Double) -> Void)? in
169
+ if bridge.has_value_std__optional_std__function_void_double____event______(callback) {
170
+ let __unwrapped = bridge.get_std__optional_std__function_void_double____event______(callback)
171
+ return { () -> (Double) -> Void in
172
+ let __wrappedFunction = bridge.wrap_Func_void_double(__unwrapped)
173
+ return { (__event: Double) -> Void in
174
+ __wrappedFunction.call(__event)
175
+ }
176
+ }()
177
+ } else {
178
+ return nil
179
+ }
180
+ }())
181
+ return bridge.create_Result_void_()
182
+ } catch (let __error) {
183
+ let __exceptionPtr = __error.toCpp()
184
+ return bridge.create_Result_void_(__exceptionPtr)
185
+ }
186
+ }
187
+ }
@@ -0,0 +1,23 @@
1
+ ///
2
+ /// HybridReactNativeCaptureProtectionSpec.cpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #include "HybridReactNativeCaptureProtectionSpec.hpp"
9
+
10
+ namespace margelo::nitro::reactnativecaptureprotection {
11
+
12
+ void HybridReactNativeCaptureProtectionSpec::loadHybridMethods() {
13
+ // load base methods/properties
14
+ HybridObject::loadHybridMethods();
15
+ // load custom methods/properties
16
+ registerHybrids(this, [](Prototype& prototype) {
17
+ prototype.registerHybridMethod("prevent", &HybridReactNativeCaptureProtectionSpec::prevent);
18
+ prototype.registerHybridMethod("allow", &HybridReactNativeCaptureProtectionSpec::allow);
19
+ prototype.registerHybridMethod("setListener", &HybridReactNativeCaptureProtectionSpec::setListener);
20
+ });
21
+ }
22
+
23
+ } // namespace margelo::nitro::reactnativecaptureprotection
@@ -0,0 +1,66 @@
1
+ ///
2
+ /// HybridReactNativeCaptureProtectionSpec.hpp
3
+ /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE.
4
+ /// https://github.com/mrousavy/nitro
5
+ /// Copyright © Marc Rousavy @ Margelo
6
+ ///
7
+
8
+ #pragma once
9
+
10
+ #if __has_include(<NitroModules/HybridObject.hpp>)
11
+ #include <NitroModules/HybridObject.hpp>
12
+ #else
13
+ #error NitroModules cannot be found! Are you sure you installed NitroModules properly?
14
+ #endif
15
+
16
+
17
+
18
+ #include <NitroModules/Promise.hpp>
19
+ #include <functional>
20
+ #include <optional>
21
+
22
+ namespace margelo::nitro::reactnativecaptureprotection {
23
+
24
+ using namespace margelo::nitro;
25
+
26
+ /**
27
+ * An abstract base class for `ReactNativeCaptureProtection`
28
+ * Inherit this class to create instances of `HybridReactNativeCaptureProtectionSpec` in C++.
29
+ * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual.
30
+ * @example
31
+ * ```cpp
32
+ * class HybridReactNativeCaptureProtection: public HybridReactNativeCaptureProtectionSpec {
33
+ * public:
34
+ * HybridReactNativeCaptureProtection(...): HybridObject(TAG) { ... }
35
+ * // ...
36
+ * };
37
+ * ```
38
+ */
39
+ class HybridReactNativeCaptureProtectionSpec: public virtual HybridObject {
40
+ public:
41
+ // Constructor
42
+ explicit HybridReactNativeCaptureProtectionSpec(): HybridObject(TAG) { }
43
+
44
+ // Destructor
45
+ ~HybridReactNativeCaptureProtectionSpec() override = default;
46
+
47
+ public:
48
+ // Properties
49
+
50
+
51
+ public:
52
+ // Methods
53
+ virtual std::shared_ptr<Promise<void>> prevent() = 0;
54
+ virtual std::shared_ptr<Promise<void>> allow() = 0;
55
+ virtual void setListener(const std::optional<std::function<void(double /* event */)>>& callback) = 0;
56
+
57
+ protected:
58
+ // Hybrid Setup
59
+ void loadHybridMethods() override;
60
+
61
+ protected:
62
+ // Tag for logging
63
+ static constexpr auto TAG = "ReactNativeCaptureProtection";
64
+ };
65
+
66
+ } // namespace margelo::nitro::reactnativecaptureprotection
package/package.json CHANGED
@@ -1,6 +1,175 @@
1
1
  {
2
2
  "name": "@onekeyfe/react-native-capture-protection",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "3.0.166",
4
+ "description": "Permission-minimal screenshot, recording and app-switcher protection",
5
+ "main": "./lib/module/index.js",
6
+ "types": "./lib/typescript/src/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "source": "./src/index.tsx",
10
+ "types": "./lib/typescript/src/index.d.ts",
11
+ "default": "./lib/module/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "lib",
18
+ "android",
19
+ "ios",
20
+ "cpp",
21
+ "nitrogen",
22
+ "nitro.json",
23
+ "*.podspec",
24
+ "react-native.config.js",
25
+ "!ios/build",
26
+ "!android/build",
27
+ "!android/gradle",
28
+ "!android/gradlew",
29
+ "!android/gradlew.bat",
30
+ "!android/local.properties",
31
+ "!**/__tests__",
32
+ "!**/__fixtures__",
33
+ "!**/__mocks__",
34
+ "!**/.*",
35
+ "docs",
36
+ "ACKNOWLEDGEMENTS.md",
37
+ "LICENSE",
38
+ "LICENSE.upstream",
39
+ "README.md"
40
+ ],
41
+ "scripts": {
42
+ "clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
43
+ "prepare": "bob build",
44
+ "nitrogen": "nitrogen",
45
+ "typecheck": "tsc",
46
+ "lint": "eslint \"**/*.{js,ts,tsx}\"",
47
+ "test": "jest",
48
+ "release": "yarn prepare && npm whoami && npm publish --access public"
49
+ },
50
+ "keywords": [
51
+ "react-native",
52
+ "ios",
53
+ "android"
54
+ ],
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/OneKeyHQ/app-modules.git",
58
+ "directory": "native-modules/react-native-capture-protection"
59
+ },
60
+ "author": "onekeyfe <huanming@onekey.so> (https://github.com/OneKeyHQ/app-modules)",
61
+ "license": "MIT",
62
+ "bugs": {
63
+ "url": "https://github.com/OneKeyHQ/app-modules/issues"
64
+ },
65
+ "homepage": "https://github.com/OneKeyHQ/app-modules/tree/main/native-modules/react-native-capture-protection#readme",
66
+ "publishConfig": {
67
+ "registry": "https://registry.npmjs.org/"
68
+ },
69
+ "devDependencies": {
70
+ "@commitlint/config-conventional": "^19.8.1",
71
+ "@eslint/compat": "^1.3.2",
72
+ "@eslint/eslintrc": "^3.3.1",
73
+ "@eslint/js": "^9.35.0",
74
+ "@react-native/babel-preset": "0.86.2",
75
+ "@react-native/eslint-config": "0.86.2",
76
+ "@release-it/conventional-changelog": "^10.0.1",
77
+ "@types/jest": "^29.5.14",
78
+ "@types/react": "^19.2.0",
79
+ "commitlint": "^19.8.1",
80
+ "del-cli": "^6.0.0",
81
+ "eslint": "^9.35.0",
82
+ "eslint-config-prettier": "^10.1.8",
83
+ "eslint-plugin-prettier": "^5.5.4",
84
+ "jest": "^29.7.0",
85
+ "lefthook": "^2.0.3",
86
+ "nitrogen": "0.37.0",
87
+ "prettier": "^2.8.8",
88
+ "react": "19.2.3",
89
+ "react-native": "patch:react-native@npm%3A0.86.2#~/.yarn/patches/react-native-npm-0.86.2-c9d546616f.patch",
90
+ "react-native-builder-bob": "^0.40.13",
91
+ "react-native-nitro-modules": "0.37.0",
92
+ "release-it": "^19.0.4",
93
+ "turbo": "^2.5.6",
94
+ "typescript": "^5.9.2"
95
+ },
96
+ "peerDependencies": {
97
+ "react": "*",
98
+ "react-native": "*",
99
+ "react-native-nitro-modules": "0.37.0"
100
+ },
101
+ "react-native-builder-bob": {
102
+ "source": "src",
103
+ "output": "lib",
104
+ "targets": [
105
+ [
106
+ "custom",
107
+ {
108
+ "script": "nitrogen",
109
+ "clean": "nitrogen/"
110
+ }
111
+ ],
112
+ [
113
+ "module",
114
+ {
115
+ "esm": true
116
+ }
117
+ ],
118
+ [
119
+ "typescript",
120
+ {
121
+ "project": "tsconfig.build.json"
122
+ }
123
+ ]
124
+ ]
125
+ },
126
+ "prettier": {
127
+ "quoteProps": "consistent",
128
+ "singleQuote": true,
129
+ "tabWidth": 2,
130
+ "trailingComma": "es5",
131
+ "useTabs": false
132
+ },
133
+ "jest": {
134
+ "preset": "react-native",
135
+ "modulePathIgnorePatterns": [
136
+ "<rootDir>/example/node_modules",
137
+ "<rootDir>/lib/"
138
+ ]
139
+ },
140
+ "commitlint": {
141
+ "extends": [
142
+ "@commitlint/config-conventional"
143
+ ]
144
+ },
145
+ "release-it": {
146
+ "git": {
147
+ "commitMessage": "chore: release ${version}",
148
+ "tagName": "v${version}"
149
+ },
150
+ "npm": {
151
+ "publish": true
152
+ },
153
+ "github": {
154
+ "release": true
155
+ },
156
+ "plugins": {
157
+ "@release-it/conventional-changelog": {
158
+ "preset": {
159
+ "name": "angular"
160
+ }
161
+ }
162
+ }
163
+ },
164
+ "create-react-native-library": {
165
+ "type": "nitro-module",
166
+ "languages": "kotlin-swift",
167
+ "tools": [
168
+ "eslint",
169
+ "jest",
170
+ "lefthook",
171
+ "release-it"
172
+ ],
173
+ "version": "0.56.0"
174
+ }
175
+ }
@@ -0,0 +1,8 @@
1
+ import type { HybridObject } from 'react-native-nitro-modules';
2
+
3
+ export interface ReactNativeCaptureProtection
4
+ extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {
5
+ prevent(): Promise<void>;
6
+ allow(): Promise<void>;
7
+ setListener(callback: ((event: number) => void) | undefined): void;
8
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,65 @@
1
+ import { NitroModules } from 'react-native-nitro-modules';
2
+ import type { ReactNativeCaptureProtection } from './ReactNativeCaptureProtection.nitro';
3
+
4
+ // Adapted from react-native-capture-protection 2.3.0; see LICENSE.upstream.
5
+ export enum CaptureEventType {
6
+ NONE = 0,
7
+ RECORDING = 1,
8
+ END_RECORDING = 2,
9
+ CAPTURED = 3,
10
+ APP_SWITCHING = 4,
11
+ UNKNOWN = 5,
12
+ ALLOW = 8,
13
+ PREVENT_SCREEN_CAPTURE = 16,
14
+ PREVENT_SCREEN_RECORDING = 32,
15
+ PREVENT_SCREEN_APP_SWITCHING = 64,
16
+ }
17
+
18
+ type CaptureCallback = (event: CaptureEventType) => void;
19
+ let native: ReactNativeCaptureProtection | undefined;
20
+ const subscribers = new Map<symbol, CaptureCallback>();
21
+
22
+ function getNative(): ReactNativeCaptureProtection {
23
+ native ??= NitroModules.createHybridObject<ReactNativeCaptureProtection>(
24
+ 'ReactNativeCaptureProtection'
25
+ );
26
+ return native;
27
+ }
28
+
29
+ export const CaptureProtection = {
30
+ prevent(): Promise<void> {
31
+ return getNative().prevent();
32
+ },
33
+ allow(): Promise<void> {
34
+ return getNative().allow();
35
+ },
36
+ addListener(callback: CaptureCallback): { remove(): void } {
37
+ if (subscribers.size >= 128) {
38
+ throw new Error('Capture protection supports at most 128 subscribers.');
39
+ }
40
+ const id = Symbol();
41
+ subscribers.set(id, callback);
42
+ if (subscribers.size === 1) {
43
+ try {
44
+ getNative().setListener((event) => {
45
+ // Read current subscriptions so queued events cannot reach removed callers.
46
+ for (const [key, listener] of Array.from(subscribers)) {
47
+ if (subscribers.has(key)) {
48
+ listener(event as CaptureEventType);
49
+ }
50
+ }
51
+ });
52
+ } catch (error) {
53
+ subscribers.delete(id);
54
+ throw error;
55
+ }
56
+ }
57
+ return {
58
+ remove() {
59
+ if (subscribers.delete(id) && subscribers.size === 0) {
60
+ getNative().setListener(undefined);
61
+ }
62
+ },
63
+ };
64
+ },
65
+ };