-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a dashboard messaging component to handle dashboard data and b…
…roken site report messages
- Loading branch information
1 parent
53504de
commit fbd3cb3
Showing
6 changed files
with
114 additions
and
99 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
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,92 @@ | ||
import browser from 'webextension-polyfill'; | ||
import { breakageReportForTab, getDisclosureDetails } from '../broken-site-report'; | ||
import { dashboardDataFromTab } from '../classes/privacy-dashboard-data'; | ||
import { registerMessageHandler } from '../message-handlers'; | ||
import { getCurrentTab } from '../utils'; | ||
import { isFireButtonEnabled } from './fire-button'; | ||
|
||
/** | ||
* Message handlers for communication from the dashboard to the extension background. | ||
* | ||
* Note, additional message handles for toggle reports is in the separate ToggleReports component. | ||
*/ | ||
export default class DashboardMessaging { | ||
/** | ||
* @param {{ | ||
* settings: import('../settings.js'); | ||
* tds: import('./tds').default; | ||
* tabManager: import('../tab-manager.js'); | ||
* }} args | ||
*/ | ||
constructor({ settings, tds, tabManager }) { | ||
this.settings = settings; | ||
this.tds = tds; | ||
this.tabManager = tabManager; | ||
|
||
registerMessageHandler('submitBrokenSiteReport', (report) => this.submitBrokenSiteReport(report)); | ||
registerMessageHandler('getPrivacyDashboardData', this.getPrivacyDashboardData.bind(this)); | ||
registerMessageHandler('getBreakageFormOptions', getDisclosureDetails); | ||
} | ||
|
||
/** | ||
* Only the dashboard sends this message, so we import the types from there. | ||
* @param {import('@duckduckgo/privacy-dashboard/schema/__generated__/schema.types').BreakageReportRequest} breakageReport | ||
* @param {string} [pixelName] | ||
* @param {string} [reportFlow] | ||
* @returns {Promise<void>} | ||
*/ | ||
async submitBrokenSiteReport(breakageReport, pixelName = 'epbf', reportFlow = undefined) { | ||
// wait for config and TDS so we can get etags and config version | ||
await Promise.all([this.tds.remoteConfig.allLoadingFinished, this.tds.tds.ready]); | ||
const { category, description } = breakageReport; | ||
const tab = await this.tabManager.getOrRestoreCurrentTab(); | ||
if (!tab) { | ||
return; | ||
} | ||
const pageParams = (await browser.tabs.sendMessage(tab.id, { getBreakagePageParams: true })) || {}; | ||
const tds = this.tds.tds.etag; | ||
const remoteConfigEtag = this.tds.remoteConfig.etag; | ||
const remoteConfigVersion = this.tds.remoteConfig.config?.version; | ||
return breakageReportForTab({ | ||
pixelName, | ||
tab, | ||
tds, | ||
remoteConfigEtag, | ||
remoteConfigVersion, | ||
category, | ||
description, | ||
pageParams, | ||
reportFlow, | ||
}); | ||
} | ||
|
||
/** | ||
* This message is here to ensure the privacy dashboard can render | ||
* from a single call to the extension. | ||
* | ||
* Currently, it will collect data for the current tab and email protection | ||
* user data. | ||
*/ | ||
async getPrivacyDashboardData(options) { | ||
let { tabId } = options; | ||
if (tabId === null) { | ||
const currentTab = await getCurrentTab(); | ||
if (!currentTab?.id) { | ||
throw new Error('could not get the current tab...'); | ||
} | ||
tabId = currentTab?.id; | ||
} | ||
|
||
// Await for storage to be ready; this happens on service worker closing mostly. | ||
await this.settings.ready(); | ||
await this.tds.config.ready; | ||
|
||
const tab = await this.tabManager.getOrRestoreTab(tabId); | ||
if (!tab) throw new Error('unreachable - cannot access current tab with ID ' + tabId); | ||
const userData = this.settings.getSetting('userData'); | ||
const fireButtonData = { | ||
enabled: isFireButtonEnabled, | ||
}; | ||
return dashboardDataFromTab(tab, userData, fireButtonData); | ||
} | ||
} |
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
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