WebClientSessionStore.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import ThreemaFramework
  22. @objc class WebClientSessionStore: NSObject {
  23. @objc static let shared = WebClientSessionStore()
  24. private var entityManager: EntityManager? = nil
  25. private override init() {
  26. entityManager = EntityManager()
  27. }
  28. @objc func webClientSessionForHash(_ hash: String) -> WebClientSession? {
  29. return entityManager!.entityFetcher.webClientSession(forInitiatorPermanentPublicKeyHash: hash)
  30. }
  31. @objc func activeWebClientSession() -> WebClientSession? {
  32. return entityManager!.entityFetcher.activeWebClientSession()
  33. }
  34. @objc func allWebClientSessions() -> [WebClientSession]? {
  35. return entityManager!.entityFetcher.allWebClientSessions() as? [WebClientSession]
  36. }
  37. func addWebClientSession(dictionary: [String: Any]) -> WebClientSession {
  38. var session: WebClientSession? = nil
  39. if let hash = dictionary["initiatorPermanentPublicKeyHash"] as? String {
  40. session = entityManager!.entityFetcher.webClientSession(forInitiatorPermanentPublicKeyHash: hash)
  41. }
  42. if session != nil {
  43. return session!
  44. }
  45. entityManager!.performSyncBlockAndSafe {
  46. session = self.entityManager!.entityCreator.webClientSession()
  47. session!.permanent = NSNumber.init(value: dictionary["permanent"] as! Bool) as NSNumber
  48. session!.saltyRTCHost = dictionary["saltyRTCHost"] as? String
  49. session!.initiatorPermanentPublicKey = dictionary["initiatorPermanentPublicKey"] as? Data
  50. session!.serverPermanentPublicKey = dictionary["serverPermanentPublicKey"] as? Data
  51. session!.saltyRTCPort = dictionary["saltyRTCPort"] as? NSNumber
  52. session!.version = dictionary["webClientVersion"] as? NSNumber
  53. session!.selfHosted = NSNumber.init(value: dictionary["selfHosted"] as! Bool) as NSNumber
  54. if let lastConnection = dictionary["lastConnection"] as? Date {
  55. session!.lastConnection = lastConnection
  56. }
  57. if let initiatorPermanentPublicKeyHash = dictionary["initiatorPermanentPublicKeyHash"] as? String {
  58. session!.initiatorPermanentPublicKeyHash = initiatorPermanentPublicKeyHash
  59. }
  60. if let privateKey = dictionary["privateKey"] as? Data {
  61. session!.privateKey = privateKey
  62. }
  63. if let browserName = dictionary["browserName"] as? String {
  64. session!.browserName = browserName
  65. }
  66. if let browserVersion = dictionary["browserVersion"] as? NSNumber {
  67. session!.browserVersion = browserVersion
  68. }
  69. if let active = dictionary["active"] as? Bool {
  70. session!.active = NSNumber.init(value: active) as NSNumber
  71. }
  72. }
  73. return session!
  74. }
  75. @objc func updateWebClientSession(session: WebClientSession, active: Bool) {
  76. entityManager!.performSyncBlockAndSafe {
  77. session.active = NSNumber.init(value: active) as NSNumber
  78. }
  79. }
  80. func updateWebClientSession(session: WebClientSession, privateKey: Data?) {
  81. entityManager!.performSyncBlockAndSafe {
  82. session.privateKey = privateKey
  83. }
  84. }
  85. func updateWebClientSession(session: WebClientSession, hash: String) {
  86. entityManager!.performSyncBlockAndSafe {
  87. session.initiatorPermanentPublicKeyHash = hash
  88. }
  89. }
  90. func updateWebClientSession(session: WebClientSession, lastConnection: Date) {
  91. entityManager!.performSyncBlockAndSafe {
  92. session.lastConnection = lastConnection
  93. }
  94. }
  95. func updateWebClientSession(session: WebClientSession, browserName: String!, browserVersion: NSNumber!) {
  96. entityManager!.performSyncBlockAndSafe {
  97. session.browserName = browserName
  98. session.browserVersion = browserVersion
  99. }
  100. }
  101. func updateWebClientSession(session: WebClientSession, sessionName: String?) {
  102. entityManager!.performSyncBlockAndSafe {
  103. session.name = sessionName
  104. }
  105. }
  106. func deleteAllWebClientSessions() {
  107. entityManager!.performSyncBlockAndSafe {
  108. let sessions = self.entityManager!.entityFetcher.allWebClientSessions() as? [WebClientSession]
  109. if sessions != nil {
  110. for session in sessions! {
  111. self.entityManager?.entityDestroyer.deleteObject(object: session)
  112. }
  113. }
  114. }
  115. }
  116. func deleteWebClientSession(_ session: WebClientSession) {
  117. entityManager!.performSyncBlockAndSafe {
  118. self.entityManager?.entityDestroyer.deleteObject(object: session)
  119. }
  120. }
  121. @objc func setAllWebClientSessionsInactive() {
  122. entityManager!.performSyncBlockAndSafe {
  123. let sessions = self.entityManager!.entityFetcher.allActiveWebClientSessions() as? [WebClientSession]
  124. if sessions != nil {
  125. for session: WebClientSession in sessions! {
  126. session.active = NSNumber.init(value: false) as NSNumber
  127. }
  128. }
  129. }
  130. }
  131. func removeAllNotPermanentSessions() {
  132. entityManager!.performSyncBlockAndSafe {
  133. let sessions = self.entityManager!.entityFetcher.allNotPermanentWebClientSessions() as? [WebClientSession]
  134. if sessions != nil {
  135. for session in sessions! {
  136. self.entityManager?.entityDestroyer.deleteObject(object: session)
  137. }
  138. }
  139. }
  140. }
  141. }