From 93953f70eba45ce33995194add39d4e1a5c4d554 Mon Sep 17 00:00:00 2001 From: Mihai Bolojan <74779930+bmihai23@users.noreply.github.com> Date: Thu, 14 Nov 2024 21:23:42 +0200 Subject: [PATCH] Refactor FXIOS-10467 - Remove force_cast violations from Frontend Features & Content View (#23044) --- .../Home/Pocket/PocketViewModel.swift | 23 +++++++++++++++---- .../Library/Downloads/DownloadsPanel.swift | 9 ++++++-- .../Frontend/Library/Reader/ReaderPanel.swift | 14 ++++++++--- .../TabContentsScripts/FindInPageHelper.swift | 12 ++++++++-- .../LocalRequestHelper.swift | 14 ++++++++++- .../CertificatesViewController.swift | 14 ++++++++--- 6 files changed, 70 insertions(+), 16 deletions(-) diff --git a/firefox-ios/Client/Frontend/Home/Pocket/PocketViewModel.swift b/firefox-ios/Client/Frontend/Home/Pocket/PocketViewModel.swift index a8df1893613e..bb3f7d781184 100644 --- a/firefox-ios/Client/Frontend/Home/Pocket/PocketViewModel.swift +++ b/firefox-ios/Client/Frontend/Home/Pocket/PocketViewModel.swift @@ -30,17 +30,20 @@ class PocketViewModel { private var pocketStoriesViewModels = [PocketStandardCellViewModel]() private var wallpaperManager: WallpaperManager private var prefs: Prefs + private let logger: Logger init(pocketDataAdaptor: PocketDataAdaptor, isZeroSearch: Bool = false, theme: Theme, prefs: Prefs, - wallpaperManager: WallpaperManager) { + wallpaperManager: WallpaperManager, + logger: Logger = DefaultLogger.shared) { self.dataAdaptor = pocketDataAdaptor self.isZeroSearch = isZeroSearch self.theme = theme self.prefs = prefs self.wallpaperManager = wallpaperManager + self.logger = logger } // The dimension of a cell @@ -208,15 +211,25 @@ extension PocketViewModel: HomepageSectionHandler { recordSectionHasShown() if isStoryCell(index: indexPath.row) { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LegacyPocketStandardCell.cellIdentifier, - for: indexPath) as! LegacyPocketStandardCell + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: LegacyPocketStandardCell.cellIdentifier, + for: indexPath) as? LegacyPocketStandardCell else { + logger.log("Failed to dequeue LegacyPocketStandardCell at indexPath: \(indexPath)", + level: .fatal, + category: .legacyHomepage) + return UICollectionViewCell() + } let viewModel = pocketStoriesViewModels[indexPath.row] viewModel.tag = indexPath.row cell.configure(viewModel: viewModel, theme: theme) return cell } else { - let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PocketDiscoverCell.cellIdentifier, - for: indexPath) as! PocketDiscoverCell + guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PocketDiscoverCell.cellIdentifier, + for: indexPath) as? PocketDiscoverCell else { + logger.log("Failed to dequeue PocketDiscoverCell at indexPath: \(indexPath)", + level: .fatal, + category: .legacyHomepage) + return UICollectionViewCell() + } cell.configure(text: .FirefoxHomepage.Pocket.DiscoverMore, theme: theme) return cell } diff --git a/firefox-ios/Client/Frontend/Library/Downloads/DownloadsPanel.swift b/firefox-ios/Client/Frontend/Library/Downloads/DownloadsPanel.swift index ef2e1aaf9c06..0241bb0f6623 100644 --- a/firefox-ios/Client/Frontend/Library/Downloads/DownloadsPanel.swift +++ b/firefox-ios/Client/Frontend/Library/Downloads/DownloadsPanel.swift @@ -283,8 +283,13 @@ class DownloadsPanel: UIViewController, // MARK: - TableView Delegate / DataSource func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell(withIdentifier: TwoLineImageOverlayCell.cellIdentifier, - for: indexPath) as! TwoLineImageOverlayCell + guard let cell = tableView.dequeueReusableCell(withIdentifier: TwoLineImageOverlayCell.cellIdentifier, + for: indexPath) as? TwoLineImageOverlayCell else { + logger.log("Failed to dequeue TwoLineImageOverlayCell at indexPath: \(indexPath)", + level: .fatal, + category: .library) + return UITableViewCell() + } return configureDownloadedFile(cell, for: indexPath) } diff --git a/firefox-ios/Client/Frontend/Library/Reader/ReaderPanel.swift b/firefox-ios/Client/Frontend/Library/Reader/ReaderPanel.swift index 3683f6816854..f20135ab0f9f 100644 --- a/firefox-ios/Client/Frontend/Library/Reader/ReaderPanel.swift +++ b/firefox-ios/Client/Frontend/Library/Reader/ReaderPanel.swift @@ -177,6 +177,7 @@ class ReadingListPanel: UITableViewController, var notificationCenter: NotificationProtocol let windowUUID: WindowUUID var currentWindowUUID: UUID? { windowUUID } + private let logger: Logger private lazy var longPressRecognizer: UILongPressGestureRecognizer = { return UILongPressGestureRecognizer(target: self, action: #selector(longPress)) @@ -188,12 +189,14 @@ class ReadingListPanel: UITableViewController, profile: Profile, windowUUID: WindowUUID, themeManager: ThemeManager = AppContainer.shared.resolve(), - notificationCenter: NotificationProtocol = NotificationCenter.default + notificationCenter: NotificationProtocol = NotificationCenter.default, + logger: Logger = DefaultLogger.shared ) { self.profile = profile self.windowUUID = windowUUID self.themeManager = themeManager self.notificationCenter = notificationCenter + self.logger = logger self.state = .readingList super.init(nibName: nil, bundle: nil) @@ -346,10 +349,15 @@ class ReadingListPanel: UITableViewController, } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let cell = tableView.dequeueReusableCell( + guard let cell = tableView.dequeueReusableCell( withIdentifier: "ReadingListTableViewCell", for: indexPath - ) as! ReadingListTableViewCell + ) as? ReadingListTableViewCell else { + logger.log("Failed to dequeue ReadingListTableViewCell at indexPath: \(indexPath)", + level: .fatal, + category: .library) + return UITableViewCell() + } if let record = records?[indexPath.row] { cell.title = record.title cell.url = URL(string: record.url, invalidCharacters: false)! diff --git a/firefox-ios/Client/Frontend/TabContentsScripts/FindInPageHelper.swift b/firefox-ios/Client/Frontend/TabContentsScripts/FindInPageHelper.swift index 9e07094763d7..3bf82f2fe6be 100644 --- a/firefox-ios/Client/Frontend/TabContentsScripts/FindInPageHelper.swift +++ b/firefox-ios/Client/Frontend/TabContentsScripts/FindInPageHelper.swift @@ -2,6 +2,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/ +import Common import Foundation import Shared import WebKit @@ -14,13 +15,15 @@ protocol FindInPageHelperDelegate: AnyObject { class FindInPageHelper: TabContentScript { weak var delegate: FindInPageHelperDelegate? fileprivate weak var tab: Tab? + private let logger: Logger class func name() -> String { return "FindInPage" } - required init(tab: Tab) { + required init(tab: Tab, logger: Logger = DefaultLogger.shared) { self.tab = tab + self.logger = logger } func scriptMessageHandlerNames() -> [String]? { @@ -31,7 +34,12 @@ class FindInPageHelper: TabContentScript { _ userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage ) { - let data = message.body as! [String: Int] + guard let data = message.body as? [String: Int] else { + logger.log("Invalid data message body in FindInPageHelper: \(message.body)", + level: .fatal, + category: .library) + return + } if let currentResult = data["currentResult"] { delegate?.findInPageHelper(self, didUpdateCurrentResult: currentResult) diff --git a/firefox-ios/Client/Frontend/TabContentsScripts/LocalRequestHelper.swift b/firefox-ios/Client/Frontend/TabContentsScripts/LocalRequestHelper.swift index 9c0b798e2e24..256209d5bf33 100644 --- a/firefox-ios/Client/Frontend/TabContentsScripts/LocalRequestHelper.swift +++ b/firefox-ios/Client/Frontend/TabContentsScripts/LocalRequestHelper.swift @@ -2,11 +2,18 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/ +import Common import Foundation import WebKit import Shared class LocalRequestHelper: TabContentScript { + private let logger: Logger + + required init(logger: Logger = DefaultLogger.shared) { + self.logger = logger + } + func scriptMessageHandlerNames() -> [String]? { return ["localRequestHelper"] } @@ -19,7 +26,12 @@ class LocalRequestHelper: TabContentScript { let internalUrl = InternalURL(requestUrl) else { return } - let params = message.body as! [String: String] + guard let params = message.body as? [String: String] else { + logger.log("Invalid data message body in LocalRequestHelper: \(message.body)", + level: .fatal, + category: .library) + return + } guard let token = params["appIdToken"], token == UserScriptManager.appIdToken diff --git a/firefox-ios/Client/Frontend/TrackingProtection/CertificatesViewController.swift b/firefox-ios/Client/Frontend/TrackingProtection/CertificatesViewController.swift index d4f8556327a5..b283f739134f 100644 --- a/firefox-ios/Client/Frontend/TrackingProtection/CertificatesViewController.swift +++ b/firefox-ios/Client/Frontend/TrackingProtection/CertificatesViewController.swift @@ -66,17 +66,20 @@ class CertificatesViewController: UIViewController, var themeObserver: NSObjectProtocol? let windowUUID: WindowUUID var currentWindowUUID: UUID? { return windowUUID } + private let logger: Logger // MARK: - View Lifecycle init(with viewModel: CertificatesModel, windowUUID: WindowUUID, and notificationCenter: NotificationProtocol = NotificationCenter.default, - themeManager: ThemeManager = AppContainer.shared.resolve()) { + themeManager: ThemeManager = AppContainer.shared.resolve(), + logger: Logger = DefaultLogger.shared) { self.model = viewModel self.windowUUID = windowUUID self.notificationCenter = notificationCenter self.themeManager = themeManager + self.logger = logger super.init(nibName: nil, bundle: nil) } @@ -182,8 +185,13 @@ class CertificatesViewController: UIViewController, } func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { - let headerView = tableView.dequeueReusableHeaderFooterView( - withIdentifier: CertificatesHeaderView.cellIdentifier) as! CertificatesHeaderView + guard let headerView = tableView.dequeueReusableHeaderFooterView( + withIdentifier: CertificatesHeaderView.cellIdentifier) as? CertificatesHeaderView else { + logger.log("Failed to dequeue CertificatesHeaderView with identifier \(CertificatesHeaderView.cellIdentifier)", + level: .fatal, + category: .certificate) + return UIView() + } var items: [CertificatesHeaderItem] = [] for (index, certificate) in model.certificates.enumerated() { let certificateValues = certificate.subject.description.getDictionary()