Skip to content

Commit

Permalink
Apply more dependency injection to the DeviceAuthenticator.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Feb 22, 2022
1 parent bd62272 commit aeb650c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 49 deletions.
32 changes: 24 additions & 8 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@
4B1E6EF227AB5E5D00F51793 /* PasswordManagementItemList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B1E6EF027AB5E5D00F51793 /* PasswordManagementItemList.swift */; };
4B2CBF412767EEC1001DF04B /* MacWaitlistStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B2CBF402767EEC1001DF04B /* MacWaitlistStoreTests.swift */; };
4B2E7D6326FF9D6500D2DB17 /* PrintingUserScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B2E7D6226FF9D6500D2DB17 /* PrintingUserScript.swift */; };
4B379C1527BD91E3008A968E /* DeviceIdleStateDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1427BD91E3008A968E /* DeviceIdleStateDetector.swift */; };
4B379C1527BD91E3008A968E /* QuartzIdleStateDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1427BD91E3008A968E /* QuartzIdleStateDetector.swift */; };
4B379C1727BD9D7C008A968E /* LoginsPreferencesTableCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1627BD9D7C008A968E /* LoginsPreferencesTableCellView.swift */; };
4B379C1927BD9EAA008A968E /* LoginsPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1827BD9EAA008A968E /* LoginsPreferences.swift */; };
4B379C1B27BD9F88008A968E /* LoginsPreferencesTableCellView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4B379C1A27BD9F88008A968E /* LoginsPreferencesTableCellView.xib */; };
4B379C1E27BDB7FF008A968E /* DeviceAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1D27BDB7FF008A968E /* DeviceAuthenticator.swift */; };
4B379C2027BDBA18008A968E /* DeviceAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C1F27BDBA18008A968E /* DeviceAuthenticationService.swift */; };
4B379C2227BDBA29008A968E /* LocalAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C2127BDBA29008A968E /* LocalAuthenticationService.swift */; };
4B379C2427BDE1B0008A968E /* FlatButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B379C2327BDE1B0008A968E /* FlatButton.swift */; };
4B3F641E27A8D3BD00E0C118 /* BrowserProfileTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B3F641D27A8D3BD00E0C118 /* BrowserProfileTests.swift */; };
Expand Down Expand Up @@ -205,6 +204,9 @@
4BB99D1026FE1A84001E4761 /* FirefoxBookmarksReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB99D0D26FE1A83001E4761 /* FirefoxBookmarksReaderTests.swift */; };
4BB99D1126FE1A84001E4761 /* SafariBookmarksReaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BB99D0E26FE1A84001E4761 /* SafariBookmarksReaderTests.swift */; };
4BB99D1326FE1A94001E4761 /* places.sqlite in Resources */ = {isa = PBXBuildFile; fileRef = 4BB99D1226FE1A94001E4761 /* places.sqlite */; };
4BBC16A027C4859400E00A38 /* DeviceAuthenticationService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBC169F27C4859400E00A38 /* DeviceAuthenticationService.swift */; };
4BBC16A227C485BC00E00A38 /* DeviceIdleStateDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBC16A127C485BC00E00A38 /* DeviceIdleStateDetector.swift */; };
4BBC16A527C488C900E00A38 /* DeviceAuthenticatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBC16A427C488C900E00A38 /* DeviceAuthenticatorTests.swift */; };
4BBE0AA727B9B027003B37A8 /* PopUpButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBE0AA627B9B027003B37A8 /* PopUpButton.swift */; };
4BBE0ADA27BC8CDE003B37A8 /* PasswordManagementListSectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BBE0AD927BC8CDE003B37A8 /* PasswordManagementListSectionTests.swift */; };
4BC68A702759AE490029A586 /* Waitlist.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4BC68A6F2759AE490029A586 /* Waitlist.storyboard */; };
Expand Down Expand Up @@ -759,12 +761,11 @@
4B1E6EF027AB5E5D00F51793 /* PasswordManagementItemList.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PasswordManagementItemList.swift; sourceTree = "<group>"; };
4B2CBF402767EEC1001DF04B /* MacWaitlistStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacWaitlistStoreTests.swift; sourceTree = "<group>"; };
4B2E7D6226FF9D6500D2DB17 /* PrintingUserScript.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrintingUserScript.swift; sourceTree = "<group>"; };
4B379C1427BD91E3008A968E /* DeviceIdleStateDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceIdleStateDetector.swift; sourceTree = "<group>"; };
4B379C1427BD91E3008A968E /* QuartzIdleStateDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuartzIdleStateDetector.swift; sourceTree = "<group>"; };
4B379C1627BD9D7C008A968E /* LoginsPreferencesTableCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginsPreferencesTableCellView.swift; sourceTree = "<group>"; };
4B379C1827BD9EAA008A968E /* LoginsPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginsPreferences.swift; sourceTree = "<group>"; };
4B379C1A27BD9F88008A968E /* LoginsPreferencesTableCellView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LoginsPreferencesTableCellView.xib; sourceTree = "<group>"; };
4B379C1D27BDB7FF008A968E /* DeviceAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceAuthenticator.swift; sourceTree = "<group>"; };
4B379C1F27BDBA18008A968E /* DeviceAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceAuthenticationService.swift; sourceTree = "<group>"; };
4B379C2127BDBA29008A968E /* LocalAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalAuthenticationService.swift; sourceTree = "<group>"; };
4B379C2327BDE1B0008A968E /* FlatButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FlatButton.swift; sourceTree = "<group>"; };
4B3F641D27A8D3BD00E0C118 /* BrowserProfileTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserProfileTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -902,6 +903,9 @@
4BB99D0D26FE1A83001E4761 /* FirefoxBookmarksReaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FirefoxBookmarksReaderTests.swift; sourceTree = "<group>"; };
4BB99D0E26FE1A84001E4761 /* SafariBookmarksReaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SafariBookmarksReaderTests.swift; sourceTree = "<group>"; };
4BB99D1226FE1A94001E4761 /* places.sqlite */ = {isa = PBXFileReference; lastKnownFileType = file; path = places.sqlite; sourceTree = "<group>"; };
4BBC169F27C4859400E00A38 /* DeviceAuthenticationService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceAuthenticationService.swift; sourceTree = "<group>"; };
4BBC16A127C485BC00E00A38 /* DeviceIdleStateDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceIdleStateDetector.swift; sourceTree = "<group>"; };
4BBC16A427C488C900E00A38 /* DeviceAuthenticatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceAuthenticatorTests.swift; sourceTree = "<group>"; };
4BBE0AA627B9B027003B37A8 /* PopUpButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PopUpButton.swift; sourceTree = "<group>"; };
4BBE0AD927BC8CDE003B37A8 /* PasswordManagementListSectionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordManagementListSectionTests.swift; sourceTree = "<group>"; };
4BC68A6F2759AE490029A586 /* Waitlist.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Waitlist.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1583,10 +1587,11 @@
4B379C1C27BDB7EA008A968E /* Device Authentication */ = {
isa = PBXGroup;
children = (
4B379C1F27BDBA18008A968E /* DeviceAuthenticationService.swift */,
4BBC169F27C4859400E00A38 /* DeviceAuthenticationService.swift */,
4B379C2127BDBA29008A968E /* LocalAuthenticationService.swift */,
4BBC16A127C485BC00E00A38 /* DeviceIdleStateDetector.swift */,
4B379C1427BD91E3008A968E /* QuartzIdleStateDetector.swift */,
4B379C1D27BDB7FF008A968E /* DeviceAuthenticator.swift */,
4B379C1427BD91E3008A968E /* DeviceIdleStateDetector.swift */,
);
path = "Device Authentication";
sourceTree = "<group>";
Expand Down Expand Up @@ -1965,6 +1970,14 @@
path = "Test Safari Data";
sourceTree = "<group>";
};
4BBC16A327C488B900E00A38 /* Device Authentication */ = {
isa = PBXGroup;
children = (
4BBC16A427C488C900E00A38 /* DeviceAuthenticatorTests.swift */,
);
path = "Device Authentication";
sourceTree = "<group>";
};
4BC68A6C2759ADC20029A586 /* Waitlist */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2456,6 +2469,7 @@
4B723E0226B0003E00E14D75 /* Data Export */,
B683097A274DCFE3004B46BB /* Database */,
4B723DFE26B0003E00E14D75 /* Data Import */,
4BBC16A327C488B900E00A38 /* Device Authentication */,
8553FF50257523630029327F /* FileDownload */,
4BA1A6CE258BF58C00F6F690 /* FileSystem */,
AA9C361D25518AAB004B1BA3 /* Fire */,
Expand Down Expand Up @@ -4033,10 +4047,12 @@
B6B1E88426D5EB570062C350 /* DownloadsCellView.swift in Sources */,
4B723E0C26B0005D00E14D75 /* CSVImportSummaryViewController.swift in Sources */,
B6AAAC2D260330580029438D /* PublishedAfter.swift in Sources */,
4BBC16A027C4859400E00A38 /* DeviceAuthenticationService.swift in Sources */,
4BE6547E271FCD4D008D1D63 /* PasswordManagementIdentityModel.swift in Sources */,
85C6A29625CC1FFD00EEB5F1 /* UserDefaultsWrapper.swift in Sources */,
85625998269C9C5F00EE44BC /* PasswordManagementPopover.swift in Sources */,
4BB99CFE26FE191E001E4761 /* FirefoxBookmarksReader.swift in Sources */,
4BBC16A227C485BC00E00A38 /* DeviceIdleStateDetector.swift in Sources */,
B6A9E4A3261475C70067D1B9 /* AppUsageActivityMonitor.swift in Sources */,
4B379C2427BDE1B0008A968E /* FlatButton.swift in Sources */,
4BA1A6A0258B079600F6F690 /* DataEncryption.swift in Sources */,
Expand Down Expand Up @@ -4232,7 +4248,7 @@
B693955426F04BEC0015B914 /* ColorView.swift in Sources */,
B6BBF17427475B15004F850E /* PopupBlockedPopover.swift in Sources */,
8589063A267BCD8E00D23B0D /* SaveCredentialsPopover.swift in Sources */,
4B379C1527BD91E3008A968E /* DeviceIdleStateDetector.swift in Sources */,
4B379C1527BD91E3008A968E /* QuartzIdleStateDetector.swift in Sources */,
AA72D5E325FE977F00C77619 /* AddEditFavoriteViewController.swift in Sources */,
B6C0B22E26E61CE70031CB7F /* DownloadViewModel.swift in Sources */,
B68458B825C7E8B200DC17B6 /* Tab+NSSecureCoding.swift in Sources */,
Expand Down Expand Up @@ -4333,7 +4349,6 @@
4BF01C00272AE74C00884A61 /* CountryList.swift in Sources */,
AAC5E4D925D6A711007F5990 /* BookmarkStore.swift in Sources */,
B6FA893F269C424500588ECD /* PrivacyDashboardViewController.swift in Sources */,
4B379C2027BDBA18008A968E /* DeviceAuthenticationService.swift in Sources */,
AA72D5FE25FFF94E00C77619 /* NSMenuItemExtension.swift in Sources */,
4BA1A6C2258B0A1300F6F690 /* ContiguousBytesExtension.swift in Sources */,
AA9B7C7E26A06E040008D425 /* TrackerInfo.swift in Sources */,
Expand Down Expand Up @@ -4395,6 +4410,7 @@
4B723E0726B0003E00E14D75 /* CSVImporterTests.swift in Sources */,
AA652CDB25DDAB32009059CC /* BookmarkStoreMock.swift in Sources */,
B62EB47C25BAD3BB005745C6 /* WKWebViewPrivateMethodsAvailabilityTests.swift in Sources */,
4BBC16A527C488C900E00A38 /* DeviceAuthenticatorTests.swift in Sources */,
4B3F641E27A8D3BD00E0C118 /* BrowserProfileTests.swift in Sources */,
4BBE0ADA27BC8CDE003B37A8 /* PasswordManagementListSectionTests.swift in Sources */,
B6106BA026A7BE0B0013B453 /* PermissionManagerTests.swift in Sources */,
Expand Down
44 changes: 3 additions & 41 deletions DuckDuckGo/Device Authentication/DeviceIdleStateDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,11 @@
//

