Chromium Code Reviews| Index: chrome/browser/resources/file_manager/js/harness.js |
| diff --git a/chrome/browser/resources/file_manager/js/harness.js b/chrome/browser/resources/file_manager/js/harness.js |
| index 1f7becc4b6fe09d9e48a4e43934f9c4057ad6074..7982ecdd9d8039d0a9e33a3ea9ef3875f015975f 100644 |
| --- a/chrome/browser/resources/file_manager/js/harness.js |
| +++ b/chrome/browser/resources/file_manager/js/harness.js |
| @@ -43,13 +43,12 @@ var harness = { |
| } |
| function loadUI() { |
| - var iframe = document.getElementById('dialog'); |
| - iframe.setAttribute('src', 'main.html' + document.location.search); |
| + harness.iframe.src = 'main.html' + document.location.search; |
| } |
| window.webkitStorageInfo.requestQuota( |
| window.PERSISTENT, |
| - 1024*1024*1024, // 1 Gig should be enough for everybody:) |
| + 1024 * 1024 * 1024, // 1 Gig should be enough for everybody:) |
| function(grantedBytes) { |
| window.webkitRequestFileSystem( |
| window.PERSISTENT, |
| @@ -62,7 +61,7 @@ var harness = { |
| var paramstr = decodeURIComponent(document.location.search.substr(1)); |
| this.params = paramstr ? JSON.parse(paramstr) : {}; |
| - var input = document.getElementById('default-path'); |
| + var input = document.querySelector('#default-path'); |
| input.value = this.params.defaultPath || ''; |
| input.addEventListener('keyup', this.onInputKeyUp.bind(this)); |
| }, |
| @@ -75,7 +74,7 @@ var harness = { |
| }, |
| changePath: function() { |
| - var input = document.getElementById('default-path'); |
| + var input = document.querySelector('#default-path'); |
| this.changeParam('defaultPath', input.value); |
| }, |
| @@ -85,13 +84,17 @@ var harness = { |
| }, |
| /** |
| - * 'Reset Fileystem' button click handler. |
| + * 'Reset Filesystem' button click handler. |
| */ |
| onClearClick: function() { |
| - util.forEachDirEntry(this.filesystem.root, function(dirEntry) { |
| + harness.resetFilesystem(this.filesystem, harness.init); |
| + }, |
| + |
| + resetFilesystem: function(filesystem, opt_callback) { |
| + util.forEachDirEntry(filesystem.root, function(dirEntry) { |
| if (!dirEntry) { |
| console.log('Filesystem reset.'); |
| - harness.init(); |
| + if (opt_callback) opt_callback(); |
| return; |
| } |
| util.removeFileOrDirectory( |
| @@ -102,10 +105,25 @@ var harness = { |
| }, |
| /** |
| + * 'Auto-populate' button click handler. |
| + */ |
| + onPopulateClick: function() { |
| + harness.importWebDirectory(this.filesystem, |
| + 'Downloads', 'harness_files', function() {}, harness.init); |
| + }, |
| + |
| + /** |
| * Change handler for the 'input type=file' element. |
| */ |
| onFilesChange: function(event) { |
| - this.importFiles([].slice.call(event.target.files)); |
| + this.importFiles(harness.filesystem, |
| + harness.fileManager.getCurrentDirectory(), |
| + [].slice.call(event.target.files), |
| + function() { |
| + harness.chrome.fileBrowserPrivate.onFileChanged.notify({ |
| + fileUrl: harness.fileManager.getCurrentDirectoryURL() |
| + }); |
| + }); |
| }, |
| /** |
| @@ -117,59 +135,135 @@ var harness = { |
| * from the JS console, we don't really need to be sure it's ready at any |
| * particular time. |
| */ |
| - get fileManager() { |
| - return document.getElementById('dialog').contentWindow.fileManager; |
| - }, |
| - get pyautoAPI() { |
| - return document.getElementById('dialog').contentWindow.pyautoAPI; |
| - }, |
| - get chrome() { |
| - return document.getElementById('dialog').contentWindow.chrome; |
| - }, |
| + get iframe() { return document.querySelector('#dialog') }, |
| + |
| + get fileManager() { return iframe.contentWindow.fileManager }, |
| + |
| + get pyautoAPI() { return iframe.contentWindow.pyautoAPI }, |
| + |
| + get chrome() { return iframe.contentWindow.chrome; }, |
| /** |
| - * Import a list of File objects into harness.filesystem. |
| + * Copy a blob into into the filesystem. |
|
dgozman
2012/09/05 08:14:05
into into
Vladislav Kaznacheev
2012/09/06 08:28:45
Done.
|
| + * |
| + * @param {DOMFileSystem} filesystem Destination file system. |
| + * @param {string} dstPath Destination file path. |
| + * @param {Blob} srcBlob Source blob. |
| + * @param {function} callback Completion callback. |
| */ |
| - importFiles: function(files) { |
| - var currentSrc = null; |
| - var currentDest = null; |
| - var importCount = 0; |
| - |
| - function onWriterCreated(writer) { |
| - writer.onerror = util.flog('Error writing: ' + currentDest.fullPath); |
| + copyBlob: function(filesystem, dstPath, srcBlob, callback) { |
| + function onWriterCreated(entry, writer) { |
| + writer.onerror = |
| + util.flog('Error writing: ' + entry.fullPath, callback); |
| writer.onwriteend = function() { |
| - console.log('Wrote: ' + currentDest.fullPath); |
| - ++importCount; |
| - processNextFile(); |
| + console.log('Wrote ' + srcBlob.size + ' bytes to ' + entry.fullPath); |
| + callback(); |
| }; |
| - writer.write(currentSrc); |
| + writer.write(srcBlob); |
| } |
| function onFileFound(fileEntry) { |
| - currentDest = fileEntry; |
| - currentDest.createWriter(onWriterCreated, |
| - util.flog('Error creating writer for: ' + |
| - currentDest.fullPath)); |
| + fileEntry.createWriter(onWriterCreated.bind(null, fileEntry), util.flog( |
| + 'Error creating writer for: ' + fileEntry.fullPath, callback)); |
| } |
| + util.getOrCreateFile(filesystem.root, dstPath, onFileFound, |
| + util.flog('Error finding path: ' + dstPath, callback)); |
| + }, |
| + |
| + /** |
| + * Import a list of File objects into harness.filesystem. |
| + * |
| + * @param {DOMFileSystem} filesystem File system. |
| + * @param {string} dstDir Destination path. |
| + * @param {Array.<File>} files Array of files. |
| + * @param {function} callback Completion callback. |
| + */ |
| + importFiles: function(filesystem, dstDir, files, callback) { |
| function processNextFile() { |
| if (files.length == 0) { |
| - console.log('Import complete: ' + importCount + ' file(s)'); |
| - harness.chrome.fileBrowserPrivate.onFileChanged.notify({ |
| - fileUrl: harness.fileManager.getCurrentDirectoryURL() |
| - }); |
| + console.log('Import complete'); |
| + callback(); |
| return; |
| } |
| - currentSrc = files.shift(); |
| - var destPath = harness.fileManager.getCurrentDirectory() + '/' + |
| - currentSrc.name.replace(/\^\^/g, '/'); |
| - util.getOrCreateFile(harness.filesystem.root, destPath, onFileFound, |
| - util.flog('Error finding path: ' + destPath)); |
| + var file = files.shift(); |
| + harness.copyBlob( |
| + filesystem, dstDir + '/' + file.name, file, processNextFile); |
| } |
| console.log('Start import: ' + files.length + ' file(s)'); |
| processNextFile(); |
| + }, |
| + |
| + /** |
| + * Copy all files recursively linked from a web page. |
| + * |
| + * Assumes the directory listing format produced by Python's SimpleHTTPServer: |
| + * Child names are in |href| attributes, subdirectory names end with '/'. |
| + * |
| + * @param {DOMFileSystem} filesystem File system. |
| + * @param {string} dstDir Destination path. |
| + * @param {string} srcDirUrl Directory page url. |
| + * @param {function(string, Blob)} onProgress Progress callback. |
| + * @param {function} onComplete Completion callback. |
| + */ |
| + importWebDirectory: function( |
| + filesystem, dstDirPath, srcDirUrl, onProgress, onComplete) { |
| + var childNames = []; |
| + |
| + function readFromUrl(url, type, callback) { |
| + var xhr = new XMLHttpRequest(); |
| + xhr.open('GET', url, true); |
| + xhr.responseType = type; |
| + xhr.onload = function() { |
| + if (xhr.status == 200) |
| + callback( |
| + xhr.responseType == 'text' ? xhr.responseText : xhr.response); |
| + else |
| + callback(null); |
| + }; |
| + xhr.send(); |
| + } |
| + |
| + function processNextChild() { |
| + if (childNames.length == 0) { |
| + onComplete(); |
| + return; |
| + } |
| + |
| + var name = childNames.shift(); |
| + var isDirectory = name.match(/\/$/); // Ends with '/'. |
| + if (isDirectory) name = name.substring(0, name.length - 1); |
| + |
| + var srcChildUrl = srcDirUrl + '/' + name; |
| + var dstChildPath = dstDirPath + '/' + name; |
| + if (isDirectory) { |
| + harness.importWebDirectory(filesystem, |
| + dstChildPath, srcChildUrl, onProgress, processNextChild); |
| + } else { |
| + readFromUrl(srcChildUrl, 'blob', function(blob) { |
| + onProgress(blob); |
| + harness.copyBlob(filesystem, dstChildPath, blob, processNextChild); |
| + }); |
| + } |
| + } |
| + |
| + readFromUrl(srcDirUrl, 'text', function(text) { |
| + if (!text) { |
| + console.log('Cannot read ' + srcDirUrl); |
| + onComplete(); |
| + return; |
| + } |
| + var links = text.match(/href=".*"/g); // Extract all links from the page. |
| + if (links) { |
| + for (var i = 0; i != links.length; i++) |
| + childNames.push(links[i].match(/href="(.*)"/)[1]); |
| + } |
| + console.log( |
| + 'Importing ' + childNames.length + ' entries from ' + srcDirUrl); |
| + processNextChild(); |
| + }); |
| } |
| }; |