1. Tom Bryant
  2. tincr

Commits

Tom  committed 1ba51c0

Fix for Chrome local.storage issues. By failover to chrome.storage.local

  • Participants
  • Parent commits 501caff
  • Branches master

Comments (0)

Files changed (3)

File devtools.js

View file
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
-* 
+*
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
-* 
+*
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 var projectState = {};
 var inspectedLocation;
 var watchPort;
+var chromeStorage = chrome.storage.local;
+
+
+function getStoredItem(storedKey, storedCallback) {
+	var storedValue;
+	try {
+		storedValue = devtoolsWindow.localStorage[storedKey];
+		if (storedCallback) {
+			storedCallback(storedValue);
+		}
+	}catch (ex) {
+		chromeStorage.get(storedKey, function (result) {
+			storedCallback(result[storedKey]);
+		});
+	}
+}
+
+function setStoredItem(storedKey, storedValue) {
+		try {
+			localStorage[storedKey] = storedValue;
+		} catch (ex) {
+			var obj = {};
+			obj[storedKey] = storedValue;
+			console.log(obj);
+			chromeStorage.set(obj, function () {
+				var projState = location.origin;
+				chromeStorage.get(storedKey, function (result) {
+					console.log("INSIDE");
+					console.log(result);
+				});
+				});
+		}
+}
 
 var saveProjectState = function(){
 	if (projectState.type != 'fileUrl'){
-		localStorage[inspectedLocation.origin] = JSON.stringify(projectState);
+		//Use the asynch Chrome Local Storage API if the localStorage call fails.
+		setStoredItem(inspectedLocation.origin,JSON.stringify(projectState));
+		console.log("O HERE");
+/*
+		try {
+			localStorage[inspectedLocation.origin] = JSON.stringify(projectState);
+		} catch (ex) {
+			var projState = inspectedLocation.origin;
+			chromeStorage.set({projState: JSON.stringify(projectState) });
+		}
+*/
 	}
 	else{
 		var temp = {autosave: projectState.autosave, watchFiles: projectState.watchFiles};
-		localStorage[inspectedLocation.origin] = JSON.stringify(temp);
+		console.log("HERE");
+		setStoredItem(inspectedLocation.origin,JSON.stringify(temp));
+		//Use the asynch Chrome Local Storage API if the localStorage call fails.
+		/*
+		try {
+			localStorage[inspectedLocation.origin] = JSON.stringify(temp);
+		} catch (ex) {
+			var projState = inspectedLocation.origin;
+			chromeStorage.set({projState: JSON.stringify(temp) });
+		}
+		*/
+
 	}
 }
 
 var loadProject = function(type, path, autosave, watchFiles){
 	checkResources();
 	registerNavListener();
-	
+
 	toggleWatchingFiles(watchFiles, path);
 	projectState = {type: type, path: path, autosave:autosave, watchFiles:watchFiles};
 	saveProjectState();
 		}
 		if (callback){
 			callback();
-		}	
+		}
 	}
