OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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'); |
| 6 |
5 var nameToIds = {}; | 7 var nameToIds = {}; |
6 var idsToEntries = {}; | 8 var idsToEntries = {}; |
7 | 9 |
8 function computeName(entry) { | 10 function computeName(entry) { |
9 return entry.filesystem.name + ':' + entry.fullPath; | 11 return entry.filesystem.name + ':' + entry.fullPath; |
10 } | 12 } |
11 | 13 |
| 14 function computeId(entry) { |
| 15 var fileSystemId = fileSystemNatives.CrackIsolatedFileSystemName( |
| 16 entry.filesystem.name); |
| 17 if (!fileSystemId) |
| 18 return null; |
| 19 // Strip the leading '/' from the path. |
| 20 return fileSystemId + ':' + entry.fullPath.slice(1); |
| 21 } |
| 22 |
12 function registerEntry(id, entry) { | 23 function registerEntry(id, entry) { |
13 var name = computeName(entry); | 24 var name = computeName(entry); |
14 nameToIds[name] = id; | 25 nameToIds[name] = id; |
15 idsToEntries[id] = entry; | 26 idsToEntries[id] = entry; |
16 }; | 27 } |
17 | 28 |
18 function getEntryId(entry) { | 29 function getEntryId(entry) { |
19 var name = null; | 30 var name = null; |
20 try { | 31 try { |
21 name = computeName(entry); | 32 name = computeName(entry); |
22 } catch(e) { | 33 } catch(e) { |
23 return null; | 34 return null; |
24 } | 35 } |
25 return nameToIds[name]; | 36 var id = nameToIds[name]; |
| 37 if (id != null) |
| 38 return id; |
| 39 |
| 40 // If an entry has not been registered, compute its id and register it. |
| 41 id = computeId(entry); |
| 42 registerEntry(id, entry); |
| 43 return id; |
26 } | 44 } |
27 | 45 |
28 function getEntryById(id) { | 46 function getEntryById(id) { |
29 return idsToEntries[id]; | 47 return idsToEntries[id]; |
30 } | 48 } |
31 | 49 |
32 exports.registerEntry = registerEntry; | 50 exports.registerEntry = registerEntry; |
33 exports.getEntryId = getEntryId; | 51 exports.getEntryId = getEntryId; |
34 exports.getEntryById = getEntryById; | 52 exports.getEntryById = getEntryById; |
OLD | NEW |