From 12c1e2792597121b6410f923b9cf9c7c6869b74e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jacek=20=C5=81yp?= Date: Wed, 6 Nov 2024 21:57:05 +0100 Subject: [PATCH] Adapt to new API --- DuckDuckGo.xcodeproj/project.pbxproj | 14 ++-- .../xcshareddata/swiftpm/Package.resolved | 9 --- .../Utilities/UserDefaultsWrapper.swift | 4 +- .../View/NavigationBarViewController.swift | 9 ++- .../PageRefreshMonitor/PageRefreshEvent.swift | 67 ------------------- .../PageRefreshMonitor.swift | 28 ++++++++ .../PageRefreshMonitor/PageRefreshStore.swift | 10 +-- DuckDuckGo/Statistics/GeneralPixel.swift | 2 + DuckDuckGo/Tab/Model/Tab.swift | 2 +- 9 files changed, 46 insertions(+), 99 deletions(-) delete mode 100644 DuckDuckGo/PageRefreshMonitor/PageRefreshEvent.swift create mode 100644 DuckDuckGo/PageRefreshMonitor/PageRefreshMonitor.swift diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index 001b064c03..bebff49cfd 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -2913,12 +2913,12 @@ C1F142DC2CB93AED003DA518 /* FreemiumDBPPromotionViewCoordinatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C1F142DA2CB93AED003DA518 /* FreemiumDBPPromotionViewCoordinatorTests.swift */; }; CB24F70C29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB24F70B29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift */; }; CB24F70D29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB24F70B29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift */; }; + CB63DECB2CDC0BBE0097986A /* PageRefreshMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB63DECA2CDC0BB80097986A /* PageRefreshMonitor.swift */; }; + CB63DECC2CDC0BBE0097986A /* PageRefreshMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB63DECA2CDC0BB80097986A /* PageRefreshMonitor.swift */; }; CB6BCDF927C6BEFF00CC76DC /* PrivacyFeatures.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB6BCDF827C6BEFF00CC76DC /* PrivacyFeatures.swift */; }; CBC83E3629B63D380008E19C /* Configuration in Frameworks */ = {isa = PBXBuildFile; productRef = CBC83E3529B63D380008E19C /* Configuration */; }; CBDD5DE329A67F2700832877 /* MockConfigurationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBDD5DE229A67F2700832877 /* MockConfigurationStore.swift */; }; CBDD5DE429A6800300832877 /* MockConfigurationStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBDD5DE229A67F2700832877 /* MockConfigurationStore.swift */; }; - CBECDB7D2CD98274005B8B87 /* PageRefreshEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBECDB7C2CD9826E005B8B87 /* PageRefreshEvent.swift */; }; - CBECDB7E2CD98274005B8B87 /* PageRefreshEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBECDB7C2CD9826E005B8B87 /* PageRefreshEvent.swift */; }; CBECDB802CD9829A005B8B87 /* PageRefreshStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBECDB7F2CD98296005B8B87 /* PageRefreshStore.swift */; }; CBECDB812CD9829A005B8B87 /* PageRefreshStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBECDB7F2CD98296005B8B87 /* PageRefreshStore.swift */; }; CBECDB842CDA813C005B8B87 /* BrokenSitePromptLimiterStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBECDB832CDA8137005B8B87 /* BrokenSitePromptLimiterStore.swift */; }; @@ -4766,9 +4766,10 @@ C1E961EE2B87AA29001760E1 /* AutofillActionBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutofillActionBuilder.swift; sourceTree = ""; }; C1F142DA2CB93AED003DA518 /* FreemiumDBPPromotionViewCoordinatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FreemiumDBPPromotionViewCoordinatorTests.swift; sourceTree = ""; }; CB24F70B29A3D9CB006DCC58 /* AppConfigurationURLProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfigurationURLProvider.swift; sourceTree = ""; }; + CB63DEC92CDBEF0A0097986A /* BrowserServicesKit */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = BrowserServicesKit; path = ../BrowserServicesKit; sourceTree = SOURCE_ROOT; }; + CB63DECA2CDC0BB80097986A /* PageRefreshMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageRefreshMonitor.swift; sourceTree = ""; }; CB6BCDF827C6BEFF00CC76DC /* PrivacyFeatures.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrivacyFeatures.swift; sourceTree = ""; }; CBDD5DE229A67F2700832877 /* MockConfigurationStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockConfigurationStore.swift; sourceTree = ""; }; - CBECDB7C2CD9826E005B8B87 /* PageRefreshEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageRefreshEvent.swift; sourceTree = ""; }; CBECDB7F2CD98296005B8B87 /* PageRefreshStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PageRefreshStore.swift; sourceTree = ""; }; CBECDB832CDA8137005B8B87 /* BrokenSitePromptLimiterStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrokenSitePromptLimiterStore.swift; sourceTree = ""; }; CBECDB862CDACE29005B8B87 /* BrokenSitePromptLimiter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrokenSitePromptLimiter.swift; sourceTree = ""; }; @@ -7611,6 +7612,7 @@ AA585D75248FD31100E9A3E2 = { isa = PBXGroup; children = ( + CB63DEC92CDBEF0A0097986A /* BrowserServicesKit */, 378B5886295CF2A4002C0CC0 /* Configuration */, 378E279C2970217400FCADA2 /* LocalPackages */, 7BB108552A43375D000AB95F /* LocalThirdParty */, @@ -9457,8 +9459,8 @@ CBECDB7B2CD9824B005B8B87 /* PageRefreshMonitor */ = { isa = PBXGroup; children = ( + CB63DECA2CDC0BB80097986A /* PageRefreshMonitor.swift */, CBECDB7F2CD98296005B8B87 /* PageRefreshStore.swift */, - CBECDB7C2CD9826E005B8B87 /* PageRefreshEvent.swift */, ); path = PageRefreshMonitor; sourceTree = ""; @@ -11202,7 +11204,6 @@ 3706FB32293F65D500E42796 /* FirefoxBookmarksReader.swift in Sources */, EE098E782C8EDE2C009EBA7F /* AutofillCredentialsImportManager.swift in Sources */, 3199AF7E2C80734A003AEBDC /* TabModal.swift in Sources */, - CBECDB7D2CD98274005B8B87 /* PageRefreshEvent.swift in Sources */, 1D39E57B2C2C0F3700757339 /* ReleaseNotesUserScript.swift in Sources */, 3706FB33293F65D500E42796 /* DeviceIdleStateDetector.swift in Sources */, 1DB67F2A2B6FEB17003DF243 /* WebViewSnapshotRenderer.swift in Sources */, @@ -11767,6 +11768,7 @@ B69A14FB2B4D705D00B9417D /* BookmarkFolderPicker.swift in Sources */, 3706FC6C293F65D500E42796 /* BookmarkViewModel.swift in Sources */, 3706FC6D293F65D500E42796 /* DaxSpeech.swift in Sources */, + CB63DECC2CDC0BBE0097986A /* PageRefreshMonitor.swift in Sources */, 3706FC6E293F65D500E42796 /* DuckURLSchemeHandler.swift in Sources */, 37445F9A2A1566420029F789 /* SyncDataProviders.swift in Sources */, 3706FC6F293F65D500E42796 /* FirePopoverViewModel.swift in Sources */, @@ -12598,7 +12600,6 @@ 841BE93E2C6F236000E9C2B5 /* BookmarkDragDropManager.swift in Sources */, 1D39E5772C2BFD5700757339 /* ReleaseNotesTabExtension.swift in Sources */, 987799F12999993C005D8EB6 /* LegacyBookmarkStore.swift in Sources */, - CBECDB7E2CD98274005B8B87 /* PageRefreshEvent.swift in Sources */, 1D220BF82B86192200F8BBC6 /* PreferencesEmailProtectionView.swift in Sources */, 4B8AC93526B3B2FD00879451 /* NSAlert+DataImport.swift in Sources */, AA7412BD24D2BEEE00D22FE0 /* MainWindow.swift in Sources */, @@ -13226,6 +13227,7 @@ 4B37EE5F2B4CFC3C00A89A61 /* HomePageRemoteMessagingStorage.swift in Sources */, 31F28C5128C8EEC500119F70 /* YoutubeOverlayUserScript.swift in Sources */, B6ABD0CA2BC03F610000EB69 /* SecurityScopedFileURLController.swift in Sources */, + CB63DECB2CDC0BBE0097986A /* PageRefreshMonitor.swift in Sources */, B6040856274B830F00680351 /* DictionaryExtension.swift in Sources */, 3199AF772C80734A003AEBDC /* DuckPlayerOnboardingViewController.swift in Sources */, B684592725C93C0500DC17B6 /* Publishers.NestedObjectChanges.swift in Sources */, diff --git a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index f2d41b117c..0232322f59 100644 --- a/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DuckDuckGo.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -27,15 +27,6 @@ "version" : "3.0.0" } }, - { - "identity" : "browserserviceskit", - "kind" : "remoteSourceControl", - "location" : "https://github.com/duckduckgo/BrowserServicesKit", - "state" : { - "branch" : "jacek/behavioral-toast-udpates", - "revision" : "fb48d9089083e72e44a57c53e5b5ee2b3d113cbb" - } - }, { "identity" : "content-scope-scripts", "kind" : "remoteSourceControl", diff --git a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift index 093f3b2263..43572f1399 100644 --- a/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift +++ b/DuckDuckGo/Common/Utilities/UserDefaultsWrapper.swift @@ -227,9 +227,7 @@ public struct UserDefaultsWrapper { // PageRefreshMonitor - case didRefreshTimestamp = "pageRefreshMonitor.did-refresh-timestamp" - case didDoubleRefreshTimestamp = "pageRefreshMonitor.did-double-refresh-timestamp" - case didRefreshCounter = "pageRefreshMonitor.did-refresh-counter" + case refreshTimestamps = "pageRefreshMonitor.refresh-timestamps" // BrokenSitePrompt diff --git a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift index 13a294b02c..bb32f3e5e8 100644 --- a/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift +++ b/DuckDuckGo/NavigationBar/View/NavigationBarViewController.swift @@ -431,7 +431,7 @@ final class NavigationBarViewController: NSViewController { NotificationCenter.default.addObserver(self, selector: #selector(attemptToShowBrokenSitePrompt(_:)), - name: .pageRefreshDidMatchBrokenSiteCriteria, + name: .pageRefreshMonitorDidDetectRefreshPattern, object: nil) UserDefaults.netP @@ -551,18 +551,17 @@ final class NavigationBarViewController: NSViewController { @objc private func attemptToShowBrokenSitePrompt(_ sender: Notification) { guard brokenSitePromptLimiter.shouldShowToast(), - let event = sender.userInfo?[PageRefreshEvent.key] as? PageRefreshEvent, let url = tabCollectionViewModel.selectedTabViewModel?.tab.url, !url.isDuckDuckGo, isOnboardingFinished else { return } - showBrokenSitePrompt(after: event) + showBrokenSitePrompt() } private var isOnboardingFinished: Bool { OnboardingActionsManager.isOnboardingFinished && Application.appDelegate.onboardingStateMachine.state == .onboardingCompleted } - private func showBrokenSitePrompt(after event: PageRefreshEvent) { + private func showBrokenSitePrompt() { guard view.window?.isKeyWindow == true, let privacyButton = addressBarViewController?.addressBarButtonsViewController?.privacyEntryPointButton else { return } brokenSitePromptLimiter.didShowToast() @@ -571,7 +570,7 @@ final class NavigationBarViewController: NSViewController { buttonText: UserText.BrokenSitePrompt.buttonTitle, buttonAction: { self.brokenSitePromptLimiter.didOpenReport() - self.addressBarViewController?.addressBarButtonsViewController?.openPrivacyDashboardPopover(entryPoint: .prompt(event.rawValue)) + self.addressBarViewController?.addressBarButtonsViewController?.openPrivacyDashboardPopover(entryPoint: .prompt) PixelKit.fire(GeneralPixel.siteNotWorkingWebsiteIsBroken) }, shouldShowCloseButton: true, diff --git a/DuckDuckGo/PageRefreshMonitor/PageRefreshEvent.swift b/DuckDuckGo/PageRefreshMonitor/PageRefreshEvent.swift deleted file mode 100644 index 97b9a901b1..0000000000 --- a/DuckDuckGo/PageRefreshMonitor/PageRefreshEvent.swift +++ /dev/null @@ -1,67 +0,0 @@ -// -// AppPageRefreshMonitor.swift -// -// Copyright © 2024 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 Common -import PageRefreshMonitor -import PixelKit - -enum PageRefreshPixel: PixelKitEventV2 { - - case twiceWithin12Seconds - case threeTimesWithin20Seconds - - var name: String { - switch self { - case .twiceWithin12Seconds: - return "m_mac_reload-twice-within-12-seconds" - case .threeTimesWithin20Seconds: - return "m_mac_reload-three-times-within-20-seconds" - } - } - - var error: (any Error)? { - return nil - } - - var parameters: [String: String]? { - return nil - } -} - -final class PageRefreshEventPixelMapping: EventMapping { - - public init() { - super.init { event, _, _, _ in - let pixel: PageRefreshPixel = { - switch event { - case .twiceWithin12Seconds: - return .twiceWithin12Seconds - case .threeTimesWithin20Seconds: - return .threeTimesWithin20Seconds - } - }() - - PixelKit.fire(pixel) - } - } - - override init(mapping: @escaping EventMapping.Mapping) { - fatalError("Use init()") - } - -} diff --git a/DuckDuckGo/PageRefreshMonitor/PageRefreshMonitor.swift b/DuckDuckGo/PageRefreshMonitor/PageRefreshMonitor.swift new file mode 100644 index 0000000000..8415b47001 --- /dev/null +++ b/DuckDuckGo/PageRefreshMonitor/PageRefreshMonitor.swift @@ -0,0 +1,28 @@ +// +// AppPageRefreshMonitor.swift +// +// Copyright © 2024 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 PageRefreshMonitor +import PixelKit + +extension PageRefreshMonitor { + + static let onDidDetectRefreshPattern: () -> Void = { + PixelKit.fire(GeneralPixel.pageRefreshThreeTimesWithin20Seconds) + } + +} diff --git a/DuckDuckGo/PageRefreshMonitor/PageRefreshStore.swift b/DuckDuckGo/PageRefreshMonitor/PageRefreshStore.swift index 4e94d9bc63..bc648f5605 100644 --- a/DuckDuckGo/PageRefreshMonitor/PageRefreshStore.swift +++ b/DuckDuckGo/PageRefreshMonitor/PageRefreshStore.swift @@ -20,13 +20,7 @@ import PageRefreshMonitor final class PageRefreshStore: PageRefreshStoring { - @UserDefaultsWrapper(key: .didRefreshTimestamp, defaultValue: .distantPast) - var didRefreshTimestamp: Date? - - @UserDefaultsWrapper(key: .didDoubleRefreshTimestamp, defaultValue: .distantPast) - var didDoubleRefreshTimestamp: Date? - - @UserDefaultsWrapper(key: .didRefreshCounter, defaultValue: 0) - var didRefreshCounter: Int + @UserDefaultsWrapper(key: .refreshTimestamps, defaultValue: []) + var refreshTimestamps: [Date] } diff --git a/DuckDuckGo/Statistics/GeneralPixel.swift b/DuckDuckGo/Statistics/GeneralPixel.swift index 22d46f9fb5..b001e58f58 100644 --- a/DuckDuckGo/Statistics/GeneralPixel.swift +++ b/DuckDuckGo/Statistics/GeneralPixel.swift @@ -440,6 +440,7 @@ enum GeneralPixel: PixelKitEventV2 { // Broken site prompt + case pageRefreshThreeTimesWithin20Seconds case siteNotWorkingShown case siteNotWorkingWebsiteIsBroken @@ -1079,6 +1080,7 @@ enum GeneralPixel: PixelKitEventV2 { case .errorPageShownWebkitTermination: return "m_mac_errorpageshown_webkittermination" // Broken site prompt + case .pageRefreshThreeTimesWithin20Seconds: return "m_mac_reload-three-times-within-20-seconds" case .siteNotWorkingShown: return "m_mac_site-not-working_shown" case .siteNotWorkingWebsiteIsBroken: return "m_mac_site-not-working_website-is-broken" } diff --git a/DuckDuckGo/Tab/Model/Tab.swift b/DuckDuckGo/Tab/Model/Tab.swift index 437394cc2a..0a8ac5bdf6 100644 --- a/DuckDuckGo/Tab/Model/Tab.swift +++ b/DuckDuckGo/Tab/Model/Tab.swift @@ -122,7 +122,7 @@ protocol NewWindowPolicyDecisionMaker { phishingState: PhishingTabStateManaging = PhishingTabStateManager(), tabsPreferences: TabsPreferences = TabsPreferences.shared, onboardingPixelReporter: OnboardingAddressBarReporting = OnboardingPixelReporter(), - pageRefreshMonitor: PageRefreshMonitoring = PageRefreshMonitor(eventMapping: PageRefreshEventPixelMapping(), + pageRefreshMonitor: PageRefreshMonitoring = PageRefreshMonitor(onDidDetectRefreshPattern: PageRefreshMonitor.onDidDetectRefreshPattern, store: PageRefreshStore()) ) {