tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ProgressDelegate.swift (2812B)


      1 // This Source Code Form is subject to the terms of the Mozilla Public
      2 // License, v. 2.0. If a copy of the MPL was not distributed with this
      3 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 public protocol ProgressDelegate {
      6    /// A View has started loading content from the network.
      7    func onPageStart(session: GeckoSession, url: String)
      8 
      9    /// A View has finished loading content from the network.
     10    func onPageStop(session: GeckoSession, success: Bool)
     11 
     12    /// Page loading has progressed.
     13    func onProgressChange(session: GeckoSession, progress: Int)
     14 
     15    /// The security status has been updated.
     16    // FIXME: Implement onSecurityChange & SecurityInformation
     17    // func onSecurityChange(session: GeckoSession, securityInfo: SecurityInformation)
     18 
     19    /// The browser session state has changed. This can happen in response to navigation, scrolling,
     20    /// or form data changes; the session state passed includes the most up to date information on
     21    /// all of these.
     22    // FIXME: Implement onSessionStateChange & SessionState
     23    // func onSessionStateChange(session: GeckoSession, sessionState: SessionState)
     24 }
     25 
     26 // All methods on ProgressDelegate are optional, provide default implementations.
     27 extension ProgressDelegate {
     28    public func onPageStart(session: GeckoSession, url: String) {}
     29    public func onPageStop(session: GeckoSession, success: Bool) {}
     30    public func onProgressChange(session: GeckoSession, progress: Int) {}
     31    // func onSecurityChange(session: GeckoSession, securityInfo: SecurityInformation) {}
     32    // func onSessionStateChange(session: GeckoSession, sessionState: SessionState) {}
     33 }
     34 
     35 enum ProgressEvents: String, CaseIterable {
     36    case pageStart = "GeckoView:PageStart"
     37    case pageStop = "GeckoView:PageStop"
     38    case progressChanged = "GeckoView:ProgressChanged"
     39    case securityChanged = "GeckoView:SecurityChanged"
     40    case stateUpdated = "GeckoView:StateUpdated"
     41 }
     42 
     43 func newProgressHandler(_ session: GeckoSession) -> GeckoSessionHandler<
     44    ProgressDelegate, ProgressEvents
     45 > {
     46    GeckoSessionHandler(moduleName: "GeckoViewProgress", session: session) {
     47        @MainActor session, delegate, event, message in
     48        switch event {
     49        case .pageStart:
     50            delegate?.onPageStart(session: session, url: message!["uri"] as! String)
     51            return nil
     52        case .pageStop:
     53            delegate?.onPageStop(session: session, success: message!["success"] as! Bool)
     54            return nil
     55        case .progressChanged:
     56            delegate?.onProgressChange(session: session, progress: message!["progress"] as! Int)
     57            return nil
     58        case .securityChanged:
     59            // TODO: Implement
     60            return nil
     61        case .stateUpdated:
     62            // TODO: Implement
     63            return nil
     64        }
     65    }
     66 }