import Foundation
import CoreGraphics
import os.log

final class DeviceIdleStateDetector {
protocol DeviceIdleStateDetector {

private enum Constants {
static let intervalBetweenIdleChecks: TimeInterval = 1
}
func beginIdleCheckTimer()

private var timer: Timer?
private let idleTimeCallback: (TimeInterval) -> Void
func cancelIdleCheckTimer()

init(idleTimeCallback: @escaping (TimeInterval) -> Void) {
self.idleTimeCallback = idleTimeCallback
}

func beginIdleCheckTimer() {
os_log("Beginning idle check timer", log: .autoLock)

self.timer?.invalidate()
self.timer = nil

let timer = Timer(timeInterval: Constants.intervalBetweenIdleChecks, repeats: true) { [weak self] _ in
self?.idleTimeCallback(Self.secondsSinceLastEvent)
}

self.timer = timer
RunLoop.current.add(timer, forMode: .common)
}

func cancelIdleCheckTimer() {
os_log("Cancelling idle check timer", log: .autoLock)
self.timer?.invalidate()
self.timer = nil
}

private static var secondsSinceLastEvent: TimeInterval {
let anyInputEventType = CGEventType(rawValue: ~0)!
let secondsSinceLastEvent = CGEventSource.secondsSinceLastEventType(.hidSystemState, eventType: anyInputEventType)

os_log("Idle duration since last user input event: %f", log: .autoLock, secondsSinceLastEvent)

return secondsSinceLastEvent
}

}
73 changes: 73 additions & 0 deletions DuckDuckGo/Device Authentication/QuartzIdleStateDetector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// DeviceIdleStateDetector.swift
//
// Copyright © 2022 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import CoreGraphics
import os.log

final class QuartzIdleStateDetector: DeviceIdleStateDetector {

private enum Constants {
static let intervalBetweenIdleChecks: TimeInterval = 1
}

private var timer: Timer?
private let idleTimeCallback: (TimeInterval) -> Void

init(idleTimeCallback: @escaping (TimeInterval) -> Void) {
self.idleTimeCallback = idleTimeCallback
}

deinit {
cancelIdleCheckTimer()
}

func beginIdleCheckTimer() {
os_log("Beginning idle check timer", log: .autoLock)

self.timer?.invalidate()
self.timer = nil

let timer = Timer(timeInterval: Constants.intervalBetweenIdleChecks, repeats: true) { [weak self] _ in
guard let self = self else {
return
}

self.idleTimeCallback(self.secondsSinceLastEvent())
}

self.timer = timer
RunLoop.current.add(timer, forMode: .common)
}

func cancelIdleCheckTimer() {
os_log("Cancelling idle check timer", log: .autoLock)
self.timer?.invalidate()
self.timer = nil
}

private func secondsSinceLastEvent() -> TimeInterval {
let anyInputEventType = CGEventType(rawValue: ~0)!
let seconds = CGEventSource.secondsSinceLastEventType(.hidSystemState, eventType: anyInputEventType)

os_log("Idle duration since last user input event: %f", log: .autoLock, seconds)

return seconds
}

}
27 changes: 27 additions & 0 deletions Unit Tests/Device Authentication/DeviceAuthenticatorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// DeviceAuthenticatorTests.swift
//
// Copyright © 2022 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import Foundation
import XCTest
@testable import DuckDuckGo_Privacy_Browser

class DeviceAuthenticatorTests: XCTestCase {



}

0 comments on commit aeb650c

Please sign in to comment.