@@ -147,9 +147,9 @@ if (GoogleSmartCard.IS_DEBUG_BUILD) { console.log("[cackey] Initialization completed, resending any queued messages"); } - cackeyInitPCSCCompleted(); + cackeyInitPCSCCompleted("success"); } if (messageEvent.data.id == null) { return; @@ -226,8 +226,9 @@ * having sent anything */ pinWindow.onClosed.addListener(function() { var messageIdx; + var chromeCallback; pinWindowPreviousHandle = null; for (messageIdx = 0; messageIdx < cackeyMessagesToRetry.length; messageIdx++) { @@ -247,11 +248,26 @@ } else { tmpMessageEvent.data.originalrequest.pin = pinWindowPINValue; cackeyCertificateToPINMap[cackeyCertificateToPINID(tmpMessageEvent.data.originalrequest.certificate)] = pinWindowPINValue; + + chromeCallback = null; + if (tmpMessageEvent.data.id) { + if (cackeyOutstandingCallbacks) { + chromeCallback = cackeyOutstandingCallbacks[tmpMessageEvent.data.id]; + } + } cackeyInitPCSC(function() { cackeyHandle.postMessage(tmpMessageEvent.data.originalrequest); + }, function() { + if (chromeCallback) { + chromeCallback(); + } + + if (tmpMessageEvent.data.id && cackeyOutstandingCallbacks[tmpMessageEvent.data.id]) { + delete cackeyOutstandingCallbacks[tmpMessageEvent.data.id]; + } }); } } @@ -335,9 +351,9 @@ if (GoogleSmartCard.IS_DEBUG_BUILD) { console.log("[cackey] Thrown."); } - }); + }, chromeCallback); return; } @@ -405,9 +421,9 @@ if (GoogleSmartCard.IS_DEBUG_BUILD) { console.log("[cackey] Thrown."); } - }); + }, chromeCallback); return; } @@ -500,32 +516,54 @@ return; } -function cackeyInitPCSCCompleted() { +function cackeyInitPCSCCompleted(state) { var idx; - cackeyPCSCHandleUsable = true; + console.log("[cackey] Connection completed (state = \"" + state + "\"), sending queued events: " + cackeyCallbackAfterInit.length); + + switch (state) { + case "success": + cackeyPCSCHandleUsable = true; + + break; + case "failure": + cackeyPCSCHandleUsable = false; + + break; + } for (idx = 0; idx < cackeyCallbackAfterInit.length; idx++) { if (!cackeyCallbackAfterInit[idx]) { continue; } - cackeyCallbackAfterInit[idx](); + switch (state) { + case "success": + (cackeyCallbackAfterInit[idx].successCallback)(); + + break; + case "failure": + (cackeyCallbackAfterInit[idx].failureCallback)(); + + break; + } } delete cackeyCallbackAfterInit; cackeyCallbackAfterInit = []; + + console.log("[cackey] All queued events processed"); return; } /* * Initialize the PCSC connection */ -function cackeyInitPCSC(callbackAfterInit) { +function cackeyInitPCSC(callbackAfterInit, callbackInitFailed) { /* * Start the Google PCSC Interface */ @@ -534,9 +572,9 @@ /* * Queue this callback to be completed when initialization is complete */ if (callbackAfterInit) { - cackeyCallbackAfterInit.push(callbackAfterInit); + cackeyCallbackAfterInit.push({"successCallback": callbackAfterInit, "failureCallback": callbackInitFailed}); } /* * No additional work is required @@ -545,9 +583,9 @@ if (cackeyPCSCHandle) { console.log("[cackey] PCSC handle is already valid, nothing to do."); if (cackeyPCSCHandleUsable) { - cackeyInitPCSCCompleted(); + cackeyInitPCSCCompleted("success"); } return; } @@ -692,8 +730,10 @@ */ oldOnPortDisconnectedFunction = GoogleSmartCard.Pcsc.prototype.onPortDisconnected_; GoogleSmartCard.Pcsc.prototype.onPortDisconnected_ = function() { oldOnPortDisconnectedFunction.apply(this); + + cackeyInitPCSCCompleted("failure"); cackeyRestart(); return;