Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handoff support #3636

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions DuckDuckGo/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
Task { @MainActor in
await subscriptionCookieManager.refreshSubscriptionCookie()
}

WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController.browserTabViewController.becomeCurrentActivity()
}

private func initializeSync() {
Expand Down Expand Up @@ -627,6 +629,17 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
urlEventHandler.handleFiles(files)
}

func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([any NSUserActivityRestoring]) -> Void) -> Bool {
guard userActivity.activityType == "com.duckduckgo.mobile.ios.web-browsing",
let mainWindowController = WindowControllersManager.shared.lastKeyMainWindowController?.mainViewController else {
return false
}

restorationHandler([mainWindowController.browserTabViewController])

return true
}

// MARK: - PixelKit

static func configurePixelKit() {
Expand Down
4 changes: 4 additions & 0 deletions DuckDuckGo/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@
<false/>
<key>NSSupportsSuddenTermination</key>
<false/>
<key>NSUserActivityTypes</key>
<array>
<string>com.duckduckgo.mobile.ios.web-browsing</string>
</array>
<key>SUBSCRIPTION_APP_GROUP</key>
<string>$(SUBSCRIPTION_APP_GROUP)</string>
<key>SUEnableAutomaticChecks</key>
Expand Down
55 changes: 55 additions & 0 deletions DuckDuckGo/Tab/View/BrowserTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,8 @@ final class BrowserTabViewController: NSViewController {

self.adjustFirstResponder(force: true)
removeExistingDialog()

self.updateCurrentActivity(url: selectedTabViewModel?.tab.content.urlForWebView)
}
.store(in: &cancellables)
}
Expand Down Expand Up @@ -615,6 +617,13 @@ final class BrowserTabViewController: NSViewController {
tabViewModel?.tab.webViewDidFinishNavigationPublisher.sink { [weak self] in
self?.updateStateAndPresentContextualOnboarding()
}.store(in: &tabViewModelCancellables)

tabViewModel?.tab.webView.publisher(for: \.url)
.dropFirst()
.receive(on: DispatchQueue.main)
.sink { [weak self] url in
self?.updateCurrentActivity(url: url)
}.store(in: &tabViewModelCancellables)
}

private func subscribeToUserDialogs(of tabViewModel: TabViewModel?) {
Expand Down Expand Up @@ -1095,6 +1104,7 @@ extension BrowserTabViewController: TabDelegate {
keyWindowSelectedTabCancellable = nil
subscribeToPinnedTabs()
hideWebViewSnapshotIfNeeded()
becomeCurrentActivity()
}

func windowDidResignKey() {
Expand Down Expand Up @@ -1510,3 +1520,48 @@ private extension NSViewController {
}

}

extension BrowserTabViewController {
override func restoreUserActivityState(_ userActivity: NSUserActivity) {
guard supportsHandoff(), userActivity.activityType == "com.duckduckgo.mobile.ios.web-browsing", let url = userActivity.webpageURL else {
return
}
openNewTab(with: .url(url, credential: nil, source: .appOpenUrl))
}

func becomeCurrentActivity() {
guard supportsHandoff() else { return }

if userActivity?.webpageURL == nil {
userActivity?.invalidate()
userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
userActivity?.webpageURL = nil
}

userActivity?.becomeCurrent()
}

private func updateCurrentActivity(url: URL?) {
guard supportsHandoff() else { return }

let newURL: URL? = {
guard let url, let scheme = url.scheme, ["http", "https"].contains(scheme) else { return nil }
return url
}()
guard newURL != userActivity?.webpageURL else { return }

userActivity?.invalidate()
if newURL != nil {
userActivity = NSUserActivity(activityType: "com.duckduckgo.mobile.ios.web-browsing")
} else {
userActivity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb)
}
userActivity?.webpageURL = newURL

userActivity?.becomeCurrent()
}

private func supportsHandoff() -> Bool {
!tabCollectionViewModel.isBurner && featureFlagger.isFeatureOn(.handoff)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public enum FeatureFlag: String, CaseIterable {

case isPrivacyProLaunchedROW
case isPrivacyProLaunchedROWOverride

case handoff
}

extension FeatureFlag: FeatureFlagDescribing {
Expand All @@ -59,6 +61,8 @@ extension FeatureFlag: FeatureFlagDescribing {
return true
case .maliciousSiteProtectionErrorPage:
return true
case .handoff:
return true
case .debugMenu,
.sslCertificatesBypass,
.appendAtbToSerpQueries,
Expand Down Expand Up @@ -101,6 +105,8 @@ extension FeatureFlag: FeatureFlagDescribing {
return .remoteReleasable(.subfeature(PrivacyProSubfeature.isLaunchedROW))
case .isPrivacyProLaunchedROWOverride:
return .remoteReleasable(.subfeature(PrivacyProSubfeature.isLaunchedROWOverride))
case .handoff:
return .internalOnly()
}
}
}
Expand Down
Loading