Skip to content

Commit 3bc1c1f

Browse files
authored
Refactor FXIOS-14456 #31308 ⁃ Fix test failing on XCode 26.2 to enable using this version on LoginHelper (#31611)
* Replace WKFrameInfo with PasswordGeneratorFrameContext + merge origin * Add back origin with meaningful name and update middle * Make scriptEvaluator and host non optional
1 parent 6f7d713 commit 3bc1c1f

File tree

7 files changed

+40
-28
lines changed

7 files changed

+40
-28
lines changed

firefox-ios/Client/Frontend/Browser/BrowserViewController/Actions/GeneralBrowserAction.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ struct GeneralBrowserAction: Action {
1717
let showOverlay: Bool?
1818
let buttonTapped: UIButton?
1919
let isNativeErrorPage: Bool?
20-
let frame: WKFrameInfo?
20+
let frameContext: PasswordGeneratorFrameContext?
2121
let summarizerConfig: SummarizerConfig?
2222
init(selectedTabURL: URL? = nil,
2323
isPrivateBrowsing: Bool? = nil,
2424
toastType: ToastType? = nil,
2525
showOverlay: Bool? = nil,
2626
buttonTapped: UIButton? = nil,
2727
isNativeErrorPage: Bool? = nil,
28-
frame: WKFrameInfo? = nil,
28+
frameContext: PasswordGeneratorFrameContext? = nil,
2929
summarizerConfig: SummarizerConfig? = nil,
3030
windowUUID: WindowUUID,
3131
actionType: ActionType) {
@@ -37,7 +37,7 @@ struct GeneralBrowserAction: Action {
3737
self.buttonTapped = buttonTapped
3838
self.showOverlay = showOverlay
3939
self.isNativeErrorPage = isNativeErrorPage
40-
self.frame = frame
40+
self.frameContext = frameContext
4141
self.summarizerConfig = summarizerConfig
4242
}
4343
}

firefox-ios/Client/Frontend/Browser/BrowserViewController/State/BrowserViewControllerState.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct BrowserViewControllerState: ScreenState {
4848
var navigateTo: NavigationType? // use default value when re-creating
4949
var displayView: DisplayType? // use default value when re-creating
5050
var buttonTapped: UIButton?
51-
var frame: WKFrameInfo?
51+
var frameContext: PasswordGeneratorFrameContext?
5252
var microsurveyState: MicrosurveyPromptState
5353
var navigationDestination: NavigationDestination?
5454

@@ -72,7 +72,7 @@ struct BrowserViewControllerState: ScreenState {
7272
navigateTo: bvcState.navigateTo,
7373
displayView: bvcState.displayView,
7474
buttonTapped: bvcState.buttonTapped,
75-
frame: bvcState.frame,
75+
frameContext: bvcState.frameContext,
7676
microsurveyState: bvcState.microsurveyState,
7777
navigationDestination: bvcState.navigationDestination)
7878
}
@@ -102,7 +102,7 @@ struct BrowserViewControllerState: ScreenState {
102102
navigateTo: NavigationType? = nil,
103103
displayView: DisplayType? = nil,
104104
buttonTapped: UIButton? = nil,
105-
frame: WKFrameInfo? = nil,
105+
frameContext: PasswordGeneratorFrameContext? = nil,
106106
microsurveyState: MicrosurveyPromptState,
107107
navigationDestination: NavigationDestination? = nil
108108
) {
@@ -116,7 +116,7 @@ struct BrowserViewControllerState: ScreenState {
116116
self.navigateTo = navigateTo
117117
self.displayView = displayView
118118
self.buttonTapped = buttonTapped
119-
self.frame = frame
119+
self.frameContext = frameContext
120120
self.microsurveyState = microsurveyState
121121
self.navigationDestination = navigationDestination
122122
}
@@ -610,7 +610,7 @@ struct BrowserViewControllerState: ScreenState {
610610
windowUUID: state.windowUUID,
611611
browserViewType: state.browserViewType,
612612
displayView: .passwordGenerator,
613-
frame: action.frame,
613+
frameContext: action.frameContext,
614614
microsurveyState: MicrosurveyPromptState.reducer(state.microsurveyState, action))
615615
}
616616

firefox-ios/Client/Frontend/Browser/BrowserViewController/Views/BrowserViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,8 +2882,8 @@ class BrowserViewController: UIViewController,
28822882
case .summarizer(let config):
28832883
navigationHandler?.showSummarizePanel(.toolbarIcon, config: config)
28842884
case .passwordGenerator:
2885-
if let tab = tabManager.selectedTab, let frame = state.frame {
2886-
navigationHandler?.showPasswordGenerator(tab: tab, frame: frame)
2885+
if let tab = tabManager.selectedTab, let frameContext = state.frameContext {
2886+
navigationHandler?.showPasswordGenerator(tab: tab, frameContext: frameContext)
28872887
}
28882888
}
28892889
}

