MockSubscriberSDK.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. import FirebaseSessions
  17. // Avoids exposing internal FirebaseCore APIs to Swift users.
  18. @_implementationOnly import FirebaseCoreExtension
  19. @objc(FIRMockSubscriberSDKProtocol)
  20. protocol MockSubscriberSDKProtocol {
  21. func emptyProtocol()
  22. }
  23. ///
  24. /// The MockSubscriberSDK pretends to be Firebase Performance because without
  25. /// any Integrated Product SDKs installed, the Sessions SDK will not send events. This
  26. /// is because data collection is handled only in product SDKs.
  27. ///
  28. @objc(FIRMockSubscriberSDK) final class MockSubscriberSDK: NSObject, Library, SessionsSubscriber,
  29. MockSubscriberSDKProtocol {
  30. static func addDependency() {
  31. FirebaseApp.registerInternalLibrary(
  32. MockSubscriberSDK.self,
  33. withName: "mock-firebase-sessions-subscriber-sdk"
  34. )
  35. SessionsDependencies.addDependency(name: SessionsSubscriberName.Performance)
  36. }
  37. init(app: FirebaseApp) {
  38. super.init()
  39. let sessions = ComponentType<SessionsProvider>.instance(for: SessionsProvider.self,
  40. in: app.container)
  41. sessions.register(subscriber: self)
  42. }
  43. // MARK: - Library Conformance
  44. static func componentsToRegister() -> [Component] {
  45. let sessionsDependency = Dependency(with: SessionsProvider.self, isRequired: false)
  46. return [Component(MockSubscriberSDKProtocol.self,
  47. instantiationTiming: .alwaysEager,
  48. dependencies: [sessionsDependency]) { container, isCacheable in
  49. // Sessions SDK only works for the default app
  50. guard let app = container.app, app.isDefaultApp else { return nil }
  51. isCacheable.pointee = true
  52. return self.init(app: app)
  53. }]
  54. }
  55. // MARK: - SessionsSubscriber Conformance
  56. func onSessionChanged(_ session: FirebaseSessions.SessionDetails) {}
  57. var isDataCollectionEnabled: Bool {
  58. return true
  59. }
  60. var sessionsSubscriberName: FirebaseSessions.SessionsSubscriberName {
  61. return FirebaseSessions.SessionsSubscriberName.Performance
  62. }
  63. // MARK: - MockSubscriberSDKProtocol Conformance
  64. func emptyProtocol() {}
  65. }