@capgo/capacitor-inappbrowser 8.6.15 → 8.6.16

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.
@@ -63,7 +63,7 @@ import org.json.JSONObject;
63
63
  )
64
64
  public class InAppBrowserPlugin extends Plugin implements WebViewDialog.PermissionHandler {
65
65
 
66
- private final String pluginVersion = "8.6.15";
66
+ private final String pluginVersion = "8.6.16";
67
67
 
68
68
  public static final String CUSTOM_TAB_PACKAGE_NAME = "com.android.chrome"; // Change when in stable
69
69
  private CustomTabsClient customTabsClient;
@@ -77,7 +77,7 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin {
77
77
  case aware = "AWARE"
78
78
  case fakeVisible = "FAKE_VISIBLE"
79
79
  }
80
- private let pluginVersion: String = "8.6.15"
80
+ private let pluginVersion: String = "8.6.16"
81
81
  public let identifier = "InAppBrowserPlugin"
82
82
  public let jsName = "InAppBrowser"
83
83
  public let pluginMethods: [CAPPluginMethod] = [
@@ -403,6 +403,7 @@ enum ProxySchemeRequestSupport {
403
403
  }
404
404
 
405
405
  final class PendingProxyTask {
406
+ let requestId: String
406
407
  let schemeTask: WKURLSchemeTask
407
408
  var requestContext: NativeRequestContext
408
409
  var responseData: NativeResponseData?
@@ -411,12 +412,32 @@ final class PendingProxyTask {
411
412
  var redirectRequest: URLRequest?
412
413
  var timeoutToken: UUID?
413
414
  var canceled = false
414
-
415
- init(schemeTask: WKURLSchemeTask, requestContext: NativeRequestContext, phase: String) {
415
+ /// Set true when WebKit calls `webView(_:stop:)` for this task. Once stopped,
416
+ /// the `WKURLSchemeTask` must never receive `didReceive`/`didFinish`/`didFailWithError`
417
+ /// again or WebKit raises an uncatchable NSException.
418
+ var isStopped = false
419
+ /// Set true the first time a terminal scheme-task call sequence is committed.
420
+ /// Guarantees at-most-once completion even when multiple async sources
421
+ /// (URLSession delegate queue, cookie-sync main hop, global timeout, cancel-all)
422
+ /// race to finish the same request.
423
+ private var isComplete = false
424
+
425
+ init(requestId: String, schemeTask: WKURLSchemeTask, requestContext: NativeRequestContext, phase: String) {
426
+ self.requestId = requestId
416
427
  self.schemeTask = schemeTask
417
428
  self.requestContext = requestContext
418
429
  self.phase = phase
419
430
  }
431
+
432
+ /// Claims the right to issue the single allowed terminal call sequence on the
433
+ /// scheme task. Returns true exactly once, and only while the task is live
434
+ /// (not stopped). Callers MUST hold the handler's `taskLock` so the claim is
435
+ /// atomic with respect to `webView(_:stop:)` marking the task stopped.
436
+ func beginCompletionIfLive() -> Bool {
437
+ if isComplete || isStopped { return false }
438
+ isComplete = true
439
+ return true
440
+ }
420
441
  }
421
442
 
422
443
  // swiftlint:disable type_body_length
@@ -491,7 +512,7 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
491
512
  base64Body: extractBody(from: urlSchemeTask.request),
492
513
  isMainFrame: ProxySchemeRequestSupport.isMainFrameRequest(urlSchemeTask.request)
493
514
  )
494
- let pendingTask = PendingProxyTask(schemeTask: urlSchemeTask, requestContext: requestContext, phase: "outbound")
515
+ let pendingTask = PendingProxyTask(requestId: requestId, schemeTask: urlSchemeTask, requestContext: requestContext, phase: "outbound")
495
516
 
496
517
  taskLock.lock()
497
518
  pendingTasks[requestId] = pendingTask
@@ -503,6 +524,7 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
503
524
  public func webView(_ webView: WKWebView, stop urlSchemeTask: WKURLSchemeTask) {
504
525
  taskLock.lock()
505
526
  if let entry = pendingTasks.first(where: { $0.value.schemeTask === urlSchemeTask }) {
527
+ entry.value.isStopped = true
506
528
  pendingTasks.removeValue(forKey: entry.key)
507
529
  let cleanupToken = UUID()
508
530
  stoppedRequests[entry.key] = cleanupToken
@@ -573,7 +595,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
573
595
 
574
596
  if pendingTask.canceled {
575
597
  finishWithCanceledResponse(task: pendingTask)
576
- removePendingTask(requestId: requestId)
577
598
  return
578
599
  }
579
600
 
@@ -583,7 +604,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
583
604
  ) {
584
605
  case .finishCachedResponse:
585
606
  guard let responseData = pendingTask.responseData else { return }
586
- removePendingTask(requestId: requestId)
587
607
  syncResponseCookies(from: responseData, fallbackURL: pendingTask.requestContext.url) {
588
608
  self.finish(task: pendingTask, with: responseData)
589
609
  }
@@ -600,14 +620,14 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
600
620
  do {
601
621
  request = try makeURLRequest(from: pendingTask.requestContext)
602
622
  } catch {
603
- pendingTask.schemeTask.didFailWithError(
604
- NSError(
623
+ failSchemeTask(
624
+ pendingTask,
625
+ with: NSError(
605
626
  domain: "ProxySchemeHandler",
606
627
  code: -3,
607
628
  userInfo: [NSLocalizedDescriptionKey: error.localizedDescription]
608
629
  )
609
630
  )
610
- removePendingTask(requestId: requestId)
611
631
  return
612
632
  }
613
633
  let task = session.dataTask(with: request) { [weak self] data, response, error in
@@ -615,13 +635,18 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
615
635
  guard let pendingTask = self.pendingTask(for: requestId) else { return }
616
636
 
617
637
  if let error {
618
- if (error as NSError).code == NSURLErrorCancelled, pendingTask.redirectRequest != nil {
638
+ if (error as NSError).code == NSURLErrorCancelled {
639
+ // The data task is only canceled by webView(_:stop:) or
640
+ // cancelAllPendingTasks(), and both own the task's lifecycle: stop has
641
+ // already removed it (and marked it stopped) before canceling, while
642
+ // cancelAll routes a terminal failSchemeTask through completeSchemeTask,
643
+ // which removes it on the main queue. Removing it here on the URLSession
644
+ // background queue would unhook it from pendingTasks before a racing
645
+ // stop could mark it stopped, reopening the post-stop completion crash.
646
+ // So just stop processing and let the owning path complete it.
619
647
  return
620
648
  }
621
- if (error as NSError).code != NSURLErrorCancelled {
622
- pendingTask.schemeTask.didFailWithError(error)
623
- }
624
- self.removePendingTask(requestId: requestId)
649
+ self.failSchemeTask(pendingTask, with: error)
625
650
  return
626
651
  }
627
652
 
@@ -680,13 +705,11 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
680
705
  switch inboundRule.action {
681
706
  case .cancel:
682
707
  finishWithCanceledResponse(task: pendingTask)
683
- removePendingTask(requestId: requestId)
684
708
  case .continue:
685
709
  if pendingTask.redirectRequest != nil {
686
710
  followPendingRedirect(requestId: requestId)
687
711
  } else {
688
712
  finish(task: pendingTask, with: responseData)
689
- removePendingTask(requestId: requestId)
690
713
  }
691
714
  case .delegateToJs:
692
715
  emitProxyEvent(requestId: requestId, pendingTask: pendingTask)
@@ -697,7 +720,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
697
720
  followPendingRedirect(requestId: requestId)
698
721
  } else {
699
722
  finish(task: pendingTask, with: responseData)
700
- removePendingTask(requestId: requestId)
701
723
  }
702
724
  }
703
725
  }
@@ -759,7 +781,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
759
781
  self.fallbackToNativePipeline(requestId: requestId)
760
782
  return
761
783
  case .finishCachedResponse:
762
- self.pendingTasks.removeValue(forKey: requestId)
763
784
  self.taskLock.unlock()
764
785
  guard let responseData else { return }
765
786
  self.syncResponseCookies(from: responseData, fallbackURL: pendingTask.requestContext.url) {
@@ -767,12 +788,12 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
767
788
  }
768
789
  return
769
790
  case .failRequest:
770
- self.pendingTasks.removeValue(forKey: requestId)
771
791
  self.taskLock.unlock()
772
792
  }
773
793
 
774
- pendingTask.schemeTask.didFailWithError(
775
- NSError(
794
+ self.failSchemeTask(
795
+ pendingTask,
796
+ with: NSError(
776
797
  domain: "ProxySchemeHandler",
777
798
  code: NSURLErrorTimedOut,
778
799
  userInfo: [NSLocalizedDescriptionKey: "Proxy handler did not respond within \(Int(self.proxyTimeoutSeconds)) seconds"]
@@ -825,14 +846,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
825
846
  return task
826
847
  }
827
848
 
828
- private func removePendingTask(requestId: String) {
829
- taskLock.lock()
830
- pendingTasks.removeValue(forKey: requestId)
831
- stoppedRequests.removeValue(forKey: requestId)
832
- timedOutRequests.removeValue(forKey: requestId)
833
- taskLock.unlock()
834
- }
835
-
836
849
  private func routeCurrentRequest(requestId: String) {
837
850
  guard let pendingTask = pendingTask(for: requestId) else { return }
838
851
  pendingTask.phase = "outbound"
@@ -842,7 +855,6 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
842
855
  switch outboundRule.action {
843
856
  case .cancel:
844
857
  finishWithCanceledResponse(task: pendingTask)
845
- removePendingTask(requestId: requestId)
846
858
  case .continue:
847
859
  executeNativePipeline(requestId: requestId)
848
860
  case .delegateToJs:
@@ -1022,27 +1034,73 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
1022
1034
  )
1023
1035
  }
1024
1036
 
1037
+ /// Runs a terminal `WKURLSchemeTask` call sequence at most once, and never after
1038
+ /// the task has been stopped.
1039
+ ///
1040
+ /// `WKURLSchemeTask` raises an uncatchable NSException if `didReceive`/`didFinish`/
1041
+ /// `didFailWithError` is called after `webView(_:stop:)`, after the task already
1042
+ /// completed, or out of order. Completion can be triggered from several racing
1043
+ /// sources — the URLSession delegate queue, the cookie-sync main-thread hop, the
1044
+ /// global timeout closure, `webView(_:stop:)`, and `cancelAllPendingTasks()`. This
1045
+ /// helper funnels every terminal call onto the main thread (the same queue WebKit
1046
+ /// uses for `start`/`stop`) and gates it behind `beginCompletionIfLive()`, so the
1047
+ /// liveness check and the actual call cannot be separated by a `stop`.
1048
+ ///
1049
+ /// This helper is also the single owner of task removal. Callers MUST NOT remove
1050
+ /// the task from `pendingTasks` eagerly on a background thread before calling a
1051
+ /// terminal method: doing so creates a window where `webView(_:stop:)` (which only
1052
+ /// finds tasks via `pendingTasks`) can no longer mark the task stopped, so a queued
1053
+ /// completion would then call `didReceive`/`didFinish`/`didFailWithError` on an
1054
+ /// already-stopped task and crash. Keeping the task tracked until this main-queue
1055
+ /// block runs lets a racing `stop` win the liveness claim.
1056
+ private func completeSchemeTask(_ task: PendingProxyTask, _ body: @escaping (WKURLSchemeTask) -> Void) {
1057
+ let runBody: () -> Void = { [weak self] in
1058
+ guard let self else { return }
1059
+ self.taskLock.lock()
1060
+ let isLive = task.beginCompletionIfLive()
1061
+ self.pendingTasks.removeValue(forKey: task.requestId)
1062
+ self.stoppedRequests.removeValue(forKey: task.requestId)
1063
+ self.timedOutRequests.removeValue(forKey: task.requestId)
1064
+ self.taskLock.unlock()
1065
+ guard isLive else { return }
1066
+ body(task.schemeTask)
1067
+ }
1068
+ if Thread.isMainThread {
1069
+ runBody()
1070
+ } else {
1071
+ DispatchQueue.main.async(execute: runBody)
1072
+ }
1073
+ }
1074
+
1075
+ private func failSchemeTask(_ task: PendingProxyTask, with error: Error) {
1076
+ completeSchemeTask(task) { schemeTask in
1077
+ schemeTask.didFailWithError(error)
1078
+ }
1079
+ }
1080
+
1025
1081
  private func finish(task: PendingProxyTask, with responseData: NativeResponseData) {
1026
- guard let url = URL(string: task.requestContext.url),
1027
- let httpResponse = HTTPURLResponse(
1028
- url: url,
1029
- statusCode: responseData.statusCode,
1030
- httpVersion: "HTTP/1.1",
1031
- headerFields: responseData.headers
1032
- )
1033
- else {
1034
- task.schemeTask.didFailWithError(
1035
- NSError(
1036
- domain: "ProxySchemeHandler",
1037
- code: -2,
1038
- userInfo: [NSLocalizedDescriptionKey: "Failed to create response"]
1082
+ completeSchemeTask(task) { schemeTask in
1083
+ guard let url = URL(string: task.requestContext.url),
1084
+ let httpResponse = HTTPURLResponse(
1085
+ url: url,
1086
+ statusCode: responseData.statusCode,
1087
+ httpVersion: "HTTP/1.1",
1088
+ headerFields: responseData.headers
1089
+ )
1090
+ else {
1091
+ schemeTask.didFailWithError(
1092
+ NSError(
1093
+ domain: "ProxySchemeHandler",
1094
+ code: -2,
1095
+ userInfo: [NSLocalizedDescriptionKey: "Failed to create response"]
1096
+ )
1039
1097
  )
1040
- )
1041
- return
1098
+ return
1099
+ }
1100
+ schemeTask.didReceive(httpResponse)
1101
+ schemeTask.didReceive(responseData.body)
1102
+ schemeTask.didFinish()
1042
1103
  }
1043
- task.schemeTask.didReceive(httpResponse)
1044
- task.schemeTask.didReceive(responseData.body)
1045
- task.schemeTask.didFinish()
1046
1104
  }
1047
1105
 
1048
1106
  private func finishWithCanceledResponse(task: PendingProxyTask) {
@@ -1138,15 +1196,19 @@ public class ProxySchemeHandler: NSObject, WKURLSchemeHandler, URLSessionTaskDel
1138
1196
  func cancelAllPendingTasks() {
1139
1197
  taskLock.lock()
1140
1198
  let pending = pendingTasks
1141
- pendingTasks.removeAll()
1199
+ // Do not clear pendingTasks here. completeSchemeTask removes each task on the
1200
+ // main queue once its failure is claimed, so a concurrent webView(_:stop:) can
1201
+ // still find and mark the task stopped before the queued failure runs. Clearing
1202
+ // eagerly would reopen the post-stop completion crash this fix closes.
1142
1203
  stoppedRequests.removeAll()
1143
1204
  taskLock.unlock()
1144
1205
  session.invalidateAndCancel()
1145
1206
 
1146
1207
  for (_, task) in pending {
1147
1208
  task.urlSessionTask?.cancel()
1148
- task.schemeTask.didFailWithError(
1149
- NSError(
1209
+ failSchemeTask(
1210
+ task,
1211
+ with: NSError(
1150
1212
  domain: "ProxySchemeHandler",
1151
1213
  code: NSURLErrorCancelled,
1152
1214
  userInfo: [NSLocalizedDescriptionKey: "WebView closed"]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-inappbrowser",
3
- "version": "8.6.15",
3
+ "version": "8.6.16",
4
4
  "description": "Capacitor plugin in app browser",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",