| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * @fileoverview CryptoToken background page | 6 * @fileoverview CryptoToken background page |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 'use strict'; | 9 'use strict'; |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 new EtldOriginChecker(), | 30 new EtldOriginChecker(), |
| 31 new UsbHelper(), | 31 new UsbHelper(), |
| 32 new XhrTextFetcher()); | 32 new XhrTextFetcher()); |
| 33 | 33 |
| 34 var DEVICE_FACTORY_REGISTRY = new DeviceFactoryRegistry( | 34 var DEVICE_FACTORY_REGISTRY = new DeviceFactoryRegistry( |
| 35 new UsbGnubbyFactory(gnubbies), | 35 new UsbGnubbyFactory(gnubbies), |
| 36 TIMER_FACTORY, | 36 TIMER_FACTORY, |
| 37 new GoogleCorpIndividualAttestation()); | 37 new GoogleCorpIndividualAttestation()); |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * @param {*} request The received request |
| 41 * @return {boolean} Whether the request is a register/enroll request. |
| 42 */ |
| 43 function isRegisterRequest(request) { |
| 44 if (!request) { |
| 45 return false; |
| 46 } |
| 47 switch (request.type) { |
| 48 case GnubbyMsgTypes.ENROLL_WEB_REQUEST: |
| 49 return true; |
| 50 |
| 51 case MessageTypes.U2F_REGISTER_REQUEST: |
| 52 return true; |
| 53 |
| 54 default: |
| 55 return false; |
| 56 } |
| 57 } |
| 58 |
| 59 /** |
| 60 * Default response callback to deliver a response to a request. |
| 61 * @param {*} request The received request. |
| 62 * @param {function(*): void} sendResponse A callback that delivers a response. |
| 63 * @param {*} response The response to return. |
| 64 */ |
| 65 function defaultResponseCallback(request, sendResponse, response) { |
| 66 response['requestId'] = request['requestId']; |
| 67 try { |
| 68 sendResponse(response); |
| 69 } catch (e) { |
| 70 console.warn(UTIL_fmt('caught: ' + e.message)); |
| 71 } |
| 72 } |
| 73 |
| 74 /** |
| 75 * Response callback that delivers a response to a request only when the |
| 76 * sender is a foreground tab. |
| 77 * @param {*} request The received request. |
| 78 * @param {!MessageSender} sender The message sender. |
| 79 * @param {function(*): void} sendResponse A callback that delivers a response. |
| 80 * @param {*} response The response to return. |
| 81 */ |
| 82 function sendResponseToActiveTabOnly(request, sender, sendResponse, response) { |
| 83 tabInForeground(sender.tab.id).then(function(result) { |
| 84 // If the tab is no longer in the foreground, drop the result: the user |
| 85 // is no longer interacting with the tab that originated the request. |
| 86 if (result) { |
| 87 defaultResponseCallback(request, sendResponse, response); |
| 88 } |
| 89 }); |
| 90 } |
| 91 |
| 92 /** |
| 93 * Common handler for messages received from chrome.runtime.sendMessage and |
| 94 * chrome.runtime.connect + postMessage. |
| 95 * @param {*} request The received request |
| 96 * @param {!MessageSender} sender The message sender |
| 97 * @param {function(*): void} sendResponse A callback that delivers a response |
| 98 * @return {Closeable} A Closeable request handler. |
| 99 */ |
| 100 function messageHandler(request, sender, sendResponse) { |
| 101 var responseCallback; |
| 102 if (isRegisterRequest(request)) { |
| 103 responseCallback = |
| 104 sendResponseToActiveTabOnly.bind(null, request, sender, sendResponse); |
| 105 } else { |
| 106 responseCallback = |
| 107 defaultResponseCallback.bind(null, request, sendResponse); |
| 108 } |
| 109 var closeable = handleWebPageRequest(/** @type {Object} */(request), |
| 110 sender, responseCallback); |
| 111 return closeable; |
| 112 } |
| 113 |
| 114 /** |
| 40 * Listen to individual messages sent from (whitelisted) webpages via | 115 * Listen to individual messages sent from (whitelisted) webpages via |
| 41 * chrome.runtime.sendMessage | 116 * chrome.runtime.sendMessage |
| 42 * @param {*} request The received request | 117 * @param {*} request The received request |
| 43 * @param {!MessageSender} sender The message sender | 118 * @param {!MessageSender} sender The message sender |
| 44 * @param {function(*): void} sendResponse A callback that delivers a response | 119 * @param {function(*): void} sendResponse A callback that delivers a response |
| 45 * @return {boolean} | 120 * @return {boolean} |
| 46 */ | 121 */ |
| 47 function messageHandlerExternal(request, sender, sendResponse) { | 122 function messageHandlerExternal(request, sender, sendResponse) { |
| 48 if (sender.id && sender.id === LOG_SAVER_EXTENSION_ID) { | 123 if (sender.id && sender.id === LOG_SAVER_EXTENSION_ID) { |
| 49 return handleLogSaverMessage(request); | 124 return handleLogSaverMessage(request); |
| 50 } | 125 } |
| 51 var closeable = handleWebPageRequest(/** @type {Object} */(request), | 126 |
| 52 sender, function(response) { | 127 messageHandler(request, sender, sendResponse); |
| 53 response['requestId'] = request['requestId']; | |
| 54 try { | |
| 55 sendResponse(response); | |
| 56 } catch (e) { | |
| 57 console.warn(UTIL_fmt('caught: ' + e.message)); | |
| 58 } | |
| 59 }); | |
| 60 return true; // Tell Chrome not to destroy sendResponse yet | 128 return true; // Tell Chrome not to destroy sendResponse yet |
| 61 } | 129 } |
| 62 chrome.runtime.onMessageExternal.addListener(messageHandlerExternal); | 130 chrome.runtime.onMessageExternal.addListener(messageHandlerExternal); |
| 63 | 131 |
| 64 // Listen to direct connection events, and wire up a message handler on the port | 132 // Listen to direct connection events, and wire up a message handler on the port |
| 65 chrome.runtime.onConnectExternal.addListener(function(port) { | 133 chrome.runtime.onConnectExternal.addListener(function(port) { |
| 134 function sendResponse(response) { |
| 135 port.postMessage(response); |
| 136 } |
| 137 |
| 66 var closeable; | 138 var closeable; |
| 67 port.onMessage.addListener(function(request) { | 139 port.onMessage.addListener(function(request) { |
| 68 closeable = handleWebPageRequest(request, port.sender, | 140 var sender = /** @type {!MessageSender} */ (port.sender); |
| 69 function(response) { | 141 closeable = messageHandler(request, sender, sendResponse); |
| 70 response['requestId'] = request['requestId']; | |
| 71 port.postMessage(response); | |
| 72 }); | |
| 73 }); | 142 }); |
| 74 port.onDisconnect.addListener(function() { | 143 port.onDisconnect.addListener(function() { |
| 75 if (closeable) { | 144 if (closeable) { |
| 76 closeable.close(); | 145 closeable.close(); |
| 77 } | 146 } |
| 78 }); | 147 }); |
| 79 }); | 148 }); |
| 80 | 149 |
| 81 /** | 150 /** |
| 82 * Handles messages from the log-saver app. Temporarily replaces UTIL_fmt with | 151 * Handles messages from the log-saver app. Temporarily replaces UTIL_fmt with |
| (...skipping 17 matching lines...) Expand all Loading... |
| 100 if (originalUtilFmt_) { | 169 if (originalUtilFmt_) { |
| 101 UTIL_fmt = originalUtilFmt_; | 170 UTIL_fmt = originalUtilFmt_; |
| 102 originalUtilFmt_ = null; | 171 originalUtilFmt_ = null; |
| 103 } | 172 } |
| 104 } | 173 } |
| 105 return false; | 174 return false; |
| 106 } | 175 } |
| 107 | 176 |
| 108 /** @private */ | 177 /** @private */ |
| 109 var originalUtilFmt_ = null; | 178 var originalUtilFmt_ = null; |
| OLD | NEW |