| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var fileSystemNatives = requireNative('file_system_natives'); | 5 define('entryIdManager', [ |
| 6 'file_system_natives', |
| 7 'string', |
| 8 ], function(fileSystemNatives, $String) { |
| 9 var nameToIds = {}; |
| 10 var idsToEntries = {}; |
| 6 | 11 |
| 7 var nameToIds = {}; | 12 function computeName(entry) { |
| 8 var idsToEntries = {}; | 13 return entry.filesystem.name + ':' + entry.fullPath; |
| 14 } |
| 9 | 15 |
| 10 function computeName(entry) { | 16 function computeId(entry) { |
| 11 return entry.filesystem.name + ':' + entry.fullPath; | 17 var fileSystemId = fileSystemNatives.CrackIsolatedFileSystemName( |
| 12 } | 18 entry.filesystem.name); |
| 19 if (!fileSystemId) |
| 20 return null; |
| 21 // Strip the leading '/' from the path. |
| 22 return fileSystemId + ':' + $String.slice(entry.fullPath, 1); |
| 23 } |
| 13 | 24 |
| 14 function computeId(entry) { | 25 function registerEntry(id, entry) { |
| 15 var fileSystemId = fileSystemNatives.CrackIsolatedFileSystemName( | 26 var name = computeName(entry); |
| 16 entry.filesystem.name); | 27 nameToIds[name] = id; |
| 17 if (!fileSystemId) | 28 idsToEntries[id] = entry; |
| 18 return null; | 29 } |
| 19 // Strip the leading '/' from the path. | |
| 20 return fileSystemId + ':' + $String.slice(entry.fullPath, 1); | |
| 21 } | |
| 22 | 30 |
| 23 function registerEntry(id, entry) { | 31 function getEntryId(entry) { |
| 24 var name = computeName(entry); | 32 var name = null; |
| 25 nameToIds[name] = id; | 33 try { |
| 26 idsToEntries[id] = entry; | 34 name = computeName(entry); |
| 27 } | 35 } catch(e) { |
| 36 return null; |
| 37 } |
| 38 var id = nameToIds[name]; |
| 39 if (id != null) |
| 40 return id; |
| 28 | 41 |
| 29 function getEntryId(entry) { | 42 // If an entry has not been registered, compute its id and register it. |
| 30 var name = null; | 43 id = computeId(entry); |
| 31 try { | 44 registerEntry(id, entry); |
| 32 name = computeName(entry); | 45 return id; |
| 33 } catch(e) { | |
| 34 return null; | |
| 35 } | 46 } |
| 36 var id = nameToIds[name]; | |
| 37 if (id != null) | |
| 38 return id; | |
| 39 | 47 |
| 40 // If an entry has not been registered, compute its id and register it. | 48 function getEntryById(id) { |
| 41 id = computeId(entry); | 49 return idsToEntries[id]; |
| 42 registerEntry(id, entry); | 50 } |
| 43 return id; | |
| 44 } | |
| 45 | 51 |
| 46 function getEntryById(id) { | 52 return { |
| 47 return idsToEntries[id]; | 53 registerEntry: registerEntry, |
| 48 } | 54 getEntryId: getEntryId, |
| 49 | 55 getEntryById: getEntryById, |
| 50 exports.registerEntry = registerEntry; | 56 }; |
| 51 exports.getEntryId = getEntryId; | 57 }); |
| 52 exports.getEntryById = getEntryById; | |
| OLD | NEW |