firefox-ios/Client/Frontend/PasswordGenerator/PasswordGeneratorAction.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ import Redux
77
import Common
88
import WebKit
99

10-
struct PasswordGeneratorFrameContext {
10+
struct PasswordGeneratorFrameContext: Equatable {
1111
let origin: String?
1212
let host: String
13-
let scriptEvaluator: PasswordGeneratorScriptEvaluator?
13+
let scriptEvaluator: PasswordGeneratorScriptEvaluator
1414
let frameInfo: WKFrameInfo?
15+
16+
static func == (lhs: PasswordGeneratorFrameContext, rhs: PasswordGeneratorFrameContext) -> Bool {
17+
lhs.origin == rhs.origin &&
18+
lhs.host == rhs.host &&
19+
lhs.frameInfo === rhs.frameInfo
20+
}
1521
}
1622

1723
struct PasswordGeneratorAction: Action {
@@ -23,19 +29,18 @@ struct PasswordGeneratorAction: Action {
2329

2430
// Used in some reducers
2531
let password: String?
26-
27-
let origin: String?
32+
let loginEntryOrigin: String?
2833

2934
init(windowUUID: WindowUUID,
3035
actionType: any ActionType,
3136
password: String? = nil,
3237
frameContext: PasswordGeneratorFrameContext? = nil,
33-
origin: String? = nil) {
38+
loginEntryOrigin: String? = nil) {
3439
self.windowUUID = windowUUID
3540
self.actionType = actionType
3641
self.password = password
3742
self.frameContext = frameContext
38-
self.origin = origin
43+
self.loginEntryOrigin = loginEntryOrigin
3944
}
4045
}
4146

firefox-ios/Client/Frontend/PasswordGenerator/PasswordGeneratorMiddleware.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class PasswordGeneratorMiddleware {
4949
self.userTappedRefreshPassword(frameContext: frameContext, windowUUID: windowUUID)
5050

5151
case PasswordGeneratorActionType.clearGeneratedPasswordForSite:
52-
guard let origin = (action as? PasswordGeneratorAction)?.frameContext?.origin else { return }
52+
guard let origin = (action as? PasswordGeneratorAction)?.loginEntryOrigin else { return }
5353
self.clearGeneratedPasswordForSite(origin: origin, windowUUID: windowUUID)
5454

5555
default:
@@ -84,8 +84,8 @@ final class PasswordGeneratorMiddleware {
8484
completion: @MainActor @escaping (String) -> Void) {
8585
let originRules = PasswordGeneratorMiddleware.getPasswordRule(for: frameContext.host)
8686
let jsFunctionCall = "window.__firefox__.logins.generatePassword(\(originRules ?? "" ))"
87-
frameContext.scriptEvaluator?.evaluateJavascriptInDefaultContentWorld(jsFunctionCall,
88-
frameContext.frameInfo) { (result, error) in
87+
frameContext.scriptEvaluator.evaluateJavascriptInDefaultContentWorld(jsFunctionCall,
88+
frameContext.frameInfo) { (result, error) in
8989
if let error = error {
9090
self.logger.log("JavaScript evaluation error",
9191
level: .warning,
@@ -102,8 +102,8 @@ final class PasswordGeneratorMiddleware {
102102
guard let escapedPassword = escapeString(string: password) else { return }
103103

104104
let jsFunctionCall = "window.__firefox__.logins.fillGeneratedPassword(\(escapedPassword))"
105-
frameContext.scriptEvaluator?.evaluateJavascriptInDefaultContentWorld(jsFunctionCall,
106-
frameContext.frameInfo) { (result, error) in
105+
frameContext.scriptEvaluator.evaluateJavascriptInDefaultContentWorld(jsFunctionCall,
106+
frameContext.frameInfo) { (result, error) in
107107
if error != nil {
108108
self.logger.log("Error filling in password info",
109109
level: .warning,

firefox-ios/Client/Frontend/TabContentsScripts/LoginsHelper.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ class LoginsHelper: @unchecked Sendable, TabContentScript, FeatureFlaggable {
148148
if type == "clearAccessoryView" {
149149
tab?.webView?.accessoryView.reloadViewFor(.standard)
150150
}
151+
let scriptEvaluator = WebKitPasswordGeneratorScriptEvaluator(webView: message.frameInfo.webView)
152+
let frameContext = PasswordGeneratorFrameContext(origin: message.frameInfo.webView?.url?.origin,
153+
host: message.frameInfo.securityOrigin.host,
154+
scriptEvaluator: scriptEvaluator,
155+
frameInfo: message.frameInfo)
151156

152157
if type == "generatePassword",
153158
let tab = self.tab,
@@ -156,7 +161,7 @@ class LoginsHelper: @unchecked Sendable, TabContentScript, FeatureFlaggable {
156161
let userDefaults = UserDefaults.standard
157162
let showPasswordGeneratorClosure = {
158163
let newAction = GeneralBrowserAction(
159-
frame: message.frameInfo,
164+
frameContext: frameContext,
160165
windowUUID: tab.windowUUID,
161166
actionType: GeneralBrowserActionType.showPasswordGenerator)
162167

@@ -390,7 +395,7 @@ class LoginsHelper: @unchecked Sendable, TabContentScript, FeatureFlaggable {
390395
if let windowUUID = self.tab?.windowUUID {
391396
let action = PasswordGeneratorAction(windowUUID: windowUUID,
392397
actionType: PasswordGeneratorActionType.clearGeneratedPasswordForSite,
393-
origin: origin)
398+
loginEntryOrigin: origin)
394399
store.dispatch(action)
395400
}
396401
}

firefox-ios/firefox-ios-tests/Tests/ClientTests/BrowserViewControllerStateTests.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,15 @@ final class BrowserViewControllerStateTests: XCTestCase, StoreTestUtility {
6262
func testShowPasswordGeneratorAction() {
6363
let initialState = createSubject()
6464
let reducer = browserViewControllerReducer()
65-
let URL = URL(string: "https://foo.com")!
66-
let webView = MockWKWebView(URL)
67-
let frame = MockWKFrameInfo(webView: webView, frameURL: URL, isMainFrame: true)
65+
let mockEvaluator = MockPasswordGeneratorScriptEvaluator()
66+
let frameContext = PasswordGeneratorFrameContext(origin: "https://foo.com",
67+
host: "foo.com",
68+
scriptEvaluator: mockEvaluator,
69+
frameInfo: nil)
6870

6971
XCTAssertNil(initialState.displayView)
7072

71-
let action = GeneralBrowserAction(frame: frame,
73+
let action = GeneralBrowserAction(frameContext: frameContext,
7274
windowUUID: .XCTestDefaultUUID,
7375
actionType: GeneralBrowserActionType.showPasswordGenerator)
7476
let newState = reducer(initialState, action)
@@ -77,7 +79,7 @@ final class BrowserViewControllerStateTests: XCTestCase, StoreTestUtility {
7779
BrowserViewControllerState.DisplayType.passwordGenerator
7880

7981
XCTAssertEqual(displayView, desiredDisplayView)
80-
XCTAssertNotNil(newState.frame)
82+
XCTAssertNotNil(newState.frameContext)
8183
}
8284

8385
func testReloadWebsiteAction() {

0 commit comments

Comments
 (0)