-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task/Issue URL: https://app.asana.com/0/1204167627774280/1209119202522826/f **Description**: Add handoff support using the C-S-S communication layer
- Loading branch information
Showing
10 changed files
with
489 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// AIChatViewControllerManager.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2025 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 UserScript | ||
import AIChat | ||
import Foundation | ||
import BrowserServicesKit | ||
import WebKit | ||
import Core | ||
|
||
protocol AIChatViewControllerManagerDelegate: AnyObject { | ||
func aiChatViewControllerManager(_ manager: AIChatViewControllerManager, didRequestToLoad url: URL) | ||
} | ||
|
||
final class AIChatViewControllerManager { | ||
weak var delegate: AIChatViewControllerManagerDelegate? | ||
private var aiChatUserScript: AIChatUserScript? | ||
private var payloadHandler = AIChatPayloadHandler() | ||
private let privacyConfigurationManager: PrivacyConfigurationManaging | ||
private let internalUserDecider: InternalUserDecider | ||
|
||
init(privacyConfigurationManager: PrivacyConfigurationManaging = ContentBlocking.shared.privacyConfigurationManager, | ||
internalUserDecider: InternalUserDecider = AppDependencyProvider.shared.internalUserDecider) { | ||
self.privacyConfigurationManager = privacyConfigurationManager | ||
self.internalUserDecider = internalUserDecider | ||
} | ||
|
||
@MainActor | ||
lazy var aiChatViewController: AIChatViewController = { | ||
let settings = AIChatSettings(privacyConfigurationManager: privacyConfigurationManager, | ||
internalUserDecider: internalUserDecider) | ||
|
||
let webviewConfiguration = WKWebViewConfiguration.persistent() | ||
let userContentController = UserContentController() | ||
userContentController.delegate = self | ||
|
||
webviewConfiguration.userContentController = userContentController | ||
let aiChatViewController = AIChatViewController(settings: settings, | ||
webViewConfiguration: webviewConfiguration) | ||
aiChatViewController.delegate = self | ||
return aiChatViewController | ||
}() | ||
|
||
@MainActor | ||
func openAIChat(_ query: URLQueryItem? = nil, payload: Any? = nil, on viewController: UIViewController) { | ||
let roundedPageSheet = RoundedPageSheetContainerViewController( | ||
contentViewController: aiChatViewController, | ||
allowedOrientation: .portrait) | ||
|
||
if let query = query { | ||
aiChatViewController.loadQuery(query) | ||
} | ||
|
||
// Force a reload to trigger the user script getUserValues | ||
if let payload = payload as? AIChatPayload { | ||
payloadHandler.setPayload(payload) | ||
aiChatViewController.reload() | ||
} | ||
viewController.present(roundedPageSheet, animated: true, completion: nil) | ||
} | ||
} | ||
|
||
extension AIChatViewControllerManager: UserContentControllerDelegate { | ||
@MainActor | ||
func userContentController(_ userContentController: UserContentController, | ||
didInstallContentRuleLists contentRuleLists: [String: WKContentRuleList], | ||
userScripts: UserScriptsProvider, | ||
updateEvent: ContentBlockerRulesManager.UpdateEvent) { | ||
|
||
guard let userScripts = userScripts as? UserScripts else { fatalError("Unexpected UserScripts") } | ||
self.aiChatUserScript = userScripts.aiChatUserScript | ||
self.aiChatUserScript?.setPayloadHandler(self.payloadHandler) | ||
} | ||
} | ||
|
||
// MARK: - AIChatViewControllerDelegate | ||
extension AIChatViewControllerManager: AIChatViewControllerDelegate { | ||
func aiChatViewController(_ viewController: AIChatViewController, didRequestToLoad url: URL) { | ||
delegate?.aiChatViewControllerManager(self, didRequestToLoad: url) | ||
viewController.dismiss(animated: true) | ||
} | ||
|
||
func aiChatViewControllerDidFinish(_ viewController: AIChatViewController) { | ||
viewController.dismiss(animated: true) | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// AIChatPayloadHandling.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2025 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. | ||
// | ||
|
||
typealias AIChatPayload = [String: Any] | ||
|
||
/// A protocol that defines a generic interface for handling payloads. | ||
/// | ||
/// Types conforming to `PayloadHandler` are responsible for managing a payload | ||
/// of a specific type, including setting, consuming, and resetting the payload. | ||
protocol AIChatPayloadHandling { | ||
/// The type of payload that the handler manages. | ||
associatedtype PayloadType | ||
|
||
/// Sets the payload to be managed by the handler. | ||
/// | ||
/// - Parameter payload: The payload to be set. | ||
func setPayload(_ payload: PayloadType) | ||
|
||
/// Consumes and returns the current payload. | ||
/// | ||
/// This method returns the current payload and resets the handler, | ||
/// clearing the payload after it is consumed. | ||
/// | ||
/// - Returns: The current payload, or `nil` if no payload is set. | ||
func consumePayload() -> PayloadType? | ||
|
||
/// Resets the handler, clearing the current payload. | ||
/// | ||
/// After calling this method, the handler will no longer have a payload set. | ||
func reset() | ||
} | ||
|
||
final class AIChatPayloadHandler: AIChatPayloadHandling { | ||
typealias PayloadType = AIChatPayload | ||
|
||
private var payload: AIChatPayload? | ||
|
||
func setPayload(_ payload: AIChatPayload) { | ||
self.payload = payload | ||
} | ||
|
||
func consumePayload() -> AIChatPayload? { | ||
defer { reset() } | ||
return payload | ||
} | ||
|
||
func reset() { | ||
self.payload = nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// AIChatScriptUserValues.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2025 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 | ||
|
||
public struct AIChatScriptUserValues: Codable { | ||
let isAIChatHandoffEnabled: Bool | ||
let platform: String | ||
let aiChatPayload: AIChatPayload? | ||
|
||
enum CodingKeys: String, CodingKey { | ||
case isAIChatHandoffEnabled | ||
case platform | ||
case aiChatPayload | ||
} | ||
|
||
init(isAIChatHandoffEnabled: Bool, platform: String, aiChatPayload: [String: Any]?) { | ||
self.isAIChatHandoffEnabled = isAIChatHandoffEnabled | ||
self.platform = platform | ||
self.aiChatPayload = aiChatPayload | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
isAIChatHandoffEnabled = try container.decode(Bool.self, forKey: .isAIChatHandoffEnabled) | ||
platform = try container.decode(String.self, forKey: .platform) | ||
|
||
if let aiChatPayloadData = try? container.decodeIfPresent(Data.self, forKey: .aiChatPayload) { | ||
aiChatPayload = try JSONSerialization.jsonObject(with: aiChatPayloadData, options: []) as? AIChatPayload | ||
} else { | ||
aiChatPayload = nil | ||
} | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(isAIChatHandoffEnabled, forKey: .isAIChatHandoffEnabled) | ||
try container.encode(platform, forKey: .platform) | ||
|
||
if let aiChatPayload = aiChatPayload, | ||
let data = try? JSONSerialization.data(withJSONObject: aiChatPayload, options: []), | ||
let jsonString = String(data: data, encoding: .utf8) { | ||
try container.encode(jsonString, forKey: .aiChatPayload) | ||
} else { | ||
try container.encodeNil(forKey: .aiChatPayload) | ||
} | ||
} | ||
} |
Oops, something went wrong.