From a42a464d1fb80e9a4dbd5068cbee08f1b78217fb Mon Sep 17 00:00:00 2001 From: cTn Date: Wed, 19 Jun 2013 13:06:14 +0200 Subject: [PATCH] fix for BUS holding after onClose --- background.js | 10 ++++++++++ js/main.js | 10 ++++++++++ js/serial_backend.js | 2 ++ 3 files changed, 22 insertions(+) diff --git a/background.js b/background.js index 2af9578184..9b9b13fd94 100644 --- a/background.js +++ b/background.js @@ -6,5 +6,15 @@ chrome.app.runtime.onLaunched.addListener(function() { maxWidth: 960, minHeight: 600, maxHeight: 600 + }, function(window_child) { + window_child.onClosed.addListener(function() { + // connectionId is passed from the script side through the chrome.runtime.getBackgroundPage refference + // allowing us to automatically close the port when application shut down + if (connectionId != -1) { + chrome.serial.close(connectionId, function(){ + console.log('CLEANUP: Connection to serial port was opened after application closed, closing the connection.'); + }); + } + }); }); }); \ No newline at end of file diff --git a/js/main.js b/js/main.js index 6ce5e6062c..e5fcec7c31 100644 --- a/js/main.js +++ b/js/main.js @@ -1,3 +1,13 @@ +// Get access to the background window object +// This object is used to pass current connectionId to the backround page +// so the onClosed event can close the port for us if it was left opened, without this +// users can experience weird behavior if they would like to access the serial bus afterwards. +var backgroundPage; +chrome.runtime.getBackgroundPage(function(result) { + backgroundPage = result; + backgroundPage.connectionId = -1; +}); + // OS detection var OS = "Unknown"; if (navigator.appVersion.indexOf("Win") != -1) OS = "Windows"; diff --git a/js/serial_backend.js b/js/serial_backend.js index 5de7386e90..7fa7d913ed 100644 --- a/js/serial_backend.js +++ b/js/serial_backend.js @@ -226,6 +226,7 @@ $(document).ready(function() { function onOpen(openInfo) { connectionId = openInfo.connectionId; + backgroundPage.connectionId = openInfo.connectionId; // also pass connectionId to the background page if (connectionId != -1) { console.log('Connection was opened with ID: ' + connectionId); @@ -273,6 +274,7 @@ function onOpen(openInfo) { function onClosed(result) { if (result) { // All went as expected connectionId = -1; // reset connection id + backgroundPage.connectionId = connectionId; // also pass latest connectionId to the background page sensor_status(sensors_detected = 0); // reset active sensor indicators $('#content').empty(); // empty content