Skip to content

Commit

Permalink
Productionize DBP web ui code (#1769)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1203581873609357/1205531000165743/f
Tech Design URL:
CC:

**Description**:
This PR formally integrates the DBP web UI into the app

**Steps to test this PR**:
1. Open Personal Information Removal. Web UI should load.

<!--
Tagging instructions
If this PR isn't ready to be merged for whatever reason it should be
marked with the `DO NOT MERGE` label (particularly if it's a draft)
If it's pending Product Review/PFR, please add the `Pending Product
Review` label.

If at any point it isn't actively being worked on/ready for
review/otherwise moving forward (besides the above PR/PFR exception)
strongly consider closing it (or not opening it in the first place). If
you decide not to close it, make sure it's labelled to make it clear the
PRs state and comment with more information.
-->

---
###### Internal references:
[Pull Request Review
Checklist](https://app.asana.com/0/1202500774821704/1203764234894239/f)
[Software Engineering
Expectations](https://app.asana.com/0/59792373528535/199064865822552)
[Technical Design
Template](https://app.asana.com/0/59792373528535/184709971311943)
[Pull Request
Documentation](https://app.asana.com/0/1202500774821704/1204012835277482/f)
  • Loading branch information
SlayterDev authored Oct 20, 2023
1 parent 111fe77 commit b05ecff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 164 deletions.
6 changes: 4 additions & 2 deletions DuckDuckGo/DBP/DBPHomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ final class DBPHomeViewController: NSViewController {

return DataBrokerProtectionViewController(scheduler: dataBrokerProtectionManager.scheduler,
dataManager: dataBrokerProtectionManager.dataManager,
notificationCenter: NotificationCenter.default,
privacyConfig: privacyConfigurationManager,
prefs: prefs)
prefs: prefs,
openURLHandler: { url in
WindowControllersManager.shared.show(url: url, newTab: true)
})
}()

init(dataBrokerProtectionManager: DataBrokerProtectionManager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,165 +23,33 @@ import WebKit
import Combine

final public class DataBrokerProtectionViewController: NSViewController {
private let navigationViewModel: ContainerNavigationViewModel
private let profileViewModel: ProfileViewModel

private enum Constants {
static let dbpUiUrl = "https://duckduckgo.com/dbp"
}

private let dataManager: DataBrokerProtectionDataManaging
private let resultsViewModel: ResultsViewModel
private let containerViewModel: ContainerViewModel
private let scheduler: DataBrokerProtectionScheduler
private let notificationCenter: NotificationCenter
private var webView: WKWebView?

private let webUIViewModel: DBPUIViewModel

private let debugPage: String = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form>
<input type="button" value="Add Name" onclick="addName()">
<input type="button" value="Add Address" onclick="addAddress()">
<input type="button" value="Set Birth Year" onclick="setBirthYear()">
<input type="button" value="Set State" onclick="handshake()">
<input type="button" value="Get Profile" onclick="getProfile()">
<input type="button" value="Start Scan" onclick="startScan()">
<input type="button" value="Initial Scan Data" onclick="initialScanStatus()">
<input type="button" value="Remove All Data" onclick="removeAllData()">
</form>
<p id="output"></p>
<script type="text/javascript">
function addName() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "addNameToCurrentUserProfile",
"id": "abc123",
"params": {
"first": "Bradley",
"middle": "Curtis",
"last": "Slayter"
}
})
}
function addAddress() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "addAddressToCurrentUserProfile",
"id": "abc123",
"params": {
"street": "3003 Lake Ridge Dr",
"city": "Sanger",
"state": "TX"
}
})
}
function setBirthYear() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "setBirthYearForCurrentUserProfile",
"params": {
"year": 1993
}
})
}
function startScan() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "startScanAndOptOut"
})
}
function removeAllData() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "deleteUserProfileData"
})
}
function handshake() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "setState",
"id": "abc123",
"params": {
"state": "ProfileReview"
}
})
}
function handshake() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "setState",
"id": "abc123",
"params": {
"state": "ProfileReview"
}
})
}
function getProfile() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "getCurrentUserProfile",
"id": "abc123",
}).then(data => {
document.getElementById('output').textContent = JSON.stringify(data, null, 4)
})
}
function initialScanStatus() {
window.webkit.messageHandlers.dbpui.postMessage({
"context": "dbpui",
"featureName": "dbpuiCommunication",
"method": "initialScanStatus",
"id": "abc123",
}).then(data => {
document.getElementById('output').textContent = JSON.stringify(data, null, 4)
})
}
</script>
</body>
</html>
"""
private let openURLHandler: (URL?) -> Void

public init(scheduler: DataBrokerProtectionScheduler,
dataManager: DataBrokerProtectionDataManaging,
notificationCenter: NotificationCenter = .default,
privacyConfig: PrivacyConfigurationManaging? = nil, prefs: ContentScopeProperties? = nil) {
privacyConfig: PrivacyConfigurationManaging? = nil,
prefs: ContentScopeProperties? = nil,
openURLHandler: @escaping (URL?) -> Void) {
self.scheduler = scheduler
self.dataManager = dataManager
self.notificationCenter = notificationCenter
self.openURLHandler = openURLHandler

self.webUIViewModel = DBPUIViewModel(dataManager: dataManager, scheduler: scheduler, privacyConfig: privacyConfig, prefs: prefs, webView: webView)

navigationViewModel = ContainerNavigationViewModel(dataManager: dataManager)
profileViewModel = ProfileViewModel(dataManager: dataManager)

resultsViewModel = ResultsViewModel(dataManager: dataManager,
notificationCenter: notificationCenter)

containerViewModel = ContainerViewModel(scheduler: scheduler,
dataManager: dataManager)

dataManager.fetchProfile(ignoresCache: true)
Task {
_ = dataManager.fetchProfile(ignoresCache: true)
}

super.init(nibName: nil, bundle: nil)
}
Expand All @@ -191,25 +59,20 @@ final public class DataBrokerProtectionViewController: NSViewController {
}

override public func loadView() {
let containerView = DataBrokerProtectionContainerView(
containerViewModel: containerViewModel,
navigationViewModel: navigationViewModel,
profileViewModel: profileViewModel,
resultsViewModel: resultsViewModel)
guard let configuration = webUIViewModel.setupCommunicationLayer() else { return }

let hostingController = NSHostingController(rootView: containerView)
view = hostingController.view
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration)
webView?.uiDelegate = self
view = webView!

// guard let configuration = webUIViewModel.setupCommunicationLayer() else { return }
//
// webView = WKWebView(frame: CGRect(x: 0, y: 0, width: 1024, height: 768), configuration: configuration)
// view = webView!

// FOR LOCAL WEB UI DEVELOPMENT:
// Comment this line 👇
// webView?.loadHTMLString(debugPage, baseURL: nil)
// Uncomment this line and add your dev URL 👇
// webView?.load(URL(string: "https://<your url>")!)
webView?.load(URL(string: Constants.dbpUiUrl)!)
}

}

extension DataBrokerProtectionViewController: WKUIDelegate {
public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
openURLHandler(navigationAction.request.url)
return nil
}
}

0 comments on commit b05ecff

Please sign in to comment.