| Index: chrome/browser/resources/cryptotoken/cryptotokenbackground.js
|
| diff --git a/chrome/browser/resources/cryptotoken/cryptotokenbackground.js b/chrome/browser/resources/cryptotoken/cryptotokenbackground.js
|
| index 800cdf8a16e654ab3500da95c89ccab5a135f0c1..e7ebcc71fb2550c376da2304e519599c12a1f0c2 100644
|
| --- a/chrome/browser/resources/cryptotoken/cryptotokenbackground.js
|
| +++ b/chrome/browser/resources/cryptotoken/cryptotokenbackground.js
|
| @@ -37,6 +37,81 @@ var DEVICE_FACTORY_REGISTRY = new DeviceFactoryRegistry(
|
| new GoogleCorpIndividualAttestation());
|
|
|
| /**
|
| + * @param {*} request The received request
|
| + * @return {boolean} Whether the request is a register/enroll request.
|
| + */
|
| +function isRegisterRequest(request) {
|
| + if (!request) {
|
| + return false;
|
| + }
|
| + switch (request.type) {
|
| + case GnubbyMsgTypes.ENROLL_WEB_REQUEST:
|
| + return true;
|
| +
|
| + case MessageTypes.U2F_REGISTER_REQUEST:
|
| + return true;
|
| +
|
| + default:
|
| + return false;
|
| + }
|
| +}
|
| +
|
| +/**
|
| + * Default response callback to deliver a response to a request.
|
| + * @param {*} request The received request.
|
| + * @param {function(*): void} sendResponse A callback that delivers a response.
|
| + * @param {*} response The response to return.
|
| + */
|
| +function defaultResponseCallback(request, sendResponse, response) {
|
| + response['requestId'] = request['requestId'];
|
| + try {
|
| + sendResponse(response);
|
| + } catch (e) {
|
| + console.warn(UTIL_fmt('caught: ' + e.message));
|
| + }
|
| +}
|
| +
|
| +/**
|
| + * Response callback that delivers a response to a request only when the
|
| + * sender is a foreground tab.
|
| + * @param {*} request The received request.
|
| + * @param {!MessageSender} sender The message sender.
|
| + * @param {function(*): void} sendResponse A callback that delivers a response.
|
| + * @param {*} response The response to return.
|
| + */
|
| +function sendResponseToActiveTabOnly(request, sender, sendResponse, response) {
|
| + tabInForeground(sender.tab.id).then(function(result) {
|
| + // If the tab is no longer in the foreground, drop the result: the user
|
| + // is no longer interacting with the tab that originated the request.
|
| + if (result) {
|
| + defaultResponseCallback(request, sendResponse, response);
|
| + }
|
| + });
|
| +}
|
| +
|
| +/**
|
| + * Common handler for messages received from chrome.runtime.sendMessage and
|
| + * chrome.runtime.connect + postMessage.
|
| + * @param {*} request The received request
|
| + * @param {!MessageSender} sender The message sender
|
| + * @param {function(*): void} sendResponse A callback that delivers a response
|
| + * @return {Closeable} A Closeable request handler.
|
| + */
|
| +function messageHandler(request, sender, sendResponse) {
|
| + var responseCallback;
|
| + if (isRegisterRequest(request)) {
|
| + responseCallback =
|
| + sendResponseToActiveTabOnly.bind(null, request, sender, sendResponse);
|
| + } else {
|
| + responseCallback =
|
| + defaultResponseCallback.bind(null, request, sendResponse);
|
| + }
|
| + var closeable = handleWebPageRequest(/** @type {Object} */(request),
|
| + sender, responseCallback);
|
| + return closeable;
|
| +}
|
| +
|
| +/**
|
| * Listen to individual messages sent from (whitelisted) webpages via
|
| * chrome.runtime.sendMessage
|
| * @param {*} request The received request
|
| @@ -48,28 +123,22 @@ function messageHandlerExternal(request, sender, sendResponse) {
|
| if (sender.id && sender.id === LOG_SAVER_EXTENSION_ID) {
|
| return handleLogSaverMessage(request);
|
| }
|
| - var closeable = handleWebPageRequest(/** @type {Object} */(request),
|
| - sender, function(response) {
|
| - response['requestId'] = request['requestId'];
|
| - try {
|
| - sendResponse(response);
|
| - } catch (e) {
|
| - console.warn(UTIL_fmt('caught: ' + e.message));
|
| - }
|
| - });
|
| +
|
| + messageHandler(request, sender, sendResponse);
|
| return true; // Tell Chrome not to destroy sendResponse yet
|
| }
|
| chrome.runtime.onMessageExternal.addListener(messageHandlerExternal);
|
|
|
| // Listen to direct connection events, and wire up a message handler on the port
|
| chrome.runtime.onConnectExternal.addListener(function(port) {
|
| + function sendResponse(response) {
|
| + port.postMessage(response);
|
| + }
|
| +
|
| var closeable;
|
| port.onMessage.addListener(function(request) {
|
| - closeable = handleWebPageRequest(request, port.sender,
|
| - function(response) {
|
| - response['requestId'] = request['requestId'];
|
| - port.postMessage(response);
|
| - });
|
| + var sender = /** @type {!MessageSender} */ (port.sender);
|
| + closeable = messageHandler(request, sender, sendResponse);
|
| });
|
| port.onDisconnect.addListener(function() {
|
| if (closeable) {
|
|
|