@bugsee/react-native 7.0.0-beta2 → 7.0.0-beta4

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 (79) hide show
  1. package/README.md +264 -9
  2. package/android/build.gradle +9 -0
  3. package/android/consumer-rules.pro +3 -0
  4. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +224 -20
  5. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +44 -21
  6. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +15 -0
  7. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +119 -5
  8. package/android/src/main/java/com/bugsee/reactnative/NetworkCaptureProbe.java +107 -0
  9. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +129 -14
  10. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +48 -0
  11. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +64 -8
  12. package/android/src/main/java/com/bugsee/reactnative/StartupHold.java +237 -0
  13. package/ios/BugseeModule.mm +293 -155
  14. package/ios/Support/Package.resolved +2 -2
  15. package/ios/Support/Package.swift +1 -1
  16. package/ios/Support/Sources/BugseeRNSupport/BGSRNBlackout.m +14 -0
  17. package/ios/Support/Sources/BugseeRNSupport/BGSRNDeleteCollectedData.m +33 -0
  18. package/ios/Support/Sources/BugseeRNSupport/BGSRNFilterArming.m +22 -0
  19. package/ios/Support/Sources/BugseeRNSupport/BGSRNFilterDeadline.m +110 -0
  20. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +40 -6
  21. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +1 -1
  22. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNBlackout.h +23 -0
  23. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +2 -0
  24. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDeleteCollectedData.h +29 -0
  25. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNFilterArming.h +26 -0
  26. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNFilterDeadline.h +68 -0
  27. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +5 -0
  28. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +5 -0
  29. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +16 -2
  30. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +5 -0
  31. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNBlackoutTests.m +38 -0
  32. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDeleteCollectedDataTests.m +135 -0
  33. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +5 -0
  34. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFilterArmingTests.m +48 -0
  35. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFilterDeadlineTests.m +230 -0
  36. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +5 -0
  37. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +86 -6
  38. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +5 -0
  39. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +1 -1
  40. package/native-versions.json +2 -2
  41. package/package.json +2 -2
  42. package/plugin/build/dsym-script.d.ts +11 -0
  43. package/plugin/build/dsym-script.js +26 -3
  44. package/plugin/build/dsym-script.js.map +1 -1
  45. package/plugin/build/index.d.ts +27 -0
  46. package/plugin/build/index.js +33 -21
  47. package/plugin/build/index.js.map +1 -1
  48. package/plugin/build/manifest.d.ts +37 -0
  49. package/plugin/build/manifest.js +67 -1
  50. package/plugin/build/manifest.js.map +1 -1
  51. package/plugin/build/native-versions.baked.json +1 -1
  52. package/scripts/bugsee-xcode.sh +0 -0
  53. package/scripts/compose-then-inject.js +4 -2
  54. package/scripts/hermes-sourcemaps.js +40 -3
  55. package/scripts/hermesc-preserve-js.js +0 -0
  56. package/scripts/hermesc-preserve-js.sh +0 -0
  57. package/src/NativeBugsee.ts +39 -22
  58. package/src/apm/span.ts +42 -7
  59. package/src/breadcrumbs/filter.ts +104 -13
  60. package/src/breadcrumbs/types.ts +6 -4
  61. package/src/breadcrumbs/url-scrub.ts +75 -0
  62. package/src/exceptions/ErrorBoundary.tsx +4 -1
  63. package/src/exceptions/RootErrorReporter.tsx +1 -1
  64. package/src/exceptions/handlers.ts +17 -1
  65. package/src/exceptions/report.ts +30 -8
  66. package/src/index.ts +125 -33
  67. package/src/logs/filter.ts +19 -8
  68. package/src/network/filter.ts +17 -5
  69. package/src/options/AndroidLaunchOptions.ts +310 -24
  70. package/src/options/BugseeLaunchOptions.ts +463 -18
  71. package/src/options/IOSLaunchOptions.ts +164 -8
  72. package/src/options/android-options-manifest.json +63 -6
  73. package/src/options/enums.ts +28 -0
  74. package/src/options/ios-enums.json +6 -5
  75. package/src/options/ios-options-manifest.json +594 -0
  76. package/src/options/keys.ts +19 -1
  77. package/src/options/noAccessor.ts +16 -0
  78. package/src/options/option-keys.json +9 -3
  79. package/src/options/unknownKeys.ts +198 -0
