MockSubscriberSDK.swift 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 FirebaseSessions
  16. import Foundation
  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. return [Component(MockSubscriberSDKProtocol.self,
  46. instantiationTiming: .alwaysEager) { container, isCacheable in
  47. // Sessions SDK only works for the default app
  48. guard let app = container.app, app.isDefaultApp else { return nil }
  49. isCacheable.pointee = true
  50. return self.init(app: app)
  51. }]
  52. }
  53. // MARK: - SessionsSubscriber Conformance
  54. func onSessionChanged(_ session: FirebaseSessions.SessionDetails) {}
  55. var isDataCollectionEnabled: Bool {
  56. return true
  57. }
  58. var sessionsSubscriberName: FirebaseSessions.SessionsSubscriberName {
  59. return FirebaseSessions.SessionsSubscriberName.Performance
  60. }
  61. // MARK: - MockSubscriberSDKProtocol Conformance
  62. func emptyProtocol() {}
  63. }