diff --git a/background.js b/background.js index 3a21662..b171ea0 100644 --- a/background.js +++ b/background.js @@ -1,8 +1,8 @@ 'use strict'; -function onMountRequested(onSuccess, onError) { +function onMountRequested(successCallback, errorCallback) { console.log("mount requested"); - getMountInfo(function(mountInfo) { mount(mountInfo, onSuccess, onError); }); + getMountInfo(function(mountInfo) { mount(mountInfo, successCallback, errorCallback); }); } @@ -26,12 +26,13 @@ function getMountInfo(callback) { chrome.app.window.create("mount-info.html", windowOptions, windowCallback); } -function mount(mountInfo, onSuccess, onError) { +function mount(mountInfo, successCallback, errorCallback) { console.log("mount()"); let request = new XMLHttpRequest(); - request.open("GET", mountInfo.url, true); + request.open("GET", mountInfo.url); request.onreadystatechange = function() { if(request.readyState != XMLHttpRequest.DONE) return; + try { JSON.parse(request.responseText); @@ -39,14 +40,78 @@ function mount(mountInfo, onSuccess, onError) { chrome.fileSystemProvider.mount(options); console.log("mount succeeded"); - onSuccess(); + successCallback(); } catch(exception) { console.log(exception); console.log("mount failed"); - onError(); + errorCallback(); + //TODO: Properly show error (e.g. notification) } }; request.send(); } +function readDirectory(options, successCallback, errorCallback) { + let request = new XMLHttpRequest(); + request.open("GET", options.fileSystemId + "/" + options.directoryPath); + request.onreadystatechange = function() { + if(request.readyState != XMLHttpRequest.DONE) return; + + try { + let json = JSON.parse(request.responseText); + let entries = json.map(function(item) { + let entry = {}; + if(options.isDirectory) { + entry.isDirectory = item.type == "directory"; + } + if(options.name) { + entry.name = item.name; + } + if(options.size) { + entry.size = item.size; + } + if(options.modificationTime) { + entry.modificationTime = new Date(item.mtime); + } + return entry; + }); + + successCallback(entries, false); + } catch(exception) { + console.log(exception); + console.log("readDirectory failed"); + errorCallback("FAILED"); + //TODO: Properly show error (e.g. notification) + } + }; + request.send(); +} + +function getMetadata(options, successCallback, errorCallback) { + if(options.entryPath == "/") { + successCallback({ isDirectory: true, name: "", size: 0, modificationTime: new Date() }); + } else { + let parentDirectory = options.entryPath.substring(0, options.entryPath.lastIndexOf('/') + 1); + let baseName = options.entryPath.substr(options.entryPath.lastIndexOf('/') + 1); + options.directoryPath = parentDirectory; + let nameRequested = options.name; + options.name = true; + readDirectory(options, function(entries, more) { + for(let entry of entries) { + if(entry.name == baseName) { + if(!nameRequested) { + delete entry.name; + options.name = false; + } + successCallback(entry); + return; + } + } + errorCallback("NOT_FOUND"); + }, errorCallback); + } +} + chrome.fileSystemProvider.onMountRequested.addListener(onMountRequested); +chrome.fileSystemProvider.onReadDirectoryRequested.addListener(readDirectory); +chrome.fileSystemProvider.onGetMetadataRequested.addListener(getMetadata);