@@ -119,6 +119,26 @@ final class LogFilterBridge {
119
119
  private final AtomicLong ids = new AtomicLong();
120
120
  /** True only while the app has a log callback. False is the native pass-through. */
121
121
  private final AtomicBoolean userFilter = new AtomicBoolean();
122
+ /** Orders attach against setEnabled; see {@link #setEnabled(boolean, Sink)}. */
123
+ private final Object stateLock = new Object();
124
+
125
+ /**
126
+ * The module whose JS has installed its filter with
127
+ * {@code setEnabled(true)}, or null. Compared to the attached sink by
128
+ * identity, so a stale module cannot arm the new one. A reload attaches a new module whose JS has
129
+ * not subscribed yet, while this bridge (and the SDK's filter) outlive
130
+ * the old runtime. Until JS arms it again, a request would be emitted to
131
+ * nobody, so it is dropped at once instead: fail closed, no wait.
132
+ */
133
+ private final AtomicReference<Sink> armedFor = new AtomicReference<>();
134
+
135
+ /** True from {@link #holdUntilJs} until JS first calls {@link #setEnabled}. For the watchdog only. */
136
+ private final AtomicBoolean held = new AtomicBoolean();
137
+
138
+ /** Whether the startup hold is still waiting for JS to call {@code setLogFilter}. */
139
+ boolean isHeld() {
140
+ return held.get();
141
+ }
122
142
 
123
143
  /** Production: one daemon thread forgets pending requests at {@link #BORROW_MS}. */
124
144
  private LogFilterBridge() {
@@ -135,7 +155,8 @@ final class LogFilterBridge {
135
155
  this(scheduler, clock, filter -> { });
136
156
  }
137
157
 
138
- private LogFilterBridge(
158
+ /** Tests pass the call that installs the SDK's filter. */
159
+ LogFilterBridge(
139
160
  @NonNull final Scheduler scheduler,
140
161
  @NonNull final ConsoleEchoDedup.Clock clock,
141
162
  @NonNull final Installer installer
@@ -162,7 +183,79 @@ final class LogFilterBridge {
162
183
  };
163
184
 
164
185
  void attach(@NonNull final Sink next) {
165
- sink.set(next);
186
+ // Disarm before the sink is visible: a request in the gap must not
187
+ // reach a runtime that has not subscribed yet. See armedFor. userFilter
188
+ // is deliberately kept: a redaction the app relied on must not silently
189
+ // stop applying across a reload.
190
+ synchronized (stateLock) {
191
+ armedFor.set(null);
192
+ sink.set(next);
193
+ }
194
+ }
195
+
196
+ /**
197
+ * What a new module does at construction: attach, then put the echo drop
198
+ * in place even when the app never calls setLogFilter. Not
199
+ * {@code setEnabled(false)}: after a reload that would turn a redaction the
200
+ * app relied on into a silent pass-through. The attach disarms the bridge,
201
+ * so with a user filter still on, lines are dropped until the new runtime
202
+ * calls setLogFilter (re-arm) or clears it (pass-through).
203
+ */
204
+ void attachModule(@NonNull final Sink next) {
205
+ attach(next);
206
+ ensureInstalled();
207
+ }
208
+
209
+ /**
210
+ * Installs the filter before JS has asked for it, so a manifest
211
+ * auto-launch does not record log lines unfiltered until
212
+ * {@code setLogFilter} runs (see {@link StartupHold}). It is the state of
213
+ * an app whose filter is on but whose runtime has not armed it: lines are
214
+ * dropped. {@link #attachModule} keeps it, so it also holds while the
215
+ * module is constructed ahead of its JS. {@code setEnabled(true)} arms
216
+ * the filter, and {@code setEnabled(false)} (what {@code setLogFilter(null)}
217
+ * sends) releases it to the pass-through.
218
+ */
219
+ void holdUntilJs() {
220
+ // userFilter first: once the filter is installed no line may pass
221
+ // through before it is on. If the install throws it is rolled back
222
+ // (the throw goes on to StartupHold, which logs it) and the hold is
223
+ // not reported, as in the other two bridges; attachModule installs
224
+ // again when the module is built.
225
+ synchronized (stateLock) {
226
+ userFilter.set(true);
227
+ }
228
+ boolean ok = false;
229
+ try {
230
+ ensureInstalled();
231
+ ok = true;
232
+ } finally {
233
+ if (!ok) {
234
+ synchronized (stateLock) {
235
+ userFilter.set(false);
236
+ }
237
+ }
238
+ }
239
+ held.set(true);
240
+ }
241
+
242
+ /**
243
+ * The module {@code caller}'s JS now answers requests. Ignored unless
244
+ * {@code caller} is still the attached module: a stale module's late
245
+ * enable must not arm the new runtime, which has not subscribed.
246
+ */
247
+ void arm(@NonNull final Sink caller) {
248
+ if (sink.get() == caller) {
249
+ armedFor.set(caller);
250
+ }
251
+ }
252
+
253
+ /** Arms for whichever module is attached. For tests, which have no module identity of their own. */
254
+ void arm() {
255
+ final Sink current = sink.get();
256
+ if (current != null) {
257
+ armedFor.set(current);
258
+ }
166
259
  }
167
260
 
168
261
  /**
@@ -174,7 +267,10 @@ final class LogFilterBridge {
174
267
  * forgotten and the callback is not touched.
175
268
  */
176
269
  void detach(@NonNull final Sink current) {
177
- sink.compareAndSet(current, null);
270
+ synchronized (stateLock) {
271
+ sink.compareAndSet(current, null);
272
+ armedFor.compareAndSet(current, null);
273
+ }
178
274
  final List<Pending> owned = new ArrayList<>();
179
275
  for (final Pending item : pending.values()) {
180
276
  if (item.owner == current && pending.remove(item.id, item)) {
@@ -196,7 +292,25 @@ final class LogFilterBridge {
196
292
  * {@link Bugsee#setLogEventFilter}.
197
293
  */
198
294
  void setEnabled(final boolean enabled) {
199
- userFilter.set(enabled);
295
+ setEnabled(enabled, sink.get());
296
+ }
297
+
298
+ /** As above, on behalf of module {@code caller}; only the attached module can arm. */
299
+ void setEnabled(final boolean enabled, final Sink caller) {
300
+ // One lock for the identity check and the write, so a stale module
301
+ // cannot pass the check, pause, and switch off a filter the new
302
+ // module has since turned on.
303
+ synchronized (stateLock) {
304
+ if (sink.get() != caller) {
305
+ // A superseded module must neither arm nor disable the process-wide filter.
306
+ return;
307
+ }
308
+ held.set(false);
309
+ if (enabled && caller != null) {
310
+ arm(caller);
311
+ }
312
+ userFilter.set(enabled);
313
+ }
200
314
  ensureInstalled();
201
315
  }
202
316
 
@@ -285,7 +399,7 @@ final class LogFilterBridge {
285
399
  ) {
286
400
  final String line = event.getMessage();
287
401
  final Sink current = sink.get();
288
- if (line == null || current == null) {
402
+ if (line == null || current == null || armedFor.get() != current) {
289
403
  drop(callback);
290
404
  return;
291
405
  }
@@ -0,0 +1,107 @@
1
+ package com.bugsee.reactnative;
2
+
3
+ import android.util.Log;
4
+
5
+ import androidx.annotation.NonNull;
6
+
7
+ import java.util.ArrayList;
8
+ import java.util.List;
9
+ import java.util.concurrent.Callable;
10
+ import java.util.concurrent.atomic.AtomicBoolean;
11
+
12
+ import okhttp3.Interceptor;
13
+ import okhttp3.OkHttpClient;
14
+
15
+ /**
16
+ * Tells whether JS {@code fetch}/XHR traffic can reach Bugsee on this build.
17
+ *
18
+ * <p>React Native's networking runs on OkHttp. The SDK sees that traffic only
19
+ * through the interceptor the Bugsee Gradle plugin injects into every
20
+ * {@code OkHttpClient.Builder.build()}. An app that skips the plugin builds
21
+ * and launches fine and records no network events, with nothing to say so.
22
+ * {@link #warnOnceIfNotInstrumented()} makes that visible: it builds a client
23
+ * the way RN does and looks for the interceptor on it.
24
+ *
25
+ * <p>The client has to come from React Native's own code
26
+ * ({@code OkHttpClientProvider.createClient()}, passed in by the module): the
27
+ * plugin rewrites the {@code build()} call sites it instruments, and a client
28
+ * built inside this package is not one of them (on a device with the plugin
29
+ * applied, such a client carried no interceptor, so probing here would warn on
30
+ * every correctly set-up app).
31
+ *
32
+ * <p>This checks a proxy: RN's {@code createClient()}, not the per-request
33
+ * {@code newBuilder().build()} call site inside NetworkingModule. A custom
34
+ * client factory living in a package the plugin excludes can therefore give a
35
+ * rare false warning; it is a log line only. An app's registered
36
+ * {@code OkHttpClientFactory} also runs once more, at the first launch(), as a
37
+ * side effect of the probe. That is deliberate: the factory's own build() is
38
+ * where the plugin's interceptor would be, so probing the default path instead
39
+ * would say nothing about the client JS really uses.
40
+ */
41
+ public final class NetworkCaptureProbe {
42
+
43
+ private static final String TAG = "BugseeRN";
44
+
45
+ /** The class the plugin's injection adds (bugsee-android-okhttp). */
46
+ static final String INTERCEPTOR_CLASS = "com.bugsee.library.okhttp.BugseeOkHttpInterceptor";
47
+
48
+ static final String MESSAGE =
49
+ "JS fetch/XHR network events will not be recorded: the Bugsee Gradle plugin "
50
+ + "(com.bugsee.android.gradle) is not applied to this app, or its OkHttp "
51
+ + "instrumentation is disabled or excluded, so no Bugsee interceptor is on "
52
+ + "React Native's OkHttp client. The same plugin uploads "
53
+ + "the R8 mapping and native symbols. See the Android: Bugsee Gradle plugin "
54
+ + "section of the @bugsee/react-native README.";
55
+
56
+ private static final AtomicBoolean PROBED = new AtomicBoolean(false);
57
+
58
+ private NetworkCaptureProbe() {}
59
+
60
+ /**
61
+ * Whether a client from {@code clientSource} carries the Bugsee interceptor. False only
62
+ * when a client was built and none of its interceptors is Bugsee's. A
63
+ * probe that cannot run (OkHttp missing, the build threw) is no evidence
64
+ * either way and answers true, so it never warns by mistake.
65
+ */
66
+ static boolean isInstrumented(@NonNull final Callable<OkHttpClient> clientSource) {
67
+ final List<String> names = new ArrayList<>();
68
+ try {
69
+ final OkHttpClient client = clientSource.call();
70
+ collect(names, client.interceptors());
71
+ collect(names, client.networkInterceptors());
72
+ } catch (Throwable t) {
73
+ return true;
74
+ }
75
+ return hasBugseeInterceptor(names);
76
+ }
77
+
78
+ /** True when {@code classNames} includes the plugin-injected interceptor. */
79
+ static boolean hasBugseeInterceptor(@NonNull final List<String> classNames) {
80
+ return classNames.contains(INTERCEPTOR_CLASS);
81
+ }
82
+
83
+ private static void collect(final List<String> into, final List<Interceptor> interceptors) {
84
+ for (final Interceptor interceptor : interceptors) {
85
+ into.add(interceptor.getClass().getName());
86
+ }
87
+ }
88
+
89
+ /** Test seam: forget that the warning was logged. */
90
+ static void resetForTest() {
91
+ PROBED.set(false);
92
+ }
93
+
94
+ /**
95
+ * Probes the client from {@code clientSource} and logs {@link #MESSAGE} at
96
+ * WARN if it is not instrumented. Runs at most once per process, so
97
+ * repeated launches build no further throwaway clients. Returns whether it
98
+ * logged.
99
+ */
100
+ static boolean warnOnceIfNotInstrumented(@NonNull final Callable<OkHttpClient> clientSource) {
101
+ if (!PROBED.compareAndSet(false, true) || isInstrumented(clientSource)) {
102
+ return false;
103
+ }
104
+ Log.w(TAG, MESSAGE);
105
+ return true;
106
+ }
107
+ }
@@ -111,10 +111,22 @@ final class NetworkFilterBridge {
111
111
  private final ConcurrentHashMap<String, Pending> pending = new ConcurrentHashMap<>();
112
112
  private final AtomicLong ids = new AtomicLong();
113
113
  private final AtomicBoolean installed = new AtomicBoolean();
114
+ /** Orders attach against setEnabled; see {@link #setEnabled(boolean, Sink)}. */
115
+ private final Object stateLock = new Object();
116
+
117
+ /**
118
+ * The module whose JS has installed its filter with
119
+ * {@code setEnabled(true)}, or null. Compared to the attached sink by
120
+ * identity, so a stale module cannot arm the new one. A reload attaches a new module whose JS has
121
+ * not subscribed yet, while this bridge (and the SDK's filter) outlive
122
+ * the old runtime. Until JS arms it again, a request would be emitted to
123
+ * nobody, so it is dropped at once instead: fail closed, no wait.
124
+ */
125
+ private final AtomicReference<Sink> armedFor = new AtomicReference<>();
114
126
 
115
127
  /** Production: one daemon thread forgets pending requests at {@link #BORROW_MS}. */
116
128
  private NetworkFilterBridge() {
117
- this(new DaemonScheduler(), NetworkFilterBridge::readDefaultSanitizerOption);
129
+ this(new DaemonScheduler(), NetworkFilterBridge::readDefaultSanitizerOption, Bugsee::setNetworkEventFilter);
118
130
  }
119
131
 
120
132
  /**
@@ -124,8 +136,64 @@ final class NetworkFilterBridge {
124
136
  * {@link Bugsee#getLaunchOptions()} needs an Android looper.
125
137
  */
126
138
  NetworkFilterBridge(@NonNull final Scheduler scheduler, @NonNull final BooleanSupplier defaultSanitizer) {
139
+ this(scheduler, defaultSanitizer, installed -> { });
140
+ }
141
+
142
+ /** As above, and the call that installs (or, with null, removes) the SDK's filter. */
143
+ NetworkFilterBridge(
144
+ @NonNull final Scheduler scheduler,
145
+ @NonNull final BooleanSupplier defaultSanitizer,
146
+ @NonNull final Consumer<EventFilter<NetworkEvent>> installer
147
+ ) {
127
148
  this.scheduler = scheduler;
128
149
  this.defaultSanitizer = defaultSanitizer;
150
+ this.installer = installer;
151
+ }
152
+
153
+ private final Consumer<EventFilter<NetworkEvent>> installer;
154
+
155
+ /** True from {@link #holdUntilJs} until JS first calls {@link #setEnabled}. For the watchdog only. */
156
+ private final AtomicBoolean held = new AtomicBoolean();
157
+
158
+ /** Whether the startup hold is still waiting for JS to call {@code setNetworkFilter}. */
159
+ boolean isHeld() {
160
+ return held.get();
161
+ }
162
+
163
+ /**
164
+ * Installs the filter with the SDK. A throw (the SDK not ready that early)
165
+ * leaves {@code installed} false, so a later {@code setEnabled(true)}
166
+ * installs it instead of believing it already is, and is rethrown for the
167
+ * caller to log.
168
+ */
169
+ private void install() {
170
+ if (installed.compareAndSet(false, true)) {
171
+ boolean ok = false;
172
+ try {
173
+ installer.accept(filter);
174
+ ok = true;
175
+ } finally {
176
+ if (!ok) {
177
+ installed.set(false);
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Installs the filter before JS has asked for it, so a manifest
185
+ * auto-launch does not record network events unfiltered until
186
+ * {@code setNetworkFilter} runs (see {@link StartupHold}). With no module
187
+ * attached yet the filter drops everything, and so does one whose JS has
188
+ * not armed it (the armed gate), so nothing waits on a reply that cannot
189
+ * come. {@code setEnabled(false)}, which {@code setNetworkFilter(null)} sends, removes
190
+ * it again. A second call, or one after {@code setEnabled(true)}, is a no-op.
191
+ */
192
+ void holdUntilJs() {
193
+ synchronized (stateLock) {
194
+ install();
195
+ held.set(true);
196
+ }
129
197
  }
130
198
 
131
199
  private final EventFilter<NetworkEvent> filter = new EventFilter<NetworkEvent>() {
@@ -143,7 +211,31 @@ final class NetworkFilterBridge {
143
211
  };
144
212
 
145
213
  void attach(@NonNull final Sink next) {
146
- sink.set(next);
214
+ // Disarm before the sink is visible: a request in the gap must not
215
+ // reach a runtime that has not subscribed yet. See armedFor.
216
+ synchronized (stateLock) {
217
+ armedFor.set(null);
218
+ sink.set(next);
219
+ }
220
+ }
221
+
222
+ /**
223
+ * The module {@code caller}'s JS now answers requests. Ignored unless
224
+ * {@code caller} is still the attached module: a stale module's late
225
+ * enable must not arm the new runtime, which has not subscribed.
226
+ */
227
+ void arm(@NonNull final Sink caller) {
228
+ if (sink.get() == caller) {
229
+ armedFor.set(caller);
230
+ }
231
+ }
232
+
233
+ /** Arms for whichever module is attached. For tests, which have no module identity of their own. */
234
+ void arm() {
235
+ final Sink current = sink.get();
236
+ if (current != null) {
237
+ armedFor.set(current);
238
+ }
147
239
  }
148
240
 
149
241
  /**
@@ -152,7 +244,10 @@ final class NetworkFilterBridge {
152
244
  * forgotten and the callback is not touched.
153
245
  */
154
246
  void detach(@NonNull final Sink current) {
155
- sink.compareAndSet(current, null);
247
+ synchronized (stateLock) {
248
+ sink.compareAndSet(current, null);
249
+ armedFor.compareAndSet(current, null);
250
+ }
156
251
  final List<Pending> owned = new ArrayList<>();
157
252
  for (final Pending item : pending.values()) {
158
253
  if (item.owner == current && pending.remove(item.id, item)) {
@@ -169,14 +264,30 @@ final class NetworkFilterBridge {
169
264
 
170
265
  /** Installs the filter, or removes it. A second {@code true} is a no-op. */
171
266
  void setEnabled(final boolean enabled) {
172
- if (enabled) {
173
- if (installed.compareAndSet(false, true)) {
174
- Bugsee.setNetworkEventFilter(filter);
267
+ setEnabled(enabled, sink.get());
268
+ }
269
+
270
+ /** As above, on behalf of module {@code caller}; only the attached module can arm. */
271
+ void setEnabled(final boolean enabled, final Sink caller) {
272
+ // One lock for the identity check and the install, so a stale module
273
+ // cannot pass the check, pause, and remove a filter the new module
274
+ // has since relied on.
275
+ synchronized (stateLock) {
276
+ if (sink.get() != caller) {
277
+ // A superseded module must neither arm nor disable the process-wide filter.
278
+ return;
279
+ }
280
+ held.set(false);
281
+ if (enabled) {
282
+ if (caller != null) {
283
+ arm(caller);
284
+ }
285
+ install();
286
+ return;
287
+ }
288
+ if (installed.compareAndSet(true, false)) {
289
+ installer.accept(null);
175
290
  }
176
- return;
177
- }
178
- if (installed.compareAndSet(true, false)) {
179
- Bugsee.setNetworkEventFilter(null);
180
291
  }
181
292
  }
182
293
 
@@ -221,6 +332,11 @@ final class NetworkFilterBridge {
221
332
  @NonNull final NetworkEvent event,
222
333
  @NonNull final Callback1<NetworkEvent> callback
223
334
  ) {
335
+ final Sink asked = sink.get();
336
+ if (asked == null || armedFor.get() != asked) {
337
+ drop(callback);
338
+ return;
339
+ }
224
340
  // setNetworkEventFilter skips DefaultNetworkDataSanitizer. The
225
341
  // capture provider runs it only when no filter is installed, and
226
342
  // only when CaptureNetworkUseDefaultSanitizer is on (default true).
@@ -243,10 +359,9 @@ final class NetworkFilterBridge {
243
359
  drop(callback);
244
360
  return;
245
361
  }
246
- final Sink current = sink.get();
247
362
  // A null URL cannot tell a recycled entry (reset nulls the URL) from
248
363
  // the one we were asked about, so it is dropped rather than lent.
249
- if (url == null || current == null) {
364
+ if (url == null) {
250
365
  drop(callback);
251
366
  return;
252
367
  }
@@ -258,11 +373,11 @@ final class NetworkFilterBridge {
258
373
  return;
259
374
  }
260
375
  final String requestId = Long.toString(ids.incrementAndGet());
261
- final Pending item = new Pending(requestId, event, id, url, callback, current);
376
+ final Pending item = new Pending(requestId, event, id, url, callback, asked);
262
377
  pending.put(requestId, item);
263
378
  try {
264
379
  item.deadline = scheduler.schedule(() -> forget(requestId), BORROW_MS);
265
- current.onNetworkFilterRequest(requestId, eventJson);
380
+ asked.onNetworkFilterRequest(requestId, eventJson);
266
381
  } catch (final Throwable e) {
267
382
  if (pending.remove(requestId, item)) {
268
383
  cancel(item.deadline);
@@ -1,8 +1,13 @@
1
1
  package com.bugsee.reactnative;
2
2
 
3
+ import android.content.Context;
4
+ import android.content.pm.ApplicationInfo;
5
+ import android.content.pm.PackageManager;
6
+ import android.os.Bundle;
3
7
  import android.util.Log;
4
8
 
5
9
  import androidx.annotation.NonNull;
10
+ import androidx.annotation.Nullable;
6
11
 
7
12
  import com.bugsee.library.BugseeExtensionInitProviderBase;
8
13
 
@@ -29,6 +34,9 @@ import com.bugsee.library.BugseeExtensionInitProviderBase;
29
34
  * bridge is up. An early report is therefore attributed to
30
35
  * {@code react_native} with a correct wrapper version and an empty context,
31
36
  * which is strictly better than the alternative of no attribution at all.
37
+ *
38
+ * <p>The same early hook can close the privacy window of an auto-launched
39
+ * SDK, opt-in: see {@link StartupHold}.
32
40
  */
33
41
  public final class ReactNativeWrapperInitProvider extends BugseeExtensionInitProviderBase {
34
42
 
@@ -39,10 +47,50 @@ public final class ReactNativeWrapperInitProvider extends BugseeExtensionInitPro
39
47
  // Through the registrar, like every registration: setWrapperInfo
40
48
  // re-registers from another thread once JS is up.
41
49
  registerEarly(WrapperRegistrar::register);
50
+ // Opt-in: close the window before JS installs the app's filters.
51
+ holdFiltersEarly(holdFiltersMetaData(getContext()), StartupHold.BRIDGES);
42
52
  // Conventional for an init provider: we expose no content.
43
53
  return false;
44
54
  }
45
55
 
56
+ /**
57
+ * The {@value StartupHold#META_DATA} meta-data, or null when it is absent
58
+ * or cannot be read. Never throws: see {@link #registerEarly}.
59
+ */
60
+ @Nullable
61
+ static Object holdFiltersMetaData(@Nullable final Context context) {
62
+ try {
63
+ if (context == null) {
64
+ return null;
65
+ }
66
+ final ApplicationInfo info = context.getPackageManager()
67
+ .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
68
+ return holdFiltersValueOf(info);
69
+ } catch (Throwable t) {
70
+ Log.e(TAG, "could not read " + StartupHold.META_DATA + ": " + t.getClass().getName());
71
+ return null;
72
+ }
73
+ }
74
+
75
+ /** The meta-data value in {@code info}, or null. */
76
+ @Nullable
77
+ static Object holdFiltersValueOf(@NonNull final ApplicationInfo info) {
78
+ final Bundle data = info.metaData;
79
+ return data == null ? null : data.get(StartupHold.META_DATA);
80
+ }
81
+
82
+ /** Never throws; see {@link StartupHold#apply}. */
83
+ static void holdFiltersEarly(@Nullable final Object metaData, @NonNull final StartupHold.Targets targets) {
84
+ try {
85
+ final java.util.Set<String> held = StartupHold.apply(metaData, targets);
86
+ if (targets == StartupHold.BRIDGES) {
87
+ StartupHold.watchFromProvider(held);
88
+ }
89
+ } catch (Throwable t) {
90
+ Log.e(TAG, "could not hold the filters at process start: " + t.getClass().getName());
91
+ }
92
+ }
93
+
46
94
  /**
47
95
  * Never throws, errors included. This runs before
48
96
  * {@code Application.onCreate} in every host app that links the library,
@@ -29,21 +29,40 @@ final class SpanHandles {
29
29
  void finish(@Nullable SpanStatus status);
30
30
 
31
31
  boolean isFinished();
32
+
33
+ /** The SDK's status ordinal, read now. A span that never finished reads what the SDK holds. */
34
+ int statusOrdinal();
35
+ }
36
+
37
+ /** A handle this registry dropped, with the status the SDK holds for it. */
38
+ static final class Released {
39
+ final String handle;
40
+ final int status;
41
+
42
+ Released(final String handle, final int status) {
43
+ this.handle = handle;
44
+ this.status = status;
45
+ }
32
46
  }
33
47
 
34
48
  private static final class Entry {
35
49
  final Object identity;
36
50
  final Retained retained;
51
+ /** True when JS created the span, false when it only adopted one the SDK or the app owns. */
52
+ final boolean owned;
37
53
 
38
- Entry(final Object identity, final Retained retained) {
54
+ Entry(final Object identity, final Retained retained, final boolean owned) {
39
55
  this.identity = identity;
40
56
  this.retained = retained;
57
+ this.owned = owned;
41
58
  }
42
59
  }
43
60
 
44
61
  private final Map<String, Entry> byHandle = new LinkedHashMap<>();
45
62
  private final Map<Object, String> byIdentity = new IdentityHashMap<>();
46
63
  private int next = 1;
64
+ /** Set by cancelOpenAndReleaseAll: the module is gone, so nothing is stored. */
65
+ private boolean closed;
47
66
 
48
67
  /**
49
68
  * Holds {@code retained} for {@code identity}. The same identity returns
@@ -53,7 +72,19 @@ final class SpanHandles {
53
72
  * calls {@link #releaseAll} on another thread.
54
73
  */
55
74
  synchronized String retain(@Nullable final Object identity, @Nullable final Retained retained) {
56
- if (identity == null || retained == null) {
75
+ return retain(identity, retained, true);
76
+ }
77
+
78
+ /**
79
+ * {@code owned} is true for a span JS started, false for one it only
80
+ * adopted (the active span). Only owned spans are cancelled at teardown.
81
+ */
82
+ synchronized String retain(
83
+ @Nullable final Object identity,
84
+ @Nullable final Retained retained,
85
+ final boolean owned
86
+ ) {
87
+ if (closed || identity == null || retained == null) {
57
88
  return "";
58
89
  }
59
90
  final String existing = byIdentity.get(identity);
@@ -62,7 +93,7 @@ final class SpanHandles {
62
93
  }
63
94
  final String handle = "sp-" + next;
64
95
  next += 1;
65
- byHandle.put(handle, new Entry(identity, retained));
96
+ byHandle.put(handle, new Entry(identity, retained, owned));
66
97
  byIdentity.put(identity, handle);
67
98
  return handle;
68
99
  }
@@ -73,7 +104,7 @@ final class SpanHandles {
73
104
  * down). Other retained spans are dropped only when they now report
74
105
  * finished. Empty when {@code handle} is not held.
75
106
  */
76
- synchronized List<String> finish(final String handle, @Nullable final SpanStatus status) {
107
+ synchronized List<Released> finish(final String handle, @Nullable final SpanStatus status) {
77
108
  final Entry entry = byHandle.get(handle);
78
109
  if (entry == null) {
79
110
  return Collections.emptyList();
@@ -88,6 +119,31 @@ final class SpanHandles {
88
119
  byIdentity.clear();
89
120
  }
90
121
 
122
+ /**
123
+ * Cancels every span JS created that is still open, then drops every
124
+ * handle and refuses later retains. Used when the module goes away: the
125
+ * next runtime cannot know these handles, so nothing would ever finish
126
+ * them. A span JS only adopted is dropped, not cancelled: the SDK or the
127
+ * app owns it. Entries are visited in the order they were retained, so a
128
+ * parent goes first. A span that throws is skipped.
129
+ */
130
+ synchronized void cancelOpenAndReleaseAll() {
131
+ try {
132
+ for (final Entry entry : new ArrayList<>(byHandle.values())) {
133
+ try {
134
+ if (entry.owned && !entry.retained.isFinished()) {
135
+ entry.retained.finish(SpanStatus.CANCELLED);
136
+ }
137
+ } catch (final RuntimeException ignored) {
138
+ // One span the SDK refuses must not strand the others.
139
+ }
140
+ }
141
+ } finally {
142
+ closed = true;
143
+ releaseAll();
144
+ }
145
+ }
146
+
91
147
  synchronized boolean contains(final String handle) {
92
148
  return byHandle.containsKey(handle);
93
149
  }
@@ -121,12 +177,12 @@ final class SpanHandles {
121
177
  }
122
178
 
123
179
  /** Caller holds the lock. {@code called} is removed whether or not it reports finished. */
124
- private List<String> releaseFinished(final String called) {
125
- final List<String> released = new ArrayList<>();
180
+ private List<Released> releaseFinished(final String called) {
181
+ final List<Released> released = new ArrayList<>();
126
182
  final Entry calledEntry = byHandle.remove(called);
127
183
  if (calledEntry != null) {
128
184
  byIdentity.remove(calledEntry.identity);
129
- released.add(called);
185
+ released.add(new Released(called, calledEntry.retained.statusOrdinal()));
130
186
  }
131
187
  final Iterator<Map.Entry<String, Entry>> entries = byHandle.entrySet().iterator();
132
188
  while (entries.hasNext()) {
@@ -134,7 +190,7 @@ final class SpanHandles {
134
190
  if (!entry.getValue().retained.isFinished()) {
135
191
  continue;
136
192
  }
137
- released.add(entry.getKey());
193
+ released.add(new Released(entry.getKey(), entry.getValue().retained.statusOrdinal()));
138
194
  byIdentity.remove(entry.getValue().identity);
139
195
  entries.remove();
140
196
  }