1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-23 18:41:38 +01:00
metamask-extension/app/scripts/lib/rpc-method-middleware/handlers/log-web3-usage.js
Brad Decker e5688c024e
consolidate segment setup (#9617)
Consolidates the background and UI segment implementations into a shared solution.

This results in the introduction of our first shared module.

Co-authored-by: Erik Marks <25517051+rekmarks@users.noreply.github.com>
2020-10-21 16:10:55 -05:00

65 lines
1.8 KiB
JavaScript

import { MESSAGE_TYPE } from '../../enums'
/**
* This RPC method is called by our inpage web3 proxy whenever window.web3 is
* accessed. We're collecting data on window.web3 usage so that we can warn
* website maintainers, and possibly our users, before we remove window.web3
* by November 16, 2020.
*/
const logWeb3Usage = {
methodName: MESSAGE_TYPE.LOG_WEB3_USAGE,
implementation: logWeb3UsageHandler,
}
export default logWeb3Usage
const recordedWeb3Usage = {}
/**
* @typedef {Object} LogWeb3UsageOptions
* @property {string} origin - The origin of the request.
* @property {Function} sendMetrics - A function that registers a metrics event.
*/
/**
* @typedef {Object} LogWeb3UsageParam
* @property {string} action - The action taken (get or set).
* @property {string} name - The window.web3 property name subject to the action.
*/
/**
* @param {import('json-rpc-engine').JsonRpcRequest<[LogWeb3UsageParam]>} req - The JSON-RPC request object.
* @param {import('json-rpc-engine').JsonRpcResponse<true>} res - The JSON-RPC response object.
* @param {Function} _next - The json-rpc-engine 'next' callback.
* @param {Function} end - The json-rpc-engine 'end' callback.
* @param {LogWeb3UsageOptions} options
*/
function logWeb3UsageHandler (
req, res, _next, end,
{ origin, sendMetrics },
) {
const { action, name } = req.params[0]
if (!recordedWeb3Usage[origin]) {
recordedWeb3Usage[origin] = {}
}
if (!recordedWeb3Usage[origin][name]) {
recordedWeb3Usage[origin][name] = true
sendMetrics({
event: `Website Accessed window.web3`,
category: 'inpage_provider',
properties: { action, web3Property: name },
eventContext: {
referrer: {
url: origin,
},
},
excludeMetaMetricsId: true,
})
}
res.result = true
return end()
}