OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // This file implements an in-memory read-only file system through the extfs |
| 6 // API. |
| 7 |
| 8 // The in-memory file system data. |
| 9 var theRoot = { |
| 10 self: { isDirectory: true, name: '', size: 0 }, |
| 11 children: [ |
| 12 { self: { isDirectory: false, name: 'file1', size: 10 } }, |
| 13 { self: { isDirectory: true, name: 'dir1', size: 0 }, |
| 14 children: [ |
| 15 { self: { isDirectory: false, name: 'file2', size: 20 } }, |
| 16 { self: { isDirectory: false, name: 'file3', size: 30 } }, |
| 17 { self: { isDirectory: true, name: 'dir2', size: 0 }, |
| 18 children: [ |
| 19 { self: { isDirectory: false, name: 'file4', size: 40 } }, |
| 20 { self: { isDirectory: true, name: 'dir3', size: 0 } }, |
| 21 ] |
| 22 }, |
| 23 ]}, |
| 24 ] |
| 25 }; |
| 26 |
| 27 // Get an entry at |path| from the in-memory file system. |
| 28 function getEntry(path) { |
| 29 console.log('@@@ getEntry: ' + path); |
| 30 // Path should start with '/testfs'. |
| 31 if (path.length < 1 || !path.match(/^\/testfs/)) |
| 32 return null; // Invalid path. |
| 33 // Drop /testfs. |
| 34 path = path.replace(/\/testfs/, '/'); |
| 35 // Normalize the path. |
| 36 path = path.replace(/\/+/g, '/'); |
| 37 if (path != '/') { |
| 38 path = path.replace(/\/$/, ''); |
| 39 } |
| 40 |
| 41 // Return the root if that's what requested. |
| 42 if (path == '/') { |
| 43 return theRoot; |
| 44 } |
| 45 |
| 46 function getEntryInternal(pathComponents, entry) { |
| 47 if (entry.self.name == pathComponents[0]) { |
| 48 if (pathComponents.length == 1) |
| 49 return entry; |
| 50 if (entry.self.isDirectory && entry.children) { |
| 51 for (var i = 0; i < entry.children.length; i++) { |
| 52 var rest = pathComponents.slice(); |
| 53 rest.shift(); |
| 54 var found = getEntryInternal(rest, entry.children[i]); |
| 55 if (found) |
| 56 return found; |
| 57 } |
| 58 } |
| 59 } |
| 60 return null; |
| 61 } |
| 62 |
| 63 var pathComponents = path.split('/'); |
| 64 return getEntryInternal(pathComponents, theRoot); |
| 65 } |
| 66 |
| 67 chrome.extfs.onEntryRequested.addListener( |
| 68 function(path, callback) { |
| 69 console.log('@@@ onEntryRequested: ' + path); |
| 70 var entry = getEntry(path); |
| 71 if (entry) |
| 72 callback("OK", entry.self); |
| 73 else |
| 74 callback("ERROR_NOT_FOUND", null); |
| 75 }); |
| 76 |
| 77 chrome.extfs.onDirectoryEntriesRequested.addListener( |
| 78 function(path, callback) { |
| 79 console.log('@@@ onDirectoryEntriesRequested: ' + path); |
| 80 var entries = []; |
| 81 var parentEntry = getEntry(path); |
| 82 if (!parentEntry) { |
| 83 callback("ERROR_NOT_FOUND", []); |
| 84 return; |
| 85 } |
| 86 if (parentEntry.children) { |
| 87 for (var i = 0; i < parentEntry.children.length; i++) { |
| 88 entries.push(parentEntry.children[i].self); |
| 89 } |
| 90 } |
| 91 callback("OK", entries); |
| 92 }); |
| 93 |
| 94 chrome.extfs.onSnapshotRequested.addListener( |
| 95 function(path, callback) { |
| 96 console.log('@@@ onSnapshotRequested: ' + path); |
| 97 var entry = getEntry(path); |
| 98 if (!entry) { |
| 99 callback("ERROR_NOT_FOUND", null); |
| 100 return; |
| 101 } |
| 102 var content = ''; |
| 103 for (var i = 0; i < entry.self.size; i++) |
| 104 content += 'o'; |
| 105 var blob = new Blob([content], {type: "text/plain;charset=UTF-8"}); |
| 106 callback("OK", blob); |
| 107 }); |
| 108 |
| 109 chrome.extfs.addMountPoint( |
| 110 'testfs', |
| 111 function(result) { |
| 112 console.log("@@@ addMountPoint result: " + result); |
| 113 } |
| 114 ); |
OLD | NEW |