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
3 // found in the LICENSE file.
6 * @fileoverview CryptoToken background page
12 var BROWSER_SUPPORTS_TLS_CHANNEL_ID = true;
15 var HTTP_ORIGINS_ALLOWED = false;
18 var LOG_SAVER_EXTENSION_ID = 'fjajfjhkeibgmiggdfehjplbhmfkialk';
20 // Singleton tracking available devices.
21 var gnubbies = new Gnubbies();
22 HidGnubbyDevice.register(gnubbies);
23 UsbGnubbyDevice.register(gnubbies);
25 var TIMER_FACTORY = new CountdownTimerFactory();
27 var FACTORY_REGISTRY = new FactoryRegistry(
28 new CryptoTokenApprovedOrigin(),
30 new CryptoTokenOriginChecker(),
32 new XhrTextFetcher());
34 var DEVICE_FACTORY_REGISTRY = new DeviceFactoryRegistry(
35 new UsbGnubbyFactory(gnubbies),
37 new GoogleCorpIndividualAttestation());
40 * @param {*} request The received request
41 * @return {boolean} Whether the request is a register/enroll request.
43 function isRegisterRequest(request) {
47 switch (request.type) {
48 case GnubbyMsgTypes.ENROLL_WEB_REQUEST:
51 case MessageTypes.U2F_REGISTER_REQUEST:
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.
65 function defaultResponseCallback(request, sendResponse, response) {
66 response['requestId'] = request['requestId'];
68 sendResponse(response);
70 console.warn(UTIL_fmt('caught: ' + e.message));
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.
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.
87 defaultResponseCallback(request, sendResponse, response);
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.
100 function messageHandler(request, sender, sendResponse) {
101 var responseCallback;
102 if (isRegisterRequest(request)) {
104 sendResponseToActiveTabOnly.bind(null, request, sender, sendResponse);
107 defaultResponseCallback.bind(null, request, sendResponse);
109 var closeable = handleWebPageRequest(/** @type {Object} */(request),
110 sender, responseCallback);
115 * Listen to individual messages sent from (whitelisted) webpages via
116 * chrome.runtime.sendMessage
117 * @param {*} request The received request
118 * @param {!MessageSender} sender The message sender
119 * @param {function(*): void} sendResponse A callback that delivers a response
122 function messageHandlerExternal(request, sender, sendResponse) {
123 if (sender.id && sender.id === LOG_SAVER_EXTENSION_ID) {
124 return handleLogSaverMessage(request);
127 messageHandler(request, sender, sendResponse);
128 return true; // Tell Chrome not to destroy sendResponse yet
130 chrome.runtime.onMessageExternal.addListener(messageHandlerExternal);
132 // Listen to direct connection events, and wire up a message handler on the port
133 chrome.runtime.onConnectExternal.addListener(function(port) {
134 function sendResponse(response) {
135 port.postMessage(response);
139 port.onMessage.addListener(function(request) {
140 var sender = /** @type {!MessageSender} */ (port.sender);
141 closeable = messageHandler(request, sender, sendResponse);
143 port.onDisconnect.addListener(function() {
151 * Handles messages from the log-saver app. Temporarily replaces UTIL_fmt with
152 * a wrapper that also sends formatted messages to the app.
153 * @param {*} request The message received from the app
154 * @return {boolean} Used as chrome.runtime.onMessage handler return value
156 function handleLogSaverMessage(request) {
157 if (request === 'start') {
158 if (originalUtilFmt_) {
159 // We're already sending
162 originalUtilFmt_ = UTIL_fmt;
163 UTIL_fmt = function(s) {
164 var line = originalUtilFmt_(s);
165 chrome.runtime.sendMessage(LOG_SAVER_EXTENSION_ID, line);
168 } else if (request === 'stop') {
169 if (originalUtilFmt_) {
170 UTIL_fmt = originalUtilFmt_;
171 originalUtilFmt_ = null;
178 var originalUtilFmt_ = null;