-	
+
 	if (watchPort){
 		watchPort.disconnect();
 		backgroundMsgSupport.unwatchDirectory(function(){
 	else{
 		registerWatch();
 	}
-	
+
+}
+
+function setState (projectStateStr) {
+	var temp;
+	if (projectStateStr){
+		temp = JSON.parse(projectStateStr);
+	}
+	else{
+		temp = {autosave: true, watchFiles: true};
+	}
+
+	return temp;
+}
+
+
+function bmsLoadProject(temp,path,type) {
+	if (type && path && projectState.path != path){
+		backgroundMsgSupport.loadProject(type, path, inspectedLocation.origin, function(data){
+			if (data.error){
+				logError(data.error);
+			}
+			else{
+				loadProject(type, path, temp.autosave, temp.watchFiles);
+			}
+		});
+	}
 }
 
 var checkLocation = function(){
 			if (path.charAt(0) == '/' && navigator.platform.indexOf('Win') == 0){
 				path = path.substring(1);
 			}
-			var projectStateStr = localStorage[location.origin];
-			if (projectStateStr){
-				temp = JSON.parse(projectStateStr);
-			}
-			else{
-				temp = {autosave: true, watchFiles: true};
+			var projectStateStr;
+			console.log("WE ARE HERE");
+			getStoredItem(location.origin,function (res) {
+
+				temp = setState(res);
+				bmsLoadProject(temp,path,type);
+
+			});
+			/*
+			try {
+				projectStateStr = localStorage[location.origin];
+				temp = setState(projectStateStr);
+				bmsLoadProject(temp,path,type);
+			} catch (ex) {
+				var projState = location.origin;
+
+				chromeStorage.get(projState, function (result) {
+					temp = setState(result.projState);
+					bmsLoadProject(temp,path,type);
+				});
 			}
+			*/
+
+
+
 		}
 		else{
-			var projectStateStr = localStorage[location.origin];
-			if (projectStateStr){
-				temp = JSON.parse(projectStateStr);
+			var projectStateStr;
+			getStoredItem(location.origin,function (res) {
+				temp = setState(res)
 				if (temp.type != 'fileUrl'){
 					type = temp.type;
 					path = temp.path;
 				}
-			}
-		}
-		if (type && path && projectState.path != path){
-			backgroundMsgSupport.loadProject(type, path, inspectedLocation.origin, function(data){
-				if (data.error){
-					logError(data.error);
-				}
-				else{
-					loadProject(type, path, temp.autosave, temp.watchFiles);
-				}
+				bmsLoadProject(temp,path,type);
+
 			});
+			/*
+			try {
+				projectStateStr = localStorage[location.origin];
+				temp = setState(projectStateStr);
+				if (temp.type != 'fileUrl'){
+					type = temp.type;
+					path = temp.path;
+				}
+				bmsLoadProject(temp,path,type);
+			} catch (ex) {
+				var projState = location.origin;
+				chromeStorage.get(projState, function (result) {
+					temp = setState(result.projState);
+					if (temp.type != 'fileUrl'){
+						type = temp.type;
+						path = temp.path;
+					}
+					bmsLoadProject(temp,path,type);
+				});
+			}
+
+		*/
 		}
+
+//bmsLoadProject(temp,path,type);
+
 	});
 }
 checkLocation();
-toggleLogging(localStorage['logging'] === 'true');
+/*getStoredItem('logging',toggleLogging);*/
+
+try {
+	toggleLogging(localStorage['logging'] === 'true');
+} catch (ex) {
+	chromeStorage.get('logging', function (result) {
+		toggleLogging(result.logging);
+});
+}
+
 
 chrome.devtools.panels.create(' Tincr ', 'icon.png', 'editorpanel.html', function(panel){
 	var windowInjector = function(panelWindow){

File editorpanel.js

View file
 var projectTypes;
-        
+var chromeStorage = chrome.storage.local;
+
 var checkProjectState = function(){
     var projectState = window.devtoolsWindow.projectState;
-        
+
+
+
+
     if (projectState.type == 'fileUrl'){
         $('#project-type').attr('disabled', 'true');
         $('#native-browse').attr('disabled', 'true');
-        
+
         $('#file-select').hide();
         $('#auto-refresh').hide();
         $('#auto-save').hide();
         var typeSelect = document.getElementById('project-type');
         $('#project-type').removeAttr('disabled');
         $('#native-browse').removeAttr('disabled');
-        
+
         for (var i = 0; i < projectTypes.length; i++){
             if (projectTypes[i].key === projectState.type){
                 typeSelect.value = String(i);
         $('#local-file-path').text(projectState.path).show();
         $('#toggle-watch')[0].checked = projectState.watchFiles;
         $('#toggle-autosave')[0].checked = projectState.autosave;
-        
+
         $('#file-select').show();
         $('#auto-refresh').show();
         $('#auto-save').show();
-        
+
         $('#load-error').hide();
         $('#error-img').hide();
         $('#success-img').show();
         $('#load-success').show();
     }
-    
-    var loggingEnabled = devtoolsWindow.localStorage['logging'] === 'true';
-    toggleLogging(loggingEnabled);
-    $('#toggle-logging')[0].checked = loggingEnabled;
-    
+
+	//Use the asynch Chrome Local Storage API if the localStorage call fails.
+	console.log("TEST LOGGING");
+	window.devtoolsWindow.getStoredItem('logging',switchLogging);
 }
 
+
+function switchLogging(loggingEnabled) {
+		devtoolsWindow.toggleLogging(loggingEnabled);
+    	$('#toggle-logging')[0].checked = loggingEnabled;
+}
+
+
 var initUI = function(){
 
     var typeSelect = document.getElementById('project-type');
-    
+
     backgroundMsgSupport.getProjectTypes(function(types){
         projectTypes = types;
         for (var i = 0; i < projectTypes.length; ++i) {
                     $('#toggle-watch')[0].checked = true;
                     $('#auto-refresh').show();
                     $('#auto-save').show();
-                    
+
                     $('#load-error').hide();
                     $('#error-img').hide();
                     $('#success-img').show();
         window.devtoolsWindow.saveProjectState();
     });
     $('#toggle-logging').on('change', function(e){
-        devtoolsWindow.localStorage['logging'] = '' + this.checked;
-        toggleLogging(this.checked);
+		console.log("CHECKD IS");
+		console.log(this.checked);
+		//Use the asynch Chrome Local Storage API if the localStorage call fails.
+		window.devtoolsWindow.setStoredItem('logging', this.checked);
+		/*
+		try {
+        	devtoolsWindow.localStorage['logging'] = '' + this.checked;
+		} catch (ex) {
+			console.log("Using Chrome Storage");
+			chromeStorage.set({'logging': this.checked });
+		}
+		*/
         devtoolsWindow.toggleLogging(this.checked);
+
     });
-    
+
 };

File manifest.json

View file
   "background": {"page" :"background.html"},
   "icons": {"48": "tools_small.png",
             "128": "tools.png" },
-  "permissions": ["http://*/", "https://*/"],
+  "permissions": ["http://*/", "https://*/","storage"],
   "plugins": [
     { "path": "NPAPIFileIOforChrome.plugin", "public": false },
     { "path": "npNPAPIFileIOforChrome.dll", "public": false },