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 FACTORY_REGISTRY = (function() {
26 var windowTimer = new WindowTimer();
27 var xhrTextFetcher = new XhrTextFetcher();
28 return new FactoryRegistry(
29 new XhrAppIdCheckerFactory(xhrTextFetcher),
30 new CryptoTokenApprovedOrigin(),
31 new CountdownTimerFactory(windowTimer),
32 new CryptoTokenOriginChecker(),
38 var DEVICE_FACTORY_REGISTRY = new DeviceFactoryRegistry(
39 new UsbGnubbyFactory(gnubbies),
40 FACTORY_REGISTRY.getCountdownFactory(),
41 new GoogleCorpIndividualAttestation());
44 * @param {*} request The received request
45 * @return {boolean} Whether the request is a register/enroll request.
47 function isRegisterRequest(request) {
51 switch (request.type) {
52 case MessageTypes.U2F_REGISTER_REQUEST:
61 * Default response callback to deliver a response to a request.
62 * @param {*} request The received request.
63 * @param {function(*): void} sendResponse A callback that delivers a response.
64 * @param {*} response The response to return.
66 function defaultResponseCallback(request, sendResponse, response) {
67 response['requestId'] = request['requestId'];
69 sendResponse(response);
71 console.warn(UTIL_fmt('caught: ' + e.message));
76 * Response callback that delivers a response to a request only when the
77 * sender is a foreground tab.
78 * @param {*} request The received request.
79 * @param {!MessageSender} sender The message sender.
80 * @param {function(*): void} sendResponse A callback that delivers a response.
81 * @param {*} response The response to return.
83 function sendResponseToActiveTabOnly(request, sender, sendResponse, response) {
84 tabInForeground(sender.tab.id).then(function(result) {
85 // If the tab is no longer in the foreground, drop the result: the user
86 // is no longer interacting with the tab that originated the request.
88 defaultResponseCallback(request, sendResponse, response);
94 * Common handler for messages received from chrome.runtime.sendMessage and
95 * chrome.runtime.connect + postMessage.
96 * @param {*} request The received request
97 * @param {!MessageSender} sender The message sender
98 * @param {function(*): void} sendResponse A callback that delivers a response
99 * @return {Closeable} A Closeable request handler.
101 function messageHandler(request, sender, sendResponse) {
102 var responseCallback;
103 if (isRegisterRequest(request)) {
105 sendResponseToActiveTabOnly.bind(null, request, sender, sendResponse);
108 defaultResponseCallback.bind(null, request, sendResponse);
110 var closeable = handleWebPageRequest(/** @type {Object} */(request),
111 sender, responseCallback);
116 * Listen to individual messages sent from (whitelisted) webpages via
117 * chrome.runtime.sendMessage
118 * @param {*} request The received request
119 * @param {!MessageSender} sender The message sender
120 * @param {function(*): void} sendResponse A callback that delivers a response
123 function messageHandlerExternal(request, sender, sendResponse) {
124 if (sender.id && sender.id === LOG_SAVER_EXTENSION_ID) {
125 return handleLogSaverMessage(request);
128 messageHandler(request, sender, sendResponse);
129 return true; // Tell Chrome not to destroy sendResponse yet
131 chrome.runtime.onMessageExternal.addListener(messageHandlerExternal);
133 // Listen to direct connection events, and wire up a message handler on the port
134 chrome.runtime.onConnectExternal.addListener(function(port) {
135 function sendResponse(response) {
136 port.postMessage(response);
140 port.onMessage.addListener(function(request) {
141 var sender = /** @type {!MessageSender} */ (port.sender);
142 closeable = messageHandler(request, sender, sendResponse);
144 port.onDisconnect.addListener(function() {
152 * Handles messages from the log-saver app. Temporarily replaces UTIL_fmt with
153 * a wrapper that also sends formatted messages to the app.
154 * @param {*} request The message received from the app
155 * @return {boolean} Used as chrome.runtime.onMessage handler return value
157 function handleLogSaverMessage(request) {
158 if (request === 'start') {
159 if (originalUtilFmt_) {
160 // We're already sending
163 originalUtilFmt_ = UTIL_fmt;
164 UTIL_fmt = function(s) {
165 var line = originalUtilFmt_(s);
166 chrome.runtime.sendMessage(LOG_SAVER_EXTENSION_ID, line);
169 } else if (request === 'stop') {
170 if (originalUtilFmt_) {
171 UTIL_fmt = originalUtilFmt_;
172 originalUtilFmt_ = null;
179 var originalUtilFmt_ = null;